Matt G
Matt G

Reputation: 1

How to organize data from a .DXF file

I need to take apart a GDS file to do more detailed analysis on it.

In particular I need to count the numbers of design layers, the number of polylines in each layer and the vertices of each of the polyline for a design.
My trouble is creating class(s) with all that information. In the end I would like to be able to get the data for each vertex by specifying something like

Lyr[5].pl[3].vertx[7] Where 5 is the fifth layer, 3 is the third polyline in that layer and 7 is the seventh vertex on that polyline.

I can get the layers and polylines fine but can't get my mind around how to start adding the vertices.

BTW, I am not working directly with a GDS file but a format called .DXF which is a simple single string per line text file.

Here is my code so far.

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()

Upvotes: 0

Views: 1802

Answers (1)

mozman
mozman

Reputation: 2239

You can use my ezdxf package to handle DXF files. It is available on 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 handles all DXF versions and can also append data to existing DXF files or create new DXF files.

Upvotes: 2

Related Questions