From ec1f3fc5460500f0a6778b489106cc5255de741b Mon Sep 17 00:00:00 2001 From: Fabrice Salvaire Date: Mon, 24 Dec 2018 03:45:31 +0100 Subject: [PATCH] added annotate-gcode example --- PythonicGcodeMachine/Gcode/Rs274/Ast.py | 12 +++++++ PythonicGcodeMachine/Gcode/Rs274/Config.py | 3 +- PythonicGcodeMachine/Gcode/Rs274/Parser.py | 11 +++++-- examples/annotate-gcode.py | 37 ++++++++++++++++++++++ examples/programs/mill-example-1.ngc | 16 ++++++++++ 5 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 examples/annotate-gcode.py create mode 100644 examples/programs/mill-example-1.ngc diff --git a/PythonicGcodeMachine/Gcode/Rs274/Ast.py b/PythonicGcodeMachine/Gcode/Rs274/Ast.py index bfd7733..4ef4702 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 8d7f255..f05140d 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 681d1c2..fdeea7c 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 0000000..99cfcc9 --- /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 0000000..d5d5a17 --- /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 -- GitLab