SpinTool = GetOEMDRO (1200)'Tool in spindle
PotTool = GetOEMDRO (1201) 'Tool in tool changer
If SpinTool <> PotTool Then
   MsgBox("Spindle Tool and Pocket Tool are not the Same")
   Dobutton(21)
   Exit Sub
End If

NewTool = GetSelectedTool() 'Programed tool
If NewTool = SpinTool Then
   Exit Sub
End If
If NewTool = 0 Then
   MsgBox("No Tool Number Given")
   Dobutton(21)
   Exit Sub
End If

If NewTool < 0 Then
   MsgBox("Invalid Tool Number")
   Dobutton(21)
   Exit Sub
End If

If NewTool > 24 Then
   MsgBox("Invalid Tool Number")
   Dobutton(21)
   Exit Sub
End If

DoSpinStop()
ActivateSignal(OUTPUT10)
Code "G01 G53 Z.174 F150."
While IsMoving()
Sleep 100
Wend

'Find shortest route to next tool

ChgTool = (NewTool - SpinTool)

If ChgTool < 0 Then 
   ChgTool = (24 + ChgTool)
End If

If ChgTool > 0 And ChgTool <= 12 Then
   Call CwMove(ChgTool)
   Exit Sub
End If

If ChgTool > 0 And ChgTool > 12 Then
   Call CcwMove(ChgTool)
   Exit Sub
End If


'Clockwise move to next tool
Sub CwMove(ByVal ChgTool As Integer) 
	If IsActive(ZHOME) = False Then
	MsgBox("Z is not Home")
	Dobutton(21)
	Exit Sub
	End If
	
	If IsOutputActive(OUTPUT10) = True Then
	MsgBox("Spindle is not Orientated")
	Dobutton(21)
	Exit Sub
	End If
	
	ActivateSignal(OUTPUT3)
	Sleep (2000)
	
	
	If IsActive(OEMTRIG9) = False Then
	MsgBox("Mag is not Forward")
	Dobutton(21)
	Exit Sub
	End If	

	ActivateSignal(OUTPUT2)
	Sleep (1500)
	
	If IsActive(OEMTRIG15) = False Then
	MsgBox("Tool is Clamped")
	Dobutton(21)
	Exit Sub
	End If	
	
	ActivateSignal(OUTPUT4)
	Sleep (1500)
	
	If IsActive(OEMTRIG11) = False Then
	MsgBox("Mag is not Down")
	Dobutton(21)
	Exit Sub
	End If
	
	NewPosition = 0
	
	Call IndexCW(NewPosition,ChgTool)
	
End Sub
	
'Counter Clockwise move to next tool
Sub CcwMove (ByVal ChgTool As Integer)

	ChgTool = (24 - ChgTool)

	If IsActive(ZHOME) = False Then
	MsgBox("Z is not Home")
	Dobutton(21)
	Exit Sub
	End If
	
	If IsOutputActive(OUTPUT10) = True Then
	MsgBox("Spindle is not Orientated")
	Dobutton(21)
	Exit Sub
	End If
	
	ActivateSignal(OUTPUT3)
	Sleep (1500)
	
	If IsActive(OEMTRIG9) = False Then
	MsgBox("Mag is not Forward")
	Dobutton(21)
	Exit Sub
	End If	

	ActivateSignal(OUTPUT2)
	Sleep (1500)
	
	If IsActive(OEMTRIG15) = False Then
	MsgBox("Tool is Clamped")
	Dobutton(21)
	Exit Sub
	End If	
	
	ActivateSignal(OUTPUT4)
	Sleep (1500)	
	
	If IsActive(OEMTRIG11) = False Then
	MsgBox("Mag is not Down")
	Dobutton(21)
	Exit Sub
	End If
	
	NewPosition = 0
	
	Call IndexCCW(NewPosition,ChgTool)
	
End Sub

Sub IndexCW(ByVal NewPosition,ChgTool As Integer)


	If IsOutputActive(OUTPUT6) = True Then
    	DeactivateSignal(OUTPUT6)
	End If
	
	ActivateSignal(OUTPUT5)
	Sleep (500)

	Do Until IsActive(OEMTRIG12)
	Loop

	If IsActive(OEMTRIG12) = True Then
	NewPosition = (NewPosition + 1)
	End If
	
	If NewPosition < ChgTool Then
	Call IndexCW (NewPosition,ChgTool)
	End If
	
	DeactivateSignal(OUTPUT5)
	
	If NewPosition = ChgTool Then
	Call ChangeTool()
	End If

End Sub

	
	
Sub IndexCCW (ByVal NewPosition,ChgTool As Integer)


	If IsOutputActive(OUTPUT5) = True Then
    	DeactivateSignal(OUTPUT5)
	End If
	
	ActivateSignal(OUTPUT6)
	Sleep (500)

	Do Until IsActive(OEMTRIG12)
	Loop

	If IsActive(OEMTRIG12) = True Then
	NewPosition = (NewPosition + 1)
	End If
	
	If NewPosition < ChgTool Then
	Call IndexCW (NewPosition,ChgTool)
	End If
	
	DeactivateSignal(OUTPUT6)
	
	If NewPosition = ChgTool Then
	Call ChangeTool()
	End If

End Sub

Sub ChangeTool
	
	
	If IsActive(OEMTRIG9) = False Then
	MsgBox("Mag is not Forward")
	Dobutton(21)
	Exit Sub
	End If
	
	If IsActive(OEMTRIG15) = False Then
	MsgBox("Tool is Clamped")
	Dobutton(21)
	Exit Sub
	End If
	
	DeactivateSignal(OUTPUT4)
	Sleep (1500)
	
	If IsActive(OEMTRIG10) = False Then
	MsgBox("Mag is not Up")
	Dobutton(21)
	Exit Sub
	End If
	
	DeactivateSignal(OUTPUT2)
	Sleep (1500)
	
	If IsActive(OEMTRIG14) = False Then
	MsgBox("Tool is not Clamped")
	Dobutton(21)
	Exit Sub
	End If
	
	DeactivateSignal(OUTPUT3)
	Sleep (2000)
	
	If IsActive(OEMTRIG8) = False Then
	MsgBox("Mag is not Back")
	Dobutton(21)
	Exit Sub
	End If
	
	DeactivateSignal(OUTPUT10)
	
	SetOEMDRO (1200,GetSelectedTool())
	SetOEMDRO (1201,GetSelectedTool())
	
Exit Sub


 
End Sub  
     
       
