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

Uploaded CPython 3.13Windows x86-64

nc_gcode_interpreter-0.0.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.0.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.0.10-cp313-cp313-macosx_11_0_arm64.whl (3.6 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

nc_gcode_interpreter-0.0.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.0.10-cp312-cp312-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

nc_gcode_interpreter-0.0.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.0.10-cp312-cp312-macosx_11_0_arm64.whl (3.6 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

nc_gcode_interpreter-0.0.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.0.10.tar.gz.

File metadata

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

File hashes

Hashes for nc_gcode_interpreter-0.0.10.tar.gz
Algorithm Hash digest
SHA256 88f0bdc130d96900ca7c4b948d453ad5cf7722135454bf59be922f28d66267fa
MD5 77e56a91e53fc13190ae36b0acfa4869
BLAKE2b-256 ab5df875b7cd3fadfc52c558f5ca84457702eb0d259cc0e8ae749150cf184eff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.0.10-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0056ed23ddb9f0cfce944e30a213a6e6911c5b0f0ba331a7e50a92dac846ba7e
MD5 cc46dc06920f1fec74e131f26650e0ce
BLAKE2b-256 616f52c68d2bc14963e6132d6ab8a927e575207d577189b72ad06a9780e23bc2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.0.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3db91deda9f6f59dbbb53a05bce277c6ca54f4b8e29472219021c345cd284024
MD5 b1a6a122d5de89129f833ff668a3a491
BLAKE2b-256 0ae5cbbb9ff494bc3f9aa1afcfc489aa43c757679879ab1965dd851098b950e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.0.10-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5965083835683cae68c6bfc697d07ea70496c91de427002f23f336246b716e80
MD5 691a14f1cfd814aefb0284c8946dc919
BLAKE2b-256 2e825016f4eb12a283d8b4cbba76faccaafb7e8013e5e80df2abcc7a66007688

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.0.10-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dd1422fcd177086388bc3c3d580cbbc191d53426803338e5a75a3dff26bcef34
MD5 ef496d2bb5ed5c9c5c20bb97429b6990
BLAKE2b-256 f9129924391292ddd5d995a476e5bffe32294ff8bc6794e64e7b299a92558cdb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.0.10-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a4b2312c64f8a7432f99658f9d20b900316762576d58a6249c6bd6d5b272a68e
MD5 776af95010a2f23b18d3fe2a68527d99
BLAKE2b-256 20b21968ebca6d9894abce1c809b9ef5fb9c5ebeb3876c42464844df0a03134b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.0.10-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e529989b7ff2609e02990ff327edc621a38f1aa615f862e702f5bc2d4b216e01
MD5 696b84edb08a5de2add194e23695b14f
BLAKE2b-256 53229fff24b98a03b8867b55dccda043301c8e90d178bda8aaffd9d031472f11

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.0.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4ef1b5a2e9dddd78cde496b81b09782cbf6265b110240368ac144bc04e337813
MD5 1027180938b9d6f758af80de287ae29b
BLAKE2b-256 1f6c5f084f0c1557d44bf6673565ae5bfc0d190fac28172e62bcbf4d99a88306

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.0.10-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 eb01400d1db69ca2c63e8052e24f2b3aa49dcf6f81c10293c7c89de7b3e4dbb3
MD5 17bffdc4b554d9201930e988b8b2d1d7
BLAKE2b-256 9d248a6eefee62ae9d684ff9b36af45e8ebbea45bc015d4ee423a3582402990c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.0.10-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 26da147b9a788b83c715a42c0c6abe69cd3beb96cd25b04a260f3640c2c3575c
MD5 f18c3c86cf0e914a2c1baec4cbc80850
BLAKE2b-256 3c1d5e58ef91d4cd48d76491ac25d9ff40ac05134efdbc3ad20501d56c160a78

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.0.10-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 747e8ac87d8de867951e30568f53436df7aef5032054be05807b955c947a665d
MD5 c4593443ae296f32cebbeb9a2e73d907
BLAKE2b-256 52c18c80e49d5a40f4fa89560a2947f5914c3d7135333478eeb39725cfe02e6f

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