-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSmoothificator.py
164 lines (140 loc) · 6.84 KB
/
Smoothificator.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
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Copyright (c) [2025] [Roman Tenger]
import re
import sys
import logging
import os
import argparse
# Get the directory where the script is located
script_dir = os.path.dirname(os.path.abspath(__file__))
# Configure logging
log_file_path = os.path.join(script_dir, "smooth_wall_log.txt")
logging.basicConfig(
filename=log_file_path,
filemode="w",
level=logging.INFO,
format="%(asctime)s - %(message)s"
)
def get_layer_height(gcode_lines):
"""Extract layer height from G-code header comments"""
for line in gcode_lines:
if "layer_height =" in line.lower():
match = re.search(r'; layer_height = (\d*\.?\d+)', line, re.IGNORECASE)
if match:
return float(match.group(1))
return None
def process_gcode(input_file, outer_layer_height):
current_layer = 0
current_z = 0.0
in_external_perimeter = False
external_block_lines = []
logging.info("Starting G-code processing")
logging.info(f"Input file: {input_file}")
logging.info(f"Desired outer wall height: {outer_layer_height}mm")
# Read the input G-code
with open(input_file, 'r') as infile:
lines = infile.readlines()
# Get base layer height
base_layer_height = get_layer_height(lines)
if not base_layer_height:
logging.error("Could not find layer height in G-code file")
sys.exit(1)
logging.info(f"Base layer height: {base_layer_height}mm")
# Validate outer layer height
if outer_layer_height <= 0:
logging.error(f"Outer layer height ({outer_layer_height}mm) must be greater than 0")
sys.exit(1)
# Calculate number of passes needed
passes_needed = round(base_layer_height / outer_layer_height)
height_per_pass = base_layer_height / passes_needed
extrusion_multiplier = 1.0 / passes_needed # Each pass gets an equal portion of the original extrusion
logging.info(f"Number of passes: {passes_needed}")
logging.info(f"Height per pass: {height_per_pass:.3f}mm")
logging.info(f"Extrusion multiplier: {extrusion_multiplier:.3f}")
# Process the G-code
modified_lines = []
i = 0
while i < len(lines):
line = lines[i]
# Detect layer changes
if line.startswith("G1 Z"):
z_match = re.search(r'Z([-\d.]+)', line)
if z_match:
current_z = float(z_match.group(1))
current_layer += 1
logging.info(f"Layer {current_layer} detected at Z={current_z:.3f}")
modified_lines.append(line)
i += 1
continue
# Start of external perimeter block
if ";TYPE:External perimeter" in line or ";TYPE:Outer wall" in line:
external_block_lines = []
# Collect all lines until next type change or empty line
while i < len(lines):
current_line = lines[i]
if i + 1 < len(lines) and (";TYPE:" in lines[i + 1] or "M" in lines[i + 1] and not "M73" in lines[i + 1]):
external_block_lines.append(current_line)
i += 1
break
external_block_lines.append(current_line)
i += 1
# Process the collected block
if external_block_lines:
# Find the starting position from the last G1 move before the external perimeter block
start_pos = None
for j in range(i - len(external_block_lines) - 1, max(0, i - len(external_block_lines) - 10), -1):
line = lines[j]
if "G1" in line and "X" in line and "Y" in line:
x_match = re.search(r'X([-\d.]+)', line)
y_match = re.search(r'Y([-\d.]+)', line)
if x_match and y_match:
start_pos = (float(x_match.group(1)), float(y_match.group(1)))
break
# Generate passes
for pass_num in range(passes_needed):
# Calculate Z height for this pass
pass_z = current_z + (pass_num * height_per_pass)
# Add travel move back to start position (except for first pass)
if pass_num > 0 and start_pos:
modified_lines.append(f"G1 X{start_pos[0]:.3f} Y{start_pos[1]:.3f} F9000 ; Travel back to start\n")
# Set Z height for this pass
modified_lines.append(f"G1 Z{pass_z:.3f} ; Pass {pass_num + 1} of {passes_needed}\n")
# Process extrusion lines
for block_line in external_block_lines:
if "G1" in block_line and "E" in block_line:
e_match = re.search(r'E([-\d.]+)', block_line)
if e_match:
e_value = float(e_match.group(1))
new_e_value = e_value * extrusion_multiplier
modified_line = re.sub(r'E[-\d.]+', f'E{new_e_value:.5f}', block_line)
modified_lines.append(modified_line)
else:
modified_lines.append(block_line)
else:
modified_lines.append(line)
i += 1
# Overwrite the input file with the modified G-code
with open(input_file, 'w') as outfile:
outfile.writelines(modified_lines)
logging.info("G-code processing completed")
logging.info(f"Log file saved at {log_file_path}")
# Main execution
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process G-code file to modify external perimeter printing.')
parser.add_argument('input_file', help='Input G-code file')
parser.add_argument('-outerLayerHeight', '--outer-layer-height', type=float, required=True,
help='Desired height for outer walls (mm)')
args = parser.parse_args()
process_gcode(input_file=args.input_file, outer_layer_height=args.outer_layer_height)