Last active
November 17, 2019 20:10
-
-
Save mirrorkeydev/cf6836d83a8820f8e1e4b64eca3c6f93 to your computer and use it in GitHub Desktop.
Python 3 code that implements servo control for gcode produced by LaserWeb4
This file contains 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
#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