-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgct.py
115 lines (92 loc) · 2.8 KB
/
gct.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
#!/usr/bin/env python3
"""
Gcode manipulation tools
"""
import logging
import argparse
import sys
from parsers.gcode_strip import GcodeStrip
from parsers.gcode_gradient_infill import GcodeGradientInfill
def main():
""" Parse arguments, sets up logging, run selected gcode parser."""
# Command line arguments
parser = argparse.ArgumentParser(
prog="gct.py",
description="Tools to parse and manipulate gcode"
)
parser.add_argument(
"--file_out",
"-f",
#"outfile",
type=argparse.FileType('w+'),
nargs='?',
help="Path to the output gcode file",
default=sys.stdout
)
parser.add_argument(
'-d', '--debug',
help="be very verbose",
action="store_const", dest="loglevel", const=logging.DEBUG,
default=logging.WARNING,
)
parser.add_argument(
'-v', '--verbose',
help="be verbose",
action="store_const", dest="loglevel", const=logging.INFO,
)
subprasers = parser.add_subparsers(dest='command', required=True)
# TODO: this should be an optional argument and default to stdin
# but setting nargs='?' breaks parsing if FILE_IN is provided.
parser.add_argument(
"file_in",
type=argparse.FileType('r'),
#nargs='?',
help="Path to the input gcode file",
default=sys.stdin
)
# 'strip' command
strip = subprasers.add_parser('strip', help='strip comments')
# 'gradient_infill' command
gradient = subprasers.add_parser('gradient_infill', help='modify infill to gradient')
gradient.add_argument(
"--flow_min",
type=float,
required=False,
default=0.5,
help="minimum extrusion flow multiplier, default 0.5",
)
gradient.add_argument(
"--flow_max",
type=float,
required=False,
default=3.0,
help="maximum extrusion flow multiplier, default 3.0",
)
gradient.add_argument(
"--width",
type=float,
required=False,
default=5.0,
help="width of the extrusion gradient in mm from closest perimeter. Default 5.0",
)
args = parser.parse_args()
# Logging
logging.basicConfig(format='%(asctime)s %(message)s', level=args.loglevel)
# Select and run the appropriate gcode parser
gcode_parser = None
gcode_parser_args = {
"file_out": args.file_out,
"file_in": args.file_in
}
if args.command == "strip":
gcode_parser = GcodeStrip(**gcode_parser_args)
elif args.command == "gradient_infill":
gcode_parser = GcodeGradientInfill(
flow_min=args.flow_min,
flow_max=args.flow_max,
width=args.width,
**gcode_parser_args
)
gcode_parser.parse()
if __name__ == '__main__':
main()