Package: gcodeparser
GcodeParserA 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 commentGcodeLine(command=('G',1),params={'X':10,'Y':-2.5},comment='this is a comment',)Installpip install gcodeparserAlternatively:pip install -e "git+https://github.com/AndyEveritt/GcodeParser.git@master#egg=gcodeparser"UsagefromgcodeparserimportGcodeParser# open gcode file and store contents as variablewithopen('my_gcode.gcode','r')asf:gcode=f.read()GcodeParser(gcode).lines# get parsed gcode linesInclude CommentsGcodeParsertakes a second argument calledinclude_commentswhich defaults toFalse. If this is set toTruethen 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).linesIfinclude_commentsisTruethen 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 FilefromgcodeparserimportGcodeParserwithopen('3DBenchy.gcode','r')asf:gcode=f.read()parsed_gcode=GcodeParser(gcode)parsed_gcode.linesoutput:[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 StringTheGcodeLineclass has a propertycommand_strwhich will return the command tuple as a string. ie('G', 91)->"G91".Changing back to Gcode StringTheGcodeLineclass has a propertygcode_strwhich will return the equivalent gcode string.This was calledto_gcode()in version 0.0.6 and before.ParametersTheGcodeLineclass has a several helper methods to get and manipulate gcode parameters.For an exampleGcodeLineline:Retrieving ParamsTo retrieve a param, use the methodget_param(param: str, return_type=None, default=None)which
returns the value of the param if it exists, otherwise it will thedefaultvalue.
Ifreturn_typeis set, the return value will be type cast.line.get_param('X')Updating ParamsTo update a param, use the methodupdate_param(param: str, value: int | float)line.update_param('X',10)If the param does not exist, it will returnNoneelse it will return the updated value.Deleting ParamsTo delete a param, use the methoddelete_param(param: str)line.delete_param('X')Converting to DataFramesIf for whatever reason you want to convert your list ofGcodeLineobjects into a pandas dataframe, simply usepd.DataFrame(GcodeParser(gcode).lines)