Skip to main content

About gCodeGen

This package provides a super simple domain language, which can be used for the generation of G-Code. The main occasion for such a language was that the supported G-Code syntax greatly differs between implementations. Some do not even allow the use of variables or control flow constructs such as loops and if statements. gCodeGen introduces a simple language, which syntactically is pretty similar to python code with a few distinct differences, and of course, a very limited feature set. It's important to understand that gCodeGen is only a convenient way to write a more extensive G-code file. Thus compilation must resolve all variables to concrete numbers. By restricting on a basic subset of common G-code commands gCodeGen strives to support any kind of G-code implementation.

Installation

pip install gCodeGen

Import and usage

For generation of G-Code from an input file use:

from gCodeGen import gCodeGen
gCodeGen.generate("inputFileName", "outputFileName")

To test gCodeGen interactively use:

gCodeGen.shell()

Basics of gCodeGen

Comments, like in python, start with a '#' symbol and go until the end of the line. Multiline comments are wrapped into tripple quotes.

# This is a line comment

""" 
Multi-line
comment
"""

A standard line of G-code will unchangedly be copied into the output file. For example: G0 X100 Y200 Z0. Variables must be declared with the 'var' keyword and variable names must be all lower case! Underscores are allowed.

var cycle_counter = 0

The initialization of the variable is optional. An assignment to a variable can happen any time after its declaration.

var my_x
my_x = 2.5

Numbers are either intergers without decimal point, or floats. A float must neither begin nor end with the decimal point:

var my_y
my_y = 0.5  # is okay
my_y = .5   # not allowed!

Calculations can be done as expected:

var pi = 3.1415
var r = 4.5
var sphere_volume = 4/3 * pi * (r + 1)**3

After intialization variables can be used within G-code by simply putting them right behind the corresponding coordinate. Mixing with static values is okay.

G0 Xmy_x Ymy_y Z0 F5000

While the above is valid its much more readable an recommended to put parenthesis around variables.

G0 X(my_x) Y(my_y) Z0 F5000

Both lines will evaluate to 'G0 X2.5 Y0.5 Z0 F5000' in our case.

Control Flow

There are only two control flow statements. The first is a 'if' statement which is similar to python syntax. However gCodeGen does not care about indentation, but uses the keyword 'end' to mark the end of a block. So indentation is merely a matter of structuring your code but doesn't alter its meaning. Bool values behave the same way they do in python.

var start = True

if start:
    G0 Z0
end

Bools can be negated using '!': var not_true = !True. Else statements work in a similar fashion. Note that the colon after 'if' respectively 'else' is mandatory.

var speed = 0

if speed > 0:
    G0 X0 Y0 Z0 F(speed)
else:
    speed = speed + 1
end

The other control mechanism is the 'repeat' statement. Its similar to a for loop without the index variable. It starts with the keyword 'repeat' followed by a number. If the number is a float it will be rounded to the closest integer. The statements within the body of repeat statement will then be looped over this many times:

var times = 10
var my_z = 100

repeat times:
    G0 Z(my_z)
    my_z = my_z - 10
end

This will output the G-code line 10 times into the output file. Of course you can mix control flow statements. To structure your output code, you can also use 'print' statements. If you are familiar with Rust thats how print statements work. To print variables we simply put some placeholders '{}' into the text and after on specify the variables in the order they should be injected.

var one = 1
var two = 2
print("{} wrongs don't make {} right!", two, one)

var h = "Hello"
print("{}, world!", h)

For more example code and its respective output have a look at the companion github repo.

Metadata

Release files for gCodeGen 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gCodeGen 1.1.1
File Size Uploaded
gCodeGen-1.1.1.tar.gz 16.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gCodeGen 1.1.1
File Interpreter ABI Platform
gCodeGen-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 33.6 kB

Release files / gCodeGen-1.1.1.tar.gz

Download URL gCodeGen-1.1.1.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
68f804583e70f7e20f4c78230401c28eebd633b121019af29424d74843cb6445
BLAKE2b-256 checksum
How to use checksums
71703a91979bb3d44375bab34c35ce87c7d0acfaa5e9ee860db06767eea2b036
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.2

Release files / gCodeGen-1.1.1-py3-none-any.whl

Download URL gCodeGen-1.1.1-py3-none-any.whl
Size 16.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf1f3fddc3dae91c337d995b940da92a7797232c64807f0c61b164665776ae61
BLAKE2b-256 checksum
How to use checksums
086b1a04f50acb1508e34af790014a6491313a9f3868462ac3962291b39ed0ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.2

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page