本文介绍了Python 和 ezdxf 复制块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个或多个块的 dxf 文件.如何使用 ezdxf 读取此 dxf 并将块复制到另一个 dxf 文件?

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?

此代码未按预期工作:

dxf = ezdxf.readfile("blocks.dxf")
block_test = dxf.blocks.get('b_test')
dxf_test = ezdxf.readfile("arc.dxf")
msp_test = dxf_test.modelspace()
flag = dxf_test.blocks.new(name='FLAG')
flag.add_lwpolyline([(0, 0), (0, 5), (4, 3), (0, 3)])
flag.add_circle((0, 0), .4, dxfattribs={'color': 2}) 
msp_test.add_blockref(block_test, (10.1, 10.1), dxfattribs={
'xscale': 1,
'yscale': 1,
'rotation': 0
})

msp_test.add_blockref('flag', (0.1, 0.1), dxfattribs={
'xscale': 5.1,
'yscale': 5.1,
'rotation': 115
})

dxf_test.saveas("blockref_tutorial.dxf")
exit()

上面的代码示例没有按预期工作.也就是说,block_test"不在保存的文件中...

The above code sample does not work as expected. That is, ´block_test` is not in the saved file...

推荐答案

由于 DXF 格式复杂的扩展性以及实体描述之外的内部结构缺乏足够的文档,复制实体或移动实体并不是那么容易它们在一个 DXF 文件中,当然不在不同的 DXF 文件之间.

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.

为了完成这种任务ezdxf有一个进口商 附加组件,可以将源文档中的一些资源、实体和块定义导入到目标文档中,但不要期望完美的结果,请阅读 文档.

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.

以下代码将块定义 'b_test' 从源 DXF 文件 'blocks.dxf' 导入到目标 DXF 文件 'arc.dxf',导入完成后,可以在目标DXF文件的模型空间中添加对块'b_test'的块引用.

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' not in 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")

这篇关于Python 和 ezdxf 复制块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 01:28