Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement