-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunbreak.py
135 lines (123 loc) · 6.23 KB
/
unbreak.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
from glob import glob
import re
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--path", required=False, default='/home/pdavies/models/tv-remote/', help="The path to .nc files which need conversion")
args = parser.parse_args()
lines_to_remove = ['(When using Fusion 360 for Personal Use, the feedrate of)',
'(rapid moves is reduced to match the feedrate of cutting)',
'(moves, which can increase machining time. Unrestricted rapid)',
'(moves are available with a Fusion 360 Subscription.)']
nc_folder = args.path
matching_filenames = glob(nc_folder + '*.nc')
# matching_filenames = glob(nc_folder + 'detail.nc')
fp_pattern = '[-+]?(\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?'
def isXY(line_str):
return re.match('X%s Y%s' % (fp_pattern, fp_pattern), line_str)
WAITING_FOR_FIRST_Z = 1
WAITING_FOR_MOVE_LINE = 2
WAITING_FOR_FINAL_Z = 3
WAITING_FOR_POSITIVE_Za = 4
WAITING_FOR_G0 = 5
WAITING_FOR_POSITIVE_Zb = 6
# print(re.match('Z%s' % fp_pattern, 'Z-6.5 F333.3'))
# print(re.match('Z%s ' % fp_pattern, 'Z-6.5'))
# exit(2)
for matching_filename in matching_filenames:
if '-fm.nc' not in matching_filename:
with open(matching_filename, 'r') as infile:
parts = os.path.split(matching_filename)
fname = parts[1]
basename = fname[:-len('.nc')]
lines = infile.readlines()
xylines = 0
# print(len(lines), 'lines in file')
with open(nc_folder + str(basename) + '-fm.nc', 'w') as outfile:
xzx_state = WAITING_FOR_FIRST_Z
zmove_state = WAITING_FOR_POSITIVE_Za
xyline = None
second_zs = 0
za_line = None
g0_line = None
line_num = 0
last_line = ''
for line in lines:
line_num += 1
unterminated_line = line[:-1]
if any(line_to_remove in line for line_to_remove in lines_to_remove):
# print('One of those comment lines')
continue
if zmove_state == WAITING_FOR_POSITIVE_Za:
if re.match('Z%s ' % fp_pattern, unterminated_line):
try:
zval = float(unterminated_line[1:])
if zval > 0.0:
print('Got first +Z')
# Save this line
za_line = unterminated_line
zmove_state = WAITING_FOR_G0
except ValueError:
# print('Failed to parse line %s:%d: %s' % (matching_filename, line_num, unterminated_line))
pass
elif zmove_state == WAITING_FOR_G0:
if isXY(unterminated_line):
# save this line
print('save XY line')
g0_line = unterminated_line
zmove_state = WAITING_FOR_POSITIVE_Zb
else:
zmove_state = WAITING_FOR_POSITIVE_Za
elif zmove_state == WAITING_FOR_POSITIVE_Zb:
if re.match('Z%s' % fp_pattern, unterminated_line):
zval = float(unterminated_line[1:])
if zval < 0.0:
print('Got second +Z')
# write first Z line,
# rapid move G0,
# second Z line
# outfile.write('### %s\n' % za_line)
outfile.write('(%s)\n' % g0_line)
# outfile.write('### %s\n' % unterminated_line)
second_zs += 1
zmove_state = WAITING_FOR_POSITIVE_Za
else:
zmove_state = WAITING_FOR_POSITIVE_Za
else:
zmove_state = WAITING_FOR_POSITIVE_Za
if xzx_state == WAITING_FOR_FIRST_Z:
if line.startswith('Z') and re.match('Z%s' % fp_pattern, unterminated_line):
# still output this line
print(line)
outfile.write(line)
xzx_state = WAITING_FOR_MOVE_LINE
else:
outfile.write(line)
if line.startswith('G54'):
# Safe retract height
outfile.write('G0 Z5 ;Safe retract height\n')
elif xzx_state == WAITING_FOR_MOVE_LINE:
if isXY(unterminated_line):
# outfile.write('(Got XY line)\n')
xyline = line
xylines += 1
xzx_state = WAITING_FOR_FINAL_Z
else:
# outfile.write('(Got something else)\n')
outfile.write(line)
modify_zxz_lines = WAITING_FOR_FIRST_Z
elif xzx_state == WAITING_FOR_FINAL_Z:
if line.startswith('Z') and re.match('Z%s' % fp_pattern, unterminated_line):
# Write out the saved XY line, but modified to a rapid move
outfile.write('G0 ' + xyline)
# outfile.write('(XY line placed)\n')
outfile.write(line)
# outfile.write('(Final Z placed)\n')
else:
# outfile.write('(Failed to get final Z with %s)\n' % unterminated_line)
outfile.write(xyline)
outfile.write(line)
xzx_state = WAITING_FOR_FIRST_Z
last_line = line
print(basename, xylines, 'xy lines')
print(second_zs, 'G0 rewrites')