
! script name: wscturn.d/roughturn
! var_topic: roughing - turning or facing

!A
!A NAME:
!A
!A 	Rough: roughing cycle 
!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/roughturn.gif
!A
!A~ image location - images/roughface.gif
!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	The default stock parameters 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 Tool_name$[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
Var_object= CurrentVarObject
IF NOT ValidGeometry(Var_object) THEN
        Var_object=OpenVarObject("Program")
END IF
! Var_object= CheckVarObject
! IF (ExitScript(Destination$)) THEN Leave

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

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

Animate_tool=0
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
Stat=SetXyzInput(WsLatheInput)

IF ARGC>1 THEN
        Answer$= ARGV(1)
        GOTO Have_tool
END IF

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

Have_tool:!
Tool_name$= Answer$
Tool=FindUserData(Tool_name$,WsToolData)
IF NOT ValidGeometry(Tool) THEN Start
Stat=GetUserString(Tool,"Tool type:",Toolid$)
Tnose_rad=ToolNoseRadius(Tool)
Quad=ToolQuadrant(Tool)

Ask_mode:!
Stat= SetInputFormat("a3b=",",T,F,TN,FN,TS,FS,TR,FR,TNR,FNR,TSR,FSR,")
Stat= SetEqualsValue(0,"T")
Stat= GetValue("Turn or Face ",Answer$)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Start
        GOTO Leave
END IF
IF POS(Answer$,"T") THEN
	IF POS(Answer$,"R") THEN
		Rough$= "longr"
		Reverse= WsTrue
	ELSE
		Rough$= "long"
		Reverse= WsFalse
	END IF
	Long= WsTrue
ELSE
	IF POS(Answer$,"R") THEN
		Rough$= "radialr"
                Reverse= WsTrue
        ELSE
		Rough$= "radial"
                Reverse= WsFalse
        END IF
	Long= WsFalse
END IF
Bar_stock=1
IF POS(Answer$,"S") THEN
	Stock$= "stock"
	Bar_stock= 0
END IF
IF POS(Answer$,"N") THEN
	Stock$= ""
	Bar_stock= 0
END IF

SELECT Rough$
CASE "radial","radialr"
	Uncut_angle=ToolVCAngle(Tool)
CASE ELSE
	Uncut_angle=ToolHCAngle(Tool)
END SELECT
Uncut_angle=(180+Uncut_angle) MOD 360

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)

Depth_cut: !
Stat=SetInputFormat("n4b=","")
Stat=SetEqualsValue(Depth_of_cut)
Depth_of_cut=GetValue("DEPTH OF CUT",Answer$)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Ask_mode
        GOTO Leave
END IF
Depth_of_cut=ABS(Depth_of_cut)
No_depth_var= LastVariableInput(Depth_var$)
Ent=CreateVariable("depth of cut",WsLatheVars,Depth_of_cut)

Dia_allow:!
Stat=SetInputFormat("n4b=","")
Dia_allow=NamedVarVal("diameter fin allowance",WsLatheVars,.04)
Stat=SetEqualsValue(Dia_allow)
Dia_allow=GetValue("DIA. FINISH ALLOWANCE",Answer$)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Depth_cut
        GOTO Leave
END IF
No_dfinish_var= LastVariableInput(Dfinish$)

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

Len_allow:!
Stat=SetInputFormat("n4b=","")
Len_allow=NamedVarVal("length fin allowance",WsLatheVars,.02)
Stat=SetEqualsValue(Len_allow)
Len_allow=GetValue("LENGTH FINISH ALLOWANCE",Answer$)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Dia_allow
        GOTO Leave
END IF
No_zfinish_var= LastVariableInput(Zfinish$)
Ent=CreateVariable("length fin allowance",WsLatheVars,Len_allow)

Pick_loop:!
Stat= SetInputFormat("n4b","")
Contour= PickGroupEnt("PICK CONTOUR",0)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Len_allow
        GOTO Leave
END IF
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("a1b",",I,O,")
	Stat=GetValue("Id or Od cycle",Answer$)
	IF (ExitScript(Destination$)) THEN
        	IF Destination$= "BACKUP" THEN Pick_loop
        	GOTO Leave
	END IF
	Id_contour= (Answer$="I")
END IF

Stock_question:!
IF LEN(Stock$) THEN
Ps_created= 0
Spick_loop:!
	Stat= SetInputFormat("n4b","")
	Stock_contour= PickGroupEnt("PICK STOCK CONTOUR",0)
	IF (ExitScript(Destination$)) THEN
        	IF Destination$= "BACKUP" THEN Pick_loop
        	GOTO Leave
	END IF
	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
	if Long	THEN
		Uncut_angle= 0.0*(NOT Reverse)+180*(Reverse)
	ELSE
		Uncut_angle= 270.0*(NOT Reverse)+90*(Reverse)
	END IF
	
	Stock_boundary=1
ELSE
	IF NOT Bar_stock THEN
		Stat=SetInputFormat("n4b","")
                Pstart=XyzInput("INPUT START POINT",WsXyOnly)
		IF (ExitScript(Destination$)) THEN
        		IF Destination$= "BACKUP" THEN Pick_loop
        		GOTO Leave
		END IF
                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$)
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
Tool=FindUserData(Tool_name$,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)
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= 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
	  IF Destination$= "SCRIPTSTART" THEN Start
          IF Destination$= "BACKUP" THEN Start
	  LOAD Destination$
END IF
END



