我正在尝试使用ezdxf将实体添加到现有.dxf文件的模型空间中。插入实体的位置完全偏离我期望的位置。
对于圆形,我通过e.dxf.insert获取实体的位置坐标,并将此点用作圆的中心。我使用了以下代码:
import ezdxf
dwg = ezdxf.readfile("drainage.dxf")
msp = dwg.modelspace()
dwg.layers.new(name='MyCircles', dxfattribs={'color': 7})
def encircle_entity(e):
if e.dxftype()=='INSERT':
circleCenter = e.dxf.insert
msp.add_circle(circleCenter, 10, dxfattribs={'layer': 'MyCircles'})
print("Circle entity added")
washBasins = msp.query('*[layer=="WASH BASINS"]')
for e in washBasins:
encircle_entity(e)
dwg.saveas('encircle.dxf')
链接到drainage.dxf(输入)和encircle.dxf(输出)文件:https://drive.google.com/open?id=1aIhZiuEdClt0warjPPcKiz4XJ7A7QWf_
这会创建一个圆圈,但位置不正确。
dxf文件中的原点和ezdxf使用的原点在哪里?如何获得所有实体的正确位置,尤其是INSERT,LINES和CIRCLES?如何使用ezdxf将实体放置在已存在的dxf文件中的所需位置?相对于坐标,线的e.dxf.start和e.dxf.end点在哪里?
我想我在这里缺少坐标。请解释坐标如何工作。
Python版@LeeMac解决方案,但忽略了OCS:
import ezdxf
from ezdxf.math import Vector
DXFFILE = 'drainage.dxf'
OUTFILE = 'encircle.dxf'
dwg = ezdxf.readfile(DXFFILE)
msp = dwg.modelspace()
dwg.layers.new(name='MyCircles', dxfattribs={'color': 4})
def get_first_circle_center(block_layout):
block = block_layout.block
base_point = Vector(block.dxf.base_point)
circles = block_layout.query('CIRCLE')
if len(circles):
circle = circles[0] # take first circle
center = Vector(circle.dxf.center)
return center - base_point
else:
return Vector(0, 0, 0)
# block definition to examine
block_layout = dwg.blocks.get('WB')
offset = get_first_circle_center(block_layout)
for e in msp.query('INSERT[name=="WB"]'):
scale = e.get_dxf_attrib('xscale', 1) # assume uniform scaling
_offset = offset.rotate_deg(e.get_dxf_attrib('rotation', 0)) * scale
location = e.dxf.insert + _offset
msp.add_circle(center=location, radius=1, dxfattribs={'layer': 'MyCircles'})
dwg.saveas(OUTFILE)
平面物体(例如弧形,圆形,二维多段线(LWPOLYLINEs
),块参考(INSERTs
),仅举几例)是相对于为它们所在平面计算的物体坐标系(OCS)定义的。
该坐标系与世界坐标系(WCS)具有相同的原点,但X和Y轴矢量使用Arbitrary Axis Algorithm计算给定的挤压矢量或垂直于平面物体所在平面的法线。
我可以看到你当前的代码在位于INSERTs
层的所有Block References(WASH BASINS
)的插入点坐标处生成Circles。
每个块参考的插入点坐标相对于使用与块参考相关联的挤出矢量(DXF组210
)计算的OCS表示。
圆的中心点坐标也相对于圆的OCS表示,因此,为了匹配块参考的位置,您需要提供add_circle
方法的块参考的挤出矢量,这样两者都可以插入点坐标和中心坐标相对于同一坐标系表示。
因此,代码应该变成:
def encircle_entity(e):
if e.dxftype()=='INSERT':
circleCenter = e.dxf.insert
msp.add_circle(circleCenter, 10, dxfattribs={'layer': 'MyCircles', 'extrusion': e.dxf.extrusion})
print("Circle entity added")