0

I'm looping through all entities in the modelspace and generating a representative set of points for each entity type and storing them in a json file. Some entities like INSERT entities have an object coordinate system different from the world coordinate system. I'm using the OCS.to_wcs() function to convert the points into world coordinates. This works almost perfectly. Unfortunately some entities apparently are mirrored about the Y-axis in a way that is not accounted for with the OCS.to_wcs() function. Is there some other transformation beyond the .to_wcs that is required?

My current code creates a set of sets of points called pointset for a particular entity. This is necessary because an entity can have two crossed lines for example. The following code is the approach I'm using to transform each point set into the world coordinates.

    wpointset=[]
    for piece in pointset: #go through each object in the point set
        piecepts=[]
        for pt in piece: #go through each point in each object

            piecepts.append([ocs.to_wcs(pt)[0],ocs.to_wcs(pt)[1]])

        wpointset.append(piecepts)

1 Answers1

0
  1. the __iter__ protocol is not the correct way to iterate the points of an entity, only LWPOLYLINE works with your code
  2. the OCS depends on the extrusion vector of the entity and is not the same for all entities, you have to acquire the OCS for each entity individually
  3. the INSERT entity is another special case because rotation and scaling is also involved and you have to use this code to get the WCS origin:
doc = ezdxf.readfile("your.dxf")
msp = doc.modelspace()

wcs_insert_points: list[tuple[float, float]] = []
for insert in msp.query("INSERT"):
    ucs = insert.ucs()
    wcs_insert_points.append((ucs.origin.x, ucs.origin.y))

What you try to achieve is a precursor to DXF rendering and it's not as easy as you might have noticed. You can look at the Frontend class of the drawing add-on how to do it correctly: https://github.com/mozman/ezdxf/blob/master/src/ezdxf/addons/drawing/frontend.py

mozman
  • 2,001
  • 8
  • 23
  • Thank you for the prompt response, and the amazing library. My code snippet and description didn't clarify that I am obtaining a new ocs for each entity. the code snippet above is run for each entity. I'm not iterating through points of an entity. Each entity goes through its own transformation into point sets. The entities are entirely a list of points by the time they are processed by the code snippet above. I've been reading through the frontend.py script and have also reviewed your example code using ezdxf to convert DXF into bitmaps and SVG. – A Baker Aug 29 '23 at 11:40