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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.12 Windows x86-64

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

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

nc_gcode_interpreter-0.1.9-cp312-cp312-macosx_11_0_arm64.whl (3.5 MB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

nc_gcode_interpreter-0.1.9-cp312-cp312-macosx_10_12_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

nc_gcode_interpreter-0.1.9-cp311-none-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.11 Windows x86-64

nc_gcode_interpreter-0.1.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

nc_gcode_interpreter-0.1.9-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

nc_gcode_interpreter-0.1.9-cp311-cp311-macosx_11_0_arm64.whl (3.5 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nc_gcode_interpreter-0.1.9-cp311-cp311-macosx_10_12_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.9.tar.gz
Algorithm Hash digest
SHA256 c68a3e1d21bba3f93c9a7f72697d925b26005b5cec4f1e050d00885645802b5d
MD5 e5b7c1e8b50556ab6e0925855885d141
BLAKE2b-256 6a2916310e448419d7dcc15cdc5ac8b3a9053d35b608cd248d090aa82267fd40

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.9-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 adbef55945ff20530087c48f82de3b298461a2de64f252fa5decb8dd08c6ddab
MD5 59e980d285b9f09e79aa546bc4bff758
BLAKE2b-256 d853bfe419bd8fe50767063821688a6dc232372abfff4c59747abedd44b30aa2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.9-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8c331028862e9cfcaea5a02da1951a441a7fbd5832a849e0479c594a42881b81
MD5 e7611e77d0fa2b615991eee5d0a8de05
BLAKE2b-256 55c02183232f32b946ad47bbb97778683c71d4b4748be3b52dc2043e90375505

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.9-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e3e014fc4c1fb1d3a419e8a3149e69ac92a0a6109646338977bffe065bc451da
MD5 d7f8c27773f4bbb108894d723ad99a9f
BLAKE2b-256 890123e266b6e26f54adc94461c20fa57fcb968da0aebe4d20118937e753b34b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.9-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 665541ed0cf811e903dbbe9997093c4267d0e29f781d2469624a4a2f5506d5b9
MD5 32babf8b87ea4c0c71fad84bbd1ca5ad
BLAKE2b-256 830a1e26587c925df29fbf32920fe713354eed6d5560cf4ddff9491a5cf8c473

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.9-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 29dc20ed759c0169589d18dca42f19e50dbd1f1f8b0477d3fb4949ce5c7c94c9
MD5 d53f3bef10ba427332bb4da24be0bb0b
BLAKE2b-256 741dd30ec207a85c13f9ed38dea273100cd7c293cabb1e77445fc6b37f46f0f8

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.9-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.9-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 3a5b4b429095aacbd274c7b94e0ce1396b74720f9ab65be050879c6da5e97065
MD5 740fe1dfd620c51ce6178b40c282f50f
BLAKE2b-256 2300c63f06932b7e0609650df4a025d7380b3abc4da71734e27c35254085e623

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0f91d2d030d43cf5e9732506e18d83a10bd430fa74afa2254e6b341faf840c5a
MD5 76eac0b492903b8b8698dacdaa69e266
BLAKE2b-256 f8c2b84ad67aec791bab84bc3b143871d768a96b723b423260e5dc4c4485b9cf

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.9-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.9-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e6016c44d170c43163dd0201cd3bbef0a68cf5dc9bc6303ef801988b7dcd8c20
MD5 fd9f5c34c963abcbce231fa22143af9b
BLAKE2b-256 6b2699b389e9cb03504e4113deaa397f110a2f778414666ea2d7c9be4826ae24

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.9-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.9-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5c62e71873e6cf0b6a8e03656a68eb556d055936c0620aa93251d46afcfc5a00
MD5 0d5dde998aeefdac954dd3e7acdd45a8
BLAKE2b-256 aaa306cf9c328df4d4cc7fbda1f553830cd5c1b24bd2812d83a9d40d3d01e878

See more details on using hashes here.

File details

Details for the file nc_gcode_interpreter-0.1.9-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.9-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 15a94f62a09d958b221a2d3ec2adba4d906656d5c2c997b08d9da7b82d8b97c0
MD5 ca53477173c0ffb5219f1754ce5ea2c3
BLAKE2b-256 cd0eb6e8de44230ce961969dc652b27c63311f05cb55079b6769443f13d50c7f

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