From f223aead469cb8013784faef6baf09749fed834c Mon Sep 17 00:00:00 2001 From: Fabrice Salvaire Date: Tue, 25 Dec 2018 01:18:08 +0100 Subject: [PATCH] improved Ast --- PythonicGcodeMachine/Gcode/Rs274/Ast.py | 15 +++++++++++++++ examples/gcode/annotate-gcode.py | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/PythonicGcodeMachine/Gcode/Rs274/Ast.py b/PythonicGcodeMachine/Gcode/Rs274/Ast.py index 7d49298..c6670d8 100644 --- a/PythonicGcodeMachine/Gcode/Rs274/Ast.py +++ b/PythonicGcodeMachine/Gcode/Rs274/Ast.py @@ -381,11 +381,26 @@ class Line(MachineMixin): if isinstance(item, Word): yield item + def iter_on_gm_word(self): + for item in self: + if isinstance(item, Word) and item.is_gm_gcode: + yield item + + def iter_on_x_word(self): + for item in self: + if isinstance(item, Word) and not item.is_gm_gcode: + yield item + def iter_on_setting(self): for item in self: if isinstance(item, ParameterSetting): yield item + def iter_in_order(self): + words = [word for word in self.iter_on_gm_word()] + words.sort(key=lambda word: word.execution_order.index) + return words + ############################################## def toggle(self): diff --git a/examples/gcode/annotate-gcode.py b/examples/gcode/annotate-gcode.py index 8e933df..0fbf750 100644 --- a/examples/gcode/annotate-gcode.py +++ b/examples/gcode/annotate-gcode.py @@ -63,6 +63,9 @@ program = machine.parser.parse_lines(lines) #r# We dump the annotated program +def str_list(a_list): + return ' '.join([str(item) for item in a_list]) + meaning_format = ' {:5}: {}' for line in program: print() @@ -77,4 +80,10 @@ for line in program: print(margin + 'Valid G-code: {}'.format(word.is_valid_gcode)) else: print(meaning_format.format(word.letter, word.meaning)) + print( + ' execution:', + str_list(line.iter_in_order()), '/', + str_list(line.iter_on_x_word()), '/', + str_list(line.iter_on_setting()), + ) #o# -- GitLab