diff --git a/PythonicGcodeMachine/Gcode/Rs274/Ast.py b/PythonicGcodeMachine/Gcode/Rs274/Ast.py index bfd7733097e2103fb765dc65c510ea4630513d3f..4ef47023eccf2bdc4f5aaaf9d499b4506a343c34 100644 --- a/PythonicGcodeMachine/Gcode/Rs274/Ast.py +++ b/PythonicGcodeMachine/Gcode/Rs274/Ast.py @@ -261,6 +261,18 @@ class Line: ############################################## + def iter_on_word(self): + for item in self: + if isinstance(item, Word): + yield item + + def iter_on_setting(self): + for item in self: + if isinstance(item, ParameterSetting): + yield item + + ############################################## + def __repr__(self): items = [] diff --git a/PythonicGcodeMachine/Gcode/Rs274/Config.py b/PythonicGcodeMachine/Gcode/Rs274/Config.py index 8d7f2551d53a793bc2387ac4088157d4039b7a66..f05140da56f28e873abd32f257ab810655bd2bd7 100644 --- a/PythonicGcodeMachine/Gcode/Rs274/Config.py +++ b/PythonicGcodeMachine/Gcode/Rs274/Config.py @@ -135,8 +135,7 @@ class Letters(YamlMixin): data = self._load_yaml(yaml_path) self._letters = {} for letter, d in data.items(): - letter = Letter(letter, d['meaning']) - self._letters[letter] = letter + self._letters[letter] = Letter(letter, d['meaning']) ############################################## diff --git a/PythonicGcodeMachine/Gcode/Rs274/Parser.py b/PythonicGcodeMachine/Gcode/Rs274/Parser.py index 681d1c286dc19351aebacc9393bd8aca6177dfcc..fdeea7c300c6f3b76e247c1ac4e2a23a9bf4316b 100644 --- a/PythonicGcodeMachine/Gcode/Rs274/Parser.py +++ b/PythonicGcodeMachine/Gcode/Rs274/Parser.py @@ -395,9 +395,16 @@ class GcodeParserMixin: Return a :class:`PythonicGcodeMachine.Gcode.Rs274.Ast.Program` instance. """ + if not isinstance(lines, (list, tuple)): + lines = lines.split('\n') + program = Ast.Program() - for line in lines.split('\n'): - program += self.parse(line) + for line in lines: + try: + program += self.parse(line) + except GcodeParserError as exception: + print('Parse Error:', line) + raise exception return program diff --git a/examples/annotate-gcode.py b/examples/annotate-gcode.py new file mode 100644 index 0000000000000000000000000000000000000000..99cfcc975f88fcbf63eb7d8b8852dbd8cc163c5e --- /dev/null +++ b/examples/annotate-gcode.py @@ -0,0 +1,37 @@ +#################################################################################################### + +"""Example to show how to annote a G-code program. +""" + +#################################################################################################### + +from pathlib import Path + +from PythonicGcodeMachine.Gcode.Rs274 import GcodeParser, gcodes, letters + +#################################################################################################### + +program_filename = 'mill-example-1.ngc' + +programs_directory = Path(__file__).parent.joinpath('programs') +program_path = programs_directory.joinpath(program_filename) +with open(program_path, 'r') as fh: + lines = fh.readlines() + if lines[0].startswith(';'): + lines = lines[1:] + +parser = GcodeParser() +program = parser.parse_lines(lines) + +meaning_format = ' {:5}: {}' +for line in program: + print() + print(line.ansi_str()) + for word in line.iter_on_word(): + if word.letter in 'GM': + meaning = gcodes[str(word)].meaning + print(meaning_format.format(str(word), meaning)) + else: + letter = word.letter + meaning = letters[letter].meaning + print(meaning_format.format(letter, meaning)) diff --git a/examples/programs/mill-example-1.ngc b/examples/programs/mill-example-1.ngc new file mode 100644 index 0000000000000000000000000000000000000000..d5d5a17b0b4d4d2529b19c583a883af03da659b3 --- /dev/null +++ b/examples/programs/mill-example-1.ngc @@ -0,0 +1,16 @@ +; http://www.helmancnc.com/cnc-mill-example-program-g01-g02-g03-g90-g91 +N40 G90 G00 X0 Y0 +N50 G01 X-10 Y-20 R8 (P1) +N60 G01 X-50 R10 (P2) +N70 Y10 (P3) +N80 X-19.97 Y25.01 (P4) +N90 G03 X7.97 Y38.99 R18 (P5) +N100 G01 X30 Y50 (P6) +N110 G91 X10.1 Y-10.1 (P7) +N120 G90 G02 X59.9 Y20.1 R14 (P8) +N130 G01 X70 Y10 (P9) +N140 Y-20 R10 (P10) +N150 X50 (P11) +N160 G03 X30 R10 (P12) +N170 G01 X10 R8 (P13) +N180 X0 Y0