0

如何计算已镜像的块或插入实体的位置?

在“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.rotation 返回的点和旋转与通过复制和旋转放置块的情况不同。

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

4

1 回答 1

1

由于您正在获取相对于块定义基点的圆心,因此您需要构建一个 4x4 变换矩阵,该矩阵对for循环中遇到的每个块参考的 XYZ 比例、旋转和方向进行编码。

ezdxf库有用地包含,它将为您处理矩阵乘法。这样一个矩阵的构造将遵循以下思路:

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

然后,您可以使用此矩阵圆心的坐标从相对于块定义的坐标系转换为相对于块参考的坐标系,即对象坐标系 (OCS)。

转换后,您还需要使用一个向量平移坐标,该向量计算为使用上述矩阵进行转换后的块参考插入点和块定义基点之间的差。

mat = Matrix44.chain ...
vec = e.dxf.insert - mat.transform(block.dxf.base_point) 

那么最终的位置就变成了:

location = mat.transform(circle.dxf.center) + vec
于 2019-12-18T12:19:24.273 回答