This repository has been archived on 2020-12-19. You can view files and clone it, but cannot push or open issues or pull requests.
gce/gce.py
2020-12-19 11:27:34 +01:00

67 lines
1.9 KiB
Python

#!/usr/bin/env python3
# usage gce mirror --axis x translate --y -200 file.gcode
# usage gce
arg_mirror_axis = 'y'
arg_translation=[200, 0, 0]
arg_file_path='file.gcode'
from os import remove, rename
from re import sub, findall, match, IGNORECASE
# import click
# modify_instructions = 'G0, G1, G2, G3, G90, G91, S, F, M3, M4'.split(', ')
modify_instructions = '(G(0|1|2|3|90|91)|S|F|M(3|4))'
temp_file_path = '.file.gcode.tmp'
out_file_path = 'file_modified.gcode'
# @click.command()
# @click.option('--axis', default='x', help='mirror axis')
# @click.argument('mirror')
def mirror(instruction, axis='Y'):
return instruction.replace(axis, f'{axis}-')
def translate(instruction, translation=[0, 0, 0]):
pattern = 'X[-0-9.]+'
x = findall(pattern, instruction, IGNORECASE)
if not x:
return instruction
try:
x = float(x[0][1:])
except ValueError:
return instruction
x = round(x + translation[0], 3)
return sub(pattern, f'X{x}', instruction, IGNORECASE)
with open(arg_file_path, 'r') as gcode:
try:
remove(temp_file_path)
except IOError:
pass
with open(temp_file_path, 'w') as tmp_gcode:
instructions = gcode.readlines()
instructions_length = len(instructions)
instructions_count = 0
for instruction in instructions:
instructions_count += 1
print(f'\rparsing instruction {instructions_count}/{instructions_length})',
end='')
instruction = instruction.strip()
if match(modify_instructions, instruction, IGNORECASE):
# if any(map(instruction.__contains__, modify_instructions)):
# import pudb; pudb.set_trace()
instruction = mirror(instruction, axis=arg_mirror_axis)
instruction = translate(instruction, translation=arg_translation)
tmp_gcode.write(f'{instruction}\n')
rename(temp_file_path, out_file_path)