Hafees Kazhunkil
Hafees Kazhunkil

Reputation: 113

how to find length of entity from dxf file using dxfgrabber or ezdxf packages

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 ?

Upvotes: 3

Views: 5024

Answers (2)

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

Upvotes: 6

mozman
mozman

Reputation: 2239

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.

Upvotes: 1

Related Questions