0

如何从 dxf 文档中的 LWPOLYLINE 获取 LINES 坐标?

我有以下代码:

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"

以上for获取块“INSERT”并将它们拆分为我只能得到LINES。我尝试为 LWPOLYLINE 做同样的事情,但没有奏效。

下面的代码获取所有坐标,但我需要逐行过滤并获取坐标:

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

下面的代码突然停止工作,我认为库 ezdxf 发生了变化。我收到错误

“LWPolyline”对象没有属性“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"
4

1 回答 1

0

如果您只是想在实体 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

希望它有点帮助。

于 2022-02-18T15:43:17.710 回答