使⽤python将plt⽂件解析成json⽂件
from math import*
import jsonpython怎么读取json文件
class PltParser():
def__init__(self, filename,jsonFileName):
self.pathList =[]
self.tf =[]
# Just for test
self.plottersPerInch =1016.0
self.plotters2mmRatio = PerInch / self.plottersPerInch
# Just for test
if filename !=None:
self.parse(filename, jsonFileName)
def moveTo(self,x,y):
for i in range(len(self.tf)):
tf = self.tf[-1-i]
x1=tf[0]*x+tf[2]*y+tf[4]
y1=tf[1]*x+tf[3]*y+tf[5]
x=x1
y=y1
initpoint =[(x, y,203,180,0,-90)]
def lineTo(self,x,y):
for i in range(len(self.tf)):
tf = self.tf[-1-i]
x1=tf[0]*x+tf[2]*y+tf[4]
y1=tf[1]*x+tf[3]*y+tf[5]
x=x1
y=y1
point =(x, y,203,180,0,-90)
def plotters2mm(self, plotters):
return plotters * self.plotters2mmRatio
def plt2pt(self, line):
validLine = line[2:len(line)-1]
result = validLine.split(' ',1)
x, y =float(result[0]),float(result[1])
point =[self.plotters2mm(x), self.plotters2mm(y)]
return point
def parse(self, filename, jsonFileName):
with open(filename, encoding='utf-8')as file_obj:
line = adline()
currentPoint =[]
while line !='':
line = adline()
line = line.strip()
if line.find("PD")!=-1:
currentPoint = self.plt2pt(line)
self.lineTo(currentPoint[0], currentPoint[1])
elif line.find("PU")!=-1:
currentPoint = self.plt2pt(line)
for index in range(iginPathList)):
list=[]
for i in range(iginPathList[index])):
for i in range(iginPathList[index])):
list.append(iginPathList[index][i]).__dict__)
t =[]
t.append(Point(list).__dict__)
with open(jsonFileName+"_%s"%(index)+".json","w")as f:                json.dump(json.loads(json.dumps(t[0])),f)
f.close()
class P:
def__init__(self,data):
self.P = data
class Point:
def__init__(self,Point):
self.point = Point
if(__name__=="__main__"):
t_plt = pltObj.PltParser("./3.plt","test")

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。