Skip to main content

A interpreter for NC (Numerical Control) GCode

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

Uploaded Source

Built Distributions

nc_gcode_interpreter-0.1.8-cp312-none-win_amd64.whl (3.5 MB view details)

Uploaded CPython 3.12 Windows x86-64

nc_gcode_interpreter-0.1.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

nc_gcode_interpreter-0.1.8-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.8 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

nc_gcode_interpreter-0.1.8-cp312-cp312-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

nc_gcode_interpreter-0.1.8-cp312-cp312-macosx_10_12_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

nc_gcode_interpreter-0.1.8-cp311-none-win_amd64.whl (3.5 MB view details)

Uploaded CPython 3.11 Windows x86-64

nc_gcode_interpreter-0.1.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

nc_gcode_interpreter-0.1.8-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.8 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

nc_gcode_interpreter-0.1.8-cp311-cp311-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nc_gcode_interpreter-0.1.8-cp311-cp311-macosx_10_12_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.8.tar.gz
Algorithm Hash digest
SHA256 05747ad5a8b1232b0a5dafb22f850ceb107a2645d48d9d22fa5d74c2985be250
MD5 991ab0364a52d45312f78c7a3802485d
BLAKE2b-256 7c52493a48330b0a5d8f3f580c3c2654f2ad7d006a0b41d4611ff2068d9bbcab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.8-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 a271f521ad11c65db6c1926c3fe6145c8f6653842cd4c7fab4c038dc6003fcbe
MD5 466ef53c32dd71475f433a6225d04826
BLAKE2b-256 deb02ad34e01848394dab9a7c65efb59f85b01b1a717312fb273868724d1f370

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 23fa2c84edb47b589571c13126f49c9b4074c0d3b7e2d672071aebc6b96c90bf
MD5 2863e3906009facd0d0a18f1ba743dfd
BLAKE2b-256 2121c66ecbc0c3d8dc3707c852bc4246fe6fa9fe583d899d3ddf689654f60ad1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.8-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 969471fb8acba6abf85d7ad2cd0af2aaa07a94ec4acffaa5e433d2703d1f8b23
MD5 26b3d2d48ff740f534e709a95590c3a0
BLAKE2b-256 93c56350ba060669e0bac33b1a2a6410669c58a6fe0cbbac15aee3f2930d6c9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.8-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9e2899e31b838fc984ba53e4f95406bab7869e7f589b8633b4f7f79590d96063
MD5 602d51261e7060ce09535e6d03834388
BLAKE2b-256 9226a0723c1f29691442010bb91fb0456b88b4499acf58fcdab4743deed3281e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.8-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 52cfcb90c6b9e4dc1cb490963cfc5dcc69aba6d248870167fd0d66948e653714
MD5 3be17face0026d701ffb78cb47efd1a0
BLAKE2b-256 acf96db9785846034d0afc550d20d5c18a4e7282783f97d9eab6c7e489b15906

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.8-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 5436e179a6e925975a43fd60f1b0fa45ff56acaa5cd2853598fea5cac1edd5cd
MD5 09ea1d7ce7ee07f7ecb081b3562c633b
BLAKE2b-256 9e8a7fd40c4815b2cc6adda7d0558640bc1f3088724dad660250b29068c4c423

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d4658f8e1c497fd1d6d972e45facbe2b2d4ccbc2712c7cfd9afb07abb509d5db
MD5 d89b8b8045eb353e00808423c402d301
BLAKE2b-256 f30dc372fd621390231c07ba8dafeac418e3b01439f2e1cf2d49697911738b63

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.8-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 aa3ddd4bd6e175da1ff97645e63ad53c4fb5286293eceee416f3a137ad8358db
MD5 f121086653be307f2139aef907da7b93
BLAKE2b-256 e13dd80e15dd90fefa5d2db86e595fe252ccf13e62faa72d45d958fad8bed0d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.8-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2136aecad3aecd07b04783b672360f358fbf9e374034a3bc19c1aaf75d650d7a
MD5 67a6888ef8100e492bacf26421ac4751
BLAKE2b-256 2995d4b6c7b076c3b59320b0f8ca8b5602e224ba0ca93fd3d4f9a1694dcfde38

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.8-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1f869993ad155d40b85e65e8249263078e4e9792b25bbac09213995506e233d5
MD5 38312007082cf4b5b2910ea54fe7a807
BLAKE2b-256 c65e4434aeed3fae2ac16c55a674b259162905d6841d68866f254e67c40634ec

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