Skip to main content

A interpreter for NC (Numerical Control) code

Project description

NC-GCode-Interpreter

A robust interpreter tailored for processing Sinumerik-flavored NC G-code, designed to convert MPF files into CSV outputs or Polars dataframes when used with Python bindings.

Overview

The NC-GCode-Interpreter provides a streamlined and efficient solution for interpreting G-code specifically designed with Sinumerik specifications in mind. This tool caters to both CLI users and those preferring a Python environment, ensuring versatility and ease of use in processing NC programming commands into structured formats like CSV files or Polars dataframes.

Features

Supported G-code Features

  • G Group Commands: Recognizes G-code groups and modal gcode commands.
  • Global Translations: Supports commands like TRANS and ATRANS for adjusting coordinates globally.
  • Looping Constructs: Handles loops using WHILE and FOR statements.
  • Variable Handling: Supports definition and manipulation of local variables.
  • Conditional Logic: Implements conditional execution with IF, ELSE, and ENDIF.
  • Arithmetic Operations: Basic operations such as addition, subtraction, multiplication, and division are supported.
  • Array Operations: Manages arrays and allows operations on them.
  • Incremental Changes: Facilitates incremental changes in axes positions like X=IC(2).

Additional Functionality

  • Custom Axes: Allows users to define additional axes beyond the standard X, Y, Z.
  • Initial State Configuration: Enables the use of an initial state MPF file to set default values for multiple runs.
  • CLI Options: Numerous command-line options to customize the processing like axis overriding, loop limits, and more.

Example Usage

Consider this example program to generate a square in two layers:

;Example.MPF
DEF INT n_layers = 2, layer=1
DEF REAL size = 100 ;size of the square
DEF REAL layer_height = 4 ;size of the square
TRANS Z = 0.5 ; move up all z coordinates by 0.5
G1 F=1000; Set feed rate in millimeters per minute
G1 X0 Y500 Z0 ; move to the starting point
WHILE (layer <= n_layers)
    X=IC(size)
    Y=IC(size)
    X=IC(-size)
    Y=IC(-size) Z=IC(layer_height)
    layer = layer + 1
ENDWHILE
M31; end of program

CLI Usage

> cargo run -- --help
A G-code interpreter

Usage: nc-gcode-interpreter [OPTIONS] <input>

Arguments:
  <input>  Input G-code file (.mpf)

Options:
  -a, --axes <AXIS>                    Override default axis identifiers (comma-separated, e.g., "X,Y,Z")
  -e, --extra-axes <EXTRA_AXIS>        Add extra axis identifiers (comma-separated, e.g., "RA1,RA2")
  -i, --initial_state <INITIAL_STATE>  Optional initial_state file to initialize state
  -l, --iteration_limit <LIMIT>             Maximum number of iterations for loops [default: 10000]
  -f, --disable-forward-fill           Disable forward-filling of null values in axes columns
  -h, --help                           Print help
  -V, --version                        Print version

> cargo run -- Example.MPF
X			,Y			,Z			,F			,M	 ,gg01_motion	,comment
			,			,			,			,	 ,			    ,;size of the square
			,			,			,			,	 ,			    ,;size of the square
			,			,			,			,	 ,			    ,; move up all z coordinates by 0.5
			,			,			,1000.000	,	 ,G1			,; Set feed rate in millimeters per minute
0.000		,500.000	,0.500		,1000.000	,	 ,G1			,; move to the starting point
100.000		,500.000	,0.500		,1000.000	,	 ,G1			,
100.000		,600.000	,0.500		,1000.000	,	 ,G1			,
0.000		,600.000	,0.500		,1000.000	,	 ,G1			,
0.000		,500.000	,5.000		,1000.000	,	 ,G1			,
100.000		,500.000	,5.000		,1000.000	,	 ,G1			,
100.000		,600.000	,5.000		,1000.000	,	 ,G1			,
0.000		,600.000	,5.000		,1000.000	,	 ,G1			,
0.000		,500.000	,9.500		,1000.000	,	 ,G1			,
0.000		,500.000	,9.500		,1000.000	,M31 ,G1			,; end of program

python example

python -c "\
from nc_gcode_interpreter import nc_to_dataframe; \
from pathlib import Path; \
df, state = nc_to_dataframe(Path('Example.MPF').open()); \
print(df)"
shape: (14, 7)
┌───────┬───────┬──────┬────────┬───────────┬─────────────┬─────────────────────────────────┐
│ X      Y      Z     F       M          gg01_motion  comment                         │
│ ---    ---    ---   ---     ---        ---          ---                             │
│ f32    f32    f32   f32     list[str]  str          str                             │
╞═══════╪═══════╪══════╪════════╪═══════════╪═════════════╪═════════════════════════════════╡
│ null   null   null  null    null       null         ;size of the square             │
│ null   null   null  null    null       null         ;size of the square             │
│ null   null   null  null    null       null         ; move up all z coordinates by… │
│ null   null   null  1000.0  null       G1           ; Set feed rate in millimeters… │
│ 0.0    500.0  0.5   1000.0  null       G1           ; move to the starting point    │
│                                                                              │
│ 100.0  500.0  5.0   1000.0  null       G1           null                            │
│ 100.0  600.0  5.0   1000.0  null       G1           null                            │
│ 0.0    600.0  5.0   1000.0  null       G1           null                            │
│ 0.0    500.0  9.5   1000.0  null       G1           null                            │
│ 0.0    500.0  9.5   1000.0  ["M31"]    G1           ; end of program                │
└───────┴───────┴──────┴────────┴───────────┴─────────────┴─────────────────────────────────┘
target/release/nc-gcode-interpreter --help
A G-code interpreter

Usage: nc-gcode-interpreter [OPTIONS] <input>

Arguments:
  <input>  Input G-code file (.mpf)

Options:
  -a, --axes <AXIS>                    Override default axis identifiers (comma-separated, e.g., "X,Y,Z")
  -e, --extra-axes <EXTRA_AXIS>        Add extra axis identifiers (comma-separated, e.g., "RA1,RA2")
  -i, --initial_state <INITIAL_STATE>  Optional initial_state file to initialize state
  -l, --iteration_limit <LIMIT>             Maximum number of iterations for loops [default: 10000]
  -f, --disable-forward-fill           Disable forward-filling of null values in axes columns
  -h, --help                           Print help
  -V, --version                        Print version

Why?

The Sinumerik NC programming guide is extensive, and some of it's functionality can be very convenient for making on the fly improvements to cde. However to better uderstand, visualize and simulate the code, it is often necessary to convert it to a more structured format like CSV or a dataframe. This tool aims to provide a simple and efficient way to convert Sinumerik-flavored G-code to a structured format, making it easier to analyze and visualize the code.

Only a limited subset is supported, but the tool is designed to be easily extensible to support more features in the future.

Contributing

We welcome contributions! Please see our Contributing Guidelines for more details on how to get started.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nc_gcode_interpreter-0.1.3.tar.gz (56.2 kB view details)

Uploaded Source

Built Distributions

nc_gcode_interpreter-0.1.3-cp312-none-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.12 Windows x86-64

nc_gcode_interpreter-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

nc_gcode_interpreter-0.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.1 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

nc_gcode_interpreter-0.1.3-cp312-cp312-macosx_11_0_arm64.whl (3.6 MB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

nc_gcode_interpreter-0.1.3-cp312-cp312-macosx_10_12_x86_64.whl (3.8 MB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

File details

Details for the file nc_gcode_interpreter-0.1.3.tar.gz.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c22e085cd6f10f986d9f6e1608822f8bbdbfd6de9350ffb137f1f21b5295fa43
MD5 2e7aac2340333ba398cd7bb501c8127b
BLAKE2b-256 62d352ff2a15344d2948726a50173b9e4b4e4658a4423570a599c8c594acac7c

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.3-cp312-none-win_amd64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.3-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 b722bcbc8aafaceb71af19006746f599259651c9971d610a5ee4b8c647eca08b
MD5 11921f76bea170ced7bc74c2f6644cae
BLAKE2b-256 6f52885416419950baa3e954afdfbbf6cf583f5b2e52508275ac98e9ddcf3ca0

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fd9b8dcdc061019289c3af83b331296cd76d174f9b6d6f441b88d6425c5b99c5
MD5 1d600464fbae8aa4fee5b5714eb9048e
BLAKE2b-256 5e88563e589a250599d14cb06d90037a6d6de2e730b6ff41abfbaa54bef35ea3

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 cdb7a9374c55414139bc5ff0ec51c529a01ff7b88900d138cc788b0cae61c291
MD5 343a9d37a94d14e152f4ac052b905b53
BLAKE2b-256 b02261d16a59cf8dff4f61c609e4775b5b8eab42284db2ba1adea2cab3182c46

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 047a34746f061643a5b108beb80c9bfcb56d3e92284c00e7a1a3165b74f2ff1a
MD5 85a1da0979fd4a2ec9a3cf0d47b6dea7
BLAKE2b-256 e04c55d7e4208e5866182f5cc1b543d78ec37111ff1eef00fba865c9dc7e6dff

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.3-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.3-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c2b36ad615c8bc5ce5aa6ef07a23b4236a5f15f2ad6aab8b78943038b5ca5c0b
MD5 52f22830809b4db16fcf8a6d2876d135
BLAKE2b-256 9674f5e9db07c7a3c3769efd9a35e8bc939b26c7cac964c84588924c0d5d8af5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page