Python 逐行编辑G代码
我想逐行读取一个g代码文件,并根据末尾的注释对其执行操作。g代码来自Slic3r。一些没有特定顺序的示例行如下所示:Python 逐行编辑G代码,python,if-statement,for-loop,g-code,Python,If Statement,For Loop,G Code,我想逐行读取一个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
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)
,和'(向下)
在表示未删除的注释前面
file_name = "file.gcode" # put your filename here
with open(file_name, 'r+') as f:
new_code = "" # where the new modified code will be put
content = f.readlines() # gcode as a list where each element is a line
for line in content:
gcode, comment = line.strip('\n').split(";") # seperates the gcode from the comments
if 'unretract' in comment:
comment = ' (down) ' + comment
elif 'retract' in comment:
comment = ' (up)' + comment
new_code += gcode + ';' + comment + '\n' # rebuild the code from the modified pieces
f.seek(0) # set the cursor to the beginning of the file
f.write(new_code) # write the new code over the old one
文件内容现在将是:
G1 Z0.242 F7800.000 ; move to next layer (0)
G1 E-2.00000 F2400.00000 ; (up) retract
G1 X0.000 Y30.140 F7800.000 ; move to first skirt point
G1 E0.00000 F2400.00000 ; (down) 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
如果您想改为修改gcode,那么假设将第一个字母替换为'U'
,如果注释指示收回,则将第一个字母替换为'D'
,如果注释指示收回,则只需替换此字母即可:
if 'unretract' in comment:
comment = ' (down) ' + comment
elif 'retract' in comment:
comment = ' (up)' + comment
据此:
if 'unretract' in comment:
gcode = 'D' + gcode[1:]
elif 'retract' in comment:
gcode = 'U' + gcode[1:]
新文件内容:
G1 Z0.242 F7800.000 ; move to next layer (0)
U1 E-2.00000 F2400.00000 ; retract
G1 X0.000 Y30.140 F7800.000 ; move to first skirt point
D1 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
我希望这有帮助
编辑 要回答您获取
X
、Y
和F
值的请求,以下是存储这些值的更新脚本:
file_name = "file.gcode" # put your filename here
with open(file_name, 'r+') as f:
coordinates = []
content = f.readlines() # gcode as a list where each element is a line
for line in content:
gcode, comment = line.strip('\n').split(";")
coordinate_set = {}
if 'retract' not in comment and 'layer' not in comment:
for num in gcode.split()[1:]:
coordinate_set[num[:1]] = float(num[1:])
coordinates.append(coordinate_set)
如果您打印(坐标)
您会得到:
[{'X': 0.0, 'F': 7800.0, 'Y': 30.14}, {'E': 2.14998, 'X': -53.493, 'F': 1800.0, 'Y': 30.14}, {'E': 22.65617, 'X': 57.279, 'Y': -37.776}, {'E': 56.83128, 'X': -52.771, 'Y': -38.586}]
编辑2 脚本1:
file_name = "file.gcode"
with open(file_name, 'r+') as f:
new_code = ""
content = f.readlines()
for line in content:
if ';' in line:
try:
gcode, comment = line.strip('\n').split(";")
except:
print('ERROR\n', line)
else: # when there are no comments
gcode = line.strip('\n')
comment = ""
if 'unretract' in comment:
comment = ' (down) ' + comment
elif 'retract' in comment:
comment = ' (up)' + comment
if comment != "":
new_code += gcode + ';' + comment + '\n'
else: # when there are no comments
new_code += gcode + '\n'
f.seek(0)
f.write(new_code)
file_name = "file.gcode"
with open(file_name, 'r+') as f:
new_code = ""
content = f.readlines()
for line in content:
if ';' in line:
try:
gcode, comment = line.strip('\n').split(";")
except:
print('ERROR\n', line)
else: # when there are no comments
gcode = line.strip('\n')
comment = ""
if 'unretract' in comment:
comment = ' (down) ' + comment
elif 'retract' in comment:
comment = ' (up)' + comment
if comment != "":
new_code += gcode + ';' + comment + '\n'
else: # when there are no comments
new_code += gcode + '\n'
f.seek(0)
f.write(new_code)
脚本2:
file_name = "file.gcode"
with open(file_name, 'r+') as f:
coordinates = []
content = f.readlines()
for line in content:
if ';' in line:
try:
gcode, comment = line.strip('\n').split(";")
except:
print(' ERROR \n', line, '\n')
else:
gcode = line.strip('\n')
comment = ""
coordinate_set = {}
if 'retract' not in comment and 'layer' not in comment and gcode:
for num in gcode.split()[1:]:
coordinate_set[num[:1]] = float(num[1:])
coordinates.append(coordinate_set)
file_name = "3Samples_0skin_3per.gcode" # put your filename here
with open(file_name, 'r+') as f:
coordinates = []
content = f.readlines()
for line in content:
if ';' in line:
try:
gcode, comment = line.strip('\n').split(";")
except:
print(' ERROR 1: \n', line, '\n')
elif '(' in line:
try:
gcode, comment = line.strip('\n').strip(')').split("(")
except:
print('ERROR 2: \n', line, '\n')
else:
gcode = line.strip('\n')
comment = ""
coordinate_set = {}
if 'retract' not in comment and 'layer' not in comment and gcode:
for num in gcode.split()[1:]:
if len(num) > 1:
try:
coordinate_set[num[:1]] = float(num[1:])
except:
print('ERROR 3: \n', gcode, '\n')
coordinates.append(coordinate_set)
Slic3r执行某些奇怪文本的某些行不起作用,这就是错误
打印的内容。另外,我建议您不要尝试打印所有坐标,因为这会导致Python崩溃。如果要查看坐标的总和,可以将它们粘贴到单独的.txt
文件中,如下所示:
with ('coordinates.txt', 'w') as f2:
f2.write(coordinates)
编辑3 已更新脚本以使用括号中的注释 脚本1:
file_name = "file.gcode"
with open(file_name, 'r+') as f:
new_code = ""
content = f.readlines()
for line in content:
if ';' in line:
try:
gcode, comment = line.strip('\n').split(";")
except:
print('ERROR\n', line)
else: # when there are no comments
gcode = line.strip('\n')
comment = ""
if 'unretract' in comment:
comment = ' (down) ' + comment
elif 'retract' in comment:
comment = ' (up)' + comment
if comment != "":
new_code += gcode + ';' + comment + '\n'
else: # when there are no comments
new_code += gcode + '\n'
f.seek(0)
f.write(new_code)
file_name = "file.gcode"
with open(file_name, 'r+') as f:
new_code = ""
content = f.readlines()
for line in content:
if ';' in line:
try:
gcode, comment = line.strip('\n').split(";")
except:
print('ERROR\n', line)
else: # when there are no comments
gcode = line.strip('\n')
comment = ""
if 'unretract' in comment:
comment = ' (down) ' + comment
elif 'retract' in comment:
comment = ' (up)' + comment
if comment != "":
new_code += gcode + ';' + comment + '\n'
else: # when there are no comments
new_code += gcode + '\n'
f.seek(0)
f.write(new_code)
脚本2:
file_name = "file.gcode"
with open(file_name, 'r+') as f:
coordinates = []
content = f.readlines()
for line in content:
if ';' in line:
try:
gcode, comment = line.strip('\n').split(";")
except:
print(' ERROR \n', line, '\n')
else:
gcode = line.strip('\n')
comment = ""
coordinate_set = {}
if 'retract' not in comment and 'layer' not in comment and gcode:
for num in gcode.split()[1:]:
coordinate_set[num[:1]] = float(num[1:])
coordinates.append(coordinate_set)
file_name = "3Samples_0skin_3per.gcode" # put your filename here
with open(file_name, 'r+') as f:
coordinates = []
content = f.readlines()
for line in content:
if ';' in line:
try:
gcode, comment = line.strip('\n').split(";")
except:
print(' ERROR 1: \n', line, '\n')
elif '(' in line:
try:
gcode, comment = line.strip('\n').strip(')').split("(")
except:
print('ERROR 2: \n', line, '\n')
else:
gcode = line.strip('\n')
comment = ""
coordinate_set = {}
if 'retract' not in comment and 'layer' not in comment and gcode:
for num in gcode.split()[1:]:
if len(num) > 1:
try:
coordinate_set[num[:1]] = float(num[1:])
except:
print('ERROR 3: \n', gcode, '\n')
coordinates.append(coordinate_set)
这很有帮助,谢谢。我将不得不处理这件事来解决所有问题,但这正是我开始工作所需要的框架。我的一个问题是,如何获取X、Y和挤出机进给值来进行计算?您可以在空格之间拆分gcode,然后删除前面的字母并将其转换为float。我将编辑答案,使其更清晰。这对我来说是有意义的。我的最后一个问题是,当您将gcode文件传递到for循环中时,它是否将其视为一个字符串?不是这样,
f.readlines()
创建一个列表,其中每个元素都是文本文件的一行(该元素是一个字符串)。因此,我可以使用所有内置列表来正确处理这些行?