My script is a little different but may be suitable for your needs. This does not have the manual positioning and makes use of spring plunger type tool probe. If you have a rigid tool probe then you will need to reduce the fast probe speed to avoid tool damage due to overtravel. I've added some comments to the code to describe the various lines.
This method keeps all the values of the tool table at zero. This means you are less likely to have issues when you set Z0 on a new part and forget to zero the table before hand or forget to enable tool length compensation.
function m6()
local inst = mc.mcGetInstance();
local selectedtool = mc.mcToolGetSelected(inst) --Variable for selected tool #
local currenttool = mc.mcToolGetCurrent(inst) --Variable for current tool #
if selectedtool == currenttool then --Compare tool numbers to see if tool change is required
return
mc.mcCntlSetLastError(inst, "Tool Change Not Required")
else
mc.mcCntlGcodeExecuteWait(inst, "G00")
mc.mcCntlGcodeExecuteWait(inst, "G49") --Cancel tool length compensation if active
mc.mcCntlGcodeExecuteWait(inst, "G91 G28 G0 Z0") --Raises Z-axis to home position
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X1.8810 Y25.2330") --Goes to probe postion in machine coordinates
mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-7.0 F20") --Probes current tool at fast speed
mc.mcCntlGcodeExecuteWait(inst, "G91 G0 Z+0.1") --Retract from the probe
mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-0.15 F1") --Probes at slow speed
local measure_1 = mc.mcAxisGetPos(inst,2) --Saves probed Z position
mc.mcCntlGcodeExecuteWait(inst, "G91 G0 Z+0.1") --Retracts from the probe
mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-0.15 F1") --Probes at slow speed
local measure_2 = mc.mcAxisGetPos(inst,2) --Saves probed Z position
local measured_average = (measure_1 + measure_2)/2 --Averages two probed values
mc.mcCntlGcodeExecuteWait(inst, "G00")
mc.mcCntlGcodeExecuteWait(inst, "G91 G28 G0 Z0") --Raises Z-axis to home position
mc.mcCntlGcodeExecuteWait(inst, "G91 G28 G0 X0 Y0") --Send spindle to XY home position
wx.wxMessageBox("Change to Tool "..selectedtool.." and Press OK to Continue") --Prompt to change tool. Press OK only after tool has been exchanged.
mc.mcCntlGcodeExecuteWait(inst, "G00")
mc.mcCntlGcodeExecuteWait(inst, "G49") --Cancel tool length compensation if active
mc.mcCntlGcodeExecuteWait(inst, "G91 G28 G0 Z0") --Raises Z-axis to home position
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X1.8810 Y25.2330") --Goes to probe postion in machine coordinates
mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-7.0 F20") --Probes new tool at fast speed
mc.mcCntlGcodeExecuteWait(inst, "G91 G0 Z+0.1") --Retracts from the probe
mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-0.15 F1") --Probes at slow speed
mc.mcAxisSetPos(inst,2,measured_average) --Sets current Z postion to saved position from previous tool
mc.mcCntlGcodeExecuteWait(inst, "G00")
mc.mcCntlGcodeExecuteWait(inst, "G91 G28 G0 Z0") --Raises Z-axis to home position
mc.mcToolSetCurrent(inst, selectedtool) --Update current tool DRO
mc.mcCntlGcodeExecuteWait(inst, "G90") --Switch back to absolute mode
mc.mcCntlSetLastError(inst, "Tool Change Complete")
--mc.mcCntlToolChangeManual(inst, true); --Uncomment to require Cycle Start press to resume G-code
end
end
if (mc.mcInEditor() == 1) then
m6()
end