Hiren Kakkad
Hiren Kakkad

Reputation: 15

How to get dimensions of the shapes in .dxf file using ezdxf package in Python?

I am having drawing files in .dxf format. Each files consists of different shapes as shown in below image.

Drawing

I want to read all the dimensions using Python. I come to know about ezdxf package. So I was able to load the file in Python environment and got the shapes which are available in drawing using below code, but I don't know how to get dimensions.

Thanks in advance.

import ezdxf as ez
import sys

doc = ez.readfile("Drawing1.dxf")
msp = doc.modelspace()

for i in msp:
    print(i)

Output

 - LWPOLYLINE(#259)
 - CIRCLE(#261)
 - LWPOLYLINE(#262)
 - ELLIPSE(#264)
 - LINE(#265)
 - CIRCLE(#266)

Upvotes: 0

Views: 2140

Answers (1)

Nyasha Musanhu
Nyasha Musanhu

Reputation: 29

You just have to specify like it is given in this example:

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])**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.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))
            longitude total = longitud_total+ ds

Note: the last part is not working due to the package being depreciated, but you could get a few insights from that. I got my outputs as shown below: just add the computations to your code and get your required dimensions.

LWPOLYLINE(#77)
CIRCLE(#79)
radio: 1042.834851203736
circulo: 6552.324614898124
CIRCLE(#7A)
radio: 973.9919030240113
circulo: 6119.771614392353
CIRCLE(#7B)
radio: 922.8928365325164
circulo: 5798.706710602399
LWPOLYLINE(#7C)
ARC(#7D)
SPLINE(#7E)

Upvotes: 3

Related Questions