How to get BLKREFS from a BLOCK_RECORD #1157
-
Hi, First of all many thanks for this great library. I have a DWG file, which I have converted to a DXF. The DXF file has (amongst other entries) BLOCK_RECORDs with a list of BLKREFS which references INSERTs. My goal is to get the list of the handles of the INSERTs in the BLKREFS section of a BLOCK_RECORD . The BLOCK_RECORD and the corresponding start BLOCK have the same name. I've done the following: A. block_record = doc.block_records.get("NAME_OF_BLOCK_RECORD_AND_BLOCK") # (this is an ezdxf.entities.blockrecord.BlockRecord) From there I can get the start and end BLOCKs: block_record.block
block_record.endblk The objects (LINE, ARC, etc) between the START and END BLOCKS are the ones that I expect; but the owner handle points back to the BLOCK_RECORD (which is kinda correct) I can also use this to get the same list of objects (LINE, ARC, etc) using: for entity in block_record.entity_space:
print(entity) The list contains the correct objects; but I can't get a reference back to the INSERTs. The BLOCK_RECORD dictionary looks like this: {'doc': <ezdxf.document.Drawing object at X>, 'dxf': <ezdxf.entities.dxfns.DXFNamespace object at Y>, 'appdata': None, 'reactors': None, 'extension_dict': None, 'xdata': <ezdxf.entities.xdata.XData object at Z>, 'proxy_graphic': None, 'entity_space': <ezdxf.entitydb.EntitySpace object at A>, 'block': <class 'ezdxf.entities.block.Block'> BLOCK(#31A92), 'endblk': <class 'ezdxf.entities.block.EndBlk'> ENDBLK(#31B24), 'block_layout': <ezdxf.layouts.blocklayout.BlockLayout object at B>} The BLOCK_RECORD has no appdata and it has xdata; but the BLKREFs are not part of the xdata. B. block_layout = doc.blocks.get("NAME_OF_BLOCK_RECORD_AND_BLOCK") (this is an ezdxf.layouts.blocklayout.BlockLayout) From there I can get the start BLOCK using: block_layout.block I can also use this one; but it only returns the INSERTs that have as onwer the BlockLayout / BLOCK_RECORD. The INSERTs referenced in BLKREFS are not there ... for entity in block_layout.query('INSERT'): So my question: "is there a way of getting the INSERTs referenced in the BLKREFS section of a BLOCK_RECORD?" I can see them when I browse the file using "ezdxf browse"; so there must be a way ... Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
TLDR; I don't know why you are dealing with BLOCK_RECORDS, here is a tutorial how to use BLOCK layouts and block references (INSERT entities): https://ezdxf.mozman.at/docs/tutorials/blocks.html |
Beta Was this translation helpful? Give feedback.
-
Apologies for the long post and many thanks for the reply. I wish I didn't have to deal with BLOCK_RECORDS at all. The document has nested BLOCK (INSERT entities). The query returns either the first level of INSERTs or nothing: This one returns nothing: for flag_ref in msp.query('INSERT[name=="AN_ACTUAL_INSERT_NAME"]'): This one returns the first level of INSERTs (just five objects from a 10000+ object DXF document): for flag_ref in msp.query('INSERT'): This one returns the correct BLOCK (INSERT entity): for flag_ref in msp.query('INSERT[name=="AN_ACTUAL_INSERT_NAME_OF_THE_0TH_LEVEL"]'): Using the "ezdxf browse" application, I can see the relationships between a BLOCK_RECORD and the various INSERTs. I will have a look at the "ezdxf browse" code to see how the relationships are formed. Many thanks, |
Beta Was this translation helpful? Give feedback.
-
Many thanks for the suggestion. My point of entry used to be this line: msp = doc.modelspace()
for e in msp: I have now changed it to: for dxfentity in doc.chain_layouts_and_blocks(): and I get to see everything (or at least all the objects that I am interested in). I'll also try your suggestion. Thanks for the support |
Beta Was this translation helpful? Give feedback.
This function maybe can help: https://ezdxf.mozman.at/docs/disassemble.html#ezdxf.disassemble.recursive_decompose
And if this implementation doesn't fit your needs, look at the source code and make your own function:
ezdxf/src/ezdxf/disassemble.py
Line 542 in 4f5e04a