Created
October 22, 2016 21:08
-
-
Save honggoff/9b896ca6856fe3590368e93d05fdcb78 to your computer and use it in GitHub Desktop.
Simple translation of command arguments in an G-code file.
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
#! /usr/bin/python | |
import argparse | |
import re | |
""" | |
Simple translation of command arguments in an G-code file. | |
This script will rewrite all X, Y and Z argument values in a G-code file, | |
incrementing them by the supplied offset values. It does simple text | |
substitution and has no notion of, e.g. relative and absolute positioning | |
modes. | |
""" | |
def make_coordinate_translator(coordinate, offset): | |
regex = re.compile(r'{0}([\d\.]+)'.format(coordinate)) | |
def process_match(match): | |
return '{0}{1}'.format(coordinate, float(match.group(1)) + offset) | |
def process_line(line): | |
return regex.sub(process_match, line) | |
if offset: | |
return process_line | |
return lambda x: x | |
def translate(input_file, output_file, x, y, z): | |
translators = [make_coordinate_translator(axis, value) for axis, value in zip(('X', 'Y', 'Z'), (x, y ,z))] | |
for line in input_file: | |
if not line.startswith(';'): | |
for translator in translators: | |
line = translator(line) | |
output_file.write(line) | |
def main(): | |
description = """ | |
This script will rewrite all X, Y and Z argument values in a G-code file, | |
incrementing them by the supplied offset values. It does simple text | |
substitution and has no notion of, e.g. relative and absolute positioning | |
modes. | |
""" | |
parser = argparse.ArgumentParser(description=description) | |
parser.add_argument('-x', type=float, default=0) | |
parser.add_argument('-y', type=float, default=0) | |
parser.add_argument('-z', type=float, default=0) | |
parser.add_argument('input_file', type=file) | |
parser.add_argument('output_file', type=argparse.FileType('w')) | |
args = parser.parse_args() | |
translate(**vars(args)) | |
if __name__ == '__main__': | |
main() |
Nice script!
But it wont change negative values.
patch:
regex = re.compile(r'{0}([\-?\d+\.]+)'.format(coordinate))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
THANKS!