Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3. import argparse
  4. import re
  5.  
  6. """
  7. Simple translation of command arguments in an G-code file.
  8.  
  9. This script will rewrite all X, Y and Z argument values in a G-code file,
  10. incrementing them by the supplied offset values. It does simple text
  11. substitution and has no notion of, e.g. relative and absolute positioning
  12. modes.
  13. """
  14.  
  15. def make_coordinate_translator(coordinate, offset):
  16. regex = re.compile(r'{0}([\d\.]+)'.format(coordinate))
  17.  
  18. def process_match(match):
  19. return '{0}{1}'.format(coordinate, float(match.group(1)) + offset)
  20.  
  21. def process_line(line):
  22. return regex.sub(process_match, line)
  23.  
  24. if offset:
  25. return process_line
  26. return lambda x: x
  27.  
  28.  
  29. def translate(input_file, output_file, x, y, z):
  30. translators = [make_coordinate_translator(axis, value) for axis, value in zip(('X', 'Y', 'Z'), (x, y ,z))]
  31.  
  32. for line in input_file:
  33. if not line.startswith(';'):
  34. for translator in translators:
  35. line = translator(line)
  36. output_file.write(line)
  37.  
  38.  
  39. def main():
  40. description = """
  41. This script will rewrite all X, Y and Z argument values in a G-code file,
  42. incrementing them by the supplied offset values. It does simple text
  43. substitution and has no notion of, e.g. relative and absolute positioning
  44. modes.
  45. """
  46.  
  47. parser = argparse.ArgumentParser(description=description)
  48. parser.add_argument('-x', type=float, default=0)
  49. parser.add_argument('-y', type=float, default=0)
  50. parser.add_argument('-z', type=float, default=0)
  51. parser.add_argument('input_file', type=file)
  52. parser.add_argument('output_file', type=argparse.FileType('w'))
  53. args = parser.parse_args()
  54. translate(**vars(args))
  55.  
  56. if __name__ == '__main__':
  57. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement