0

How do you calculate the location of a block or an insert entity that has been mirrored?

There is a circle inside a 'wb' insert/block entity. I'm trying to identify it's location on msp and draw a circle it. There are 2 'wb' blocks in the attached DXF file, one of which is mirrored.

DXF File link: 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)

The above code doesn't work for the block that is mirrored in the AutoCAD file. It's circle is drawn at a very different location. For a block placed through the mirror command, the entity.dxf.insert and entity.dxf.rotation returns a point and rotation that is different than that if the block was placed there by copying and rotating.

Kindly help in such cases. Similarly, how will we handle lines and circle entities? Kindly share python functions/code for the same.

1 Answer 1

1

Since you are obtaining the circle center relative to the block definition base point, you will need to construct a 4x4 transformation matrix which encodes the X-Y-Z scale, rotation & orientation of each block reference encountered within your for loop.

The library usefully includes the Matrix44 class which will take care of the matrix multiplication for you. The construction of such a matrix will be something along the lines of the following:

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

You can then use this matrix to transform the coordinates of the circle centre from the coordinate system relative to the block definition, to that relative to the block reference, i.e. the Object Coordinate System (OCS).

After transformation, you will also need to translate the coordinates using a vector calculated as the difference between the block reference insertion point and the block definition base point following transformation using the above matrix.

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

Then the final location becomes:

location = mat.transform(circle.dxf.center) + vec
1
  • Would it be possible if you could explain what vectors to transform using this matrix, their relationship with the base point and how to use this matrix function object? Dec 19, 2019 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.