-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-gcode.py
executable file
·87 lines (73 loc) · 2.06 KB
/
fix-gcode.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import re
import sys
DEFAULTS = {
"input": sys.stdin,
"output": sys.stdout,
"speed": 800,
}
SUBSTITUTIONS = [
("G00 Z5.000000", "M03 S05\nG04 P0.3"),
(" Z-0.125000", ""),
("G01 F100.0(Penetrate)", "G04 P0.3\nM03 S120"),
("F400", "F{speed}"),
]
def main(
input=DEFAULTS["input"],
output=DEFAULTS["output"],
speed=DEFAULTS["speed"],
substitutions=SUBSTITUTIONS
):
context = {
"speed": speed,
}
for line in input:
for sub in substitutions:
line = line.replace(sub[0], sub[1].format(**context))
output.write(line)
description = """
Replaces common G-Code commands (eg: those generated by
the Path to GCode Inkscape extension) with equivalents
that McGraw the Pen Plotter understands.
By default, incorrect G-Code is read from stdin,
and fixed G-Code is written to stdout.
"""
examples = """examples:
cat /some/file.gcode | %(prog)s > /some/file-fixed.gcode
cat /some/file.gcode | %(prog)s --speed 1200 | ./send-serial.py
%(prog)s -i /some/file.gcode -o /some/file-fixed.gcode -s 1200
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=description,
epilog=examples,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"-i",
"--input",
metavar="FILE",
nargs="?",
type=argparse.FileType('r'),
default=DEFAULTS["input"],
help="read lines from the file at the given path, rather than from stdin"
)
parser.add_argument(
"-o",
"--output",
metavar="FILE",
nargs="?",
type=argparse.FileType('w'),
default=DEFAULTS["output"],
help="output to a file at the given path, rather than stdout"
)
parser.add_argument(
"-s",
"--speed",
default=DEFAULTS["speed"],
help="drawing speed (default: %(default)s)"
)
args = parser.parse_args()
main(input=args.input, output=args.output, speed=args.speed)