Skip to main content

A simple domain language to generate G-Code

Project description

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.

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

gCodeGen-1.1.1.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gCodeGen-1.1.1-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file gCodeGen-1.1.1.tar.gz.

File metadata

  • Download URL: gCodeGen-1.1.1.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for gCodeGen-1.1.1.tar.gz
Algorithm Hash digest
SHA256 68f804583e70f7e20f4c78230401c28eebd633b121019af29424d74843cb6445
MD5 04c2311bce59be8c84ad00f115543c0a
BLAKE2b-256 71703a91979bb3d44375bab34c35ce87c7d0acfaa5e9ee860db06767eea2b036

See more details on using hashes here.

File details

Details for the file gCodeGen-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: gCodeGen-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for gCodeGen-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cf1f3fddc3dae91c337d995b940da92a7797232c64807f0c61b164665776ae61
MD5 756298de4f3b2337109d88d16cdcc776
BLAKE2b-256 086b1a04f50acb1508e34af790014a6491313a9f3868462ac3962291b39ed0ba

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