-1

I am working on a project where I have to edit offsets manually in CNC programs. I would like to do this in python, however, my experience with it has not involved something like this. Any help is greatly appreciated.

example of code:

N0880M41
N0890G50S3600
M03
N0900G96S0210M03
N0910Z23.274M08
N0920M07
N0930Z23.2063
N0940X1.39
N0950G99
N0960G01X1.29F.009
N0970X1.2558
N0980G02X1.189Z23.1662R.944
N0990G01Z14.7569F.012
N1000G02X1.2558Z14.7168R.944
N1010G01X1.29
N1020G00X1.3608Z14.7522
N1030Z23.1309
N1040X1.2656
N1050G01X1.189Z23.1662F.009
N1060G02X1.088Z23.0955R.944
N1070G01Z14.8276F.012
N1080G02X1.2528Z14.7185R.944
N1090G00X1.3236Z14.7538
N1100Z23.0602
N1110X1.1646
N1120G01X1.088Z23.0955F.009
N1130G02X.987Z23.0075R.944
N1140G01Z14.9157F.012
N1150G02X1.1446Z14.7864R.944
N1160G00X1.2152Z14.8217
N1170Z22.9721
N1180X1.0636
N1190G01X.987Z23.0075F.009
N1200G02X.886Z22.8873R.944
N1210G01Z15.0359F.012
N1220G02X1.0344Z14.8716R.944
N1230G00X1.105Z14.907
N1240Z22.8519

I need to change the Z value with a user input via prompt and I wanted to have it output with the same text as the input just with a new value in Z

for example: user prompted and entered value .226

input = N0910Z23.274M08

I would like output to be

output = N0910Z23.500M08
  • You need to find out how to: 1) get user prompt info, 2) read file, 3) parse file to get the value you want, 4) write change back to file, 5) print output to user. Which one is your problem? – dwitvliet Jul 23 '14 at 20:19
  • In your example, it's relatively trivial to split your input value on ".", then take the value before the "M", convert it to an integer, add `226` and create your output value. I'm just not sure how at all this relates to your file, or other letters in input, or multiple dots. Maybe you could elaborate more on exactly what you're after & what you've tried. – Gerrat Jul 23 '14 at 20:21
  • I have done this in MATLAB, however, this current project I do not have access. I have never really worked in PYTHON before. I just am not familiar with syntax and structure of Python programming – brandocommando Jul 23 '14 at 20:21
  • We're not actually here to write your code for you. Read/Learn python (some tutorials here: https://wiki.python.org/moin/BeginnersGuide/Programmers), then come back if you still have a question. – Gerrat Jul 23 '14 at 20:23
  • I didn't ask for that, just a pointer in the right direction. I have looked at tutorials and not found quite what I am looking for. I will try your site. I am sorry if I came across asking for my work to be done, that was not my intention. – brandocommando Jul 23 '14 at 20:25

1 Answers1

0

Here is a quick and dirty program that does what you want:

# GCode utility - adjust Z offset
# This code assumes Python 3
import re

def get_float(prompt):
    while True:
        try:
            return float(input(prompt))
        except ValueError:
            pass

# Regular expression to recognize a Z parameter
z_re = re.compile("Z(-?\d+(?:\.\d*)?)", re.I)

# Create callback function for re.sub
def make_z_callback(dz, fmt="Z{:0.4f}"):
    def z_callback(match):
        z_value = match.group(1)        # get just the number
        z_value = float(z_value) + dz   # add offset
        return fmt.format(z_value)      # convert back to string
    return z_callback

def main():
    # read GCode file contents
    fname = input("Enter GCode file name: ")
    with open(fname) as inf:
        gcode = inf.read()

    # do search-and-replace on Z parameters
    z_offset = get_float("Enter Z offset: ")
    z_callback = make_z_callback(z_offset)
    gcode = z_re.sub(z_callback, gcode)

    # write result back to file
    with open(fname, "w") as outf:
        outf.write(gcode)

if __name__=="__main__":
    main()
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99