Commit ec1f3fc5 authored by Fabrice Salvaire's avatar Fabrice Salvaire

added annotate-gcode example

parent c359f01f
...@@ -261,6 +261,18 @@ class Line: ...@@ -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): def __repr__(self):
items = [] items = []
......
...@@ -135,8 +135,7 @@ class Letters(YamlMixin): ...@@ -135,8 +135,7 @@ class Letters(YamlMixin):
data = self._load_yaml(yaml_path) data = self._load_yaml(yaml_path)
self._letters = {} self._letters = {}
for letter, d in data.items(): for letter, d in data.items():
letter = Letter(letter, d['meaning']) self._letters[letter] = Letter(letter, d['meaning'])
self._letters[letter] = letter
############################################## ##############################################
......
...@@ -395,9 +395,16 @@ class GcodeParserMixin: ...@@ -395,9 +395,16 @@ class GcodeParserMixin:
Return a :class:`PythonicGcodeMachine.Gcode.Rs274.Ast.Program` instance. Return a :class:`PythonicGcodeMachine.Gcode.Rs274.Ast.Program` instance.
""" """
if not isinstance(lines, (list, tuple)):
lines = lines.split('\n')
program = Ast.Program() program = Ast.Program()
for line in lines.split('\n'): for line in lines:
program += self.parse(line) try:
program += self.parse(line)
except GcodeParserError as exception:
print('Parse Error:', line)
raise exception
return program return program
......
####################################################################################################
"""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))
; 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment