Skip to main content

A interpreter for NC (Numerical Control) GCode

Project description

NC-GCode-Interpreter

A robust interpreter designed for processing Sinumerik-flavored NC G-code, capable of converting MPF files into CSV outputs or Polars DataFrames via Python bindings.

Overview

The NC-GCode-Interpreter offers a streamlined and efficient solution for interpreting G-code specifically tailored to Sinumerik specifications. This tool caters to both command-line interface (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 G-code commands.
  • Global Transformations: 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: Supports basic operations such as addition, subtraction, multiplication, and division.
  • 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, such as 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 ; height of each layer
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
```bash
$ 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 e.g. define global variables or set axis positions
  -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

To install the Python bindings, run:

pip install nc-gcode-interpreter

Then, you can use the Python bindings to convert an MPF file to a DataFrame:

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                │
└───────┴───────┴──────┴────────┴───────────┴─────────────┴─────────────────────────────────┘

The Python bindings also return the state of the program after execution, which can be used for inspection.

Additionally, conversion from a Polars DataFrame back to an MPF (NC) program is also supported:

python -c "\
from nc_gcode_interpreter import nc_to_dataframe, dataframe_to_nc; \
from pathlib import Path; \
df, state = nc_to_dataframe(Path('Example.MPF').open(), extra_axes=['ELX']); \
dataframe_to_nc(df, Path('Example_out.MPF').open('w'))" 
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 its functionality can be very convenient for making on-the-fly improvements to code. However, to better understand, 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.

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.12.tar.gz (127.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

nc_gcode_interpreter-0.1.12-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

nc_gcode_interpreter-0.1.12-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.8 MB view details)

Uploaded CPython 3.13tmanylinux: glibc 2.17+ ARM64

nc_gcode_interpreter-0.1.12-cp313-cp313-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.13Windows x86-64

nc_gcode_interpreter-0.1.12-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

nc_gcode_interpreter-0.1.12-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

nc_gcode_interpreter-0.1.12-cp313-cp313-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

nc_gcode_interpreter-0.1.12-cp313-cp313-macosx_10_12_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

nc_gcode_interpreter-0.1.12-cp312-cp312-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.12Windows x86-64

nc_gcode_interpreter-0.1.12-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

nc_gcode_interpreter-0.1.12-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

nc_gcode_interpreter-0.1.12-cp312-cp312-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

nc_gcode_interpreter-0.1.12-cp312-cp312-macosx_10_12_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: nc_gcode_interpreter-0.1.12.tar.gz
  • Upload date:
  • Size: 127.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.9.0

File hashes

Hashes for nc_gcode_interpreter-0.1.12.tar.gz
Algorithm Hash digest
SHA256 b5285e59d29dd0f563dd8d0adb5b8513f8d16df66129d643c2362d6f3f3f54b2
MD5 f43ebde063cac0fa897b68cf9ba0f284
BLAKE2b-256 0aba5d3b54e190044efff152f62339703a98bb9c73d78f0e683644cdc97c3e3d

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.12-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 59568691a9c8d9eb00147fd70285da0f4de33f12ad9a6a987d771b8c7c5b1b3d
MD5 1b9845309383085f15425be8798821b7
BLAKE2b-256 e695cc5901843111ac7bfba8a5ea4b2b85f4a67f6372edeeaa883a2cf73af53e

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.12-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 cdb9676aa8c8f4c6a7e6724c6e58ff382a1a2ffce75e24a181f247a01cecbe2f
MD5 25e960a39fb96b7c64871c9c0a799c0c
BLAKE2b-256 4da3e593b872b1f763027dc5f326e4beaf1a00b7e0226de813eb47cd7edd4a1b

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.12-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 4626e6419f97eff01aaf56af331da9dabc4b0f59b04512dd3e5c39ea5ae90c63
MD5 4698ad7f501541de23db51588d15103d
BLAKE2b-256 a0dffc69057e87866c3da62879ee4dfe4ba77b8fd5655b1e7d64a12d9cd31642

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.12-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c7ae65c128dac429f3527be21c5beecab12008efd076e06fd625beb30136dff5
MD5 cdc58c454e1f7826e058f20b0112e859
BLAKE2b-256 b51fa4491bf95d1edaa816593580dd8f84fd9b3abf5b6793cbde5b066709f31b

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.12-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c51deff77261b1c0e036858d858890f7d6d3404034fe6b255e5c17be0b08ea4f
MD5 07e0eb6d5176dd228a818287d3be2da0
BLAKE2b-256 144283b294f8e849d905d5f8bbd6e35725c8a21463e37489997f2afafbab1161

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.12-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cd81b630a392371359687d7bb5bf8ae725f1cbe3fefd3dcc3329eb612e1d69db
MD5 d635623abf07c002868ada667d8156ba
BLAKE2b-256 fad4abc05708550bb129e6b0b24b428436f6899cd5e50b22d4a86d7c798b3306

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.12-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9772428e174ad2ae2f2e5cbfced92a84abc7e2f5394967f40d9d1d482b982e53
MD5 8d40526426b8a439d05d2dc637e64daa
BLAKE2b-256 e862647c9b16e9c35837e5bcb7e3b0bdfeeaa16a7184a3eb5dc94a039cb88ce3

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.12-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 251a323fb41b6dc58829e52786082e853f5cb752379715d50b3e09bd08d4c107
MD5 8bcb808e00e28483219d872a83818adf
BLAKE2b-256 64f033653798f07aa3866e91f042615f4823a3b876822ea633a74222e3a05e9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b5b505c404b37c155620f4f2be414f4c7d543c0ff7bdfc5273b581b1e06ff4c0
MD5 ad984e460d7aa715d160c0a17781e529
BLAKE2b-256 c442a7a3725a8c50e76f5b4374232c9874f6ce4f3faba90ad0c9bdaaa9ce127b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b13715a86a3badd046e5d4224e83f29322522c25cba75bcf9f9d5d3509ca8b20
MD5 ba3135e70d699d6bd85124cbdef19427
BLAKE2b-256 97af2bc217a6e731e1eec8684f7bf5632a2327623e44d40b215da1068c4ffba8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b2b44007cb55a53f2137b398a8857010c96cba98849428148fd08dc6013d40bc
MD5 b2725d09dda1b62d8ac1efa78a7eb452
BLAKE2b-256 1139898c1e3007bb0a46d1c5cbbde75678c804b54f95487222740b9e1850bd17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.12-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 07a8a07ab78729e4931e5de710a45bc35c9a767b0081ca19e037219b4144e7da
MD5 6c027be1f5346fd33882a4ceef70cafc
BLAKE2b-256 1b6954dad717c8575381315fd9eefced8bb7bedc8699bee011d1825c1e061bc6

See more details on using hashes here.

Supported by

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