'ToolChange Macro
Sub Main()
OldTool = GetOEMDRO (1200) 'Tool In spindle DRO
x = GetToolChangeStart( 0 )
y = GetToolChangeStart( 1 ) 
z = GetToolChangeStart( 2 ) 
a = GetToolChangeStart( 3 )  
tool = GetSelectedTool()
NewTool = tool
'Tool Changer Macro 
MaxToolNum = 12      'Max number off tools for the changer
ToolDown1   = -168.8 'Z Pos for toolrack to grab a tool
ToolDown   = -167.6 'Z Pos to Grab or drop a tool
ToolUp1     = -155  'Z Pos for activate or deactivate cleaning taper
ToolUp     = -75    'Z Hieght - Rapid from tool to tool
If NewTool = OldTool Then 
	Exit Sub
End If
While NewTool > MaxToolNum
NewTool = Question ("Enter New Tool Number up to " & MaxToolNum)
Wend
Code "G00 G53 Z" & ToolUp
While IsMoving()
Wend
Call MovePos(OldTool)
While IsMoving()
Wend
Code "G53 Z" & ToolDown1
Code "G4 P.75"
While IsMoving()
Wend
ActivateSignal(Output3) 'Turn On Piston to bring toolrack in position
Code "G4 P3.0"    'Wait for the toolrack to be in position
While IsMoving()
Wend
ActivateSignal(Output1) 'Turn On Draw bar to release the tool
Code "G4 P1.5"    'Wait for the tool to release
Code "G53 Z" & ToolUp1
While IsMoving()
Wend
ActivateSignal(Output6) 'Turn On cleaning taper
Code "G4 P1.0"    'Wait to clean
While IsMoving()
Wend
DeActivateSignal(Output6) 'Turn Off cleaning taper
Code "G4 P.75"
Code "G53 Z" & ToolUp 'move to rapid height
Code "G4 P.50"
Call MovePos(NewTool)
While IsMoving()
Wend
Code "G53 Z" & ToolUp1
Code "G4 P.75"
While IsMoving()
Wend
ActivateSignal(Output6) 'Turn On cleaning taper
Code "G4 P1.0"    'Wait to clean
While IsMoving()
Wend
DeActivateSignal(Output6) 'Turn Off cleaning taper
Code "G4 P.75"
While IsMoving()
Wend
Code "G53 Z" & ToolDown 'z pos to grab a tool
Code "G4 P1.5"
While IsMoving()
Wend
DeActivateSignal(Output1) 'Turn Off Draw bar to Clamp the tool
Code "G4 P2.0"    'Wait for the tool to Clamp
While IsMoving()
Wend
DeActivateSignal(Output3) 'Turn OFF Piston - toolrack move away
Code "G4 P2.0"    'Wait for the toolrack to move away
While IsMoving()
Wend
Code "G53 Z" & ToolUp
Call SetUserDRO (1200,NewTool)
SetCurrentTool( NewTool )
Code "G00 X" & x & " Y" & y 'Move back to where the tool change was prompted
End Sub

Sub MovePos(ByVal ToolNumber As Integer)

Select Case ToolNumber
       Case Is = 1
         Xpos = 19.2
         YPos = -1343.98
       Case Is = 2
         Xpos = 19.2
         YPos = -1235.3
       Case Is = 3
         Xpos = 19.2
         YPos = -1126.62
       Case Is = 4
         Xpos = 19.2
         YPos = -1017.94
       Case Is = 5
         Xpos = 19.2
         YPos = -909.26
       Case Is = 6
         Xpos = 19.2
         YPos = -800.58
       Case Is = 7
         Xpos = 19.2
         YPos = -691.9
       Case Is = 8
         Xpos = 19.2
         YPos = -583.22
       Case Is = 9
         Xpos = 19.2
         YPos = -474.54
       Case Is = 10
         Xpos = 19.2
         YPos = -365.86
       Case Is = 11
         Xpos = 19.2
         YPos = -257.18
       Case Is = 12
         Xpos = 19.2
         YPos = -148.5
End Select

Code "G53 X" & XPos & " Y" & YPos
End Sub
Main            