解析gcode文件以提取坐标 [英] parsing gcode file to extract coordinates

查看:1061
本文介绍了解析gcode文件以提取坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满文本的gcode文件,更具体地说是针对机器进行协调的.我正在尝试读取文件,剥离掉无用的信息,以便我可以在将协调的信息发送到计算机之前对其进行操作.

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.

到目前为止,我有:

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

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

现在我有了文件中所有行的列表,如何继续获取相关的X和Y坐标?

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

TestCube.gcode的样本:

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

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]))

推荐答案

正则表达式似乎可以解决问题:

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]))

结果:

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

请尝试以下操作.我已经将您的代码与我提供的示例集成在一起,并进行了一些调整:

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]))

通过用with替换open,可以防止您忘记文件close(),并据我所知降低了引起任何内存问题的风险.

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.

由于尚不清楚您的代码是否会在其他gcode文件上使用,因此我的初始正则表达式可能不合适.使用 Cura ,我创建了一个gcode文件并查看了提供的坐标.根据此信息,您可能会发现模式'[XY].?\d+.\d+'更有用. Regex101 是测试这些网站的好网站

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

这篇关于解析gcode文件以提取坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