File: test_230b_image_in_layout_context.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (423 lines) | stat: -rw-r--r-- 12,063 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# Copyright (c) 2016-2023, Manfred Moitzi
# License: MIT License
import pytest
import logging

import ezdxf
from ezdxf.entities.image import ImageDef, Image

logger = logging.getLogger("ezdxf")


@pytest.fixture(scope="module")
def doc():
    return ezdxf.new("R2000")


@pytest.fixture(scope="module")
def image_def(doc):
    return ImageDef.from_text(IMAGE_DEF, doc)


def test_set_raster_variables():
    doc = ezdxf.new("R2000")
    assert "ACAD_IMAGE_VARS" not in doc.rootdict
    doc.set_raster_variables(frame=0, quality=1, units="m")
    raster_vars = doc.rootdict["ACAD_IMAGE_VARS"]
    assert raster_vars.dxftype() == "RASTERVARIABLES"
    assert raster_vars.dxf.frame == 0
    assert raster_vars.dxf.quality == 1
    assert raster_vars.dxf.units == 3  # m

    assert "ACAD_IMAGE_VARS" in doc.rootdict
    doc.set_raster_variables(frame=1, quality=0, units="km")
    raster_vars = doc.rootdict["ACAD_IMAGE_VARS"]
    assert raster_vars.dxftype() == "RASTERVARIABLES"
    assert raster_vars.dxf.frame == 1
    assert raster_vars.dxf.quality == 0
    assert raster_vars.dxf.units == 4  # km


def test_imagedef_attribs(image_def):
    assert image_def.dxftype() == "IMAGEDEF"
    assert image_def.dxf.class_version == 0
    assert image_def.dxf.filename == "mycat.jpg"
    assert image_def.dxf.image_size.isclose((640.0, 360.0))
    assert image_def.dxf.pixel_size.isclose((0.01, 0.01))
    assert image_def.dxf.loaded == 1
    assert image_def.dxf.resolution_units == 0


@pytest.fixture(scope="module")
def image(doc):
    return Image.from_text(IMAGE, doc)


def test_image_dxf_attribs(image):
    assert "IMAGE" == image.dxftype()
    assert image.dxf.insert.isclose((0.0, 0.0, 0.0))
    assert image.dxf.u_pixel.isclose((0.01, 0.0, 0.0))
    assert image.dxf.v_pixel.isclose((0.0, 0.01, 0.0))
    assert image.dxf.image_size.isclose((640.0, 360.0))
    assert image.dxf.flags == 7
    assert image.dxf.clipping == 0
    assert image.dxf.brightness == 50
    assert image.dxf.contrast == 50
    assert image.dxf.fade == 0
    assert image.dxf.image_def_reactor_handle == "DEAD"
    assert image.dxf.clipping_boundary_type == 1
    assert image.dxf.count_boundary_points == 2
    x, y, *_ = image.dxf.image_size
    assert image.boundary_path == [(-0.5, -0.5), (x - 0.5, y - 0.5)]


def test_boundary_path(image):
    assert [(-0.5, -0.5), (639.5, 359.5)] == image.boundary_path


def test_reset_boundary_path(image):
    image.reset_boundary_path()
    assert 2 == image.dxf.count_boundary_points
    assert image.get_flag_state(image.USE_CLIPPING_BOUNDARY) is False
    assert image.dxf.clipping == 0
    x, y, *_ = image.dxf.image_size
    assert [(-0.5, -0.5), (x - 0.5, y - 0.5)] == image.boundary_path


def test_set_boundary_path(image):
    image.set_boundary_path(
        [(0, 0), (640, 180), (320, 360)]
    )  # 3 vertices triangle
    assert image.dxf.count_boundary_points == 4
    assert image.dxf.clipping_boundary_type == 2
    # auto close
    assert [(0, 0), (640, 180), (320, 360), (0, 0)] == image.boundary_path
    assert image.dxf.clipping == 1
    assert image.get_flag_state(image.USE_CLIPPING_BOUNDARY) is True


def test_post_load_hook_creates_image_def_reactor(doc):
    image_def = doc.add_image_def("test.jpg", (1, 1))
    msp = doc.modelspace()
    image = msp.add_image(image_def, (0, 0), (1, 1))
    old_reactor_handle = image.dxf.image_def_reactor_handle

    # Hack to unlink Image from ImageDefReactor!
    image.dxf.image_def_reactor_handle = None
    command = image.post_load_hook(doc)

    assert command is not None, "must return a fixer as callable object"
    command()  # execute fix
    handle = image.dxf.image_def_reactor_handle
    reactor = doc.entitydb[handle]

    assert handle is not None, "must have an ImageDefReactor"
    assert handle != old_reactor_handle, "must have a new ImageDefReactor"
    assert (
        handle in image_def.reactors
    ), "ImageDef must be linked to ImageDefReactor"
    assert (
        reactor.dxf.image_handle == image.dxf.handle
    ), "ImageDefReactor must be linked to Image"


