CurrentAbsInc = GetOemLED (48) 'Copy current G90/G91 state
CurrentGMode = GetOemDRO (819) 'Copy current G0/G1 state
CurrentFeed = GetOemDRO (818) 'Copy current feedrate
PlateThickness = 0.062 'Touch Plate thickness is set here
ProbeFeed = 10 'Probing feedrate is set here
SetVar (1,-5.0) 'Maximum probing distance is set here
Response = MsgBox ("Ready to Z Zero?", 4 , "Auto Tool Zero")
If (Response = 6) Then 'User chose Yes
Code "M5" 'Ensures spindle is not running
Zs = GetOemDRO (61) 'Copy current Z-Scale DRO
Call SetOemDRO (61,1) 'Set Z-Scale DRO to 1
DoOemButton (1010) 'Zero Z-Axis DRO
Code "(Setting Tool Zero)" 'Message for status bar
While IsMoving () 'Wait until task has been completed
Wend
If GetOemLED (825) = 0 Then 'Check to see if touch plate is already grounded
Code "G90 G31 Z #1 F" & ProbeFeed 'Probing move
While IsMoving () 'Wait until task has been completed
Wend
ProbePos = GetVar (2002) 'Exact point probe touches
Code "G0 Z" & ProbePos 'Go back to exact point of touch if there was any overrun
While IsMoving () 'Wait until task has been completed
Wend
Call SetDRO (2,PlateThickness) 'Set Z-Axis DRO to plate thickness
Code "G0 Z0.5" 'Z-Axis retract height is set here
While IsMoving () 'Wait until task has been completed
Wend
Code "(Z-Axis is now Zeroed)" 'Message for status bar
Code "F" & CurrentFeed 'Restore feedrate to original setting
Else
Code "(ERROR - Touch Plate is grounded)" 'Message for status bar
End If
If ProbePos < (GetVar(1)+0.1) Then 'Probe reached max travel without touching
Code "(ERROR - Probe did not touch)" 'Message for status bar
End If
Call SetOemDRO (61,Zs) 'Restore Z-Scale DRO to original setting
If CurrentAbsInc = 0 Then 'If G91 was in effect before then return to it
Code "G91"
End If
If CurrentGMode = 0 Then 'If G0 was in effect before then return to it
Code "G0"
End If
Response = MsgBox ("Return to Work XY Zeros?", 4 , "Auto Tool Zero")
If (Response = 6) Then 'User chose Yes 
Code "(Returning to Work Zeros.)"
Code "G4 P1" 'Pause
Code "G0 X0 Y0" 'Return to XY Zeros 
End If
Else 'User chose No
Code "(Z axis routine terminated.)"
End If
   
