' 12/25/08 Rem G95 from being output... 
' 07/12/05 Fixed last pass for ID threads Brian
' 07/10/05 Added code for Dia/Rad mode for infeed and pullout.  Brian 
' 07/05/05 Fixed the mindepth per pass code, and added a lock to keep it in the min depth. Brian
' 07/05/05 Added in the code for minDepth per pass and took out the number off passes from test. Brian
' Current as of 07/04/05 G32 added back in for infeed. --Art

' Code "G01 Z" & Z_pass_start Added to the infeed move to take the Z out of the in feed move           

' Date 11/14/04 G64 and G61 taken out to see if the G32 works better (thanks Steve).           

' No G64 or G61 in code so user can test.           

' Testing the problem with the depth           

' Notes are being added as work is done           

' Test added to the macro           

' Decrement bug fixed           

' Added call to GetMinPass 11/14/04 -- Art           

' Cleared default Chamfer. Program will set to optional default.           

' Set PI to value earlier in file so Retract_dis woudl be OK           

' Date 11/13/04 G61 and G64 added to the macro to see if it will fix the bad thread pass           

' also added was the chamfer in degrees default is now one pitch of the thread           

' min Decrement added default is .001 as a Test           

' 23/12/04 - Sandvik, Kennam sequences added - Rudy         

'1/9/05 - Added in the Macro calls for the Cut type and Feed Type. I also changed the code that sets the Z_Pass_Stat pos

'               The Calcs were done as ABS But now are done INC this was done for alt flank infeed.   

'--------------------------           

' To test set TEST = 1=true and it will write to a Teach.Tap and will add notes           

'           

Test =false        'test = false turns it off           

'if GetUserLED(1053) then Test = True           

'           

'           

Min_Decrement = GetMinPass()  'test of min decement           

          

SetUserDRO 1240, tXStart()  'diagnostics fill           

SetUserDRO 1241, tEndX()           

SetUserDRO 1242, tZStart()           

SetUserDRO 1243, tEndZ()           

SetUserDRO 1244, tLead()           

SetUserDRO 1245, tTaper()           

SetUserDRO 1246, tClearX()           

SetUserDRO 1247, tFirstPass()           

SetUserDRO 1248, tFirstPass()           

SetUserDRO 1249, tInFeed()           

SetUserDRO 1250, tgetInfeedType()           

SetUserDRO 1251, tgetCutType()           

'           

PI = 4.0*atn(1.0)           

XClearance = tClearX()           

StartX = tXStart()           

StartZ = tZStart()           

EndX = tEndX()           

Total_depth = abs(StartX - EndX)           

'           

EndZ = tEndZ()           

Pitch = tLead()           

Taper = tTaper()           

InFeed_Angle = tInFeed()           

Spring_Pass = tSpring()           

First_pass_Depth = tFirstPass()           

Depth_Lastpass = tDepthLastPass()           

cut_type = tGetCutType()           

seq = tGetInfeedType()           

npass = GetUserDRO(1022)           

'           

Cut_Dir = 1       

MinDepth = 0
IF cut_type = 2 Then Cut_Dir = -1       


Retract_dis = tChamfer()           

' Code remed out by Graham Waterworth

' Retract_dis = (Retract_dis * PI)/180           

' Retract_dis = round ( ABS (StartX - EndX)* tan(Retract_dis) , 4 )           

' Code added by Graham Waterworth

Retract_Ratio  = Retract_dis/360

Retract_dis = Pitch * Retract_ratio
           
' If IsDiameter() = 1 Then 

' Retract_dis = Round (Retract_dis /2 ,4) 

' End If 
           
' End of Graham Waterworth code mods

If test = true then           

   OpenTeachFile( "ThreadTest.tap")             

          

   Code "(Taper = " & taper & ")"           

   Code "(Infeed Angle = " & InFeed_Angle & ")"           

   Code "(Total_Depth = " & Round(Total_Depth,4) & ")"           

   Code "(StartZ = " & StartZ & ")"           

   Code "(EndZ = " & EndZ & ")"           

   Code "(Seq = "&seq & " )"           

   Code "(Min_Decrement = " & Min_Decrement & " )" 
       If IsDiameter() = 1 Then 

          Code "(Dia Mode )"

       Else

          Code "(Rad Mode )"

       End If                 
End If           

'           

'Section to see if the Thread is an OD or an ID Thread           

if StartX < EndX Then           

   ID_OD = -1           

Else           

   ID_OD = 1           

End if           

'           

' Section to see the direction of the Thread           

If StartZ > EndZ Then           

  Right_Left = 1           

else           

  Right_Left = -1           

End if           

'           

'Calc the infeed in radians from degrees           

InFeed_Angle = (InFeed_Angle * PI)/180           

'Calc the distance X needed to make the taper angle           

Taper = ABS (StartZ - EndZ-Retract_dis)* tan((Taper * PI)/180)           

'set last pass to 0           

Last_pass = 0           

'Feed per Rev mode for the out feed move           

'Code "G95"           

'Start pass counter at 1           

Pass_counter= 1           

'           

'Move the tool into the start position plus X clearance           

Code "G0 X"&StartX+XClearance*ID_OD & " Z"&StartZ 'Move to start position if not there           

'Move to the X in to the start pos           

Code "G0 X" &StartX           

'  

Z_pass_start = StartZ 

          

'Do Loop is to rough out the thread           

DO           

   

            

' Calc Decrement and clip if necessary           

if seq = 1 then           

  if pass_counter = 1 then           

     Cut_Depth = (Total_depth/sqr(npass-1)*sqr(0.3))           

  else           

     Cut_Depth = (Total_depth/sqr(npass-1)*sqr(pass_counter-1))           

  end if           

else           

  Cut_depth = (First_pass_Depth*  pass_counter  ^ .5)           

end if           

'           


Decrement =  ABS(Cut_depth - Prev_Cut_Depth)           
 If  Decrement  < Min_Decrement OR MinDepth =1 Then           

     Cut_depth = Prev_Cut_Depth + Min_Decrement           

     Decrement = ABS(Cut_depth - Prev_Cut_Depth)
     MinDepth = 1           

     If test = true then Code "(Min decremant being used)"           


 End if           

'           

 Depth_pass_start=  StartX - Cut_Depth * ID_OD           

 Prev_Cut_Depth = Cut_Depth           

'           

' now do the thread line           

' This section is to test the depth and see if it is over           

' If it is over then set it to the last pass depth and set last_Pass to 1           

 If   ID_OD = -1 Then           

  If Depth_pass_start >= EndX -Depth_Lastpass Then           

    Decrement =  Decrement  - (Depth_pass_start - (EndX - Depth_Lastpass))           

    Depth_pass_start =  EndX -Depth_Lastpass           

     If test = true then  Code "(Last Pass ID_OD =-1)"           

    Last_pass = 1           

  End if           

 Else           

  if Depth_pass_start <= EndX +Depth_Lastpass Then           

    Decrement = Decrement - (EndX + Depth_Lastpass - Depth_pass_start)           

    Depth_pass_start = EndX +Depth_Lastpass           

     If test = true then  Code "(Last Pass ID_OD = 1)"           

    Last_pass = 1           

  End if           

 End if           

'           

 If test = true then Code "(Decrement = " & Round(Decrement,4) & " pass " & pass_counter & ")"           

'           

 'Calc the start point of the thread with respect to the infeed angle           


If IsDiameter() = 1 Then 

ZinfeedShift =  ( ABS(Decrement) * Tan(InFeed_Angle)*Right_Left )/2

else

ZinfeedShift =  ( ABS(Decrement) * Tan(InFeed_Angle)*Right_Left )

End If   



 Z_pass_start= Round( Z_pass_start- Cut_Dir * ZinfeedShift,4)                  




'           

 ' Infeed move to start the thread           

 Code "G01 Z" & StartZ           

 Code "G32 X" & Round (Depth_pass_start ,4) & " Z" & Round (Z_pass_start ,4) & " F"&Pitch           

 Code "G32 X" & Round (Depth_pass_start + Taper * ID_OD ,4) & " Z" & Round (ENDZ +Retract_dis *Right_Left ,4) & " F" &Pitch           

 Code "G01 X" & Round (StartX+(XClearance+ Taper) * ID_OD ,4) & " Z" & Round (ENDZ,4)  & " F" & Pitch           

 Code "G00 X" & Round (StartX+XClearance * ID_OD ,4) &" Z"& StartZ           

 Code "G00 X" &StartX           

 Pass_counter = Pass_counter + 1           

 If  Last_pass =1 or Pass_counter = 200 Then           

  Exit do           

 End if           

'           

If cut_type =1 Then 'This code is for the alternating flank infeed     

   IF Cut_Dir = 1 Then    

      Cut_Dir = -1   

   Else  

     Cut_Dir = 1  

  End If  

End If  

Loop  

Call SetUserDRO(1022, pass_counter-1)           

IF Depth_Lastpass <> 0 Then           

  If test = true then Code "( Last pass at depth of " & Depth_Lastpass & ")"           

  Code "G01 Z" & StartZ           

  Code "G32 X" & EndX & " Z" & Round(Z_pass_start ,4) & " F"&Pitch           

  Code "G32 X" & Round (EndX + Taper* ID_OD  ,4) & " Z" & Round (ENDZ +Retract_dis *Right_Left ,4) & " F" &Pitch           

  Code "G01 X" & Round (StartX+(XClearance+Taper)* ID_OD ,4)  & " Z" & ENDZ  & " F" & Pitch           

  Code "G00 X" & Round (StartX+XClearance * ID_OD ,4)  &" Z"&StartZ           

  Code "G00 X" &StartX           

End if           

'           

IF Spring_Pass >0 Then           

Pass_counter= 0           

'           

  DO           

  If test = true then Code "( Spring pass number " & Pass_counter+1 & ")"           

    Code "G01 Z" & StartZ           

    Code "G32 X" & Round (EndX ,4) & " Z" & Round (Z_pass_start ,4) & " F"&Pitch           

    Code "G32 X" & Round (EndX + Taper * ID_OD ,4) & " Z" & Round (ENDZ +Retract_dis*Right_Left ,4) & " F" &Pitch           

    Code "G01 X" & Round (StartX+(XClearance+Taper) * ID_OD,4) & " Z" & ENDZ  & " F" & Pitch           

    Code "G00 X" & Round (StartX+XClearance *ID_OD ,4) &" Z" & StartZ           

    Code "G00 X" & StartX          

    Pass_counter = Pass_counter + 1           

  Loop While  Spring_Pass > Pass_counter           

End If           

Code "G00 X" & StartX + XClearance *ID_OD & " Z" & StartZ  
'           

If test = true then CloseTeachFile()           

If test = true then LoadTeachFile()           

