-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcu.py
307 lines (273 loc) · 8.56 KB
/
gcu.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/python
#
# gcu.py
# G-Code Utilities command line program.
#
from gcode_utils import GcodeUtils
import argparse
import sys
if __name__ == "__main__":
# Parser
parser = argparse.ArgumentParser(
description="G-Code utiltity functions for simple G-Code files."
)
parser.add_argument("file", help="G-Code input file name.")
filegroup = parser.add_argument_group(
title="Output file for G-Code transforms"
)
filegroup.add_argument(
"-o", "--out", default=None, help="Output file name.."
)
filegroup.add_argument(
"-r",
"--replace",
action="store_true",
help="Replace input file with output.",
)
# Transformative - Basic
btransformgroup = parser.add_argument_group(
title="Basic G-Code tranformation commands"
)
btransformgroup.add_argument(
"-tc",
"--translate_center",
action="store_true",
help="Translate G-Code so that bounding box center point is (0,0,0).",
)
btransformgroup.add_argument(
"-tll",
"--translate_lower_left",
action="store_true",
help="Translate G-Code so that bounding box lower left point is (0,0,0).",
)
btransformgroup.add_argument(
"-tlr",
"--translate_lower_right",
action="store_true",
help="Translate G-Code so that bounding box lower right point is (0,0,0).",
)
btransformgroup.add_argument(
"-tul",
"--translate_upper_left",
action="store_true",
help="Translate G-Code so that bounding box lower left point is (0,0,0).",
)
btransformgroup.add_argument(
"-tur",
"--translate_upper_right",
action="store_true",
help="Translate G-Code so that bounding box lower right point is (0,0,0).",
)
btransformgroup.add_argument(
"-mx",
"--mirrorx",
action="store_true",
help="Mirror G-Code about the x-axis.",
)
btransformgroup.add_argument(
"-my",
"--mirrory",
action="store_true",
help="Mirror G-Code about the y-axis.",
)
# TranTransformative - Complex
ctransformgroup = parser.add_argument_group(
title="Complex G-Code tranformation commands"
)
ctransformgroup.add_argument(
"-tx",
"--translate",
help="Translate G-Code position based on values set by 'x', 'y' and 'z' options.",
action="store_true",
)
ctransformgroup.add_argument(
"-x", help="X direction value.", type=float, default=0.0
)
ctransformgroup.add_argument(
"-y", help="Y direction value.", type=float, default=0.0
)
ctransformgroup.add_argument(
"-z", help="Z direction value.", type=float, default=0.0
)
ctransformgroup.add_argument(
"-s",
"--scale",
help="Scale G-Code by value provided.",
type=float,
default=None,
)
ctransformgroup.add_argument(
"--change_speed",
help="Change speed value from old value to new value, specified by --old and --new.",
action="store_true",
)
ctransformgroup.add_argument(
"--change_power",
help="Change power value from old value to new value, specified by --old and --new.",
action="store_true",
)
ctransformgroup.add_argument(
"--old",
help="Original value for value changes.",
type=float,
default=None,
)
ctransformgroup.add_argument(
"--new", help="New value for value changes.", type=float, default=None
)
# Informational
infogroup = parser.add_argument_group(
title="Informational commands",
description="Processed after any trasnformation commands.",
)
infogroup.add_argument(
"-c",
"--center",
action="store_true",
help="Display G-Code center position coordinates.",
)
infogroup.add_argument(
"-e",
"--extents",
action="store_true",
help="Display G-Code bounding box extents.",
)
infogroup.add_argument(
"--speeds",
action="store_true",
help="Display unique speed values in G-Code.",
)
infogroup.add_argument(
"--powers",
action="store_true",
help="Display unique laser power values in G-Code.",
)
infogroup.add_argument(
"--zlevels",
action="store_true",
help="Display unique Z values in G-Code.",
)
args = parser.parse_args()
# TODO: Allow input of G-Code text
# Check if file is value file, if not assume text
gcu = GcodeUtils(args.file)
# Transformative
have_transform = False
if args.translate_center:
have_transform = True
gcu.TranslateCenter()
if args.translate_lower_left:
have_transform = True
gcu.TranslateLowerLeft()
if args.translate_lower_right:
have_transform = True
gcu.TranslateLowerRight()
if args.translate_upper_left:
have_transform = True
gcu.TranslateUpperLeft()
if args.translate_upper_right:
have_transform = True
gcu.TranslateUpperRight()
if args.mirrorx:
have_transform = True
gcu.MirrorX()
if args.mirrory:
have_transform = True
gcu.MirrorY()
if args.translate:
have_transform = True
gcu.Translate(x=args.x, y=args.y, z=args.z)
if args.scale is not None:
have_transform = True
gcu.Scale(scale_factor=args.scale)
if args.change_speed:
have_transform = True
if args.old is None:
print("error: old value required for change_speed")
if args.new is None:
print("error: new value required for change_speed")
gcu.ReplaceValue(command="F", oldvalue=args.old, newvalue=args.new)
if args.change_power:
have_transform = True
if args.old is None:
print("error: old value required for change_power")
if args.new is None:
print("error: new value required for change_power")
gcu.ReplaceValue(command="M4 S", oldvalue=args.old, newvalue=args.new)
# Output G-Code
if have_transform:
# Mutual exclusion on replace or output file
if args.replace and (args.out is not None):
print(
f"{__file__}: error: argument -r/--replace: not allowed with argument -o/--out"
)
sys.exit()
if args.replace:
gcu.Save()
elif args.out is not None:
gcu.SaveAs(args.out)
else:
# Dump G-Code to console
print(str(gcu))
# Informational
have_informational = False
def print_center():
c = gcu.Center()
print(f"Center : x={c[0]:0.3f}, y={c[1]:0.3f}, z={c[2]:0.3f}")
if args.center:
have_informational = True
print_center()
def print_extents():
ext = gcu.Extents()
print(f"Extents: ")
print(
f" x_min={ext[0]:0.3f}\t\ty_min={ext[1]:0.3f}\t\tz_min={ext[2]:0.3f}"
)
print(
f" x_max={ext[3]:0.3f}\t\ty_max={ext[4]:0.3f}\t\tz_max={ext[5]:0.3f}"
)
print(
f" dx={ext[3]-ext[0]:0.3f}\t\t dy={ext[4]-ext[1]:0.3f}\t\t dz={ext[5]-ext[2]:0.3f}"
)
if args.extents:
have_informational = True
print_extents()
def print_speeds():
values = gcu.Speeds()
if values is None:
return
values = list(values) # str will now comma separate
values = str(values).replace("[", "").replace("]", "")
print(f"Speeds : {values}")
if args.speeds:
have_informational = True
print_speeds()
def print_powers():
values = gcu.Powers()
if values is None:
return
values = list(values) # str will now comma separate
values = str(values).replace("[", "").replace("]", "")
print(f"Powers : {values}")
if args.powers:
have_informational = True
print_powers()
def print_zlevels():
values = gcu.ZLevels()
if values is None:
return
values = set(values)
values = str(values).replace("{", "").replace("}", "")
print(f"ZLevels: {values:0.3f}")
if args.zlevels:
have_informational = True
print_zlevels()
# Default if given filename and nothing else:
if not (have_informational or have_transform):
print()
print("G-Code Utilities Information")
print(f"File : {gcu.filename}")
print_speeds()
print_powers()
print_center()
print_extents()