Extracting all lines (in the generic sense) from DXF #365
Replies: 5 comments 4 replies
-
Remark to your polyline processing: The possible problems:
import ezdxf
def process_lines(entities):
for e in entities:
print(str(e))
dxftype = e.dxftype()
if dxftype == "LINE":
print(e.dxf.start)
print(e.dxf.end)
elif dxftype == "POLYLINE":
if e.is_2d_polyline or e.is_3D_polyline:
# other types: is_polygon_mesh and is_poly_face_mesh
# 2D polylines can have an extrusion != (0, 0, 1) and bulges
# see LWPOLYLINE processing.
for point in e.points():
print(point)
elif dxftype == "LWPOLYLINE":
# vertices_in_wcs() returns also correct WCS locations for LWPOLYLINE
# entities placed in 3D space, OCS with extrusion != (0, 0, 1).
# An extrusion (0, 0, -1) can also happen by mirroring block references!
# LWPolylines can also contain bulges (arcs), in this case a bulge
# is processed as straight line.
for point in e.vertices_in_wcs():
print(point)
elif dxftype == "SPLINE":
# flatten curve with max distance of 0.01 drawing units between
# spline and approximation polyline.
for point in e.flattening(0.01):
print(point)
def process(entities):
process_lines(entities)
# recursive processing of block references:
for e in entities:
if e.dxftype() == 'INSERT':
# create virtual entities based on the block definition
# and store the content in a list, virtual_entities() returns
# a generator. The content will be transformed into the parent
# coordinate system:
process(list(e.virtual_entities()))
doc = ezdxf.readfile("your.dxf")
# Start processing at the top level: model space
process(doc.modelspace()) NEW in V0.16 (not released yet, beta version on PyPI): disassemble DXF entities into primitives import ezdxf
from ezdxf import disassemble
doc = ezdxf.readfile("your.dxf")
msp = doc.modelspace()
# recursive_decompose() dissolves nested layout structures into a flat stream of
# DXF entities:
lines = ( # generator
e for e in disassemble.recursive_decompose(msp)
if e.dxftype() in {'LINE', 'POLYLINE', 'LWPOLYLINE', 'SPLINE'}
)
for e in lines:
print(str(e))
# create a primitive with a common interface, flattens bulges
# into polylines and handles OCS for 2D entities:
p = disassemble.make_primitive(e, max_flattening_distance=0.01)
for vertex in p.vertices():
print(vertex) |
Beta Was this translation helpful? Give feedback.
-
Thanks for your prompt response, I went for option 2 since this is much clearer to me. The code increases my saved set of vertices from 19,000 to 75,000. If I slice it and take every 200th set of coordinates and export to my other software (which can't handle the whole dataset) unfortunately the picture looks almost exactly the same as before. I get an exception for "for vertex in p.vertices():" periodically, which I bypass with a try/except, however maybe this is key to the missing lines? Exception as follows:
-- Can you explain the max_flattening_distance parameter a little more? Is it perhaps generating too many component lines? Note I am working in the km range, so my coordinates are in the order of: x= 700555.6....., y = 5704368.5..... is a flattening distance of 0.01 much too small? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I fixed the error caused by SPLINE entities, but only 517 SPLINE entities were skipped. These are all existing entity types in your DXF file: You can also include CIRCLE, ARC and ELLIPSE into the entity filter for flattening, maybe these are the missing objects. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Hi,
I'm not the best Python programmer, but I have a problem I'm hoping to get some help with. I have a 2D plan view of pipelines etc (i.e. a 2D map with a series of polylines) in DXF, however when I've tried to export using ezdxf and my own code I only get some of the polylines, not the complete set despite them all being identified as polylines in AutoCAD. I was wondering if they were different entity types so I've been trying to export any coordinates I can get hold of.
Original AutoCAD DXF:
Export using code below:
Can anyone please give me any guidance on what I need to try?
Here is a copy of my hacky loop that tries to get any kind of coordinate it can and dump it to my Excel file:
for entity in msp:
print('Entity #{}'.format(entity.dxf.handle))
Thanks,
Russell.
Beta Was this translation helpful? Give feedback.
All reactions