' G77    
'08/20/05 Edited the macro for the new engine. Brian
'Rev1  2/3/05 ID and OD have been tested and seem to be working  fine... The Right to left should be working as well.. we will know more when Steve gets done with me ;)   
'Rev1.1 2/7/05 There was a bug that would make a bad cut if a large angle was called Fixed with "sometrig"  
'(Xx.xxxx XDia NEEDED)
'(Zx.xxxx End Z NEEDED)
'(Fx.xxxx Feedrate)
'(can be Set In the settings page: 'Hx.xxxx Depth of cut)
'(Cx.xxxx Clearance In the X)
'(Qx.xxxx Clearance In the Z)
'(Kx.xxxx ZStartpoint)
'(Rx.xxxx XStartpoint)
'(Tx.xxxx Taper In Deg)   
   
Sub Main()                     
ZClearance = Abs(tZClear()) 'This one it not avalable                     
XClearance = Abs(tClearX())                     
StartX = tXStart()                     
StartZ = tZStart()                     
EndX = tEndX()                       
EndZ = tEndZ()                     
Taper = tTaper()                     
FirstPassDepth = tFirstPass()                     
RoughAmount =  tCutDepth()                    
Feed = Feedrate()                     
                    
                     
PI = 3.1415926                     
'OpenTeachFile "TEST.Tap"                     
'Calc the distance X needed to make the taper angle                     
Taper = Abs (StartZ - EndZ)* Tan((Taper * PI)/180)                     
        
'Code "(Rough amout = " & rough_amount &")"                    
If StartX < EndX Then                     
   IDOD = -1                     
   XClearance = XClearance * -1                     
Else                     
   IDOD = 1                     
End If                     
                     
If StartZ > EndZ Then                     
  RightLeft = 1                     
Else                     
  RightLeft = -1                     
  ZClearance = ZClearance * -1                     
End If                     

If( Taper = 0 ) Then
  ZCalc = 0
 Else                     
  ZCalc = Abs(StartX - EndX) /Tan((Taper * PI)/180)  
 end if 
'Code  "( Zcalc =" & ZCalc & ")"  
If ZCalc < Abs(StartZ-EndZ) Then  
    If RightLeft = 1 Then  
       EndZ = StartZ - Zcalc  
       Else  
       EndZ = StartZ + ZCalc  
   End If  
End If                     
'Code "(Taper = " &Taper& ")"           
           
code "G0 X" & StartX + XClearance & " Z" & StartZ +  ZClearance           
Code "F" & Feed           
PassNum = 1           
EndLoop = 0           
LastPassDia = StartX           
LoopExit = 0           
Do           
  PassDia = StartX - (roughAmount * PassNum) * IDOD           
  If IDOD =1 And PassDia < EndX Or IDOD =-1 And PassDia > EndX Then           
   PassDia = EndX           
   LoopExit = 1           
  End If           
  Code "G01 X" & PassDia           
           
  If IDOD =1 And Taper <> 0 And PassDia < EndX + Taper Or IDOD =-1 And Taper <> 0 And PassDia > EndX - Taper Then 'To get into taper mode           
  'Taper turn section           
'Code "(TAPER SECTION)"           
  Code "Z" & StartZ           
   ZendCalc = StartZ - RightLeft * Abs((  Abs (StartZ - EndZ)   *(  PassDia -EndX)) / Taper )           
   Code "Z" & nFmt(ZendCalc,4)           
   ZendCalc = StartZ - RightLeft * Abs((  Abs (StartZ - EndZ)   *(  LastPassDia  -EndX)) / Taper )           
   If IDOD =1 And LastPassDia > EndX+Taper Or IDOD =-1 And LastPassDia < EndX-Taper Then           
     Code "(Transition Code)"           
     Code "X" & EndX +Taper * IDOD  & "Z" & EndZ           
     Code "X" & LastPassDia           
     PosZ = EndZ           
   Else           
  
     Code "X" & LastPassDia & "Z" & ZendCalc           
     PosZ = nFmt(ZendCalc,4)           
   End If           
  Else           
  'Straight turn section           
   Code "Z" & EndZ           
   PosZ = EndZ           
   Code "X" & LastPassDia           
           
  End If           
            
  Code "X" & LastPassDia+ XClearance & "Z" & PosZ+ ZClearance           
  Code "G00 Z" & StartZ + ZClearance           
    
  If LoopExit = 1 Then           
       Exit Do           
   End If           
   If Pass_Num = 300 Then           
       Exit Do           
   End If           
    
LastPassDia = PassDia           
  PassNum = PassNum + 1           
Loop           
           
Code "G0 X" & StartX & " Z" & StartZ           
           
           
'CloseTeachFile           
'Call LoadTeachFile()           
           
End Sub           
           
Main            
