Message "Tool Length Measurement"CurrentAbsInc = GetOemLED (48) 'Copy current G90/G91 stateCurrentGMode = GetOemDRO (819) 'Copy current G0/G1 stateCurrentFeed = GetOemDRO (818) 'Copy current feedrateContact = 0 'Clear the contact flagPlateThickness = 39.22 'Touch Plate thickness is set hereProbeFeed = 1000 'Probing feedrate is set hereSetVar (1, -250) 'Maximum probing distance is set hereSetVar (2, -10) 'Retract height is set hereCode "M5" 'Ensures spindle is not runningrem Code "G21" 'Ensure metric units are usedCode "G49" 'Turn of tool length offsetZs = GetOemDRO (61) 'Copy current Z-Scale DROCall SetOemDRO (61,1) 'Set Z-Scale DRO to 1Label1: 'Entry point for RetryCode "G59 G90 G0 X-3.5 Y-247 Z0"Tool=GetSelectedTool() 'Load the tool numberSetCurrentTool(Tool) 'Set the toolMessage "Setting Length For Tool T" & GetSelectedTool() '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 "G59 G31 Z#1 F" & ProbeFeed 'Probing moveWhile IsMoving() 'Wait until task has been completedWendRem Code "G4 P0.5"Code "G91 G0 Z3"Code "G4 P0.5"Code "G31 Z-10 F50"While IsMoving()WendCode "G90"If GetOemLED (825) = True Then 'Check to see if probe has touched plateContact = 1 'Set the contact flagEnd IfProbePos = GetVar (2002) 'Exact point probe touched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NewLength = GetABSPosition(2)+260-PlateThicknessSetToolParam(GetSelectedTool(), 2, NewLength)Code "G90 G0 Z #2" 'Retract off Touch Plate the set distanceWhile IsMoving () 'Wait until task has been completedWendCode "G90"Message "Tool Length for T" & GetSelectedTool() & " is now set to: " & NewLength 'Message for status barCode "F" & CurrentFeed 'Restore feedrate to original settingIf Contact = 0 Then 'Probe reached max travel without touchingCode "(ERROR - Probe did not touch.)" 'Message for status barResponse = MsgBox ("ERROR - Probe did not touch.",37,"Auto Tool Zero")If (Response = 4) Then 'User chose RetryGoTo Label1 'Retry Probing routineEnd IfEnd IfElseCode "(ERROR - Touch Plate is grounded.)" 'Message for status barResponse = MsgBox ("ERROR - Touch Plate is grounded - Check connection.",16,"Auto Tool Zero")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                        