Skip to main content

A interpreter for NC (Numerical Control) code

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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.12 Windows x86-64

nc_gcode_interpreter-0.1.7-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.7-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.7-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.7-cp312-cp312-macosx_10_12_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

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

Uploaded CPython 3.11 Windows x86-64

nc_gcode_interpreter-0.1.7-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.7-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.7-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.7-cp311-cp311-macosx_10_12_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.7.tar.gz
Algorithm Hash digest
SHA256 656bde3995d8ddcacb7115e14a900825eb5019e26ddfbf3b4842eb09aea0a1f9
MD5 f0b2a6247b390fb6a657625c9324b3b9
BLAKE2b-256 234341e09ddf2a97d9a1e7e6cff2fc0bbbb31e2703c2e84baa5337825fee5079

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.7-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 ee4fe1bf45e58e8823d07913288dc895fbdcc55e14831dda425a8dc6c546f27e
MD5 b3c8f37c0ca9cb59cf65d5f35110b7fb
BLAKE2b-256 a957d766a53bc321fef3cb39e13f00867890def178328fe6930e5f26b7811dd0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 aa7083193386557da8c6343426faef010bffb0fe51dcd8957520ff6f9694bd19
MD5 1b1d40f5183672ea94a369d03e731b14
BLAKE2b-256 3057a518f9d71fa89085e436be9888dbf1d65d3a25184794b5423c00dc358213

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.7-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5e486051fbb780a7a0a657f1b4df479939dd251f1d1f27b5d7c4f084d42f33a4
MD5 40b84cbd8eeb2e9eaa0fee37b241042f
BLAKE2b-256 7a05e49657aabfe1a986baf1da037c12e9b191abf6aabeafd7a4111ccf35b5ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 88468b4c19594a5e0837243c5f41993109b3c4465d136f83299b38c1537b7bd6
MD5 be8715dfc8b4c3942b76da3a476c53a1
BLAKE2b-256 27b7436848eb257627f9513f5a845bc5560d6928fc65921a985df13e16aff3fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.7-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b3c0a07ddfbef0892c8ca3a4e45c466ce1656fa9a06f3009df14167d068090d6
MD5 5dd7b70710f688013dca3767f320463b
BLAKE2b-256 0c0a3dc7826d14d0f2c4404b5c328eba38bf60c6ff3a3580d0112b56aeba56e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.7-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 d81f03feded8e65ccf4d0d04bae4dc6cd3b1b055b5625205c2e0279d4d68abe5
MD5 06fef687a345dbd23e9fca1730909179
BLAKE2b-256 565c73095d2e33ce42a16b6f8764c2b365a4df79d44b6afabe690b33786e649a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0794ef71ab4407257c3d55ccb9a41660ce828760eaeeaf28bbf41509a62429ab
MD5 f73af8e7f9cc8e18794452a68952214c
BLAKE2b-256 c54b08ae1ee40a448bf3cda80b37a9981e3c57c1dda03a489a584b45947724ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.7-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 67e627fb60dac25e7dcb1f586764b7a010bc05e226fe88823dd4351c65041ab9
MD5 7177f18cb06948cc4124eed35da7b424
BLAKE2b-256 deed00f814aa058e7c209bdcf4def6db249ef6cd03c7f3c91f7334c06e5eb53f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2ff14c42ac96748d6faf96a27b80e1734b32aaf30dfdd11f72549cb2eca64fe3
MD5 bc98fcbcbdcda0b9c5f52aa6db753d92
BLAKE2b-256 07870f7e71c3c9afe5afb85d79d7a37654be7922efe2578c64e27adb756368ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nc_gcode_interpreter-0.1.7-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 352aa30b59252a702ef6677fb8fda837081768718c6a7f3d046ebb282e698ec8
MD5 a118d385108b9ec8960bbf0dcb5e572c
BLAKE2b-256 a749e51cc807e07d9c47f3852a0c03473a2fcde503e0cee55b934d9292668151

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