主要使用了regex库匹配所需要的坐标值。
活不多说,直接上代码:
首先是我输入的G代码大概如下格式(这里简单贴部分)
G00 X 0.0000 Y 0.0000 Z 5.0000
M03
G00 X -1.8759 Y 14.4227 Z 5.0000
G01 X -1.8759 Y 14.4227 Z 0.0000 F 600.0
G01 X -1.7200 Y 14.5422 Z 0.0000 F 1200.0
G01 X -1.5892 Y 14.5778 Z 0.0000
G01 X -1.4803 Y 14.5440 Z 0.0000
G01 X -1.3643 Y 14.4162 Z 0.0000
G01 X -1.2539 Y 14.1600 Z 0.0000
G01 X -1.1642 Y 13.8954 Z 0.0000
G01 X -1.0960 Y 13.6249 Z 0.0000
G01 X -0.9348 Y 13.3434 Z 0.0000
G01 X -0.7566 Y 13.0719 Z 0.0000
G01 X -0.5625 Y 12.8120 Z 0.0000
G01 X -0.3196 Y 12.5231 Z 0.0000
G01 X -0.0614 Y 12.2475 Z 0.0000
G01 X 0.2110 Y 11.9862 Z 0.0000
G01 X 1.2239 Y 11.1648 Z 0.0000
python处理代码:
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 4 23:16:05 2019
@author: tanhao
"""
import numpy as np
import re
import os
f1=open('C:\\Users\\tanhao\\Desktop\\5.mmg','r') #输入.mmg刀具路径
data1=f1.readlines()
f1.close()
resultsX = []
resultsY = []
resultsZ = []
i=0
def save_txt(list1, list2,list3, save_path):
if os.path.isfile(save_path):
os.remove(save_path)
with open(save_path, "a") as f:
list4 = []
for i in range(len(list1)):
list4.append(i)
for i in range(len(list1)):
#=====================================================最终输出点=============================================#
f.write('{:} X Y Z = {:} {:} {:} \n'.format(list4[i],list1[i], list2[i],list3[i]) )
f.close()
for i in range(len(data1)):
data2=data1[i]
X = re.findall(r"X (.+?) Y", data2)
Y = re.findall(r"Y (.+?) Z", data2)
Z = re.findall(r"Z (.+?)", data2)
#print (m)
if X:
resultsX.append(X)
resultsY.append(Y)
resultsZ.append(Z)
i=i+1
#resultsX = arryX.tolist()
save_path = 'C:\\Users\\tanhao\\Desktop\\GETXYZ.txt' #输出位置
save_txt(resultsX, resultsY,resultsZ, save_path)
#去掉'
f4=open(save_path,'r')
data3=f4.read()
f4=data3.replace("'",'')
f5=open(save_path,'w')
f5.write(f4)
f5.close()
#去掉[
f4=open(save_path,'r')
data3=f4.read()
f4=data3.replace("[",'')
f5=open(save_path,'w')
f5.write(f4)
f5.close()
#去掉]
f4=open(save_path,'r')
data3=f4.read()
f4=data3.replace("]",'')
f5=open(save_path,'w')
f5.write(f4)
f5.close()
其中核心在于
X = re.findall(r"X (.+?) Y", data2)
Y = re.findall(r"Y (.+?) Z", data2)
Z = re.findall(r"Z (.+?)", data2)
分别代表了:
后面的一小部分是为了去掉元素中多余的部分
0 X Y Z = 0.0000 0.0000 5
1 X Y Z = 1.1462 -1.5935 5
2 X Y Z = 1.1462 -1.5935 0
3 X Y Z = 1.3246 -1.4136 0
4 X Y Z = 1.5298 -1.2583 0
5 X Y Z = 1.7563 -1.1334 0
6 X Y Z = 1.9971 -1.0427 0
7 X Y Z = 2.2445 -0.9879 0
8 X Y Z = 2.8321 -0.9617 0
9 X Y Z = 3.4202 -0.9742 0
10 X Y Z = 3.5807 -0.9993 0
11 X Y Z = 3.7365 -1.0483 0
12 X Y Z = 3.8824 -1.1198 0
13 X Y Z = 4.5067 -1.5934 0
14 X Y Z = 4.6786 -1.7566 0
15 X Y Z = 4.8314 -1.9401 0
16 X Y Z = 4.9615 -2.1402 0
NIiiiiiiCE! 搞定!