我正在开发一个项目,我必须在CNC程序中手动编辑偏移。我想在python中这样做,但是,我对它的体验并没有涉及到这样的事情。非常感谢任何帮助。
代码示例:
N0880M41
N0890G50S3600
M03
N0900G96S0210M03
N0910Z23.274M08
N0920M07
N0930Z23.2063
N0940X1.39
N0950G99
N0960G01X1.29F.009
N0970X1.2558
N0980G02X1.189Z23.1662R.944
N0990G01Z14.7569F.012
N1000G02X1.2558Z14.7168R.944
N1010G01X1.29
N1020G00X1.3608Z14.7522
N1030Z23.1309
N1040X1.2656
N1050G01X1.189Z23.1662F.009
N1060G02X1.088Z23.0955R.944
N1070G01Z14.8276F.012
N1080G02X1.2528Z14.7185R.944
N1090G00X1.3236Z14.7538
N1100Z23.0602
N1110X1.1646
N1120G01X1.088Z23.0955F.009
N1130G02X.987Z23.0075R.944
N1140G01Z14.9157F.012
N1150G02X1.1446Z14.7864R.944
N1160G00X1.2152Z14.8217
N1170Z22.9721
N1180X1.0636
N1190G01X.987Z23.0075F.009
N1200G02X.886Z22.8873R.944
N1210G01Z15.0359F.012
N1220G02X1.0344Z14.8716R.944
N1230G00X1.105Z14.907
N1240Z22.8519
我需要通过提示输入用户输入更改Z值,我希望输出与输入相同的文本输出只是在Z中使用新值
例如:用户提示并输入值.226
input = N0910Z23.274M08
我想输出
output = N0910Z23.500M08
答案 0 :(得分:0)
这是一个快速而又脏的程序,可以满足您的需求:
# GCode utility - adjust Z offset
# This code assumes Python 3
import re
def get_float(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
pass
# Regular expression to recognize a Z parameter
z_re = re.compile("Z(-?\d+(?:\.\d*)?)", re.I)
# Create callback function for re.sub
def make_z_callback(dz, fmt="Z{:0.4f}"):
def z_callback(match):
z_value = match.group(1) # get just the number
z_value = float(z_value) + dz # add offset
return fmt.format(z_value) # convert back to string
return z_callback
def main():
# read GCode file contents
fname = input("Enter GCode file name: ")
with open(fname) as inf:
gcode = inf.read()
# do search-and-replace on Z parameters
z_offset = get_float("Enter Z offset: ")
z_callback = make_z_callback(z_offset)
gcode = z_re.sub(z_callback, gcode)
# write result back to file
with open(fname, "w") as outf:
outf.write(gcode)
if __name__=="__main__":
main()