Skip to content

Instantly share code, notes, and snippets.

@nathanielstenzel
Last active February 5, 2020 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanielstenzel/2400c1a1f9cd7b137281b7a68c31f08f to your computer and use it in GitHub Desktop.
Save nathanielstenzel/2400c1a1f9cd7b137281b7a68c31f08f to your computer and use it in GitHub Desktop.
Gcode scaler script
#Written by Nathaniel J Stenzel on February 4, 2020
#The purpose of this is to scale up gcode after it is already sliced
#You can find a copy of this script at https://gist.github.com/nathanielstenzel/2400c1a1f9cd7b137281b7a68c31f08f
import sys
xscale = yscale = zscale = escale = 100.0
params = {}
for param in ["xscale","yscale","zscale","escale"]:
params[param] = locals()[param]
if len(sys.argv) < 2 or "help" in sys.argv or "-h" in sys.argv or sys.argv[1].count("="):
print("Usage:")
print("\tscale.py filename [xscale=#] [yscale=#] [zscale=#] [escale=#]")
print("\tScaling Z too much is not recommended since your layers will stop sticking together.")
exit()
file_name = sys.argv[1]
for arg in sys.argv[2:]:
if arg.count("="):
exec(arg,{},params)
else:
print("Ignoring %s since it does not seem to be setting a parameter" % arg)
gcode = open(file_name,"r")
locals().update(params)
suggested_escale = 0.0001 * xscale * yscale * zscale
if escale == 100.0 and type(escale) == float:
print("The extrusion was not scaled up. The movement was scaled by %f." % suggested_escale)
altered_gcode = ""
for line in gcode:
altered_line = comment = ""
end_line = line
line = line.rstrip("\r\n")
end_line = end_line[len(line):]
line = line.split(";",1)
if len(line) == 2:
comment = line[1]
line = line[0]
parts = line.split(" ")
if parts[0].upper() in ["G0","G1"]:
for part in parts:
part = part.upper()
if part == "":
pass
elif part[0].startswith("X"):
scaled = float(part[1:]) * xscale/100.0
elif part.startswith("Y"):
scaled = float(part[1:]) * yscale/100.0
elif part.startswith("Z"):
scaled = float(part[1:]) * zscale/100.0
elif part.startswith("E"):
scaled = float(part[1:]) * escale/100.0
if part and part[0] in "XYZE":
altered_line = altered_line + " " + part[0] + "%.05f" % scaled
altered_line = altered_line.rstrip("0")
else:
altered_line = altered_line + " " + part
else:
altered_line = line
altered_gcode = altered_gcode + altered_line
if comment:
altered_gcode = altered_gcode + " ;" + comment
altered_gcode = altered_gcode + end_line
#print altered_gcode
gcode.close()
file_name = file_name.rsplit(".",1)
file_name[0] = file_name[0] + "-scaled"
file_name = ".".join(file_name)
output_gcode = open(file_name,"w")
output_gcode.write(altered_gcode)
output_gcode.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment