`
青蜂侠
  • 浏览: 50783 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Python脚本读取DXF文件多边形顶点坐标

阅读更多

关于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)
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics