Sub Main()


' Based on the macros written by Big-Tex November 29 2010
' and modified by Poppa Bear, with Greolt's and Arbo's changes 08Dec2010
' Edited 1/21/15 - Zero all Offsets


If GetOEMLED(1866) Then Exit Sub

Dim XWork, YWork, ZWork, XMachine, YMachine, ZMachine
Dim XScale, YScale, ZScale
Dim ZNew, ZPlate, Zplatetomaterial
Dim TCZ
Dim XPlate, YPlate
Dim ZClear, ClearAllow, ZMaxRetract
Dim FirstProbeDist, FirstRetractDist, SecProbeDist
Dim CurrentFeed
Dim CurrentAbsInc
Dim Response, CurrUnit
Dim CurrenOffset

TCZ=GetOEMDRO(1202)
XWork = GetOEMDRO(800) ' Get current X Work Coordinate
YWork = GetOEMDRO(801) ' Get current Y Work Coordinate
ZWork = GetOEMDRO(802) ' Get current Z Work Coordinate
XMachine = GetOemDRO(83) ' Get current X Machine Coordinate
YMachine = GetOemDRO(84) ' Get current Y Machine Coordinate
ZMachine = GetOemDRO(85) ' Get current Z Machine Coordinate
XPlate = GetUserDRO(1811) ' Get X Machine Coordinate of Fixed Plate
YPlate = GetUserDRO(1812) ' Get Y Machine Coordinate of Fixed Plate
CurrentFeed = GetOemDro(818) ' Get Current Feed Rate
Zplatetomaterial = GetUserDRO(1813) ' Get calculated material offset
ZClear = GetOEMDRO(1814) ' Get Z Clearance Plane
CurrentAbsInc = GetOemLED(48) ' Get the current G90/G91 state
CurrentOffset = GetOEMDRO(46)  ' Get Current Coordinate system 1 = G54, 2 = G55, ....


'Get Axis Scale factors
XScale = GetOEMDRO(59)
YScale = GetOEMDRO(60)
ZScale = GetOEMDRO(61)

'Set All Axis' Scale to 1
Call SetOEMDRO(59,1)
Call SetOEMDRO(60,1)
Call SetOEMDRO(61,1)
Sleep(250)



'//////// the block below will set all your reusable vars depending on Inch or mm.
'//////// this sets the vars so you only need ONE large block of probing code.

If GetOEMLED(801) Then ' On = English Measure INCH
FirstProbeDist = 6.0 ' Probe Down 6 inches
FirstRetractDist = 0.05 ' Then Retract .05 inches
SecProbeDist = 0.25 ' Then Probe down .25 inches
FirstProbeFeed = 10 ' First Probe Feed @ 10 ipm
SecondProbeFeed = 1 'Second Probe Feed @ 1 ipm
ClearAllow = 0.125 ' Max Z Travel = .125in below Machine Z zero
CurrUnit = "inches"
Else ' Off = Metric Measure MM
FirstProbeDist = 150.0 ' Probe Down 150 mm's
FirstRetractDist = 1.0 ' Then Retract 1 mm
SecProbeDist = 6.0 ' Then Probe down 6 mm's
FirstProbeFeed = 250 ' First Probe Feed @ 250mm/min
SecondProbeFeed = 25 ' Second Probe Feed @ 25mm/min
ClearAllow = 2.0 ' Max Z Travel = 2mm below Machine Z zero
CurrUnit = "mm's"
End If

'//////// Error Condition checking code

If GetOemLED(16)<>0 Then 'Checks for machine coordinates
MsgBox "Must be in Working Coordinates - Putting System in Reset",16,"Machine Coordinates!!!!!"
'Reset Scale before E-Stop
Call SetOEMDRO(59,XScale)
Call SetOEMDRO(60,YScale)
Call SetOEMDRO(61,ZScale)
DoOEMButton(1021) ' Put Mach3 in Reset to end Tool Change
Exit Sub 'ERROR! exit the macro
End If

If GetOemLED(825)<>0 Then
MsgBox "Z Plate is Grounded - Putting System in Reset",16,"Plate Grounded Error!!!!!"
'Reset Scale before E-Stop
Call SetOEMDRO(59,XScale)
Call SetOEMDRO(60,YScale)
Call SetOEMDRO(61,ZScale)
DoOEMButton(1021) ' Put Mach3 in Reset to end Tool Change
Exit Sub 'ERROR! exit the macro
End If

