gcodeparser


Namegcodeparser JSON
Version 0.2.4 PyPI version JSON
download
home_pagehttps://github.com/AndyEveritt/GcodeParser
SummaryPython gcode parser
upload_time2024-09-18 11:09:24
maintainerNone
docs_urlNone
authorAndy Everitt
requires_pythonNone
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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`

```python
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

```python
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.

```py
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:

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

## Converting a File

```python
from gcodeparser import GcodeParser

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

_output:_

```py
[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=''),
 ...
]
```

## Convert Command Tuple to String

The `GcodeLine`class has a property `command_str` which will return the command tuple as a string. ie `('G', 91)` -> `"G91"`.

## Changing back to Gcode String

The `GcodeLine` class has a property `gcode_str` which will return the equivalent gcode string.

> This was called `to_gcode()` in version 0.0.6 and before.

## Parameters

The `GcodeLine` class has a several helper methods to get and manipulate gcode parameters.

For an example `GcodeLine` `line`:

### Retrieving Params

To retrieve a param, use the 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.

```python
line.get_param('X')
```

### Updating Params

To update a param, use the method `update_param(param: str, value: int | float)`

```python
line.update_param('X', 10)
```

If the param does not exist, it will return `None` else it will return the updated value.

### Deleting Params

To delete a param, use the method `delete_param(param: str)`

```python
line.delete_param('X')
```

## 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)`

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/AndyEveritt/GcodeParser",
    "name": "gcodeparser",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Andy Everitt",
    "author_email": "andreweveritt@e3d-online.com",
    "download_url": "https://files.pythonhosted.org/packages/0f/58/2ace874aa9611ec2d2b4313728b1a00421ae5a6e27001d539ccb17576c9e/gcodeparser-0.2.4.tar.gz",
    "platform": null,
    "description": "# GcodeParser\n\nA simple gcode parser that takes a string of text and returns a list where each gcode command is seperated into a python object.\n\nThe structure of the python object is:\n\n`G1 X10 Y-2.5 ; this is a comment`\n\n```python\nGcodeLine(\n  command = ('G', 1),\n  params = {'X': 10, 'Y': -2.5},\n  comment = 'this is a comment',\n)\n```\n\n# Install\n\n```\npip install gcodeparser\n```\n\nAlternatively:\n\n```\npip install -e \"git+https://github.com/AndyEveritt/GcodeParser.git@master#egg=gcodeparser\"\n```\n\n# Usage\n\n```python\nfrom gcodeparser import GcodeParser\n\n# open gcode file and store contents as variable\nwith open('my_gcode.gcode', 'r') as f:\n  gcode = f.read()\n\nGcodeParser(gcode).lines    # get parsed gcode lines\n```\n\n## Include Comments\n\n`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.\n\n```py\ngcode = (\n  'G1 X1 ; this comment is always included\\n',\n  '; this comment will only be included if `include_comments=True`',\n)\n\nGcodeParser(gcode, include_comments=True).lines\n```\n\nIf `include_comments` is `True` then the comment line will be in the form of:\n\n```python\nGcodeLine(\n  command = (';', None),\n  params = {},\n  comment = 'this comment will only be included if `include_comments=True`',\n)\n```\n\n## Converting a File\n\n```python\nfrom gcodeparser import GcodeParser\n\nwith open('3DBenchy.gcode', 'r') as f:\n    gcode = f.read()\nparsed_gcode = GcodeParser(gcode)\nparsed_gcode.lines\n```\n\n_output:_\n\n```py\n[GcodeLine(command=('G', 10), params={'P': 0, 'R': 0, 'S': 0}, comment='sets the standby temperature'),\n GcodeLine(command=('G', 29), params={'S': 1}, comment=''),\n GcodeLine(command=('T', 0), params={}, comment=''),\n GcodeLine(command=('G', 21), params={}, comment='set units to millimeters'),\n GcodeLine(command=('G', 90), params={}, comment='use absolute coordinates'),\n GcodeLine(command=('M', 83), params={}, comment='use relative distances for extrusion'),\n GcodeLine(command=('G', 1), params={'E': -0.6, 'F': 3600.0}, comment=''),\n GcodeLine(command=('G', 1), params={'Z': 0.45, 'F': 7800.0}, comment=''),\n GcodeLine(command=('G', 1), params={'Z': 2.35}, comment=''),\n GcodeLine(command=('G', 1), params={'X': 119.575, 'Y': 89.986}, comment=''),\n GcodeLine(command=('G', 1), params={'Z': 0.45}, comment=''),\n GcodeLine(command=('G', 1), params={'E': 0.6, 'F': 3600.0}, comment=''),\n GcodeLine(command=('G', 1), params={'F': 1800.0}, comment=''),\n GcodeLine(command=('G', 1), params={'X': 120.774, 'Y': 88.783, 'E': 0.17459}, comment=''),\n GcodeLine(command=('G', 1), params={'X': 121.692, 'Y': 88.145, 'E': 0.11492}, comment=''),\n GcodeLine(command=('G', 1), params={'X': 122.7, 'Y': 87.638, 'E': 0.11596}, comment=''),\n GcodeLine(command=('G', 1), params={'X': 123.742, 'Y': 87.285, 'E': 0.11317}, comment=''),\n ...\n]\n```\n\n## Convert Command Tuple to String\n\nThe `GcodeLine`class has a property `command_str` which will return the command tuple as a string. ie `('G', 91)` -> `\"G91\"`.\n\n## Changing back to Gcode String\n\nThe `GcodeLine` class has a property `gcode_str` which will return the equivalent gcode string.\n\n> This was called `to_gcode()` in version 0.0.6 and before.\n\n## Parameters\n\nThe `GcodeLine` class has a several helper methods to get and manipulate gcode parameters.\n\nFor an example `GcodeLine` `line`:\n\n### Retrieving Params\n\nTo retrieve a param, use the method `get_param(param: str, return_type=None, default=None)` which\nreturns the value of the param if it exists, otherwise it will the `default` value.\nIf `return_type` is set, the return value will be type cast.\n\n```python\nline.get_param('X')\n```\n\n### Updating Params\n\nTo update a param, use the method `update_param(param: str, value: int | float)`\n\n```python\nline.update_param('X', 10)\n```\n\nIf the param does not exist, it will return `None` else it will return the updated value.\n\n### Deleting Params\n\nTo delete a param, use the method `delete_param(param: str)`\n\n```python\nline.delete_param('X')\n```\n\n## Converting to DataFrames\n\nIf for whatever reason you want to convert your list of `GcodeLine` objects into a pandas dataframe, simply use `pd.DataFrame(GcodeParser(gcode).lines)`\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python gcode parser",
    "version": "0.2.4",
    "project_urls": {
        "Homepage": "https://github.com/AndyEveritt/GcodeParser"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4a34755b610e1a5c9cb6a7935318fec6a9ce7553316ee8764a77ab3a7dc8546f",
                "md5": "bc65bd6c33f2c0464bff781eec7ac683",
                "sha256": "5c58ac8e016d8335129029d63757e97d572f1d504ca7326b3192a473a2c16ec4"
            },
            "downloads": -1,
            "filename": "gcodeparser-0.2.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bc65bd6c33f2c0464bff781eec7ac683",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7968,
            "upload_time": "2024-09-18T11:09:23",
            "upload_time_iso_8601": "2024-09-18T11:09:23.407082Z",
            "url": "https://files.pythonhosted.org/packages/4a/34/755b610e1a5c9cb6a7935318fec6a9ce7553316ee8764a77ab3a7dc8546f/gcodeparser-0.2.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0f582ace874aa9611ec2d2b4313728b1a00421ae5a6e27001d539ccb17576c9e",
                "md5": "442db83879dd759feda1ec9ea9b52dbe",
                "sha256": "21a4270862dbf6f4225e110b27431b227eb4fdd2cdf753584776e5817ceb4330"
            },
            "downloads": -1,
            "filename": "gcodeparser-0.2.4.tar.gz",
            "has_sig": false,
            "md5_digest": "442db83879dd759feda1ec9ea9b52dbe",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6159,
            "upload_time": "2024-09-18T11:09:24",
            "upload_time_iso_8601": "2024-09-18T11:09:24.295355Z",
            "url": "https://files.pythonhosted.org/packages/0f/58/2ace874aa9611ec2d2b4313728b1a00421ae5a6e27001d539ccb17576c9e/gcodeparser-0.2.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-18 11:09:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "AndyEveritt",
    "github_project": "GcodeParser",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "gcodeparser"
}
        
Elapsed time: 1.29521s