
! script name: vlathe.d/canned_rough
! var_topic: roughing - longitudinal or radial

!A
!A NAME:
!A
!A 	Rough: canned roughing cycle   VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script creates a tool path for canned cycle roughing.
!A
!B INSTRUCTIONS:
!B
!B	SELECT TOOL: Select the turning tool.
!B
!B 	DEPTH OF CUT: Enter the depth of cut (the distance between
!B		successive passes).
!B
!B 	FINISH ALLOWANCE: Enter the diameter and length amount of material
!B		to be left for a finish pass.
!B
!B 	PICK CONTOUR: Pick the contour to be roughed.
!B
!C NOTES:
!C
!C	lathe.d/setvals sets user definable constants.
!C
!C	Bar Stock: When called with the bar stock option, the variational
!C		   record is created with default stock parameters. The
!C		   defaults are:
!C
!C		   Diameter stock: Extra stock above the highest(lowest for id)
!C				   point in the contour.
!C
!C		   Face stock: Extra stock on the face(back face for reverse)
!C			       of the part.
!C
!C		   Overcut: Extra cut length added to the contour in the
!C			    direction to the cut. For example, if a cutoff
!C			    tool is being used, a positive overcut will
!C			    cut past the end of the contour. If the part
!C			    is being held in a chuck, then a negative 
!C			    overcut will keep the cut away from the end
!C			    of the contour.
!C
!C
!# see also - lathe.d/dialenfin.hlp
!# see also - lathe.d/tooltiptan.hlp

Stat=ChangeLabelStr("ScriptNameStatus","Function: rough ")

