def test_insert_scaling(): # Insert.transform() changes the extrusion vector # sign of scaling factors depend from new extrusion vector # just compare absolute values insert = Insert() insert.dxf.insert = (0, 0, 0) insert.scale(2, 3, 4) assert abs(insert.dxf.xscale) == 2 assert abs(insert.dxf.yscale) == 3 assert abs(insert.dxf.zscale) == 4 insert.scale(-1, 1, 1) assert abs(insert.dxf.xscale) == 2 assert abs(insert.dxf.yscale) == 3 assert abs(insert.dxf.zscale) == 4 insert.scale(-1, -1, 1) assert abs(insert.dxf.xscale) == 2 assert abs(insert.dxf.yscale) == 3 assert abs(insert.dxf.zscale) == 4 insert.scale(-2, -2, -2) assert abs(insert.dxf.xscale) == 4 assert abs(insert.dxf.yscale) == 6 assert abs(insert.dxf.zscale) == 8
def test_get_block(doc): blk = doc.blocks.new('TEST-2020-02-29') msp = doc.modelspace() insert = msp.add_blockref('TEST-2020-02-29', (0, 0)) assert insert.block() is blk insert = msp.add_blockref('NOT-EXIST-2020-02-29', (0, 0)) assert insert.block() is None insert = Insert() assert insert.block() is None
def test_matrix44_insert(): insert = Insert.new(handle='ABBA', owner='0', dxfattribs={ 'insert': (1, 2, 3), }) m = insert.matrix44() assert m.transform((0, 0, 0)) == (1, 2, 3) assert m.transform_direction((1, 0, 0)) == (1, 0, 0)
def test_load_malformed_insert(): insert = Insert.from_text(MALFORMED_INSERT) assert insert.dxf.layer == "LY_EZDXF" assert insert.dxf.linetype == "LT_EZDXF" assert insert.dxf.color == 7 assert insert.dxf.insert.isclose((1, 2, 3)) assert insert.dxf.name == "BLOCKNAME"
def test_matrix44_direction(): insert = Insert.new(handle='ABBA', owner='0', dxfattribs={ 'insert': (1, 2, 3), 'xscale': 2, }) m = insert.matrix44() assert m.transform((1, 0, 0)) == (3, 2, 3) assert m.transform_direction((1, 0, 0)) == (2, 0, 0), \ 'only scaling has to be applied for directions'
def test_insert_transformation_error(): insert = Insert.new(dxfattribs={ 'name': 'AXIS', 'insert': (0, 0, 0), 'rotation': 45, }) m = Matrix44.scale(0.5, 1, 1) with pytest.raises(InsertTransformationError): insert.transform(m)
def test_has_scaling(): entity = Insert.new(handle='ABBA', owner='0', dxfattribs={'xscale': 2}) assert entity.has_scaling is True assert entity.has_uniform_scaling is False entity = Insert.new(handle='ABBA', owner='0', dxfattribs={'yscale': 2}) assert entity.has_scaling is True assert entity.has_uniform_scaling is False entity = Insert.new(handle='ABBA', owner='0', dxfattribs={'zscale': 2}) assert entity.has_scaling is True assert entity.has_uniform_scaling is False # reflections are under control, so (-2, 2, 2) is a uniform scaling entity = Insert.new(handle='ABBA', owner='0', dxfattribs={ 'xscale': -2, 'yscale': 2, 'zscale': 2, }) assert entity.has_uniform_scaling is True
def test_insert_to_code(): from ezdxf.entities.insert import Insert entity = Insert.new(handle='ABBA', owner='0', dxfattribs={ 'name': 'block1', 'insert': (2, 3, 4), }) new_entity = translate_to_code_and_execute(entity) for name in ('name', 'insert'): assert new_entity.get_dxf_attrib(name) == entity.get_dxf_attrib(name)
def test_write_dxf(txt, ver): expected = basic_tags_from_text(txt) vertex = Insert.from_text(txt) collector = TagCollector(dxfversion=ver, optional=True) vertex.export_dxf(collector) assert collector.tags == expected collector2 = TagCollector(dxfversion=ver, optional=False) vertex.export_dxf(collector2) assert collector.has_all_tags(collector2)
def test_matrix44_scaled(): insert = Insert.new(handle='ABBA', owner='0', dxfattribs={ 'xscale': 2, 'yscale': 3, 'zscale': 4, }) m = insert.matrix44() assert m.transform((1, 1, 1)) == (2, 3, 4) assert m.transform_direction((1, 0, 0)) == ( 2, 0, 0), 'scaling has to be applied for directions'
def test_matrix44_rotation(): insert = Insert.new(handle='ABBA', owner='0', dxfattribs={ 'insert': (0, 0, 0), 'rotation': 90, }) m = insert.matrix44() assert list(m.transform_vertices([(1, 0, 0), (0, 0, 1)])) == [(0, 1, 0), (0, 0, 1)] assert m.transform_direction((1, 0, 0)) == (0, 1, 0)
def test_matrix44_rotation(): insert = Insert.new(handle='ABBA', owner='0', dxfattribs={ 'insert': (0, 0, 0), 'rotation': 90, }) m = insert.matrix44() for v1, v2 in zip( m.transform_vertices([(1, 0, 0), (0, 0, 1)]), [(0, 1, 0), (0, 0, 1)]): assert v1.isclose(v2) assert m.transform_direction((1, 0, 0)).isclose((0, 1, 0))
def test_matrix44_insert(): insert = Insert.new( handle="ABBA", owner="0", dxfattribs={ "insert": (1, 2, 3), }, ) m = insert.matrix44() assert m.transform((0, 0, 0)) == (1, 2, 3) assert m.transform_direction((1, 0, 0)) == (1, 0, 0)
def test_has_scaling(): entity = Insert.new(handle="ABBA", owner="0", dxfattribs={"xscale": 2}) assert entity.has_scaling is True assert entity.has_uniform_scaling is False entity = Insert.new(handle="ABBA", owner="0", dxfattribs={"yscale": 2}) assert entity.has_scaling is True assert entity.has_uniform_scaling is False entity = Insert.new(handle="ABBA", owner="0", dxfattribs={"zscale": 2}) assert entity.has_scaling is True assert entity.has_uniform_scaling is False # reflections are under control, so (-2, 2, 2) is a uniform scaling entity = Insert.new( handle="ABBA", owner="0", dxfattribs={ "xscale": -2, "yscale": 2, "zscale": 2, }, ) assert entity.has_uniform_scaling is True
def test_clone_with_insert(): # difference of clone() to copy_entity() is: # - clone returns and unassigned entity without handle, owner or reactors # - copy_entity clones the entity and assigns the new entity to the same owner as the source and adds the entity # and it linked entities (ATTRIB & VERTEX) to the entity database, but does not adding entity to a layout, setting # owner tag is not enough to assign an entity to a layout, use Layout.add_entity() insert = Insert() insert.add_attrib('T1', 'value1', (0, 0)) clone = insert.copy() assert clone.dxf.handle is None assert clone.dxf.owner is None assert len(clone.attribs) == 1 attrib = clone.attribs[0] assert attrib.dxf.handle is None assert attrib.dxf.tag == 'T1' assert attrib.dxf.text == 'value1' # change cloned attrib attrib.dxf.tag = 'T2' attrib.dxf.text = 'value2' # source attrib is unchanged assert insert.attribs[0].dxf.tag == 'T1' assert insert.attribs[0].dxf.text == 'value1'
def test_add_virtual_insert_with_attribs_to_layout(doc): doc.blocks.new("TestAddVirtualInsert") msp = doc.modelspace() insert = Insert.new(dxfattribs={"name": "TestAddVirtualInsert"}) insert.add_attrib("TAG", "TEXT", (0, 0)) msp.add_entity(insert) assert factory.is_bound(insert, doc) is True assert (factory.is_bound(insert.seqend, doc) is True), "SEQEND must be bound to document" assert insert.attribs_follow is True assert (factory.is_bound(insert.attribs[0], doc) is True), "ATTRIB must be bound to document"
def test_add_virtual_insert_with_attribs_to_layout(doc): doc.blocks.new('TestAddVirtualInsert') msp = doc.modelspace() insert = Insert.new(dxfattribs={'name': 'TestAddVirtualInsert'}) insert.add_attrib('TAG', 'TEXT', (0, 0)) msp.add_entity(insert) assert factory.is_bound(insert, doc) is True assert factory.is_bound(insert.seqend, doc) is True, \ 'SEQEND must be bound to document' assert insert.attribs_follow is True assert factory.is_bound(insert.attribs[0], doc) is True, \ 'ATTRIB must be bound to document'
def test_matrix44_rotation(): insert = Insert.new( handle="ABBA", owner="0", dxfattribs={ "insert": (0, 0, 0), "rotation": 90, }, ) m = insert.matrix44() for v1, v2 in zip(m.transform_vertices([(1, 0, 0), (0, 0, 1)]), [(0, 1, 0), (0, 0, 1)]): assert v1.isclose(v2) assert m.transform_direction((1, 0, 0)).isclose((0, 1, 0))
def test_insert_to_code(): from ezdxf.entities.insert import Insert entity = Insert.new( handle="ABBA", owner="0", dxfattribs={ "name": "block1", "insert": (2, 3, 4), }, ) new_entity = translate_to_code_and_execute(entity) for name in ("name", "insert"): assert new_entity.get_dxf_attrib(name) == entity.get_dxf_attrib(name)
def test_insert_transform_interface(): insert = Insert() insert.dxf.insert = (1, 0, 0) insert.transform(Matrix44.translate(1, 2, 3)) assert insert.dxf.insert == (2, 2, 3) # optimized translate implementation insert.translate(-1, -2, -3) assert insert.dxf.insert == (1, 0, 0)
def test_matrix44_direction(): insert = Insert.new( handle="ABBA", owner="0", dxfattribs={ "insert": (1, 2, 3), "xscale": 2, }, ) m = insert.matrix44() assert m.transform((1, 0, 0)) == (3, 2, 3) assert m.transform_direction((1, 0, 0)) == ( 2, 0, 0, ), "only scaling has to be applied for directions"
def test_matrix44_scaled(): insert = Insert.new( handle="ABBA", owner="0", dxfattribs={ "xscale": 2, "yscale": 3, "zscale": 4, }, ) m = insert.matrix44() assert m.transform((1, 1, 1)) == (2, 3, 4) assert m.transform_direction((1, 0, 0)) == ( 2, 0, 0, ), "scaling has to be applied for directions"
def test_new_constructor(): insert = Insert.new(handle='ABBA', owner='0', dxfattribs={ 'color': '7', 'insert': (1, 2, 3), }) assert insert.is_virtual is True, 'Has no assigned document' assert insert.dxf.layer == '0' assert insert.dxf.color == 7 assert insert.dxf.linetype == 'BYLAYER' assert insert.dxf.insert == (1, 2, 3) assert insert.dxf.insert.x == 1, 'is not Vec3 compatible' assert insert.dxf.insert.y == 2, 'is not Vec3 compatible' assert insert.dxf.insert.z == 3, 'is not Vec3 compatible' assert insert.has_scaling is False assert insert.has_uniform_scaling is True # can set DXF R2007 value insert.dxf.shadow_mode = 1 assert insert.dxf.shadow_mode == 1
def test_new_constructor(): insert = Insert.new( handle="ABBA", owner="0", dxfattribs={ "color": "7", "insert": (1, 2, 3), }, ) assert insert.is_virtual is True, "Has no assigned document" assert insert.dxf.layer == "0" assert insert.dxf.color == 7 assert insert.dxf.linetype == "BYLAYER" assert insert.dxf.insert == (1, 2, 3) assert insert.dxf.insert.x == 1, "is not Vec3 compatible" assert insert.dxf.insert.y == 2, "is not Vec3 compatible" assert insert.dxf.insert.z == 3, "is not Vec3 compatible" assert insert.has_scaling is False assert insert.has_uniform_scaling is True # can set DXF R2007 value insert.dxf.shadow_mode = 1 assert insert.dxf.shadow_mode == 1
def test_add_attribs(): insert = Insert() assert insert.attribs_follow is False insert.add_attrib('T1', 'value1', (0, 0)) assert len(insert.attribs) == 1 assert insert.attribs_follow is True
def test_default_constructor(): insert = Insert() assert insert.dxftype() == 'INSERT' assert insert.is_virtual assert insert.seqend is None, 'SEQEND must not exist'
def entity(request): return Insert.from_text(request.param)
def test_matrix44_no_transform(): insert = Insert.new(handle='ABBA', owner='0') m = insert.matrix44() assert m.transform((0, 0, 0)) == (0, 0, 0) assert m.transform_direction((1, 0, 0)) == (1, 0, 0)