Skip to main content

Python gcode parser

Project description

GcodeParser

A simple gcode parser that takes a string of text and returns a list where each gcode command is seperated into a python object.

The structure of the python object is:

G1 X10 Y-2.5 ; this is a comment

GcodeLine(
  command = ('G', 1),
  params = {'X': 10, 'Y': -2.5},
  comment = 'this is a comment',
)

Install

pip install gcodeparser

Alternatively:

pip install -e "git+https://github.com/AndyEveritt/GcodeParser.git@master#egg=gcodeparser"

Usage

from gcodeparser import GcodeParser

# open gcode file and store contents as variable
with open('my_gcode.gcode', 'r') as f:
  gcode = f.read()

GcodeParser(gcode).lines    # get parsed gcode lines

Include Comments

GcodeParser takes a second argument called include_comments which defaults to False. If this is set to True then any line from the gcode file which only contains a comment will also be included in the output.

gcode = (
  'G1 X1 ; this comment is always included\n',
  '; this comment will only be included if `include_comments=True`',
)

GcodeParser(gcode, include_comments=True).lines

If include_comments is True then the comment line will be in the form of:

GcodeLine(
  command = (';', None),
  params = {},
  comment = 'this comment will only be included if `include_comments=True`',
)

Converting a File

from gcodeparser import GcodeParser

with open('3DBenchy.gcode', 'r') as f:
    gcode = f.read()
parsed_gcode = GcodeParser(gcode)
parsed_gcode.lines

output:

[GcodeLine(command=('G', 10), params={'P': 0, 'R': 0, 'S': 0}, comment='sets the standby temperature'),
 GcodeLine(command=('G', 29), params={'S': 1}, comment=''),
 GcodeLine(command=('T', 0), params={}, comment=''),
 GcodeLine(command=('G', 21), params={}, comment='set units to millimeters'),
 GcodeLine(command=('G', 90), params={}, comment='use absolute coordinates'),
 GcodeLine(command=('M', 83), params={}, comment='use relative distances for extrusion'),
 GcodeLine(command=('G', 1), params={'E': -0.6, 'F': 3600.0}, comment=''),
 GcodeLine(command=('G', 1), params={'Z': 0.45, 'F': 7800.0}, comment=''),
 GcodeLine(command=('G', 1), params={'Z': 2.35}, comment=''),
 GcodeLine(command=('G', 1), params={'X': 119.575, 'Y': 89.986}, comment=''),
 GcodeLine(command=('G', 1), params={'Z': 0.45}, comment=''),
 GcodeLine(command=('G', 1), params={'E': 0.6, 'F': 3600.0}, comment=''),
 GcodeLine(command=('G', 1), params={'F': 1800.0}, comment=''),
 GcodeLine(command=('G', 1), params={'X': 120.774, 'Y': 88.783, 'E': 0.17459}, comment=''),
 GcodeLine(command=('G', 1), params={'X': 121.692, 'Y': 88.145, 'E': 0.11492}, comment=''),
 GcodeLine(command=('G', 1), params={'X': 122.7, 'Y': 87.638, 'E': 0.11596}, comment=''),
 GcodeLine(command=('G', 1), params={'X': 123.742, 'Y': 87.285, 'E': 0.11317}, comment=''),
 ...
]

Changing back to Gcode String

The GcodeLine class has a method to_gcode() which will return the equivalent gcode string.

Retrieving Params

The GcodeLine class has a method get_param(param: str, return_type=None, default=None) which returns the value of the param if it exists, otherwise it will the default value. If return_type is set, the return value will be type cast.

Converting to DataFrames

If for whatever reason you want to convert your list of GcodeLine objects into a pandas dataframe, simply use pd.DataFrame(GcodeParser(gcode).lines)

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

gcodeparser-0.0.2.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

gcodeparser-0.0.2-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file gcodeparser-0.0.2.tar.gz.

File metadata

  • Download URL: gcodeparser-0.0.2.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.4

File hashes

Hashes for gcodeparser-0.0.2.tar.gz
Algorithm Hash digest
SHA256 742eaa4360879e6c635033e51d4805c13c76ea0d3016c37730b0620c47a695c4
MD5 ffbd01d1b5e9a79ef8513e31d4371f28
BLAKE2b-256 34227a30a1f94f809a200e99dc2240128c68cd7c96d9d0545317a89d836bbe64

See more details on using hashes here.

File details

Details for the file gcodeparser-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: gcodeparser-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.4

File hashes

Hashes for gcodeparser-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fe7c781a7d729a0bda0ccf52e001acdfd6faafd9e9df0b9daa36a5b7970105f7
MD5 7b657263eed5745cc83b9ddabc078f18
BLAKE2b-256 44475ef60a30656be4e7be149117e2585dc86c6d4b400341f1e0665f98799d5e

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