-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreprint.py
More file actions
229 lines (201 loc) · 8.1 KB
/
preprint.py
File metadata and controls
229 lines (201 loc) · 8.1 KB
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
#!/usr/bin/env python3
import sys
import os
import re
import hashlib
from typing import Optional
import shutil
from pathlib import Path
import tempfile
PRINTER_PATH = "/tmp/printer"
def write_to_file(path: str, content: str):
"""Write text content to file."""
try:
with open(path, "a", encoding="utf-8") as f:
f.write(content)
except OSError as e:
print(f"Error when writing in {path}: {e}")
def get_exclude_object_define(lines) -> Optional[str]:
"""bounding box first layer"""
minx = miny = float("inf")
maxx = maxy = float("-inf")
for line in lines:
parts = line.split()
if not parts:
continue
cmd = parts[0]
if cmd.startswith(("G1", "G2", "G3")):
x = y = e = None
for p in parts[1:]:
if p.startswith("X"):
x = float(p[1:])
elif p.startswith("Y"):
y = float(p[1:])
elif p.startswith("E"):
e = float(p[1:])
if e is None or e <= 0:
continue # Ignore movements without extrusion
if x is not None:
minx = min(minx, x)
maxx = max(maxx, x)
if y is not None:
miny = min(miny, y)
maxy = max(maxy, y)
if minx == float("inf") or miny == float("inf"):
return None
center_x = (minx + maxx) / 2
center_y = (miny + maxy) / 2
exclude_str = (
f"EXCLUDE_OBJECT_DEFINE NAME=First_Layer CENTER={center_x:.4f},{center_y:.4f} "
f"POLYGON=[[{minx:.6f},{miny:.6f}],"
f"[{maxx:.6f},{miny:.6f}],"
f"[{maxx:.6f},{maxy:.6f}],"
f"[{minx:.6f},{maxy:.6f}]]"
)
return exclude_str
def parse_list_from_line(line, separator=";"):
"""Parse a list from a text"""
match = re.search(r"=\s*(.+)$", line)
if match:
return [item.strip() for item in match.group(1).split(separator) if item.strip()]
return []
def main():
if len(sys.argv) < 2:
print("Use: preprint.py <file.gcode>")
sys.exit(1)
tool_re = re.compile(r"^\s*T(\d+)")
color_re = re.compile(r";\s*filament_colour\s*=")
feedrate_re = re.compile(r"; filament_max_volumetric_speed =")
type_re = re.compile(r";\s*filament_type\s*=")
version_re = re.compile(r";\s*Bambufy:\s*v*([\d.]+)")
bambu_re = re.compile(r";.*BambuStudio")
orca_re = re.compile(r";.*OrcaSlicer")
after_layer_change_re = re.compile(r"^;\s*AFTER_LAYER_CHANGE.*$")
already_processed_re = re.compile(r"^;\s*_IFS_COLORS.*$")
tools = set()
colors = None
types = None
feedrates = []
first_layer = []
first_after_layer_change = False
second_after_layer_change = False
exclude = None
bambu_metadata = ""
version = None
slicer = ""
filament_used_g=""
filament_used_mm=""
estimated_printing_time=""
total_filament_used_g=""
filament_type=""
filament_settings_id=""
layer_height=""
nozzle_diameter=""
nozzle_diameter=""
nozzle_diameter=""
nozzle_temperature=""
file_path = sys.argv[1]
with open(file_path, 'r', encoding='utf-8') as f:
for line_num, line in enumerate(f):
if line_num < 10:
match = already_processed_re.search(line)
if match:
print("Already post-processed")
existing = match.group(0).lstrip("; ").rstrip() + "\n"
print(existing)
write_to_file(PRINTER_PATH, existing)
sys.exit(0)
if bambu_re.search(line):
slicer = "bambu"
elif orca_re.search(line):
slicer = "orca"
if colors is None and color_re.search(line):
colors = parse_list_from_line(line)
if not feedrates and feedrate_re.search(line):
feedrates = parse_list_from_line(line,",")
if types is None and type_re.search(line):
types = parse_list_from_line(line)
if version is None:
v_match = version_re.search(line)
if v_match:
version = v_match.group(1) if v_match else '1.2.2'
if not first_after_layer_change:
first_after_layer_change = bool(after_layer_change_re.search(line))
elif not second_after_layer_change:
second_after_layer_change = bool(after_layer_change_re.search(line))
first_layer.append(line)
t_match = tool_re.match(line)
if t_match:
tools.add(t_match.group(1))
if slicer == "bambu" and line.startswith(";"):
if line.startswith("; nozzle_temperature ="):
nozzle_temperature = line.split('=')[1].strip().split(',')[0]
if line.startswith("; hot_plate_temp ="):
hot_plate_temp = line.split('=')[1].strip().split(',')[0]
if line.startswith("; filament_colour ="):
filament_colour = line
if line.startswith("; nozzle_diameter ="):
nozzle_diameter = line
if line.startswith("; filament_type ="):
filament_type = line
if line.startswith("; layer_height ="):
layer_height = line
if line.startswith("; estimated printing time"):
estimated_printing_time = line
if line.startswith("; filament_settings_id = "):
filament_settings_id = line
if line.startswith("; total filament length"):
filament_used_mm = f"; filament used [mm] = {line.split(':')[1].strip()}"
if line.startswith("; total filament weight"):
total_filament_weight = line.split(':')[1].strip()
filament_used_g = f"; filament used [g] = {total_filament_weight}"
total_filament_used_g = f"; total filament used [g] = {sum(float(x) for x in total_filament_weight.split(','))}"
tools = sorted(tools)
feedrates = ",".join(str(round(float(v) / 4 * 3 * 60)) for v in feedrates)
exclude = get_exclude_object_define(first_layer)
from_slicer = any(k.startswith("SLIC3R_") for k in os.environ)
if slicer == "bambu":
bambu_metadata = (f"\n{filament_used_mm}\n"
f"{filament_used_g}\n"
f"{total_filament_used_g}\n"
f"{estimated_printing_time}"
f"{filament_type}"
f"{filament_settings_id}"
f"{layer_height}"
f"{nozzle_diameter}"
f"{filament_colour}"
f"; first_layer_bed_temperature = {hot_plate_temp}\n"
f"; first_layer_temperature = {nozzle_temperature}\n")
# _IFS_COLORS header
ifs_colors = (
f'_IFS_COLORS START=1 '
f'TYPES={",".join(types)} '
f'E_FEEDRATES={feedrates} '
f'COLORS={",".join(c[1:] for c in colors)} '
f'TOOLS={",".join(tools)} '
f'VERSION={version} '
f'EXCLUDE="{exclude}"\n'
)
print(ifs_colors)
md5 = hashlib.md5()
md5.update(("; " + ifs_colors).encode('utf-8'))
with open(file_path, 'rb') as f:
for line_num, line in enumerate(f):
if line_num == 0 and re.search(rb';\s*MD5\s*[:=]', line, re.IGNORECASE):
continue
md5.update(line)
md5.update(bambu_metadata.encode('utf-8'))
md5.hexdigest()
file_path = Path(file_path)
with tempfile.NamedTemporaryFile(mode='wb', delete=False, dir=file_path.parent) as tmp:
tmp.write(("; MD5:" + md5.hexdigest() + "\n").encode('utf-8'))
tmp.write(("; " + ifs_colors).encode('utf-8'))
with open(file_path, 'rb') as f:
shutil.copyfileobj(f, tmp)
tmp.write(bambu_metadata.encode('utf-8'))
Path(tmp.name).replace(file_path)
# Append to printer if not called from slicer
if not from_slicer:
write_to_file(PRINTER_PATH, ifs_colors + "\n")
if __name__ == "__main__":
main()