如何组织.DXF文件中的数据

时间:2015-12-17 15:44:21

标签: class dxf

我需要拆开GDS文件对其进行更详细的分析。

特别是我需要计算设计图层的数量,每层中折线的数量以及设计中每条折线的顶点。
我的麻烦是创建包含所有信息的类。最后,我希望能够通过指定类似

的内容来获取每个顶点的数据

Lyr [5] .pl [3] .vertx [7]其中5是第五层,3是该层中的第三条折线,7是该折线上的第七个顶点。

我可以让图层和折线很好但是无法理解如何开始添加顶点。

顺便说一句,我不是直接使用GDS文件,而是使用名为.DXF的格式 每行文本文件一个简单的单个字符串。

到目前为止,这是我的代码。

import sys   #see sys modules using goggle
import string
import math

class layer:
    def __init__(self,layer):
        self.layer=layer
        self.nplines=0
        self.pl=[]

    def add_pline(self,y):
        self.pl.append(y)
    def add_nplines(self):
        self.nplines+=1

'''
class vertx     
    def __init__(self,n):     ## Running out of ideas here

'''     

def main():
    my_file=sys.argv[1]
    inFile=open(my_file,'r')
    lyr=[]
    nlayers=-1

##  Get the layers  
    while 1:
        s=inFile.readline()
        if "CONTINUOUS" in s : 
            nlayers+=1
            lyr.append(0)
            s=inFile.readline()  #burn one line in DXF file

            s=inFile.readline()   #name of the layer

            lyr[nlayers]=layer(s)  # save the layer

        if 'POLYLINE' in s: break

    inFile.close()

##  Get the polylines

    inFile=open(my_file,'r')

    while 1:
        s=inFile.readline() 
        if 'POLYLINE' in s: 
            s=inFile.readline()   #burn a line 
            s=inFile.readline()   #layer name 
            for i in range(0,nlayers+1):
                if s==lyr[i].layer:
                    lyr[i].add_nplines()

        if 'EOF' in s: break

    inFile.close()

    for i in range(0,nlayers+1):
        print i,'Layer=',lyr[i].layer,'   no plines= ',lyr[i].nplines


main()

1 个答案:

答案 0 :(得分:2)

您可以使用我的ezdxf包来处理DXF文件。它可以在PyPI https://pypi.python.org/pypi/ezdxf上找到。

import ezdxf

dwg = ezdxf.readfile("your.dxf")
msp = dwg.modelspace()  # contains all drawing entities

polylines = msp.query("POLYLINE")  # get all polylines in modelspace
for polyline in polylines:
    layer = polyline.dxf.layer  # layername as string
    points = polyline.points()  # all vertices as (x, y [,z]) tuples 
    # for more see http://ezdxf.readthedocs.org

ezdxf处理所有DXF版本,还可以将数据附加到现有DXF文件或创建新的DXF文件。

相关问题