#?##################################################################################################
#?#
#?# PythonicGcodeMachine - A Python G-code Toolkit
#?# Copyright (C) 2018 Fabrice Salvaire
#?#
#?# This program is free software: you can redistribute it and/or modify
#?# it under the terms of the GNU General Public License as published by
#?# the Free Software Foundation, either version 3 of the License, or
#?# (at your option) any later version.
#?#
#?# This program is distributed in the hope that it will be useful,
#?# but WITHOUT ANY WARRANTY; without even the implied warranty of
#?# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#?# GNU General Public License for more details.
#?#
#?# You should have received a copy of the GNU General Public License
#?# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#?#
#?##################################################################################################

####################################################################################################

#r# ===========================
#r#  Annotate a G-code program
#r# ===========================
#r#
#r# For API see
#r#
#r# * :mod:`PythonicGcodeMachine.Gcode.Rs274`
#r# * :mod:`PythonicGcodeMachine.Gcode.Rs274.Ast`
#r# * :mod:`PythonicGcodeMachine.Gcode.Rs274.Parser`

####################################################################################################

from pathlib import Path

from PythonicGcodeMachine.Gcode.Rs274 import GcodeParser, config

####################################################################################################

program_filename = 'mill-example-1.ngc'

programs_directory = Path(__file__).parents[1].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()) # Fixme: pyterate
    print(str(line))
    for word in line.iter_on_word():
        if word.letter in 'GM':
            meaning = config.gcodes[str(word)].meaning
            print(meaning_format.format(str(word), meaning))
        else:
            letter = word.letter
            meaning = config.letters[letter].meaning
            print(meaning_format.format(letter, meaning))
#o#

6.1.1. Annotate a G-code programΒΆ

For API see

from pathlib import Path

from PythonicGcodeMachine.Gcode.Rs274.Machine import GcodeMachine

We build a RS-274 G-code Machine

machine = GcodeMachine()

We load a G-code program

program_filename = 'mill-example-1.ngc'

programs_directory = Path(__file__).parents[1].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:]

We parse the program

program = machine.parser.parse_lines(lines)

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()
    # print(line.ansi_str()) # Fixme: pyterate
    print(str(line))
    line.check_modal_group()
    for word in line.iter_on_word():
        if word.is_gm_gcode:
            margin = ' '*9
            print(meaning_format.format(str(word), word.meaning))
            print(margin + 'Modal group: {}'.format(word.modal_group.meaning))
            print(margin + 'Execution order: {}'.format(word.execution_order.index))
            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()),
    )
N40 G90 G0 X0 Y0
  G90  : absolute distance mode
         Modal group: distance mode
         Execution order: 17
         Valid G-code: True
  G0   : rapid positioning
         Modal group: None
         Execution order: 20
         Valid G-code: True
  X    : X-axis of machine
  Y    : Y-axis of machine
  execution: G90 G0 / X0 Y0 /

N50 G1 X-10 Y-20 R8 (P1)
  G1   : linear interpolation
         Modal group: None
         Execution order: 20
         Valid G-code: True
  X    : X-axis of machine
  Y    : Y-axis of machine
  R    : arc radius
  execution: G1 / X-10 Y-20 R8 /

N60 G1 X-50 R10 (P2)
  G1   : linear interpolation
         Modal group: None
         Execution order: 20
         Valid G-code: True
  X    : X-axis of machine
  R    : arc radius
  execution: G1 / X-50 R10 /

N70 Y10 (P3)
  Y    : Y-axis of machine
  execution:  / Y10 /

N80 X-19.97 Y25.01 (P4)
  X    : X-axis of machine
  Y    : Y-axis of machine
  execution:  / X-19.97 Y25.01 /

N90 G3 X7.97 Y38.99 R18 (P5)
  G3   : circular/helical interpolation (counterclockwise)
         Modal group: None
         Execution order: 20
         Valid G-code: True
  X    : X-axis of machine
  Y    : Y-axis of machine
  R    : arc radius
  execution: G3 / X7.97 Y38.99 R18 /

N100 G1 X30 Y50 (P6)
  G1   : linear interpolation
         Modal group: None
         Execution order: 20
         Valid G-code: True
  X    : X-axis of machine
  Y    : Y-axis of machine
  execution: G1 / X30 Y50 /

N110 G91 X10.1 Y-10.1 (P7)
  G91  : incremental distance mode
         Modal group: distance mode
         Execution order: 17
         Valid G-code: True
  X    : X-axis of machine
  Y    : Y-axis of machine
  execution: G91 / X10.1 Y-10.1 /

N120 G90 G2 X59.9 Y20.1 R14 (P8)
  G90  : absolute distance mode
         Modal group: distance mode
         Execution order: 17
         Valid G-code: True
  G2   : circular/helical interpolation (clockwise)
         Modal group: None
         Execution order: 20
         Valid G-code: True
  X    : X-axis of machine
  Y    : Y-axis of machine
  R    : arc radius
  execution: G90 G2 / X59.9 Y20.1 R14 /

N130 G1 X70 Y10 (P9)
  G1   : linear interpolation
         Modal group: None
         Execution order: 20
         Valid G-code: True
  X    : X-axis of machine
  Y    : Y-axis of machine
  execution: G1 / X70 Y10 /

N140 Y-20 R10 (P10)
  Y    : Y-axis of machine
  R    : arc radius
  execution:  / Y-20 R10 /

N150 X50 (P11)
  X    : X-axis of machine
  execution:  / X50 /

N160 G3 X30 R10 (P12)
  G3   : circular/helical interpolation (counterclockwise)
         Modal group: None
         Execution order: 20
         Valid G-code: True
  X    : X-axis of machine
  R    : arc radius
  execution: G3 / X30 R10 /

N170 G1 X10 R8 (P13)
  G1   : linear interpolation
         Modal group: None
         Execution order: 20
         Valid G-code: True
  X    : X-axis of machine
  R    : arc radius
  execution: G1 / X10 R8 /

N180 X0 Y0
  X    : X-axis of machine
  Y    : Y-axis of machine
  execution:  / X0 Y0 /