
! script name: vlathe.d/rough
! var_topic: roughing - horizontal or radial

!A
!A NAME:
!A
!A 	Rough: roughing cycle   VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script creates successive cuts in a horizontal or
!A	vertical orientation.  The tool clearance angle is used to
!A	determine uncutable areas of the starting contour, called
!A	undercuts.
!A
!A~ image location - images/longrough.jpg
!A
!A~ image location - images/radialrough.jpg
!A
!A~ image location - images/stockbdy.jpg
!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
!B	PICK STOCK CONTOUR: Pick the contour used for the stock.(optional)
!B
!C NOTES:
!C
!C	lathe.d/setvals sets user definable constants.
!C
!C	Stock contour: The stock contour must have no undercuts. It
!C			must be described in the same direction as
!C			the roughing contour. With a stock boundary
!C			the Clearance angle is assumed to be zero.
!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 Datastr$[1256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Scontour$[256]
DIM Spoint$[256]
DIM Depth_var$[256]
DIM Dfinish$[256]
DIM Zfinish$[256]
DIM Name$[256]
DIM Rough$[18],Stock$[18]
INTEGER Id_contour


ActivateUndo
Rough$=ARGV(1)
IF ARGC>2 THEN
	Stock$=ARGV(2)
ELSE
	Stock$=""
END IF

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")
IF POS(Stock$,"barstock") OR POS(Stock$,"Barstock") THEN
	Stock$=""
	Bar_stock=1
END IF

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

Stock_question:!
IF LEN(Stock$) THEN
Ps_created= 0
Spick_loop:!
	Stat= SetInputFormat("n4","")
	Stock_contour= PickGroupEnt("PICK STOCK CONTOUR",0)
	IF (ExitScript(Destination$)) THEN Leave 
	IF NOT ValidGeometry(Stock_contour) THEN Spick_loop
	IF (NOT ValidDependency(Stock_contour)) THEN Pick_loop
	Stat= EntityNameCheck(Stock_contour,SContour$)
	IF NOT LEN(SContour$) THEN Spick_loop
	
	Stock_boundary=1
ELSE
	IF NOT Bar_stock THEN
		Stat=SetInputFormat("n4","")
                Pstart=XyzInput("INPUT START POINT",WsXyOnly)
                IF (ExitScript(Destination$)) THEN Leave
                IF NOT ValidGeometry(Pstart) THEN Stock_question

                E1= LastPickEnt
                Pick_para= LastPickPara
                Pick_return= CurrPickReturn
                IF NOT ValidDependency(E1) THEN
                        E1= CopyEntity(Pstart,WsFalse)
                        Stat= EntityNameCheck(E1,Spoint$)
                        Ps_created= 1
                ELSE
                        Stat= EntityNameCheck(E1,Spoint$)
                        Ps_created= 0
                END IF
                Stat= DeleteEntity(Pstart,WsTrue)
	END IF
	Stock_boundary=0
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$)
RecordFunctionOn
Stat= RecordDescr("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))
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("Feed to depth",Depth_code)
V10= FreeParameter("Auto cuts",Cut_type)
V15= FreeParameter("Collision point color",7)
V20= FreeParameter("First cuts skipped",0)
V21= FreeParameter("First cuts only",0)
Path= LatheToolPtShift(Contour$,V3,V5,V6,Otype,V1,V4,V2,V8)
If Stock_boundary THEN
	V11= FreeParameter("Tool clearance",Tool_clearance)
        Stock_path= LatheToolPtShift(Scontour$,0.0,V11,V11,Stype,V1,V4,V2)
        Ps= LatheStockStartPt(Stock_path,Long)
	Rough_path= RoughTurning(Path,Ps,Rough_cycle,V7,V3,V8,V1,V4,V9,V10,V2,Stock_path)
	Stat= DeleteGroup(Stock_path,WsTrue)
ELSE
	IF Bar_stock THEN
           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)
	ELSE
	   SELECT Pick_return
	   CASE WsGeometryEnd
       		Ps= ParameterReturnPt(Spoint$,Pick_para,WsGeometryEnd)
	   CASE WsGeometryMid
        	Ps= ParameterReturnPt(Spoint$,Pick_para,WsGeometryMid)
	   CASE WsGeomCenter
       		Ps= ParameterReturnPt(Spoint$,Pick_para,WsGeomCenter)
	   END SELECT
	END IF
	Rough_path= RoughTurning(Path,Ps,Rough_cycle,V7,V3,V8,V1,V4,V9,V10,V2)
END IF
Stat= RemoveFirstCuts(Rough_path,V20)
Stat= SaveFirstCuts(Rough_path,V21)
Stat= AutoNameGroup(Rough_path,Name$)
Stat= AssociateTool(Rough_path,Tool)
P1= LthToolInterfere(Tool,Rough_path,Contour$)
Stat= SetColor(P1,V15)
Stat= SetLineType(P1,11)
Stat= DeleteGroup(Path,WsTrue)
Stat= DeleteEntity(Ps)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
PlotOn
IF NOT ValidGeometry(Rough_path) THEN
        DelLastVarRecord
ELSE
        ActivateUndo
        Stat= AdvanceUndoNumber("lathe rough")
	IF Ps_created THEN
                Stat= AddItemToUndo(WsUBCreateMode,Spoint$)
        END IF
        Stat= AddGroupToUndo(WsUBCreateMode,Rough_path,1,WsTrue)
        Stat= AddItemToUndo(WsUBCreateMode,P1)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
	Stat= PlotEntity(Rough_path)
        Stat= PlotEntity(P1)
        IF Animate_tool AND NOT ValidGeometry(P1) THEN
       		 Work_trans= CurrentWorkplane
       		 Stat= AnimateTool(Tool,Rough_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