If ZClear <=0 Then
'MsgBox "(Clearance Plane Must be > 0 - Putting System in Reset)",16,"Clearance Plane Error!!!!"
'Reset Scale before E-Stop
Call SetOEMDRO(59,XScale)
Call SetOEMDRO(60,YScale)
Call SetOEMDRO(61,ZScale)
'DoOEMButton(1021) ' Put Mach3 in Reset to end Tool Change
'Exit Sub 'ERROR! exit the macro
'End If


Response = MsgBox ("Warning !!!" & (Chr(13)) & "Z Clearance Plane <= 0 !!!" & (Chr(13)) & "Press OK To Set Retract Plane To  " & Cstr(ClearAllow) & " " & CurrUnit & (Chr(13)) & "Or Press Cancel for E-Stop",49,"Clearance Plane Less Than Or Equal To Zero!!")

	If Response = 1 Then ' If OK
	ZClear = ClearAllow ' Retract to .125in or 2mm above fixed plate
	ElseIf Response = 2 Then ' If Cancel
	'Reset Scale before E-Stop
	Call SetOEMDRO(59,XScale)
	Call SetOEMDRO(60,YScale)
	Call SetOEMDRO(61,ZScale)
	Sleep(250)
	DoOEMButton(1021) ' Put Mach3 in Reset to end Tool Change
	Exit Sub
	End If
End If

'//////// Start Probing Code, Probe In -Z direction.
'//////// The vars will be Inch or Metric from above if/else statment

Code "G90"  ' Change to Absolute Distance Mode
Sleep(100)

Code "G53 G0 Z" & TCZ ' Move to Tool Change Z Position - Should already be there

While IsMoving()
Wend

Code "G53 G0 X" & XPlate & " Y" & YPlate ' Move to Fixed Plate Location

While IsMoving()
Wend

If GetOemLED(825)<>0 Then
MsgBox "Z Plate is Grounded - Putting System in Reset",16,"Plate Grounded Error!!!!!"
DoOEMButton(1021) ' Put Mach3 in Reset to end Tool Change
Exit Sub 'ERROR! exit the macro
End If

Code "F" & FirstProbeFeed ' Set Feedrate to 10 ipm, or 300 mm/m

Code "(Probing for Z Zero....)"

Zplate = (GetOEMDro(802) - FirstProbeDist) ' Probe move to Current Z - 6in, or 150mm
Code "G90 G31 Z" & Zplate

While IsMoving()
Wend

Sleep (200)

Zplate = GetOEMDro(802) ' Read the Touch Point

Code "G1 Z" & (Zplate + FirstRetractDist) ' Move up .05 in, or 1mm in case of overshoot

While IsMoving()
Wend

Code "F" & SecondProbeFeed ' Set Feedrate to 1 ipm, or 25mm/min

Zplate = (GetOEMDro(802)- SecProbeDist) ' Probe move to Current Z - .25in, or 6mm

Code "G90 G31 Z" & Zplate

While IsMoving()
Wend

Call SetOEMDRO(802,Zplatetomaterial) ' Set Z axis DRO to Calculated Offset, making tool tip @ Z=0
Sleep(500) ' Pause for Dro to update


'Make Sure Z Clearance Plane is below Home Switch. If not, Notify User and Proceed.

If GetOEMLED(85) <= Zplatetomaterial + ClearAllow Then

ZMaxRetract = Abs(GetOEMDRO(85) - Zplatetomaterial + ClearAllow) 
Else
ZMaxRetract = Abs(GetOEMDRO(85)) + Zplatetomaterial - ClearAllow 
End If

If ZClear > ZMaxRetract Then

Response = MsgBox ("Warning !!!" & (Chr(13)) & "Z Clearance Plane is Above Z Axis Home Switch." & (Chr(13)) & "Press OK To Retract Safely Below Switch" & (Chr(13)) & "Or Press Cancel for E-Stop",49,"Insufficient Z Clearance!!")

	If Response = 1 Then ' If OK
	ZClear = ZMaxRetract ' Retract to Z Machine zero - .125in or 2mm
	ElseIf Response = 2 Then ' If Cancel
	'Reset Scale before E-Stop
	Call SetOEMDRO(59,XScale)
	Call SetOEMDRO(60,YScale)
	Call SetOEMDRO(61,ZScale)
	Sleep(250)
	DoOEMButton(1021) ' Put Mach3 in Reset to end Tool Change
	Exit Sub
	End If
End If


If ZClear <= Zplatetomaterial then 'Plunge into Fixed Plate !!!!

Response = MsgBox ("Warning !!!" & (Chr(13)) & "Z Clearance Plane is Below Fixed Plate!!!" & (Chr(13)) & "Press OK To Retract " & Cstr(ClearAllow) & " " & CurrUnit & (Chr(13)) & "Or Press Cancel for E-Stop",49,"Plunging Into Plate!!")

	If Response = 1 Then ' If OK
	ZClear = Zplatetomaterial + ClearAllow ' Retract to .125in or 2mm above fixed plate
	ElseIf Response = 2 Then ' If Cancel
	'Reset Scale before E-Stop
	Call SetOEMDRO(59,XScale)
	Call SetOEMDRO(60,YScale)
	Call SetOEMDRO(61,ZScale)
	Sleep(250)
	DoOEMButton(1021) ' Put Mach3 in Reset to end Tool Change
	Exit Sub
	End If
End If


Code "G0 Z" & ZClear 'Retract to Z Clearance Plane
While IsMoving ()
Wend

Code "G0 X" & GetVar(1) & " Y" & GetVar(2) ' Move to Previous Position prior to M6 being called

While IsMoving()
Wend

Code "F" & CurrentFeed ' Reset Feedrate

If GetOEMLED(801) Then 'ON = English Measure INCH
Code "(Z Axis is now Zeroed in Inches)" ' Puts this message in the status bar
Else 'OFF = Metric Measure MM
Code "(Z Axis is now Zeroed in mm's)" ' Puts this message in the status bar
End If

If CurrentAbsInc = 0 Then 'if G91 was in effect before then return to it
Code "G91"
End If

Call SetOEMDRO(59,XScale)
Call SetOEMDRO(60,YScale)
Call SetOEMDRO(61,ZScale)
Sleep(250)


If CurrentOffset = 1 Then
ZOffset=GetVar(5223)
ElseIf CurrentOffset = 2 Then
ZOffset=GetVar(5243)
ElseIf CurrentOffset = 3 Then
ZOffset=GetVar(5263)
ElseIf CurrentOffset = 4 Then
ZOffset=GetVar(5283)
ElseIf CurrentOffset = 5 Then
ZOffset=GetVar(5303)
ElseIf CurrentOffset = 6 Then
ZOffset=GetVar(5323)
ElseIf CurrentOffset = 7 Then
ZOffset=GetVar(5343)
ElseIf CurrentOffset = 8 Then
ZOffset=GetVar(5363)
ElseIf CurrentOffset = 9 Then
ZOffset=GetVar(5383)
ElseIf CurrentOffset = 10 Then
ZOffset=GetVar(5403)
ElseIf CurrentOffset = 11 Then
ZOffset=GetVar(5423)
ElseIf CurrentOffset = 12 Then
ZOffset=GetVar(5443)
ElseIf CurrentOffset = 13 Then
ZOffset=GetVar(5463)
ElseIf CurrentOffset = 14 Then
ZOffset=GetVar(5483)
ElseIf CurrentOffset = 15 Then
ZOffset=GetVar(5503)
ElseIf CurrentOffset = 16 Then
ZOffset=GetVar(5523)
ElseIf CurrentOffset = 17 Then
ZOffset=GetVar(5543)
ElseIf CurrentOffset = 18 Then
ZOffset=GetVar(5563)
End If

For X = 5223 To 5563 Step 20
SetVar(X, ZOffset)
Next X
Sleep(250)

If Not FileName() = "No File Loaded." Then
If GetOEMLED(1865) Then
MsgBox "Start Spindle, then press OK to Continue",48,"Start Spindle"

' g-code should have an M3 after the M6, so the following commented code is not required

'Else
'DoSpinCW()
'While IsMoving()
'Wend

End If
End If


End Sub  
              
