pythonautocaddxfezdxf

How to find which Lines and Arcs are connected when imported from DXF?


I am using ezdxf to import a .dxf file into Python. It works smoothly. I get a list of lines and arcs.

How do I figure out which lines and arcs are connected to each other? Are they somehow indexed or do I need to search start and end points of lines and arcs and match them afterwards?

What I need to find are the closed lines in the .dxf file.


Solution

  • You have to match line and arc end points manually.

    Get the end points of arcs with default extrusion (0, 0, 1):

    from ezdxf.math import Vector
    
    start_point = Vector.from_deg_angle(arc.dxf.start_angle, arc.dxf.radius)
    end_point = Vector.from_deg_angle(arc.dxf.end_angle, arc.dxf.radius)
    

    Add to arc center:

    s = start_point + arc.dxf.center
    e = end_point + arc.dxf.center
    

    If the Object Coordinate System (OCS) defined by arc.dxf.extrusion is different from (0, 0, 1), a transformation to the World Coordinate System (WCS) has to be done:

    ocs = arc.ocs()
    s = ocs.to_wcs(s)
    e = ocs.to_wcs(e)
    

    Next ezdxf version v0.11 will have Arc.start_point and Arc.end_point properties, which will return the WCS coordinates.

    Important: Don't compare with the equal operator (==), use math.isclose() or better Vector.isclose() like:

    s.isclose(line.dxf.start, abs_tol=1e-6)
    e.isclose(line.dxf.start, abs_tol=1e-6)
    s.isclose(line.dxf.end, abs_tol=1e-6)
    e.isclose(line.dxf.end, abs_tol=1e-6)
    

    Set absolute tolerance according to your needs.