Skip to content

Instantly share code, notes, and snippets.

@AshMartian
Last active February 13, 2021 05:56
Show Gist options
  • Save AshMartian/52604137259924d5b36bb5bd2fd888be to your computer and use it in GitHub Desktop.
Save AshMartian/52604137259924d5b36bb5bd2fd888be to your computer and use it in GitHub Desktop.
Convert GCode M678 to Sailfish A/B extrusion values
#!/usr/bin/env python
import sys
import os
AFactor = 0.8
BFactor = 0.2
ALast = 0.0
BLast = 0.0
ELast = 0
NewGcode = ""
with open(sys.argv[1], 'r') as gcode_file:
for line in gcode_file:
if 'M567' in line:
commands = line.split(" ")
for command in commands:
if "E" in command:
command = command.replace("E", "").replace('\n', '')
values = command.split(":")
AFactor = float(values[0])
BFactor = float(values[1])
#resetTools = 'T0\nG92 P0\nT1\nG92 P0\n'
line = '; Debug (A{0:.5f} B{1:.5f}) '.format(AFactor, BFactor) + line #comment out M567
if 'G1' in line:
commands = line.split(" ")
newLine = ""
for command in commands:
if "E" in command:
command = command.replace("E", "").replace('\n', '')
totalExtrusion = float(command)
ALast += ((totalExtrusion - ELast) * AFactor)
BLast += ((totalExtrusion - ELast) * BFactor)
ELast = totalExtrusion
command = 'A{0:.5f} B{1:.5f}'.format(ALast, BLast)
newLine += command + " "
if "E" in line:
newLine += '\n'
line = newLine
NewGcode += line
fileName = os.path.splitext(os.path.basename(sys.argv[1]))[0]
newF = open(fileName + "-converted.gcode", "w+b")
newF.truncate()
newF.write(NewGcode.encode('utf-8'))
@AshMartian
Copy link
Author

Usage: python ./convert.py file.gcode outputs file-converted.gcode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment