-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcode-bounds
executable file
·60 lines (50 loc) · 1.59 KB
/
gcode-bounds
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
#!/bin/python3
import sys
USAGE = """
Usage: gcode-bounds <file>.gcode
""".strip()
BLACKLIST = set("""
G1 Z150 F600 ; Move print head further up
G1 Z50 F240
""".strip().split("\n"))
NO_STARTUP = set("""
G1 X2.0 Y10 F3000
G1 X2.0 Y140 E10 F1500 ; prime the nozzle
G1 X2.3 Y140 F5000
G1 X2.3 Y10 E10 F1200 ; prime the nozzle
""".strip().split("\n")) | BLACKLIST
def parse_gcode(filename, lines, blacklist=BLACKLIST):
x = []
y = []
z = []
for line in lines:
line = line.rstrip()
if line.startswith("G1") and line not in blacklist:
parts = line.split()
for p in parts:
if p.startswith("X"):
x.append(float(p[1:]))
if p.startswith("Y"):
y.append(float(p[1:]))
if p.startswith("Z"):
z.append(float(p[1:]))
min_x, max_x = min(x), max(x)
min_y, max_y = min(y), max(y)
min_z, max_z = min(z), max(z)
size_x = max_x - min_x
size_y = max_y - min_y
size_z = max_z - min_z
print("X range: {}-{} mm".format(min_x, max_x))
print("Y range: {}-{} mm".format(min_y, max_y))
print("Z range: {}-{} mm".format(min_z, max_z))
print("Size: {}x{}x{} mm".format(size_x, size_y, size_z))
# Print the x/y range of a gcode thing
for filename in sys.argv[1:]:
print("(Not including startup line)")
with open(filename, "r") as f:
parse_gcode(filename, f, blacklist=NO_STARTUP)
print("(including startup line)")
with open(filename, "r") as f:
parse_gcode(filename, f)
if len(sys.argv) == 1:
print(USAGE)