python dxf import_Python和ezdxf复制块

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("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()

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

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值