def test_exception_while_fixing_image_def_reactor(doc):
    from io import StringIO

    stream = StringIO()
    logging_handler = logging.StreamHandler(stream)
    logger.addHandler(logging_handler)

    image_def = doc.add_image_def("test.jpg", (1, 1))
    msp = doc.modelspace()
    image = msp.add_image(image_def, (0, 0), (1, 1))

    # Hack to unlink Image from ImageDefReactor!
    image.dxf.image_def_reactor_handle = None
    command = image.post_load_hook(doc)

    # Sabotage execution:
    image.doc = None
    try:
        command()
        msg = stream.getvalue()
    finally:
        logger.removeHandler(logging_handler)

    assert image.is_alive is False, "Exception while fixing must destroy IMAGE"
    assert "AttributeError" in msg
    # Example logging message:
    # ------------------------
    # An exception occurred while executing fixing command for IMAGE(#30), destroying entity.
    # Traceback (most recent call last):
    #   File "D:\Source\ezdxf.git\src\ezdxf\entities\image.py", line 173, in _fix_missing_image_def_reactor
    #     self._create_image_def_reactor()
    #   File "D:\Source\ezdxf.git\src\ezdxf\entities\image.py", line 186, in _create_image_def_reactor
    #     image_def_reactor = self.doc.objects.add_image_def_reactor(
    # AttributeError: 'NoneType' object has no attribute 'objects'


def test_post_load_hook_destroys_image_without_valid_image_def(doc):
    image_def = doc.add_image_def("test.jpg", (1, 1))
    msp = doc.modelspace()
    image = msp.add_image(image_def, (0, 0), (1, 1))

    # Hack to unlink Image from ImageDef!
    image.dxf.image_def_handle = None
    image.post_load_hook(doc)

    assert (
        image.is_alive is False
    ), "Image with invalid ImageDef must be destroyed"


@pytest.fixture
def new_doc():
    return ezdxf.new("R2000")


def test_new_image_def(new_doc):
    rootdict = new_doc.rootdict
    assert "ACAD_IMAGE_DICT" not in rootdict
    imagedef = new_doc.add_image_def("mycat.jpg", size_in_pixel=(640, 360))

    # check internals image_def_owner -> ACAD_IMAGE_DICT
    image_dict = rootdict["ACAD_IMAGE_DICT"]
    assert imagedef.dxf.owner == image_dict.dxf.handle

    assert imagedef.dxf.filename == "mycat.jpg"
    assert imagedef.dxf.image_size.isclose((640.0, 360.0))

    # rest are default values
    assert imagedef.dxf.pixel_size.isclose((0.01, 0.01))
    assert imagedef.dxf.loaded == 1
    assert imagedef.dxf.resolution_units == 0


def test_create_and_delete_image(new_doc):
    msp = new_doc.modelspace()
    image_def = new_doc.add_image_def("mycat.jpg", size_in_pixel=(640, 360))
    image = msp.add_image(
        image_def=image_def, insert=(0, 0), size_in_units=(3.2, 1.8)
    )
    assert image.dxf.insert.isclose((0, 0, 0))
    assert image.dxf.u_pixel.isclose((0.005, 0, 0))
    assert image.dxf.v_pixel.isclose((0.0, 0.005, 0))
    assert image.dxf.image_size.isclose((640, 360))
    assert image_def.dxf.handle == image.dxf.image_def_handle
    assert image.dxf.flags == 3
    assert image.dxf.clipping == 0
    assert image.dxf.count_boundary_points == 2
    x, y = image.dxf.image_size.vec2
    assert image.boundary_path == [(-0.5, -0.5), (x - 0.5, y - 0.5)]

    image_def2 = image.image_def
    assert image_def.dxf.handle, image_def2.dxf.handle

    # does image def reactor exists
    reactor_handle = image.dxf.image_def_reactor_handle
    assert reactor_handle in new_doc.objects
    reactor = new_doc.entitydb[reactor_handle]
    assert (
        image.dxf.handle == reactor.dxf.owner
    ), "IMAGE is not owner of IMAGEDEF_REACTOR"
    assert (
        image.dxf.handle == reactor.dxf.image_handle
    ), "IMAGEDEF_REACTOR does not point to IMAGE"

    assert (
        reactor_handle in image_def2.get_reactors()
    ), "Reactor handle not in IMAGE_DEF reactors."

    # delete image
    msp.delete_entity(image)
    new_doc.entitydb.purge()
    assert reactor.is_alive is False
    assert (
        reactor_handle not in new_doc.objects
    ), "IMAGEDEF_REACTOR not deleted for objects section"
    assert (
        reactor_handle not in new_doc.entitydb
    ), "IMAGEDEF_REACTOR not deleted for entity database"
    assert (
        reactor_handle not in image_def2.get_reactors()
    ), "Reactor handle not deleted from IMAGE_DEF reactors."


