Just adding a thankyou!

I Made some Modifications
- Probemove for approach probe to avoid Crash

and back off probe before real probing.
- Lots of Variables to tune the script
---Preset for probelocation
---Preset for ToolChange position
---Preset for defaults and speeds
function M6()
local inst = mc.mcGetInstance();
local changeToTool = 3--mc.mcToolGetSelected(inst)
local changeFromTool = 2--mc.mcToolGetCurrent(inst)
local XPositionBeforeToolChange = mc.mcAxisGetPos(inst,0)
local YPositionBeforeToolChange = mc.mcAxisGetPos(inst,1)
local XToolProbePosition = "1"
local YToolProbePosition = "1"
local XManualToolChangePosition = "2"
local YManualToolChangePosition = "2"
local ColletAtProbeZCoordinate = -103 -- Machine Z-coordinate when collet tip touches TouchPlate (Must be more than ProbeOperationDistance - DefaultToolLength
local ExtraProbeDistance = 5 -- SafetyMargin
local ProbeOperationDistance = -30 -- Length of Probemove before giving up
local ProbePrepSpeed = 100
local ProbeSpeed = 25
local DefaultToolLength = 70 -- Used if tool info is not found. (Should be a value slightly longer than the longest tool in the shop :D)
-- QUICK REFERENCE
-- G90 - Absolute (Go to coordinate)
-- G91 - Incremental (Godistance along axis from current position)
-- G53 - Machine Coordinate Move
if changeToTool == changeFromTool then
return
mc.mcCntlSetLastError(inst, "ToolChange Activated But Not Required")
else
mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0\nM5" )
RunProbe(inst,changeFromTool,XToolProbePosition,YToolProbePosition,DefaultToolLength,ColletAtProbeZCoordinate,ExtraProbeDistance,ProbePrepSpeed,ProbeSpeed)
local ToolZCoordinate = mc.mcAxisGetPos(inst,2)
mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0 \n X"..XManualToolChangePosition.." Y"..YManualToolChangePosition)
local changeToToolDescription = mc.mcToolGetDesc(inst,changeToTool)
wx.wxMessageBox("Please change to tool number "..changeToTool.." "..changeToToolDescription.." and press ok to continue")
RunProbe(inst,changeToTool,XToolProbePosition,YToolProbePosition,DefaultToolLength,ColletAtProbeZCoordinate,ExtraProbeDistance,ProbePrepSpeed,ProbeSpeed)
mc.mcAxisSetPos(inst, 2 , ToolZCoordinate)
mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
mc.mcCntlGcodeExecuteWait(inst,"G90 G0 X"..XPositionBeforeToolChange.." Y"..YPositionBeforeToolChange)
mc.mcToolSetCurrent(inst, changeToTool)
wx.wxMessageBox("If Toolchange was Sucessful - click ok to continue")
mc.mcCntlSetLastError(inst, "ToolChange Finished")
end
end
function RunProbe(finst,ftool,fXToolProbePosition,fYToolProbePosition,fDefaultToolLength,fColletAtProbeZCoordinate,fExtraProbeDistance,fProbePrepSpeed,fProbeSpeed)
mc.mcCntlGcodeExecuteWait(finst, "G90 G53 G0 X"..fXToolProbePosition.." Y"..fYToolProbePosition)
local toollen = mc.mcToolGetData(finst, mc.MTOOL_MILL_HEIGHT, ftool)
if toollen == 0 then toollen = fDefaultToolLength end
mc.mcCntlSetLastError(finst, "Tool length not found - using Default Length")
local probestart = fColletAtProbeZCoordinate + fExtraProbeDistance + toollen
local GCODE = "G90 G53 G31 G0 Z"..probestart.." F"..fProbePrepSpeed
GCODE = ""..GCODE.." \n G91 G0 Z5 F"..fProbePrepSpeed.." \n G91 G31 Z-15 F"..fProbeSpeed
mc.mcCntlGcodeExecuteWait(finst,GCODE)
end
if (mc.mcInEditor() == 1) then
M6()
end