Skip to content

Instantly share code, notes, and snippets.

@astronautlevel2
Last active February 1, 2017 22:18
Show Gist options
  • Select an option

  • Save astronautlevel2/c62fd7bfb271490ec3d87a7cac97faac to your computer and use it in GitHub Desktop.

Select an option

Save astronautlevel2/c62fd7bfb271490ec3d87a7cac97faac to your computer and use it in GitHub Desktop.

Revisions

  1. astronautlevel2 revised this gist Jan 11, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ConvertScript.py
    Original file line number Diff line number Diff line change
    @@ -29,9 +29,9 @@
    if fo.name:
    if fo.name.split(".")[-1] != "nc":
    print("Error: File must be of type .nc")
    sys.exit
    sys.exit()
    else:
    "Usage: python CNCScript.py <file>"
    "Usage: python ConvertScript.py <file>"
    name = fo.name.split(".")
    lines = fo.readlines()
    fo.close()
  2. astronautlevel2 created this gist Jan 10, 2017.
    68 changes: 68 additions & 0 deletions ConvertScript.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    '''
    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
    Consider it a dirty hack until proven otherwise
    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 rege and sys for command line arguments

    import re
    import sys

    # Open file and copy contents if extension is .nc - otherwise throw error

    fo = open(sys.argv[1], 'r')
    if fo.name:
    if fo.name.split(".")[-1] != "nc":
    print("Error: File must be of type .nc")
    sys.exit
    else:
    "Usage: python CNCScript.py <file>"
    name = fo.name.split(".")
    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')
    for line in lines:
    fo.writelines(line)