Skip to content

Instantly share code, notes, and snippets.

@mirrorkeydev
Last active November 17, 2019 20:10
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 mirrorkeydev/cf6836d83a8820f8e1e4b64eca3c6f93 to your computer and use it in GitHub Desktop.
Save mirrorkeydev/cf6836d83a8820f8e1e4b64eca3c6f93 to your computer and use it in GitHub Desktop.
Python 3 code that implements servo control for gcode produced by LaserWeb4
#open the file in local memory and make a list containing every line as a new string element
fileobj = open("C:/Users/Name/Downloads/sample.gcode", "r")
contents = fileobj.readlines()
fileobj.close()
#find instances of "G0" and sandwich it between the servo up and down commands
count = 0
index = 0
length = len(contents)
flag = False
while 0 <= index <= length:
if flag == True: #fixes indexing to avoid infinite loop after sandwich
index = index + 1
length = length + 1
flag = False
try: #the sandwich function
if contents[index][0] == 'G' and contents[index][1] == '0':
count = count + 1 #one more G0 for the books
contents.insert(index, "M03S75\n")
contents.insert(index + 2, "M05\n")
flag = True
except IndexError:
pass #if it catches an empty line, just ignore it
index = index + 1 #pseudo for-loop
#write the new lines to the file
fileobj = open("C:/Users/Name/Downloads/sample.gcode", "w")
fileobj.writelines(contents)
fileobj.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment