# read caliper data file "HoleCal.txt, with offset 1st line # Iterates with linearized calibration, assuming small cross corrections import numpy as np FilePath = r"C:\Users\jmart\AppData\Local\LightBurn" #r for raw string, allows single \ with open('HoleCal.txt', "r") as file: content = file.read() # read line by line numbers = content.split() # into a list of text numbers offset = float(numbers[0]) meas = np.zeros(14) for i in range(14): meas[i] = round(float(numbers[i+1])+offset,2) H1,H2,H3,H4,H5,H6 = meas[0:6] V1,V2,V3,V4,V5,V6 = meas[6:12] D1,D2 = meas[12:14] Sx = (H1+H2+H3+H4+H5+H6)/6 # average x, for scale Sy = (V1+V2+V3+V4+V5+V6)/6 # average y Bx = ((H1-H3)+(H5-H3)+(H2-H4)+(H6-H4))/4 # bulge x By = ((V1-V2)+(V3-V2)+(V4-V5)+(V6-V5))/4 # bulge y K = (D1-D2)/4 # skew x and y Tx = ( (H1-H5)/2 + (H2-H6)/2 )/2 # trapezoid x Ty = ( (V1-V3)/2 + (V4-V6)/2 )/2 # trapezoid y print('Measurements (mm)') print('Avg. X,Y = ',f'{Sx:7.4f}',f'{Sy:7.4f}') print('Bulge X,Y = ',f'{Bx:7.4f}',f'{By:7.4f}') print('Skew = ',f'{K:7.4f}') print('Trap X,Y = ',f'{Tx:7.4f}',f'{Ty:7.4f}') DelBx = -0.1*(Bx/2.51) # neg feedback, from cal of DelP = 0.1, 60mm spacing DelBy = -0.1*(By/2.76) DelKx = +0.1*(K /9.19) DelKy = +0.1*(K /9.19) DelTx = -0.1*(Tx/3.93) DelTy = +0.1*(Ty/4.14) DelSx = (1-Sx/60)*0.7 DelSy = (1-Sy/60)*0.7 # empirical correction 0.7 print('Change in LightBurn parameters (dim\'less)') print('Delta S_X,Y = ',f'{DelSx:7.4f}',f'{DelSy:7.4f}') print('Delta B_X,Y = ',f'{DelBx:7.4f}',f'{DelBy:7.4f}') print('Delta Skew = ',f'{DelKx:7.4f}') print('Delta T_X,Y = ',f'{DelTx:7.4f}',f'{DelTy:7.4f}') print() print("Close Lightburn to update 'prefs' file") print("Type 'y' to update; ctl^c to exit") while True: user_input = input().strip().lower() if user_input == 'y': break print("'prefs' -> 'prefs_old', updating 'prefs' of LightBurn") def linecheck(line, check): # Does line begin with check for i in range(len(check)): if line.startswith(check[i]): # returns check index, -1 if not found return i return -1 check = [ # Galvo parameters, exact text and columns " \"Galvo_1_Scale\":", " \"Galvo_1_Bulge\":", " \"Galvo_1_Skew\":", " \"Galvo_1_Trapezoid\":", " \"Galvo_2_Scale\":", " \"Galvo_2_Bulge\":", " \"Galvo_2_Skew\":", " \"Galvo_2_Trapezoid\":", ] param = np.array([DelSy,DelBy,DelKy,DelTy,DelSx,DelBx,DelKx,DelTx]) with open(FilePath+'\\prefs.ini' , 'r') as infile, \ open(FilePath+'\\prefs_old.ini', 'w') as outfile: outfile.write(infile.read()) # copies file with open(FilePath+'\\prefs_old.ini', 'r') as infile, \ open(FilePath+'\\prefs.ini' , 'w') as outfile: for line in infile: # iterate through prefs index = linecheck(line,check) if index >= 0: # find lines with lens parameters newparam = float(line[len(check[index]):-2]) + param[index] newline = line[:len(check[index])] + str(round(newparam,16)) + ',\n' outfile.write(newline) # iterates prefs else: outfile.write(line) # copies prefs print('Done')