Created
November 30, 2017 16:46
-
-
Save m0n5t3r/66c06090bfc6faf9f4b638382222c97e to your computer and use it in GitHub Desktop.
Clean up slic3r Prusa edition gcode for multi-material prints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Gcode cleaner to work around prusa slic3r annoyances for multi-filament | |
single-tool printing on non-Prusa printers. | |
Based on this gist: | |
* https://gist.github.com/ex-nerd/22d0a9796f4f5df7080f9ac5a07a381f | |
""" | |
import os | |
import re | |
import argparse | |
def comment(str): | |
return '; ' + str | |
def rewrite(infile, outfile, verbose=False): | |
WIPE = 1 | |
UNLOAD = 2 | |
LOAD = 3 | |
toolchange = 0 | |
priming = False | |
temp_change = None | |
for line in infile: | |
outline = line | |
# Priming | |
if line.startswith('; CP PRIMING'): | |
if 'START' in line: | |
priming = True | |
elif 'STOP' in line: | |
priming = False | |
if verbose: | |
print "Priming: %s" % priming | |
# Detect toolchange state | |
elif line.startswith('; CP TOOLCHANGE'): | |
if 'WIPE' in line: | |
toolchange = WIPE | |
elif 'UNLOAD' in line: | |
toolchange = UNLOAD | |
elif 'LOAD' in line: | |
toolchange = LOAD | |
else: | |
toolchange = 0 | |
if verbose: | |
print "Toolchange: %s" % toolchange | |
if line.startswith('M907 '): | |
outline = comment(line) | |
elif line.startswith('M1 '): | |
outline = comment(line) | |
elif toolchange in (LOAD, UNLOAD): | |
if toolchange == LOAD and re.search('E40.0000 F3000', line): | |
outline = re.sub('E40.0000', 'E80.0000', line) | |
elif toolchange == UNLOAD and re.search('E-50.0000 F5400', line): | |
outline = re.sub('E-50.0000', 'E-90.0000', line) | |
if line.startswith('T'): | |
if temp_change: | |
# Duplicate the last temperature change. | |
# https://github.com/prusa3d/Slic3r/issues/559 | |
outfile.write(outline) | |
outline = temp_change | |
temp_change = None | |
else: | |
if line.startswith('M104 S'): | |
temp_change = line | |
outfile.write(outline) | |
if verbose: | |
print outline.rstrip() | |
def parse_args(): | |
parser = argparse.ArgumentParser( | |
description='Gcode cleaner to work around some multi-extruder bugs in slic3r Prusa edition.' | |
) | |
parser.set_defaults( | |
verbose=False, | |
overwrite=False, | |
) | |
parser.add_argument( | |
'--verbose', '-v', | |
action='store_true', | |
help="Enable additional debug output", | |
) | |
parser.add_argument( | |
'filename', | |
type=argparse.FileType('r'), | |
nargs='+', | |
help="One or more paths to .gcode files to clean", | |
) | |
return parser.parse_args() | |
if __name__ == '__main__': | |
args = parse_args() | |
for infile in args.filename: | |
infilename = infile.name | |
tmpfilename = '{}.tmp{}'.format(*os.path.splitext(infilename)) | |
with open(tmpfilename, 'w') as tmpfile: | |
rewrite(infile, tmpfile, args.verbose) | |
outfilename = infilename | |
os.rename(tmpfilename, outfilename) | |
print("{}\n => {}".format(infilename, outfilename)) | |
infile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment