parsing gcode file to extract coordinates

泄露秘密 提交于 2020-02-23 07:23:25

问题


I have a gcode file that is full of text, more specifically coordinated for a machine. I am trying to read the file, strip away the useless information so that I can then perform an operation on the coordinated before they are sent out to the machine.

so far I have:

file = open('TestCube.gcode','r')
lines = file.readlines()
file.close

for line in lines:
    line = line.strip() 

Now I have a list of all the lines in the file how should I proceed in getting the relevant X and Y coordinates?

sample fo TestCube.gcode:

;TYPE:SKIN
G1 F1200 X-9.914 Y-9.843 E3.36222
G0 F9000 X-9.843 Y-9.914
G1 F1200 X9.914 Y9.843 E3.65844
G0 F9000 X9.914 Y9.702
G1 F1200 X-9.702 Y-9.914 E3.95254
G0 F9000 X-9.560 Y-9.914
G1 F1200 X9.914 Y9.560 E4.24451
G0 F9000 X9.914 Y9.419
G1 F1200 X-9.419 Y-9.914 E4.53437
G0 F9000 X-9.277 Y-9.914
G1 F1200 X9.914 Y9.277 E4.82211
G0 F9000 X9.914 Y9.136
G1 F1200 X-9.136 Y-9.914 E5.10772
G0 F9000 X-8.995 Y-9.914
G1 F1200 X9.914 Y8.995 E5.39123
G0 F9000 X9.914 Y8.853
G1 F1200 X-8.853 Y-9.914 E5.67260

EDIT:

import re

file = open('TestCube.gcode','r')
gcode = file.readlines()

for line in gcode:
    coord = re.findall(r'[XY]-\d.\d\d\d', line)
    if coord:
        print("{} - {}".format(coord[0], coord[1]))

回答1:


Regex seems to do the trick:

import re

gcode = [
    ';TYPE:SKIN',
    'G1 F1200 X-9.914 Y-9.843 E3.36222',
    'G0 F9000 X-9.843 Y-9.914',
    'G1 F1200 X9.914 Y9.843 E3.65844',
    'G0 F9000 X9.914 Y9.702',
    'G1 F1200 X-9.702 Y-9.914 E3.95254',
    'G0 F9000 X-9.560 Y-9.914',
    'G1 F1200 X9.914 Y9.560 E4.24451',
    'G0 F9000 X9.914 Y9.419',
    'G1 F1200 X-9.419 Y-9.914 E4.53437',
    'G0 F9000 X-9.277 Y-9.914',
    'G1 F1200 X9.914 Y9.277 E4.82211',
    'G0 F9000 X9.914 Y9.136',
    'G1 F1200 X-9.136 Y-9.914 E5.10772',
    'G0 F9000 X-8.995 Y-9.914',
    'G1 F1200 X9.914 Y8.995 E5.39123',
    'G0 F9000 X9.914 Y8.853',
    'G1 F1200 X-8.853 Y-9.914 E5.67260'
    ]

for line in gcode:
    coord = re.findall(r'[XY]-\d.\d\d\d', line)
    if coord:
        print("{} - {}".format(coord[0], coord[1]))

Result:

X-9.914 - Y-9.843
X-9.843 - Y-9.914
X-9.702 - Y-9.914
X-9.560 - Y-9.914
X-9.419 - Y-9.914
X-9.277 - Y-9.914
X-9.136 - Y-9.914
X-8.995 - Y-9.914
X-8.853 - Y-9.914

EDIT:

Please try the following. I've integrated your code with the sample I provided and made some adjustments:

import re

with open('PI_25mm_cube.gcode') as gcode:
    for line in gcode:
        line = line.strip()
        coord = re.findall(r'[XY].?\d+.\d+', line)
        if coord:
            print("{} - {}".format(coord[0], coord[1]))

By replacing open with with, it prevents you from forgetting to close() the file, and reduces the risk of causing any memory issues, as I understand it.

As its not clear whether your code will be used on other gcode files, my initial regex may not be suitable. Using Cura I created a gcode file and reviewed the coordinates provided. Based on this information you may find the pattern '[XY].?\d+.\d+' more useful. Regex101 is great website for testing these



来源:https://stackoverflow.com/questions/49210059/parsing-gcode-file-to-extract-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!