Python And Ezdxf Copying Blocks
I have a dxf file with one or more blocks. How can I use ezdxf to read this dxf and copy a block to another dxf file? This code does not work as expected: dxf = ezdxf.readfile('blo
Solution 1:
Because of the complex extensibility of the DXF format and the lack of sufficient documentation of the internal structures beyond entity descriptions, it is not that easy to copy entities or move them inside of a DXF file and certainly not between different DXF documents.
To accomplish this kind of task ezdxf has an Importer add-on, which can import some resources, entities and block definitions from a source document into a target document, but don't expect perfect results and please read the docs.
The following code imports the block definition 'b_test'
from the source DXF file 'blocks.dxf'
into the target DXF file 'arc.dxf'
, after the import is done, you can add block references to block 'b_test'
to the modelspace of the target DXF file.
import ezdxf
from ezdxf.addons import Importer
source_dxf = ezdxf.readfile("blocks.dxf")
if'b_test'notin source_dxf.blocks:
print("Block 'b_test' not defined.")
exit()
target_dxf = ezdxf.readfile("arc.dxf")
importer = Importer(source_dxf, target_dxf)
importer.import_block('b_test')
importer.finalize()
msp = target_dxf.modelspace()
msp.add_blockref('b_test', insert=(10, 10))
target_dxf.saveas("blockref_tutorial.dxf")
Post a Comment for "Python And Ezdxf Copying Blocks"