0

How can I get the LINES coordinates from a LWPOLYLINE in a dxf doc?

I have the following code:

import sys
import ezdxf
Coordenadas = str()
Coordenadas_texto = str()

try:
    doc = ezdxf.readfile(str(arq)+".dxf")
except IOError:
    print('Erro de arquivo. Certifique-se que é um arquivo .dxf')
    sys.exit(1)
except ezdxf.DXFStructureError:
    print('Arquivo invalido ou corrompoido.')
    sys.exit(2)       

msp = doc.modelspace()
            
for insert in msp.query('INSERT'):
    block = doc.blocks[insert.dxf.name]
    for e in block:
        if e.dxftype() == 'LINE':
        
            Coordenadas = Coordenadas + str(e.dxf.start) + "\n"              
            Coordenadas = Coordenadas + str(e.dxf.end) + "\n"

The for above gets the block "INSERT" and splits them to I can get only LINES. I tried to do the same thing for LWPOLYLINE but it didn't work.

The code below gets all coordinates but I need to filter and get coordinates line by line:

for flag_ref in msp.query('LWPOLYLINE'):
    for entity in flag_ref:
            print(entity)

The code below stopped working suddenly, I think the library ezdxf changed. I'm getting the error

'LWPolyline' object has no attribute 'virtual_entities'

for flag_ref in msp.query('LWPOLYLINE'):
    for entity in flag_ref.virtual_entities():
        if entity.dxftype() == 'LINE':
            Coordenadas = Coordenadas + str(entity.dxf.start)+ "\n"
            Coordenadas = Coordenadas + str(entity.dxf.end)+ "\n"
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • I do not use this library but have you Googled it? For example: https://ezdxf.readthedocs.io/en/stable/tutorials/lwpolyline.html. It refers to `with line.points("xyseb") as points:`. – Andrew Truckle Jan 07 '22 at 22:19

1 Answers1

0

Here is my method if you were merely intended to find all line entities in entity lwpolyine:

def process_lwpolyline(e:LWPolyline):
   lines = []
   points = e.get_points()
   """
    [ref](https://ezdxf.readthedocs.io/en/master/dxfentities/lwpolyline.html)
    get_points: Returns point at position index as (x, y, start_width, end_width, bulge) tuple. 
   """
   for i, point in enumerate(points):
      if i==0:  continue
      # I ignored a arc_shape line and force it to be a straight one in here
      line_start = (points[i-1][0], points[i-1][1])
      line_end = (points[i][0], points[i][1]]) 
      line = [ line_start, line_end]
      lines.append(line)
   if hasattr(e, 'virtual_entities'):
     for e2 in e.virtual_entities():
        dtype = e2.dxftype()
        if dtype = 'LINE':
           line = [ e2.dxf.start, e2.dxf.end]
           lines.append(line)           
        elif dtype == 'LWPOLYLINE':
           lines += process_lwpolyline(e2)
   return lines

Hope it helps a bit.

Jog Dan
  • 171
  • 3
  • 8