def create_image(doc):
    msp = doc.modelspace()
    image_def = doc.add_image_def("mycat.jpg", size_in_pixel=(640, 360))
    image = msp.add_image(
        image_def=image_def, insert=(0, 0), size_in_units=(3.2, 1.8)
    )
    image.boundary_path = [(0, 0), (1, 1), (2, 2), (3, 3)]
    return image_def, image


def test_create_and_copy_image(new_doc):
    msp = new_doc.modelspace()
    entitydb = new_doc.entitydb
    image_def, image = create_image(new_doc)
    reactor_handle = image.dxf.image_def_reactor_handle
    copy = image.copy()
    msp.add_entity(copy)
    reactor_handle_of_copy = copy.dxf.image_def_reactor_handle

    # Each image has it's own ImageDefReactor
    assert (
        reactor_handle in entitydb
    ), "ImageDefReactor of source image not stored in EntityDB."
    assert (
        reactor_handle_of_copy in entitydb
    ), "ImageDefReactor of copy not stored in EntityDB."
    assert (
        reactor_handle != reactor_handle_of_copy
    ), "Source image and copy must not have the same ImageDefReactor."

    # Both images are linked to same ImageDef object.
    assert (
        reactor_handle in image_def.reactors
    ), "ImageDefReactor of source image not stored in ImageDef object."
    assert (
        reactor_handle_of_copy in image_def.reactors
    ), "ImageDefReactor of copied image not stored in ImageDef object."
    assert (
        image.boundary_path == copy.boundary_path
    ), "Invalid copy of boundary path."
    assert (
        image.boundary_path is not copy.boundary_path
    ), "Copied image needs an independent copy of the boundary path."


def test_moving_to_another_layout(new_doc):
    msp = new_doc.modelspace()
    image_def, image = create_image(new_doc)
    old_reactor_handle = image.dxf.image_def_reactor_handle
    blk = new_doc.blocks.new("TEST")
    msp.move_to_layout(image, blk)
    # Moving between layouts should not alter the ImageDefReactor
    assert (
        image.dxf.image_def_reactor_handle == old_reactor_handle
    ), "Moved image got a new ImageDefReactor without necessity."


def test_copying_to_another_layout(new_doc):
    image_def, image = create_image(new_doc)
    old_reactor_handle = image.dxf.image_def_reactor_handle
    blk = new_doc.blocks.new("TEST")
    copy = new_doc.entitydb.duplicate_entity(image)

    # duplicate_entity() binds entity to document and triggers the
    # post_bind_hook() to create a new ImageDefReactor:
    copy_reactor_handle = copy.dxf.image_def_reactor_handle
    assert (
        copy_reactor_handle != "0"
    ), "Image copy did not get a new ImageDefReactor"
    assert (
        copy_reactor_handle != old_reactor_handle
    ), "Image copy has the same ImageDefReactor as the source image."

    blk.add_entity(copy)

    # unlinking an image, does not remove the ImageDefReactor
    blk.unlink_entity(copy)
    assert (
        copy.dxf.image_def_reactor_handle == copy_reactor_handle
    ), "Removed ImageDefReactor from unlinked image."

    # Keep reactor if adding an image with existing ImageDefReactor
    blk.add_entity(copy)
    assert (
        copy.dxf.image_def_reactor_handle == copy_reactor_handle
    ), "Relinked image got new ImageDefReactor without necessity."


IMAGE_DEF = """  0
IMAGEDEF
  5
DEAD
330
DEAD
100
AcDbRasterImageDef
 90
        0
  1
mycat.jpg
 10
640.0
 20
360.0
 11
0.01
 21
0.01
280
     1
281
     0
"""

IMAGE = """  0
IMAGE
  5
1DF
330
1F
100
AcDbEntity
  8
0
100
AcDbRasterImage
 90
        0
 10
0.0
 20
0.0
 30
0.0
 11
0.01
 21
0.0
 31
0.0
 12
0.0
 22
0.01
 32
0.0
 13
640.0
 23
360.0
340
DEAD
 70
     7
280
     0
281
    50
282
    50
283
     0
360
DEAD
 71
     1
 91
        2
 14
-.5
 24
-.5
 14
639.5
 24
359.5
"""