' This area does not need adjusting 
Currentfeed = GetOemDRO(818) ' Gets the current feed rate for return after tool change
CurrentAbsInc = GetOemLed(48) ' Gets current G90/G91 to return it later
CurrentGMode = GetOemDRO(819) ' Gets current G00/G01 to return it later
CurrentTool = GetOeMDRO(824) ' Gets current tool number

'Define constants for MachMSG dialog type
MachMsgTypeOK = 0
MachMsgTypeCancel = 1
MachMsgTypeAbortRetryIgnore = 2
MachMsgTypeYesNoCancel = 3
MachMsgTypeYesNo = 4 
MachMsgTypeRetryCancel = 5
MachMsgTypeCancelTryAgainContinue = 6
'define Constants for MachMsg return codes
MachMsgReturnok = 1
MachMsgReturnCancel = 2
MachMsgReturnAbort = 3
MachMsgReturnRetry = 4
MachMsgReturnIgnore = 5
MachMsgReturnYes = 6
MachMsgReturnNo = 7
MachMsgReturnTryAgain = 10
MachMsgReturn Continue =11


ZMove = 5.0 'Max allowable Z travel before spindle contacts tool length setter

DoStopSpin()
Code "G59P199 X0 Y0 Z4" ' This sets the spindle so you can change the tool
While IsMoving() 'Pauses the code read while machine moves
MachMsg("Click ok when tool has been changed and ready to be measured"," Tool Change",0) 
Ret = MachMsgReturnok Then

If GetOemLed(825) = 0 Then 'This checks the tool probe before moving forward
Code "G59P200 X0 Y0 Z0"
DoOemButton(1010) 
Code "G00 G31 z" &zmove"F4" 'Feed rate here for how fast you want z to go down
While IsMoving()
Wend
ZprobePos = GetVar(2002)
Code "G00 Z" &ZprobePos 'place tool at the exact where the tool touched the tool setter
While IsMoving()
Wend
Call SetOemDro(109)'sets the current tool Offset
Sleep 200  'allows time for update
Code "G59P199 X0 Y0 Z0 F50" 'Moves the tool straight up to starting point for tool change
While IsMoving()
Wend
Code "This tool's Length is now set"
Code "F" &CurrentFeed
else
Code "Tool SEtter Error"
End If
If CurrentAbsInc = 0 Then
Code "G91"
End If
If CurrentGMode = 0 Then
Code "G00"
End If
