Skip to content

Instantly share code, notes, and snippets.

@dayt0n
Created December 2, 2017 04:59
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 dayt0n/88e4f3bda2d5dbb4a30483412799aba6 to your computer and use it in GitHub Desktop.
Save dayt0n/88e4f3bda2d5dbb4a30483412799aba6 to your computer and use it in GitHub Desktop.
increase or decrease dimensions of GCode in .gcode/.gco/.nc files for things like CNC routers
# gcsize.py - increase or decrease dimensions of GCode in .gcode/.gco/.nc files for things like CNC routers
#
# written by dayt0n
import sys
import os
import re
if len(sys.argv) < 3:
print("Insufficient arguments\nusage: %s [GCode file] [output] [size multiplier]") % sys.argv[0]
sys.exit(0)
filename = sys.argv[1]
file = open(filename,"r")
lines = file.readlines()
lines = [re.sub("\ \ +"," ",l.rstrip()) for l in lines] # replace every multiple occurrence of spaces with one space and remove newline chars
print("%s has %d lines") % (filename,len(lines))
newfile = open(sys.argv[2],"w")
print("Resizing by a factor of %s") % sys.argv[3]
for i in range(len(lines)):
newStr = ""
if " " in lines[i]:
if lines[i][0] == " ":
lines[i] = lines[i][1:]
splitString = lines[i].split(" ")
newSplitStr = [] * len(splitString)
for i in range(len(splitString)):
if splitString[i][0] == "X" or splitString[i][0] == "Y" or splitString[i][0] == "I" or splitString[i][0] == "J":
if splitString[i][-1] is ".":
splitString[i] = splitString[i][:-1] # remove unnecessary period
newSplitStr.append(splitString[i][0] + str(float(splitString[i][1:]) * float(sys.argv[3]))) # multiply it
else:
newSplitStr.append(splitString[i])
for i in range(len(splitString)):
newStr += (newSplitStr[i] + " ")
else:
newStr = lines[i]
newfile.write(newStr + "\n")
print("Finished writing to %s") % sys.argv[2]
newfile.close()
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment