Iterating through block entities, or exploding a block

1,015 views
Skip to first unread message

Stefano Menci

unread,
Apr 17, 2015, 8:33:21 PM4/17/15
to python...@googlegroups.com
I am using ezdxf to import drawings into my home made CAM.

The geometries are organized in layers, so the query language is really helpful. For example I do something like:

for e in itertools.chain(self.dwg.entities.query('LINE CIRCLE ARC[layer=="External Profile"]'),
                         
self.dwg.entities.query('LINE CIRCLE ARC[layer=="Internal Profiles"]')):

Some drawings have the geometries inside a block called "a". I can find the block with:
dwg.entities.query('INSERT[name=="a"]')
but I can't find the geometries inside the block.

Is it possible to either explode the block or to iterate through its geometries?

Thanks,
Stefano

Manfred Moitzi

unread,
Apr 17, 2015, 11:50:24 PM4/17/15
to python...@googlegroups.com
The INSERT entity just inserts a reference to a block definition (with scaling, rotation and individual attributes).

Insert Reference: http://ezdxf.readthedocs.org/en/latest/blocks.html#block-reference

This blocks are defined in dwg.blocks, dwg.blocks is a collection, use blk_a=dwg.blocks.get("a") to get the block definition for block "a".
And Blocks itself are like layouts, iteration and queries work the same way like on layouts: blk_a.query(...)

Blocks reference: http://ezdxf.readthedocs.org/en/latest/blocks.html
Layout (and Block-Layout) reference: http://ezdxf.readthedocs.org/en/latest/reference.html#layouts
Blocks tutorial: http://ezdxf.readthedocs.org/en/latest/tutorials/blocks.html

Remark to your code:
If you iterate over dwg.entities you iterate over all entities in model space and all existing paper space layouts.
To iterate just over entities from model space use the layout: modelspace=dwg.modelspace()
It is also a little bit faster, if there are some paper space layouts.

Exploding block refernces is not possible with ezdxf and will never be possible because this is a too complex operation (ezdxf!=CAD).

Best regards,
Manfred

Stefano Menci

unread,
Apr 18, 2015, 5:39:14 PM4/18/15
to python...@googlegroups.com
Thanks for your help, it works beautifully:

entity_container = dwg.blocks.get('a') or dwg.modelspace()

for entity in entity_container.query('LINE CIRCLE ARC[layer=="External Profile"]'):
   
[...]

Reply all
Reply to author
Forward
0 new messages