3

I am trying to find total length(perimeter),area of a spline from dxf file. Is there any function in dxfgrabber or ezdxf to find total length of an entity from dxf file ?

  • You need to think about using a library like Teigha https://www.opendesign.com/the_oda_platform/Teigha which will provide you full functionality for not only reading the DXF / DWG data but also working out useful things. Splines are complicated elements because there are so many variations to way data is curved and that is going to affect the line length. I would use a library like this. – Andrew Truckle Apr 16 '16 at 19:14

2 Answers2

6

You can iterate over the entities, each entity has coordinates of the lines that make it up, then you can use these points to calculate the length of the entity. This code calculates the length of straight lines, curves and circles in a .DXF file

import ezdxf
import math

dwg = ezdxf.readfile("arc.dxf")
msp = dwg.modelspace()
longitud_total = 0
for e in msp:
    print(e)
    if e.dxftype() == 'LINE':
        dl = math.sqrt((e.dxf.start[0]-e.dxf.end[0])**2 + (e.dxf.start[1]- 
        e.dxf.end[1])**2)
        print('linea: '+str(dl))
        longitud_total = longitud_total + dl
    elif e.dxftype() == 'CIRCLE':
        dc = 2*math.pi*e.dxf.radius
        print('radio: '+str(e.dxf.radius))
        print('circulo: '+str(dc))
        longitud_total = longitud_total + dc
    elif e.dxftype() == 'SPLINE':
        puntos = e.get_control_points()
        for i in range(len(puntos)-1):
            ds = math.sqrt((puntos[i][0]-puntos[i+1][0])**2 + (puntos[i][1]- 
            puntos[i+1][1])**2)  
            print('curva: '+str(ds))
            longitud_total = longitud_total + ds


print('Longitud Total: '+ str(longitud_total))
  • 1
    You'd paste the code to your answer so it could help someone else some time after. External sources may remove information without any notice. – Arfeo Apr 05 '19 at 19:49
  • e.get_control_points() does not work at the version 0.15.2 "'Spline' object has no attribute 'get_control_points'" – TomK Mar 13 '21 at 16:50
1

dxfgrabber and ezdxf are just interfaces to the DXF format and do not provide any kind of CAD or calculation functions, and the geometrical length of DXF entities are not available attributes in the DXF format.

mozman
  • 2,001
  • 8
  • 23