Skip to content

Instantly share code, notes, and snippets.

@Defozo
Created April 14, 2024 15:31
Show Gist options
  • Save Defozo/97c842a012552cb4cd96e28476852516 to your computer and use it in GitHub Desktop.
Save Defozo/97c842a012552cb4cd96e28476852516 to your computer and use it in GitHub Desktop.
# G-code Post-processing Script for Temperature Management during Ironing Sections
# This script modifies G-code files to set a specific lower temperature at the start of the ironing section and reverts to the previously set temperature after the ironing section ends. This modification is particularly useful when printing with hardened steel nozzles at higher temperatures, which can lead to clogging issues when the filament is not being extruded.
# How It Works:
# - The script scans the G-code for temperature commands and ironing section markers.
# - It inserts a command to reduce the temperature at the start of ironing to a specified lower level and another to revert it back to the previous temperature after ironing ends.
# Quickstart Guide:
# 1. Download and save this script.
# 2. Setup in PrusaSlicer:
# - In Print Settings -> Post-processing scripts:
# Add the path to your Python interpreter and this script followed by the temperature to reduce to during ironing. Specify it like this:
#
# C:\Python312\python.exe C:\Users\defozo\Downloads\change_ironing_temp.py 215
#
# Replace '215' with the temperature (in degrees Celsius) that you want to reduce to during the ironing section.
import sys
def ensure_temp_change_with_revert(file_path, reduced_temp):
"""
Adjusts the G-code file to set a specific temperature at the start of the ironing section
and reverts to the previous temperature after the ironing section ends, regardless of
whether original temperature commands are present within the ironing section.
The script overwrites the original file with the modified content.
:param file_path: Path to the G-code file to be modified.
:param reduced_temp: The temperature to set at the start of the ironing section.
"""
ironing_started = False
last_known_temp = None
modified_lines = []
with open(file_path, 'r') as f:
for line in f:
if line.startswith('M104 S') or line.startswith('M109 S'):
# Track the last known temperature before ironing starts
last_known_temp = line
if ';TYPE:Ironing' in line:
ironing_started = True
if last_known_temp:
# Insert the reduced temperature command before ironing starts
modified_lines.append(f'M104 S{reduced_temp}\n') # Set new reduced temp
modified_lines.append(line)
continue
if ironing_started and line.startswith(';TYPE:'):
ironing_started = False
if last_known_temp:
# Revert to the last known temperature after ironing ends
modified_lines.append(last_known_temp) # Revert to original temp
modified_lines.append(line)
continue
modified_lines.append(line)
# Overwrite the original file with the modified content
with open(file_path, 'w') as out_f:
out_f.writelines(modified_lines)
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python change_ironing_temp.py <reduced_temp> <gcode_file_path>")
sys.exit(1)
reduced_temperature = int(sys.argv[1]) # Convert the first argument to an integer
file_path = sys.argv[2]
ensure_temp_change_with_revert(file_path, reduced_temperature)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment