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
|
# Copyright (c) 2021, Manfred Moitzi
# License: MIT License
from typing import List
import pytest
import ezdxf
from ezdxf.disassemble import recursive_decompose
from ezdxf.entities import Point, Insert
@pytest.fixture(scope="module")
def doc():
d = ezdxf.new()
l0 = d.blocks.new("L0")
build_level_0(l0)
l1 = d.blocks.new("L1")
build_nesting_level_1(l1)
return d
def build_level_0(l0):
# Block of 4 lines in 4 different colors
l0.add_line((1, -1), (1, 1), dxfattribs={"color": 1})
l0.add_line((1, 1), (-1, 1), dxfattribs={"color": 2})
l0.add_line((-1, 1), (-1, -1), dxfattribs={"color": 3})
l0.add_line((-1, -1), (1, -1), dxfattribs={"color": 4})
def scale(sx, sy, sz):
return {
"xscale": sx,
"yscale": sy,
"zscale": sz,
}
def build_nesting_level_1(l1, name="L0"):
l1.add_blockref(name, (0, 0), dxfattribs=scale(1, 1, 1))
l1.add_blockref(name, (3, 0), dxfattribs=scale(-1, 1, 1))
l1.add_blockref(name, (6, 0), dxfattribs=scale(1, -1, 1))
l1.add_blockref(name, (9, 0), dxfattribs=scale(-1, -1, 1))
l1.add_blockref(name, (0, 3), dxfattribs=scale(1, 1, -1))
l1.add_blockref(name, (3, 3), dxfattribs=scale(-1, 1, -1))
l1.add_blockref(name, (6, 3), dxfattribs=scale(1, -1, -1))
l1.add_blockref(name, (9, 3), dxfattribs=scale(-1, -1, -1))
def count(doc, block_names) -> int:
count = 1
for name in block_names:
block = doc.blocks.get(name)
count *= len(block)
return count
def test_decompose_block_level_0(doc):
l0 = doc.blocks.get("L0")
result = list(recursive_decompose(l0))
assert len(result) == count(doc, ["L0"])
REFLECTIONS = [(1, 1, 1), (-1, 1, 1), (1, -1, 1), (1, 1, -1)]
NAMES = [
"normal",
"reflect-x",
"reflect-y",
"reflect-z",
]
@pytest.mark.parametrize("sx,sy,sz", REFLECTIONS, ids=NAMES)
def test_decompose_block_reference_level_0(doc, sx, sy, sz):
msp = doc.modelspace()
msp.delete_all_entities()
msp.add_blockref("L0", (0, 0), dxfattribs=scale(sx, sy, sz))
result = list(recursive_decompose(msp))
assert len(result) == count(doc, ["L0"])
def test_decompose_block_level_1(doc):
l1 = doc.blocks.get("L1")
types = [e.dxftype() for e in recursive_decompose(l1)]
assert len(types) == count(doc, ["L0", "L1"])
assert set(types) == {"LINE"}, "expected only LINES"
@pytest.mark.parametrize("sx,sy,sz", REFLECTIONS, ids=NAMES)
def test_decompose_block_reference_level_1(doc, sx, sy, sz):
msp = doc.modelspace()
msp.delete_all_entities()
msp.add_blockref("L1", (0, 0), dxfattribs=scale(sx, sy, sz))
types = [e.dxftype() for e in recursive_decompose(msp)]
assert len(types) == count(doc, ["L0", "L1"])
assert set(types) == {"LINE"}, "expected only LINES"
def test_decompose_minsert_level_1(doc):
nrows = 2
ncols = 2
expected_count = count(doc, ["L0", "L1"]) * nrows * ncols
msp = doc.modelspace()
msp.delete_all_entities()
msp.add_blockref(
"L1",
(0, 0),
dxfattribs={
"row_count": nrows,
"row_spacing": 5,
"column_count": ncols,
"column_spacing": 5,
},
)
types = [e.dxftype() for e in recursive_decompose(msp)]
assert len(types) == expected_count
assert set(types) == {"LINE"}, "expected only LINES"
class TestSourceBlockReferences:
@pytest.fixture(scope="class")
def doc(self):
doc_ = ezdxf.new()
blk0 = doc_.blocks.new("BLK0")
blk1 = doc_.blocks.new("BLK1")
blk2 = doc_.blocks.new("BLK2")
blk2.add_point((2, 1))
blk2.add_point((2, 2))
blk1.add_point((1, 1))
blk1.add_point((1, 2))
# block reference 2
blk1.add_blockref("BLK2", (2, 2))
blk0.add_point((0, 1))
blk0.add_point((0, 2))
# block reference 1
blk0.add_blockref("BLK1", (1, 1))
# block reference 0
doc_.modelspace().add_blockref("BLK0", (0, 0))
return doc_
@pytest.fixture(scope="class")
def entities(self, doc):
return list(recursive_decompose(doc.modelspace()))
def test_count_of_expected_virtual_entities(self, entities: List[Point]):
"""The virtual INSERT entities are not returned by the
recursive_decompose() function.
"""
assert len(entities) == 6, "expected only 6 POINT entities"
def test_same_source_block_references_blk0(self, entities):
"""Entities from the same INSERT entity have the same virtual source
block reference.
"""
insert0 = entities[0].source_block_reference
insert1 = entities[1].source_block_reference
assert isinstance(insert0, Insert)
assert insert0 is insert1
assert insert0.dxf.name == "BLK0"
def test_same_source_block_references_blk1(self, entities):
"""Entities from the same INSERT entity have the same virtual source
block reference.
"""
insert0 = entities[2].source_block_reference
insert1 = entities[3].source_block_reference
assert insert0 is insert1
assert insert0.dxf.name == "BLK1"
def test_same_source_block_references_blk2(self, entities):
"""Entities from the same INSERT entity have the same virtual source
block reference.
"""
insert0 = entities[4].source_block_reference
insert1 = entities[5].source_block_reference
assert insert0 is insert1
assert insert0.dxf.name == "BLK2"
def test_link_structure_of_virtual_block_references(self, entities):
"""It is possible to trace back the nesting structure of the block
references by the source_block_reference property of the virtual
INSERT entities.
"""
blkref0 = entities[0].source_block_reference
blkref1 = entities[2].source_block_reference
blkref2 = entities[4].source_block_reference
# blkref2 resides in blkref1
assert blkref2.source_block_reference is blkref1
# and blkref1 resides in blkref0
assert blkref1.source_block_reference is blkref0
# and blkref0 has no source_block_reference
assert blkref0.source_block_reference is None
if __name__ == "__main__":
pytest.main([__file__])
|