1# Copyright (c) 2019-2020 Manfred Moitzi 2# License: MIT License 3import pytest 4 5from ezdxf.entities.block import Block, EndBlk 6from ezdxf.lldxf.const import DXF12, DXF2000 7from ezdxf.lldxf.tagwriter import TagCollector, basic_tags_from_text 8 9TEST_CLASS = Block 10TEST_TYPE = 'BLOCK' 11 12ENTITY_R12 = """0 13BLOCK 145 150 168 170 182 19BLOCKNAME 2070 210 2210 230.0 2420 250.0 2630 270.0 283 29BLOCKNAME 301 31 32""" 33 34ENTITY_R2000 = """0 35BLOCK 365 370 38330 390 40100 41AcDbEntity 428 430 44100 45AcDbBlockBegin 462 47BLOCKNAME 4870 490 5010 510.0 5220 530.0 5430 550.0 563 57BLOCKNAME 581 59 60""" 61 62 63@pytest.fixture(params=[ENTITY_R12, ENTITY_R2000]) 64def entity(request): 65 return TEST_CLASS.from_text(request.param) 66 67 68def test_registered(): 69 from ezdxf.entities.factory import ENTITY_CLASSES 70 assert TEST_TYPE in ENTITY_CLASSES 71 72 73def test_default_init(): 74 entity = TEST_CLASS() 75 assert entity.dxftype() == TEST_TYPE 76 77 78def test_default_new(): 79 entity = TEST_CLASS.new(handle='ABBA', owner='0', dxfattribs={ 80 'base_point': (1, 2, 3), 81 }) 82 assert entity.dxf.layer == '0' 83 assert entity.dxf.base_point == (1, 2, 3) 84 assert entity.dxf.base_point.x == 1, 'is not Vec3 compatible' 85 assert entity.dxf.base_point.y == 2, 'is not Vec3 compatible' 86 assert entity.dxf.base_point.z == 3, 'is not Vec3 compatible' 87 88 89def test_load_from_text(entity): 90 assert entity.dxf.layer == '0' 91 assert entity.dxf.base_point == (0, 0, 0) 92 93 94@pytest.mark.parametrize("txt,ver", [(ENTITY_R2000, DXF2000), (ENTITY_R12, DXF12)]) 95def test_write_block_dxf(txt, ver): 96 expected = basic_tags_from_text(txt) 97 block = TEST_CLASS.from_text(txt) 98 collector = TagCollector(dxfversion=ver, optional=True) 99 block.export_dxf(collector) 100 assert collector.tags == expected 101 102 collector2 = TagCollector(dxfversion=ver, optional=False) 103 block.export_dxf(collector2) 104 assert collector.has_all_tags(collector2) 105 106 107ENDBLK_R12 = " 0\nENDBLK\n 5\n0\n 8\n0\n" 108 109ENDBLK_R2000 = """0 110ENDBLK 1115 1120 113330 1140 115100 116AcDbEntity 1178 1180 119100 120AcDbBlockEnd 121""" 122 123 124@pytest.mark.parametrize("txt,ver", [(ENDBLK_R2000, DXF2000), (ENDBLK_R12, DXF12)]) 125def test_write_endblk_dxf(txt, ver): 126 expected = basic_tags_from_text(txt) 127 endblk = EndBlk.from_text(txt) 128 collector = TagCollector(dxfversion=ver, optional=True) 129 endblk.export_dxf(collector) 130 assert collector.tags == expected 131 132 collector2 = TagCollector(dxfversion=ver, optional=False) 133 endblk.export_dxf(collector2) 134 assert collector.has_all_tags(collector2) 135