tool for shifting GCode

This commit is contained in:
lars 2012-10-28 23:27:04 +00:00
parent 8d6ddd09d9
commit e4b7b7d00a

View file

@ -0,0 +1,69 @@
#!/usr/bin/env python
import argparse
import os
import re
import sys
from decimal import Decimal as d
class GCodePosition(object):
def __init__(self, lower, upper, shift):
self.pos = [d(0), d(0), d(0)]
self.lower = lower
self.upper = upper
self.shift = shift
def is_inside(self):
for i in range(len(self.pos)):
if not ((self.lower[i] is None or (self.lower[i] <= self.pos[i])) and \
(self.upper[i] is None or (self.upper[i] >= self.pos[i]))):
return False
return True
def get_shifted_line(self, line):
""" parse the line, update current position replace shifted values
"""
axis_pattern = r"\b([XYZ][\+\-0-9\.]+)\b"
result = []
# first: update the x/y/z position (make sure that we are in the box)
for token in re.findall(axis_pattern, line, flags=re.I):
for i, axis in enumerate("XYZ"):
if token.upper().startswith(axis):
self.pos[i] = d(token[1:])
# second: replace values
if self.is_inside():
def replace_value(match):
token = match.group(0).upper()
for i, axis in enumerate("XYZ"):
if token.startswith(axis):
value = d(token[1:])
return token[0] + str(value + self.shift[i])
return token
return re.sub(axis_pattern, replace_value, line, re.I)
else:
return line
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Shift parts of gcode")
parser.add_argument('--minx', dest="minx", type=d)
parser.add_argument('--maxx', dest="maxx", type=d)
parser.add_argument('--miny', dest="miny", type=d)
parser.add_argument('--maxy', dest="maxy", type=d)
parser.add_argument('--minz', dest="minz", type=d)
parser.add_argument('--maxz', dest="maxz", type=d)
parser.add_argument('--shiftx', dest="shiftx", type=d, default=d(0))
parser.add_argument('--shifty', dest="shifty", type=d, default=d(0))
parser.add_argument('--shiftz', dest="shiftz", type=d, default=d(0))
infile = sys.stdin
outfile = sys.stdout
options = parser.parse_args()
shifter = GCodePosition((options.minx, options.miny, options.minz),
(options.maxx, options.maxy, options.maxz),
(options.shiftx, options.shifty, options.shiftz))
for line in infile.readlines():
outfile.write(shifter.get_shifted_line(line))