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.11.tar.gz (118.5 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.11-cp313-cp313-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.13Windows x86-64

nc_gcode_interpreter-0.1.11-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

nc_gcode_interpreter-0.1.11-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

nc_gcode_interpreter-0.1.11-cp313-cp313-macosx_11_0_arm64.whl (3.7 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

nc_gcode_interpreter-0.1.11-cp313-cp313-macosx_10_12_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

nc_gcode_interpreter-0.1.11-cp312-cp312-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.12Windows x86-64

nc_gcode_interpreter-0.1.11-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.1.11-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

nc_gcode_interpreter-0.1.11-cp312-cp312-macosx_11_0_arm64.whl (3.7 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

nc_gcode_interpreter-0.1.11-cp312-cp312-macosx_10_12_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for nc_gcode_interpreter-0.1.11.tar.gz
Algorithm Hash digest
SHA256 4f4d8cbcc9237e187694c7441264277a867aeda1a35bcd114cf8765eac1b4097
MD5 1446087fddeac077e2361cc63554ddf0
BLAKE2b-256 f7aafcb91e6baf11adfb4594c7c8118e0670ed7fab6b61f2b4d4a02e25c48c24

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.11-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b77a721a31cbcefffc1ef9b72ff685774d92e9fa5c89de0b24911205a6c41aa4
MD5 897f4c195b78021144dea81c49da6273
BLAKE2b-256 0eeaf4a805174d8b9dcded78b265414bae0a1572ec1e60cdb24729c5f6a828cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.11-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8da78b2125f73d6060e748c33e7a7014c6bd041ac01aaf980360d85c34cbdd05
MD5 b39f3c40672cf6f9d3c4fd09dc1fd33d
BLAKE2b-256 72e3baf9376d0621b0602e0bc1dcee6079eba4feb2232f398dc8e174d7b0a4f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.11-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6399022fde33b375ea7911e2266f302c37aba1943b75a08abee27bc8a825e127
MD5 64cae146f754b1cb9a022db3058fc62d
BLAKE2b-256 fd82121871502704dd2f7050422415efe4ed60daf7769cafbbb37c7b4f2983d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.11-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 aeaf88f3bdbc08dea4376f72712c49755c4b4b8501ee10fbc4e9f9ffa53a0c98
MD5 21e4a470e7d899c8f910f72c3e7f20be
BLAKE2b-256 0d20d3aa6e715a81d7c9efe94f7de8a6cb655ddf3954dc0d81d9a16a739ca5cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.11-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d845cf092dee09ff2cfd3c849563d05b0d3412c7cd0097f26cc5476e8bff6b29
MD5 79e05e024b2604e94dae6f27eed0bf73
BLAKE2b-256 7fe876910620e8892bc178dba98314df18d8eeb15f5d0330dfd50f613107d579

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.11-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 15b7a4776bab5044f7f42f7fe10694948edb9b2de516cd691c6339fdb9fb4cca
MD5 d4c14f3a7ec4f87539c8b7a588532267
BLAKE2b-256 b749c1daecb4656b9778a3c23f815c97ddeaa2d6f93356e8935c3799012bfab0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.11-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c02de8c5fd13e9a94e4ff823bf34250b35a441d838f36bfb017667eda1dbefdc
MD5 07e2d1d1e7e4aa6daa92b577138d497b
BLAKE2b-256 cc723025004f8d9c43cab50702685120dc1147a2fc3fda0995daae59ee90f88f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.11-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d8e222bd3b65f570174a27b41b71ee4d00fc716878e5d443985f7b83d6835511
MD5 44c1d53705dd91b87227cc6ff71a4328
BLAKE2b-256 2565956a32051b12556c6c81ef9434abcbc9373bf9b6db2796890efca49e0ce4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.11-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 81d160f1dc1f6b07ec1eb3ba4a63feeedb2933bf071d0573a0e121fd7cc43970
MD5 5e3dd68cec42190330d86acf8983d71e
BLAKE2b-256 b37d9e77fe6dec38c87d43b691a39c6aca238dc4ead0b2956c3f15c82590158d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.11-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 50fa7a6899c868bbe1c531e88fcd11192f0d45c603aac5df41d75f0973901aa9
MD5 4e2e1ce42c1006e264b6e7d0c7b68aa4
BLAKE2b-256 7c0a1692ecba53cbda33a88f7f1bfe35aa721cce7336077815a9c688ca952225

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