Python和ezdxf复制块 [英] Python and ezdxf copying blocks

查看:654
本文介绍了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',导入完成后,您可以将对块'b_test'的块引用添加到模型空间目标DXF文件.

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复制块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