Search code examples
pythondxfezdxf

Get circles x and y center


My target is to get all circles from dxf file with 3 information like: circumference, X center, Y center. So far i am able to get circumference. How can i get Y & X? This is my current code:

import sys
import ezdxf

doc = ezdxf.readfile("File.dxf")
msp = doc.modelspace()

for e in msp:    
    if e.dxftype() == 'CIRCLE':
            dc = 2 * math.pi * e.dxf.radius
            print('circumference: ' + str(dc))

Solution

  • The center of the circle is e.dxf.center as Vec3 object in the Object Coordinate System (OCS). The OCS is the WCS if the extrusion vector is (0, 0, 1), which is the case most of the time for 2D entities.

    Sometimes mirrored 2D entities have an inverted extrusion vector (0, 0, -1) in this case, it is necessary to transform the OCS coordinates into WCS coordinates:

    for e in msp.query("CIRCLE"):
        ocs = e.ocs()
        wcs_center = ocs.to_wcs(e.dxf.center)
        x = wcs_center.x
        y = wcs_center.y