Skip to content

Instantly share code, notes, and snippets.

@TheMatt2
Forked from astronautlevel2/ConvertScript.py
Last active February 1, 2017 22:35
Show Gist options
  • Save TheMatt2/639597ce191ae5bb09691c695c3fa961 to your computer and use it in GitHub Desktop.
Save TheMatt2/639597ce191ae5bb09691c695c3fa961 to your computer and use it in GitHub Desktop.
'''
This was a project started by Alex that I am fixing some problems with. The logic itself is all Alex's.
Fix Autodesk CAM General LabVolt code to work with LabVolt M600 Mills
This probably requires some generalisation before it works with all setups
Code is heavily commented, but a basic knowledge of regex is required still
This is now less of a hack and an actual work around.
Current features:
Moves G00 to the front of the line when necessary
Changes T# codes to M06 # codes
Changes S#### M3 to M03 S####
Removes all M1 codes
Usage: python ConvertScript.py [file]
All code is licensed under the MIT License
'''
# Import re for regex and sys for command line arguments
# os is for working with file addresses.
import re
import os
import sys
# Open file and copy contents if extension is .nc - otherwise throw error
fo = open(sys.argv[1:2]or raw_input("Enter file to convert: "), 'r')
if fo.name:
if os.path.splitext(fo.name)[1] != ".nc":
print("Error: File must be of type .nc")
sys.exit(1)
else:
"Usage: python ConvertScript.py <file>"
name = os.path.splitext(fo.name)
lines = fo.readlines()
fo.close()
# Iterate through string and fix errors
g = re.compile('^[XYZ].+G00.*$') # Regex for embedded G00 codes
t = re.compile('^T\d\n$') # Regex character for T codes
m = re.compile('^S\d{4} M3$') # Regex character for M3 codes
for s in lines:
match = g.match(s) # Check if string has a G00, if so...
if match:
modStr = re.sub(re.escape('G00 '), '', s) # Remove G00
modStr = "G00 " + modStr # Add it to beginning
lines[lines.index(s)] = modStr # And replace the original line
match = t.match(s) # Check if format for string is T and a number, if so...
if match:
num = s[1] # Get tool number that we're trying to change to
lines[lines.index(s)] = 'M06 ' + num + '\n' # Replace original string
match = m.match(s) # Check if string has an M3 code, if so...
if match:
modStr = re.sub(re.escape(' M3'), '', s) # Remove M3
modStr = "M03 " + modStr # Add it to beginning
lines[lines.index(s)] = modStr # And replace the original line
if s == "M1\n": # Remove all instances of M1
lines.pop(lines.index(s))
fo = open(name[0] + ".m5", 'w')
print("File saved to " + name[0] + ".m5")
fo.writelines(lines)
fo.flush()
fo.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment