﻿'General
Sub OnAppStart()
  'MsgBox "OnAppStart"
End Sub

Sub OnOpen()
  'MsgBox "OnOpen: Units=" & State.Units & " X=" & State.X & " Y=" & State.Y & " Z=" & State.Z
End Sub

'Machine
Sub OnMachineStart()
  'MsgBox "OnMachineStart"

  'turn spindle on
  'Set command = Cmd.Create()
  'command.SpindleCW = true
  'CmdBuffer.Add(command)
  
  'wait 1 second
  'Set command = command.Create()
  'command.Dwell = 1
  'CmdBuffer.Add(command)

  'turn flood and mist on 
  'Set command = command.Create()
  'command.Flood = true
  'command.Mist = true
  'CmdBuffer.Add(command)
        
End Sub

Sub OnMachineStartLoop(LoopCount)
  'MsgBox "OnMachineStartLoop: " & LoopCount
  'move to X0 Y0 Z20 at start of every loop
  'Set command = Cmd.Create()
  'command.UseFeedRate()
  'command.X = 0
  'command.Y = 0
  'command.Z = 20
  'CmdBuffer.Add(command)
End Sub

Sub OnMachineEnd()
  'MsgBox "OnMachineEnd"
  'move Z to 100 at end
  'Set command = Cmd.Create()
  'command.UseTraverseRate()
  'command.Z = 100
  'CmdBuffer.Add(command)
End Sub

Sub OnMachineStop(EStop, UserStop)
  'MsgBox "OnMachineStop: EStop=" & EStop & " UserStop=" & UserStop
  If (not EStop And not UserStop) Then
    'If program stoped without user intervention start it again. 
    'This makes endless loop until user stops program manualy
    Cmd.Start
  End If
End Sub

'G-Code
Function OnCommand(LineNo, Line, CommandType, AxisDir)

  'MsgBox "OnCommand LineNo: " & LineNo & " Line: '" & Line & "' CmdType: " & CommandType & " AxisDir: " & AxisDir

  If (CommandType = 1) Then 'Mist - also turn flood on/off
    'Set commandBefore = Cmd.Create()
    'commandBefore.Flood = CmdNext.Mist
    'CmdBufferBefore.Add(commandBefore) 
    'OnCommand = False 'leave original mist command to execute  
  End If 
  
  If (CommandType = 2) Then 'Flood - replace with mist
    'Set commandBefore = Cmd.Create()
    'commandBefore.Mist = CmdNext.Flood
    'CmdBufferBefore.Add(commandBefore) 
    'OnCommand = True 'remove original flood command from execute  
  End If 
   
  If (CommandType = 3) Then 'Spindle - turn mist and flood on/off
    'Set commandBefore = Cmd.Create()
    'commandBefore.Flood = CmdNext.SpindleCW Or CmdNext.SpindleCCW
    'commandBefore.Mist = CmdNext.SpindleCW Or CmdNext.SpindleCCW
    'CmdBufferBefore.Add(commandBefore) 
    'OnCommand = False 'leave original Spindle command to execute  
  End If 
  
  If ((CommandType = 4) Or (CommandType = 5)) Then 'MoveFeed or MoveTraverse
	Dim dx, dy, dz
	dx = CmdNext.X - Cmd.X 
	dy = CmdNext.Y - Cmd.Y 
	dz = CmdNext.Z - Cmd.Z 
	'MsgBox "dx: " & dx & " dy: " & dy & " dz: "& dz & " AxisDir: " & AxisDir
	
	'if Z axis is moved we will replace move with spindle  
	If (AxisDir = 32) Then
	  'move up will turn spindle off
	  'Set commandBefore = Cmd.Create()
	  'commandBefore.SpindleCW = False
	  'CmdBufferBefore.Add(commandBefore) 
	  'OnCommand = True 'remove original Z move command from execution 
	ElseIf (AxisDir = 16) Then
	  'move down will turn spindle on
	  'Set commandBefore = Cmd.Create()
	  'commandBefore.SpindleCW = True
	  'CmdBufferBefore.Add(commandBefore) 
	  'OnCommand = True 'remove original Z move command from execution
	Else
	  'OnCommand = False 'leave original move command    
	End If

  End If 
  
  If (CommandType = 8) Then 'ToolChange
    'Set commandBefore = Cmd.Create()
    'CmdBufferBefore.Add(commandBefore) 
    'OnCommand = True 'remove original toolchange command from execution   
  End If

End Function




