-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNC Outline Optimize.py
110 lines (98 loc) · 2.9 KB
/
NC Outline Optimize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Document parsing script
# Replace newline characters with space
# except when immediately following a > symbol
FILEIN = '1smotl.nc'
FILEOUT = FILEIN.replace('.',' Optimum.',1)
LASERFEED = 1200
RAPIDFEED = 2000
LASERPOWER = 255
from random import randint
from math import sqrt
traversestr = "G01 X{0} Y{0} M03 S0 F{1}\n".format('{}',RAPIDFEED)
#print(traversestr)
#laserstr = "X{0} Y{0} M03 S{0} F{1}\n".format('{}',LASERFEED)
laserstr = "X{0} Y{0} M03 S{2} F{1}\n".format('{}',LASERFEED,LASERPOWER)
#print(laserstr)
#open the file and read it into memory as "data"
f = open(FILEIN, 'r')
data = f.read()
f.close()
pos_found = data.find("G01 ",0)
header = data[:pos_found]
# find the origin
XYOr = header.split()[-3:-1]
try: origin = (float(XYOr[0][1:]),float(XYOr[1][1:]))
except: origin = (0,0)
#print(origin)
# import all the points
points = []
loops = []
loop = []
search_from = pos_found +1
Y_end = 0
X_found = data.find("X",pos_found)
nextG1 = data.find("G01 ",X_found)
lastloop = False
while True:
Y_found = data.find("Y",X_found)
if Y_found == -1: break
X = float(data[X_found+1:Y_found-1])
Y_end = data.find(" ",Y_found)
if Y_end == -1: break
Y = float(data[Y_found+1:Y_end])
X_found = data.find("X",Y_end)
if X_found == -1: break
## p = (X,Y,loop)
## loop.append(p)
## points.append(p)
if X_found > nextG1:
if lastloop: break
nextG1 = data.find("G01 ",X_found)
if nextG1 == -1:
lastloop = True
nextG1 = data.find("M03 S0\nM05",Y_end)
loops.append(loop)
loop = []
else:
p = (X,Y,loop)
loop.append(p)
points.append(p)
loops.append(loop)
pos_found = data.find("M03 S0\nM05",Y_end)
footer = data[pos_found:]
print('collected',len(points),'points in',len(loops),'loops')
# process them
#sort the loops by length?
##for loop in loops:
## loop.append(len(loop))
##def k(l):
## return l[-1]
##loops.sort(key=k, reverse=True)
def dist(p1,p2):
return sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)
curpos = origin
dataout = ""
while len(points):
def close(p1): return dist(p1, curpos)
points.sort(key=close)
closest = points[0]
#print(closest[:-1])
loop = closest[2]
loops.remove(loop)
idx = loop.index(closest)
reordered = loop[idx+1:] + loop[:idx+1]
dataout += traversestr.format(closest[0],closest[1])
#print(loop)
for p in reordered:
#dataout += laserstr.format(p[0],p[1],randint(1,LASERPOWER))
dataout += laserstr.format(p[0],p[1])
points.remove(p)
#dataout += laserstr.format(pstrt[0],pstrt[1],randint(1,LASERPOWER))
#dataout += laserstr.format(pstrt[0],pstrt[1],LASERPOWER)
curpos = (closest[0],closest[1])
#save the file out
f = open(FILEOUT, 'w')
f.write(header)
f.write(dataout)
f.write(footer)
f.close()