關於DXF文件格式說明在上一篇已經有較爲詳細的說明,由於這個程序是爲了特定項目所服務的,所以對文件有一定的限制條件:在DXF文件中只存在一個多邊形實體,獲取這個多邊形所有的頂點座標即可。
class Point:
'this class is used to record coordinate of vertex'
def __init__(self,x,y):
self.x = x
self.y = y
point是記錄點座標的數據結構
下面是相應的讀取處理代碼:
from Point import Point
class DxfReader:
def __init__(self,file):
self.file = file
#pointList record the vertexes of polyline
self.pointList = []
def readDXF(self):
line1 =""
line1 = self.file.readline()
line2=""
counter = 0
# record the sum of vertex
sumofVertext = 1
x = 0
y =0
for line in file:
#find the polyline
if line1.strip() == "0" and line.strip()== "LWPOLYLINE":
for linetemp in file:
if line2.strip() =="90":
numofVertex = int(linetemp.strip())
# while 10 occur,then the x is recorded
if line2.strip() == "10":
x = float(linetemp.strip())
if line2.strip() == "20":
y = float(linetemp.strip())
point = Point (x,y)
self.pointList.append(point)
counter += 1
if counter >= numofVertex:
break
line2 = linetemp
line1 = line
file.close()
if __name__=="__main__":
file = open("D:\\test.dxf")
reader = DxfReader(file)
reader.readDXF()
for temp in reader.pointList:
print str(temp.x) + " " + str(temp.y)