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.10.tar.gz (122.1 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.10-cp313-cp313-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.13Windows x86-64

nc_gcode_interpreter-0.1.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

nc_gcode_interpreter-0.1.10-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

nc_gcode_interpreter-0.1.10-cp313-cp313-macosx_11_0_arm64.whl (3.6 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

nc_gcode_interpreter-0.1.10-cp313-cp313-macosx_10_12_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

nc_gcode_interpreter-0.1.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

nc_gcode_interpreter-0.1.10-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

nc_gcode_interpreter-0.1.10-cp312-cp312-macosx_10_12_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for nc_gcode_interpreter-0.1.10.tar.gz
Algorithm Hash digest
SHA256 5c9006d3dbd7562095813b9f7080f071a723edb4b2b45aed1b2e9b0a228ac65f
MD5 e0c21504b3749cfae1c1a297381c246e
BLAKE2b-256 7d9baca3dbcd12c7399f261a218b552f2c64bc7ec70b0daad4a8379003706520

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.10-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 746f4ebd91b78208aae27d70b65afd98a8eeb64119d76b7fd099004a4fedf898
MD5 a7b9aa5a0735ca53bec9a582fccf5e9b
BLAKE2b-256 acdbf7d696acaf85ee04af79c672b392d65312e1412061d5ffb97c76edd3ea79

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ec8107d39f3c1b8fe0e4b6214e30fc82fc6c72813d67cfa4235eeccd219461f4
MD5 ae455ca34d9653589013d9887b5e4829
BLAKE2b-256 19578e98de4c5c63ff626c049cdbe0ead3a01b9cf80da6ebcf2bda868146806d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.10-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3d8bfd919d23424717e16ec320289eeff7b66f9c90327ea2da333f5ccd71a290
MD5 890f294acda0fb2c4bcf3c5e01981106
BLAKE2b-256 8c922e452b1be0c623e757651112a4aa40a65a59f12be3e3b97ad1b65f95eb95

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.10-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7a83e0eb08480e4567f2b233b2614c990ba38cbaced68bbe9eca5b879a7709a8
MD5 12d3299b131c92a4231a268408c16eb6
BLAKE2b-256 a9972ba083b0d136b2302dedbf98e18cf2ed4be5f5cd951508511b106e8a04c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.10-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 da85a4698714b81fdcd17176bf8c01745397b0366eac03dc5c306b9779971f2b
MD5 b1124325be4d114ea8199f66ff6dedc6
BLAKE2b-256 e9b65a5def9b7ef2fa7b22db2e571dbbe70f752207a3f50bb34d8490900eb905

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.10-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 fd40b23705b3898b3d7bb58c86ca654b363a49cc8e7d2db4de422ee66e05e6ca
MD5 1adfbb4156e6b11fb1a64da6bbfa59bf
BLAKE2b-256 e49ea243efd86b996e5f7dd9cdda2ee39dfece12ccf0b186ce94f259b1a828a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bb7fc85bebb55250469c4fedc17aecb9c9f7d3bc409e4637de4bf470fa189212
MD5 a627669d1f8a4ee9b9a385a996b907ad
BLAKE2b-256 75289172574b4861f4a92bf83acd752d61d007ea2a8586ec9cd9e38bf70ba11f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.10-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 59953ec3c548ae3237247c9df55e276f6ebabf069cb0d99c110807125d8b24b0
MD5 c6e3ad45a3cb720871b74e71258d5bb4
BLAKE2b-256 07f72ab16d014e0a92364cdae6c61aeaca55df1515d5793a25542aa41f1b9e8c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.10-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 978d489b81e3a26eb3f4851f4ae126bbbcb99dd7d2c0c211e7b95dd93a2d32d6
MD5 0dbf2566a2e7f1760345df576e57ab88
BLAKE2b-256 89c7d71fa06b8e42d0afa8aa00862d7a14cfbca27eee7b985c3f2fc5777fb1c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.10-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cdf20e2a32febb685ef59fce0adb79b596a6663f22f007914143c11f907a8031
MD5 52f6d184094ad77c3603d3794dcd124a
BLAKE2b-256 83043135b267dd2eac6d7734d9279cfc8f6cb333362358ff420217618c1f7c94

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