我想逐行读取一个g代码文件,并根据末尾的注释,对其执行操作。g代码来自Slic3r。一些没有特定顺序的示例行如下所示:
G1 Z0.242 F7800.000 ; move to next layer (0)
G1 E-2.00000 F2400.00000 ; retract
G1 X0.000 Y30.140 F7800.000 ; move to first skirt point
G1 E0.00000 F2400.00000 ; unretract
G1 X-53.493 Y30.140 E2.14998 F1800.000 ; skirt
G1 X57.279 Y-37.776 E22.65617 ; perimeter
G1 X-52.771 Y-38.586 E56.83128 ; infill
注释始终以分号开头,并使用一致的术语,如周长或填充。理想情况下,脚本将读取该行,搜索特定的注释事例,基于该行执行操作,然后更新文件,然后转到下一行。我对python有点陌生,所以我知道这可以用一个带有嵌套if语句的for循环来完成,但是我不确定如何建立基于这些关键术语的体系结构。在
我不确定您到底要修改什么,所以我选择在表示收回的注释前面添加单词
'(up)'
,并在表示未收回的注释前面添加'(down)'
。在文件内容现在将是:
^{pr2}$如果您想改为修改gcode,假设将第一个字母替换为
'U'
,如果注释表示收回,'D'
如果注释表示未收回,则只需将其替换:据此:
新文件内容:
我希望这有帮助!在
编辑
要响应您获取}值的请求,下面是存储这些值的更新脚本:
X
、Y
和{如果
print(coordinates)
你得到:编辑2
脚本1:
脚本2:
Slic3r处理一些奇怪文本的行不起作用,这就是
ERROR
打印的内容。另外,我建议您不要尝试print
所有的坐标,因为这会导致Python崩溃。如果要查看坐标的整体,可以将它们粘贴到单独的.txt
文件上,如下所示:编辑3
更新脚本以使用括号中的注释。在
脚本1:
脚本2:
相关问题 更多 >
编程相关推荐