DIM Destination$[256], Answer$[256]
DIM Toolname$[256]
DIM Destination$[256]
DIM Answer$[256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Spoint$[256]
DIM Depth_var$[256]
DIM Dfinish$[256]
DIM Zfinish$[256]
DIM Name$[256]
DIM Prop_name$[256]
DIM Rough$[18],Stock$[18]
INTEGER Id_contour


ActivateUndo
Rough$=ARGV(1)

Depth_code= 0   ! 0=rapid to next depth; 1=feed to next depth
Cut_type= WsAutoRoughCuts ! or WsFixedRoughCuts  
Stat=SetXyzInput(WsLatheInput)

Restart:!
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

Animate_tool=1
Units= CurrentUnits
IF Units= WsMetric THEN
	Tool_clearance= .125
	Dia_stock= 6.0
	Face_stock= 3.0
	Overcut= 0.0
ELSE
	Tool_clearance= .05
	Dia_stock= 0.250
	Face_stock= 0.125
	Overcut= 0.0
END IF
Current_pen= CurrentColor
Work= CurrentWorkplane
Id_contour= 0
Bar_stock=0


Reverse=(Rough$[LEN(Rough$)]="r")
Long=(Rough$[1;4]="long")
Bar_stock=1

Stat=ToolSelectBox("TOOLS","selected tool",WsAllTurn)
IF Stat THEN 
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("PATH TOOL",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
                LowerSelectBox
                GOTO Leave
END IF
Toolname$= Answer$
LowerSelectBox

Tool=FindUserData(Toolname$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
Tnose_rad=ToolNoseRadius(Tool)
Quad=ToolQuadrant(Tool)
SELECT Rough$
CASE "radial","radialr"
	Uncut_angle=ToolVCAngle(Tool)
	SELECT Quad
	CASE 1,2
		IF Uncut_angle<180 THEN Uncut_angle= Uncut_angle+180
	CASE 3,4
		IF Uncut_angle>180 THEN Uncut_angle= Uncut_angle-180
	END SELECT
CASE ELSE
	Uncut_angle=ToolHCAngle(Tool)
	SELECT Quad
	CASE 1
		IF Uncut_angle<180 THEN Uncut_angle= Uncut_angle+180
	CASE 2
		IF Uncut_angle<=180 THEN Uncut_angle= Uncut_angle+180
	CASE 3
		IF Uncut_angle>=180 THEN Uncut_angle= Uncut_angle-180
	CASE 4
		IF Uncut_angle>270 OR Uncut_angle=0 THEN Uncut_angle= (Uncut_angle+180) MOD 360
	END SELECT
END SELECT

Ent=CreateVariable("tool quadrant",WsLatheVars,Quad)
Ent=CreateVariable("tool nose radius",WsLatheVars,Tnose_rad)
Ent=CreateVariable("tool clearance angle",WsLatheVars,Uncut_angle)

Depth_of_cut=NamedVarVal("depth of cut",WsLatheVars,.1)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Depth_of_cut)
Depth_of_cut=GetValue("DEPTH OF CUT",Answer$)
IF ExitScript(Destination$) THEN Leave
Depth_of_cut=ABS(Depth_of_cut)
No_depth_var= LastVariableInput(Depth_var$)
Ent=CreateVariable("depth of cut",WsLatheVars,Depth_of_cut)

Dia_allow=NamedVarVal("diameter fin allowance",WsLatheVars,.04)
Stat=SetEqualsValue(Dia_allow)
Dia_allow=GetValue("DIAMETER FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
No_dfinish_var= LastVariableInput(Dfinish$)

Ent=CreateVariable("diameter fin allowance",WsLatheVars,Dia_allow)

Len_allow=NamedVarVal("length fin allowance",WsLatheVars,.02)
Stat=SetEqualsValue(Len_allow)
Len_allow=GetValue("LENGTH FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
No_zfinish_var= LastVariableInput(Zfinish$)
Ent=CreateVariable("length fin allowance",WsLatheVars,Len_allow)

Pick_loop:!
Stat= SetInputFormat("n4","")
Contour= PickGroupEnt("PICK CONTOUR",0)
IF ExitScript(Destination$) THEN Leave
IF (NOT ValidDependency(Contour)) THEN Pick_loop
Stat= EntityNameCheck(Contour,Contour$)
IF NOT LEN(Contour$) THEN pick_loop

Closed=IsContClosed(Contour)
IF Closed THEN Pick_loop

Calc_start_end:!
Id_contour= IdContourCheck(Contour,(Long)+2*(NOT Long),Reverse)
IF Id_contour=2 THEN
	Stat=SetInputFormat("a1",",I,O,")
	Stat=GetValue("Id or Od cycle",Answer$)
	IF ExitScript(Destination$) THEN Leave
	Id_contour= (Answer$="I")
END IF

IF Id_contour THEN
        Pen_color=WsConvCutPen
ELSE
        Pen_color=WsClimbCutPen
END IF
Stat= SetCurrentColor(Pen_color)


Otype= 1*(Long)+ 2*(NOT Long)
Stype= 4*(Long)+ 5*(NOT Long)
Rough_cycle= WsLongitudinalTurn*(Long)+ WsRadialTurn*(NOT Long)

Answer$= RPT$("Longitudinal ",Long)&RPT$("Radial ",NOT Long)
Answer$= Answer$&RPT$("od ",NOT Id_contour)&RPT$("id ",Id_contour)
Answer$= Answer$&RPT$("reverse ",Reverse)

PlotOff
Stat= GenerateVarName(WsGroupEnt,Name$)
Stat= GenerateVarName(WsUserDataEnt,Prop_name$)
RecordFunctionOn
Stat= RecordDescr("CANNED LATHE ROUGH: "&Answer$&Toolname$)
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
Tool=FindUserData(Toolname$,WsToolData)
V1= FreeParameter("Id contour",Id_contour)
V2= FreeParameter("Reverse",Reverse)
!    V3= FreeParameter("Tool nose radius",ToolNoseRadius(Tool))
V3= FreeParameter("Tool nose radius",0.00001)
V4= FreeParameter("Tool quadrant",ToolQuadrant(Tool))
IF No_dfinish_var THEN
        V5= FreeParameter("Diameter finish",Dia_allow)
ELSE
        V5= FreeParameter("Diameter finish",Dfinish$)
END IF
IF No_zfinish_var THEN
        V6= FreeParameter("Length finish",Len_allow)
ELSE
        V6= FreeParameter("Length finish",Zfinish$)
END IF
IF No_depth_var THEN
        V7= FreeParameter("Depth cut",Depth_of_cut)
ELSE
        V7= FreeParameter("Depth cut",Depth_var$)
END IF
V8= FreeParameter("Clearance angle",Uncut_angle)
V9= FreeParameter("Escape distance",0.100)
!  Path= LatheToolPtShift(Contour$,V3,V5,V6,Otype,V1,V4,V2,V8)
Path= LatheToolPtShift(Contour$,V3,0,0,Otype,V1,V4,V2,V8)

V12= FreeParameter("Diameter stock",Dia_stock)
V13= FreeParameter("Face stock",Face_stock)
V14= FreeParameter("Overcut",Overcut)
Ps= BarStockContMod(Path,V12,V13,V14,Long,V1,V4,V2)
Stat= AutoNameGroup(Path,Name$)
Stat= AssociateTool(Path,Tool)
V21=StringConstant("/usr/weber/database/properties/")
RecordFunctionOff
IF Rough_cycle= WsLongitudinal THEN
RecordFunctionOn
	V22=StringConstant("long_rough")
	V24=StringConstant("long_rough")
RecordFunctionOff
ELSE
RecordFunctionOn
	V22=StringConstant("radial_rough")
	V24=StringConstant("radial_rough")
RecordFunctionOff
END IF
RecordFunctionOn
V23=StringConstant("TurboData.d")
V25=StringConstant(FileNameString(V21,V23,V24))
Prop_ent=ReadUserData(WsTurboData,V22,V25,WsTrue)
Stat=PutUserValue(Prop_ent,"X_finish:",V5)
Stat=PutUserValue(Prop_ent,"Z_finish:",V6)
Stat=PutUserValue(Prop_ent,"Depth_of_cut:",V7)
Stat=PutUserValue(Prop_ent,"Uncut_angle:",V8)
Stat=PutUserValue(Prop_ent,"Escape_dist:",V9)
Stat=NameEntity(Prop_ent,Prop_name$)
Stat= AssocUserData(Path,Prop_name$,WsTurboData)
Stat= DeleteEntity(Ps)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
PlotOn
IF NOT ValidGeometry(Path) THEN
        DelLastVarRecord
ELSE
        ActivateUndo
        Stat= AdvanceUndoNumber("lathe rough")
        Stat= AddGroupToUndo(WsUBCreateMode,Path,1,WsTrue)
        Stat= AddItemToUndo(WsUBCreateMode,Prop_ent)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
	Stat= PlotEntity(Path)
        IF Animate_tool THEN
       		 Work_trans= CurrentWorkplane
       		 Stat= AnimateTool(Tool,Path)
	END IF
END IF
GOTO Pick_loop

Leave:!
PlotOn
Stat=SetCurrentColor(Current_pen)
IF ValidGeometry(Work) THEN Stat=SetWorkplane(Work)
Stat=SetInputFormat("n4","")
LowerSelectBox
IF NOT LEN(Destination$) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END



