如何使用ezdxf来查找镜像实体的位置,比如块/圆?

2024-04-20 07:27:19 发布

您现在位置:Python中文网/ 问答频道 /正文

如何计算已镜像的块或插入图元的位置?在

“wb”插入/块实体内有一个圆。我试着在msp上确定它的位置,然后画一个圆圈。在附加的DXF文件中有2个“wb”块,其中一个被镜像。在

DXF文件链接:https://drive.google.com/file/d/1T1XFeH6Q2OFdieIZdfIGNarlZ8tQK8XE/view?usp=sharing

import ezdxf
from ezdxf.math import Vector

DXFFILE = 'washbasins.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=3, dxfattribs={'layer': 'MyCircles'})

dwg.saveas(OUTFILE)

上述代码不适用于在AutoCAD文件中镜像的块。它的圆是在一个非常不同的位置画的。对于通过“镜像”命令放置的块,则entity.dxf.insert以及entity.dxf.旋转返回一个点和旋转,它与通过复制和旋转将块放置在那里时不同。在

在这种情况下请帮忙。同样,我们将如何处理线和圆实体?请共享python函数/代码。在


Tags: 文件dxfget镜像blockoffsetfirstcenter
1条回答
网友
1楼 · 发布于 2024-04-20 07:27:19

由于要获得相对于块定义基点的圆心,因此需要构造一个4x4变换矩阵,该矩阵对for循环中遇到的每个块参照的X-Y-Z比例、旋转和方向进行编码。在

库有用地包含了Matrix44 class,它将为您处理矩阵乘法。这样一个矩阵的构建将遵循以下原则:

import math
import ezdxf
from ezdxf.math import OCS, Matrix44

ocs = math.OCS(e.dxf.extrusion)
Matrix44.chain
(
    Matrix44.ucs(ocs.ux, ocs.uy, ocs.uz),
    Matrix44.z_rotate(e.get_dxf_attrib('rotation', 0)),
    Matrix44.scale
    (
        e.get_dxf_attrib('xscale', 1),
        e.get_dxf_attrib('yscale', 1),
        e.get_dxf_attrib('zscale', 1)
    )
)

然后,您可以使用该矩阵来transform圆心的坐标系(相对于块定义)到相对于块参考的坐标系,即对象坐标系(OCS)。在

转换之后,您还需要使用一个向量来转换坐标,该向量计算为块引用插入点与transformation后面的块定义基点之间的差。在

^{pr2}$

最终位置变成:

location = mat.transform(circle.dxf.center) + vec

相关问题 更多 >