How to Import a Block From a Different DXF File #1221
Unanswered
yuanshanhuhuan
asked this question in
Q&A
Replies: 1 comment 1 reply
-
This is not a CAD application! There is no magic behind the scenes. Referencing a block from a different DXF file just don't work that way. This example shows how to import a block from a different DXF file by the import ezdxf
from ezdxf import xref
SRC_BLK_NAME = "JustAnyBlockName"
def create_source_doc():
doc = ezdxf.new()
blk = doc.blocks.new(SRC_BLK_NAME)
blk.add_circle((0, 0), radius=5)
return doc
def main():
sdoc = create_source_doc()
tdoc = ezdxf.new()
# import data from sdoc into tdoc
loader = xref.Loader(sdoc, tdoc)
blk = sdoc.blocks.get(SRC_BLK_NAME)
# import block layout SRC_BLK_NAME
loader.load_block_layout(blk)
# run import process
loader.execute()
# test if SRC_BLK_NAME was imported
assert SRC_BLK_NAME in tdoc.blocks
msp = tdoc.modelspace()
# create a new block reference to the imported block
msp.add_blockref(SRC_BLK_NAME, insert=(5, 5))
tdoc.saveas("imported_block.dxf")
if __name__ == "__main__":
main() |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am currently using Python 3.12.3 and the ezdxf library version 1.3.0 to manipulate DXF files. I am trying to insert a block named "防火锁" from the "9-114.dxf" file into the "9-118.dxf" file. The code I am using seems to run without throwing any exceptions. It successfully reads the source file, locates the block, and attempts to insert it into the target file. However, the block does not appear in the target "9-118.dxf" file after the insertion process. I can see from the print statements that the block is found in the source file and the insertion operation is executed, but when I open the target file, there is no visible trace of the inserted block.
Code Used:
Additional Information:
Python Version: 3.12.3
ezdxf Version: 1.3.0
The "9-114.dxf" and "9-118.dxf" files were created using [specify the software used to create them, if known].
I have already tried several troubleshooting steps, but none of them worked:
I have checked the layer visibility of both files, and all layers seem to be properly configured.
I have adjusted the block's scale, rotation, and position, but it still does not show up.
I have verified the integrity of the block definitions in both files, and they seem to be fine.
I have also checked the entity types within the block, and there does not seem to be anything unusual.
I have ensured that the files exist and are accessible.
I have tried using different DXF versions and simple test DXF files with basic blocks, but the problem persists.
Expected Result:
I expect the "防火锁" block from the "9-114.dxf" file to be inserted into the "9-118.dxf" file and be visible when I open the target file using a DXF viewer.
Actual Result:
The block appears to be inserted (as indicated by the print statements), but when I open the "9-118.dxf" file, there is no visible representation of the inserted block.
This issue has been causing me a lot of trouble, and I would appreciate any help or suggestions on how to resolve it. Thank you very much!
Beta Was this translation helpful? Give feedback.
All reactions