[dxf] ignore unreferenced internal dxf blocks
This commit is contained in:
parent
bbe3586ba9
commit
cf0a245143
2 changed files with 42 additions and 1 deletions
|
|
@ -192,8 +192,37 @@ def read(
|
|||
|
||||
top_name, top_pat = _read_block(msp)
|
||||
mlib = Library({top_name: top_pat})
|
||||
|
||||
blocks_by_name = {
|
||||
bb.name: bb
|
||||
for bb in lib.blocks
|
||||
if not bb.is_any_layout
|
||||
}
|
||||
|
||||
referenced: set[str] = set()
|
||||
pending = [msp]
|
||||
seen_blocks: set[str] = set()
|
||||
while pending:
|
||||
block = pending.pop()
|
||||
block_name = getattr(block, 'name', None)
|
||||
if block_name is not None and block_name in seen_blocks:
|
||||
continue
|
||||
if block_name is not None:
|
||||
seen_blocks.add(block_name)
|
||||
for element in block:
|
||||
if not isinstance(element, Insert):
|
||||
continue
|
||||
target = element.dxfattribs().get('name')
|
||||
if target is None or target in referenced:
|
||||
continue
|
||||
referenced.add(target)
|
||||
if target in blocks_by_name:
|
||||
pending.append(blocks_by_name[target])
|
||||
|
||||
for bb in lib.blocks:
|
||||
if bb.name == '*Model_Space':
|
||||
if bb.is_any_layout:
|
||||
continue
|
||||
if bb.name.startswith('_') and bb.name not in referenced:
|
||||
continue
|
||||
name, pat = _read_block(bb)
|
||||
mlib[name] = pat
|
||||
|
|
|
|||
|
|
@ -170,3 +170,15 @@ def test_dxf_read_legacy_polyline() -> None:
|
|||
polys = [shape for shape in top_pat.shapes["legacy"] if isinstance(shape, Polygon)]
|
||||
assert len(polys) == 1
|
||||
assert _matches_closed_vertices(polys[0].vertices, numpy.array([[0, 0], [10, 0], [10, 10]]))
|
||||
|
||||
|
||||
def test_dxf_read_ignores_unreferenced_setup_blocks() -> None:
|
||||
lib = Library({"top": Pattern()})
|
||||
stream = io.StringIO()
|
||||
|
||||
dxf.write(lib, "top", stream)
|
||||
stream.seek(0)
|
||||
|
||||
read_lib, _ = dxf.read(stream)
|
||||
|
||||
assert set(read_lib) == {"Model"}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue