
! script name: lathe.d/rough
! topic: roughing - horizontal or radial

!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/longrough.gif
!A
!A~ image location - images/radialrough.gif
!A
!A~ image location - images/stockbdy.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
!B 		amount of material 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
!B 		the stock.(optional)
!B
!B	If the tool is not selected the following questions will appear:
!B
!B 	TOOL NOSE RADIUS: Enter the tool nose radius of the
!B 		tool to be used.
!B
!B 	TOOL QUADRANT: Enter the tool nose quadrant.
!B
!B 	TOOL CLEARANCE ANGLE: Enter the cutting edge
!B 		clearance angle.
!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
!# see also - lathe.d/dialenfin.hlp
!# see also - lathe.d/tooltiptan.hlp

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

COM /Rough/ Allow_delta,Conv,Depth_code,Cut_type$[18],Rough$[18],Stock$[18]
DIM Destination$[256], Answer$[256], Rough_name$[40]
DIM Name$[40], Datastr$[1024], Stock_name$[40], Toolname$[256], Toolname2$[256]
INTEGER Id_contour,Dp_code


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

LOAD "lathe.d/setvals:rough"
Restart:!
Animate_tool=1
Tool_clearance= .05*Conv
Current_pen= CurrentColor
Work= CurrentWorkplane
StdWorkplane
Id_contour= 0

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


Tool_assoc=NamedVarVal("tool association",WsToolVars,0.)

Start:!
IF NOT Tool_assoc THEN
	GOSUB Get_tool_data
ELSE
	Stat=ToolSelectBox("TOOLS","selected tool",WsAllTurn)
	IF Stat THEN
		Tool_assoc=0
		GOTO Start
	ELSE
Tool_ques:!
		Stat=SetInputFormat("a99","")
		Stat=GetValue("PATH TOOL",Toolname$)
		IF (ExitScript(Destination$)) THEN Leave
		IF Toolname$= "CANCEL" THEN
			LowerSelectBox
			Tool_assoc=0
			GOTO Start
		END IF
		Tool=FindUserData(Toolname$,WsToolData)
		Tnose_rad=GetUserValue(Tool,"Tool nose radius:")
		Quad=GetUserValue(Tool,"Tool quadrant:")
		IF (Rough$="radial") OR (Rough$="radialr") THEN
			Uncut_angle=ToolVCAngle(Tool)
		ELSE
			Uncut_angle=ToolHCAngle(Tool)
		END IF
		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) MOD 360
		END SELECT
		LowerSelectBox
	END IF
END IF

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

Depth_val=FindNamedVar("factor for depth of cut",WsToolVars)
IF ValidGeometry(Depth_val) THEN
	Depth_factor=GetVariableVal(Depth_val)
	Depth_of_cut=Tnose_rad*Depth_factor
ELSE
	Depth_of_cut=NamedVarVal("depth of cut",WsLatheVars,.1)
END IF

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)
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
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
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 ValidGeometry(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)

IF LEN(Stock$) THEN 
if Long THEN
                Uncut_angle= 0.0*(NOT Reverse)+180*(Reverse)
        ELSE
                Uncut_angle= 270.0*(NOT Reverse)+90*(Reverse)
END IF
END IF
Shift_type= 1*(Long)+ 2*(NOT Long)

PlotOff
Path= LatheToolPtShift(Contour,Tnose_rad,Dia_allow,Len_allow,Shift_type,Id_contour,Quad,Reverse,Uncut_angle)
PlotOn
Stat= PlotEntity(Path)

IF LEN(Stock$) THEN
Spick_loop:!
	Stat= SetInputFormat("n4","")
	Stock_contour= PickGroupEnt("PICK STOCK CONTOUR",0)
	IF (ExitScript(Destination$)) THEN 
		Deletepaths(Path)
		GOTO Leave
	END IF
	IF NOT ValidGeometry(Stock_contour) THEN Spick_loop

	Shift_type= 4*(Long)+ 5*(NOT Long)
	PlotOff
	Stock_path= LatheToolPtShift(Stock_contour,0.0,Tool_clearance,Tool_clearance,Shift_type,Id_contour,Quad,Reverse)
	PlotOn
	IF ValidGeometry(Stock_path) THEN
		Closed=IsContClosed(Stock_path)
		IF Closed THEN Spick_loop
                Stock_copied=1
                Pt= LatheStockStartPt(Stock_path,Long)
                Xs= XPointValue(Pt)
                Ys= YPointValue(Pt)
                Stat= DeleteEntity(Pt)
        END IF
ELSE
	!Stat=SetXyzInput(WsXyzInput)
	Pt=XyzInput("PICK STARTING",WsXyOnly)
	IF (ExitScript(Destination$)) THEN 
		Deletepaths(Path)
		GOTO Leave
	END IF
	Xs= XPointValue(Pt)
	Ys= YPointValue(Pt)
	Stat= DeleteEntity(Pt)
END IF

PlotOff
Dp_code=Depth_code
IF ValidGeometry(Stock_path) THEN
	Rough_path= TurnRough(Path,0,Xs,Ys,Depth_of_cut,ABS(Tnose_rad),Uncut_angle,Id_contour,Dp_code,Cut_type$,Rough$,Stock_path)
ELSE
	Rough_path= TurnRough(Path,0,Xs,Ys,Depth_of_cut,ABS(Tnose_rad),Uncut_angle,Id_contour,Dp_code,Cut_type$,Rough$)
END IF
Deletepaths(Path)
IF Stock_copied THEN CALL Deletepaths(Stock_path)
PlotOn
Stat= PlotEntity(Rough_path)
P1= LthToolInterfere(Tool,Rough_path,Contour)
IF ValidGeometry(P1) THEN
	Stat= DeleteEntity(P1)
ELSE
	IF Animate_tool THEN
		Work_trans= CurrentWorkplane
		Stat= AnimateTool(Tool,Rough_path,Work_trans)
	END IF
END IF

IF ValidGeometry(Rough_path) THEN
	Stat= GroupName(Rough_path,Rough_name$)
	Stat= GroupName(Contour,Name$)
	Datastr$=" TURNING ROUGH "&CHR$(10)&" "&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="NAME: "&Rough_name$&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="TYPE:  "&UPC$(Rough$)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="CONTOUR: "&Name$&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="CONTOUR TYPE: "&RPT$("OD",NOT Id_contour)&RPT$("ID",Id_contour)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="TOOL NOSE RAD: "&VAL$(ABS(Tnose_rad))&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="TOOL QUADRANT: "&VAL$(Quad)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="CLEARANCE ANGLE: "&VAL$(Uncut_angle)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="DEPTH OF CUT: "&VAL$(Depth_of_cut)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="DIA ALLOWANCE: "&VAL$(Dia_allow)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="LEN ALLOWANCE: "&VAL$(Len_allow)&CHR$(10)
	IF ValidGeometry(Stock_contour) THEN
		Stat= GroupName(Stock_contour,Stock_name$)
		Datastr$[LEN(Datastr$)+1]="STOCK NAME: "&Stock_name$&CHR$(10)
	ELSE
		Datastr$[LEN(Datastr$)+1]="STARTING X: "&VAL$(Xs)&CHR$(10)
		Datastr$[LEN(Datastr$)+1]="STARTING Y: "&VAL$(Ys)&CHR$(10)
	END IF

	Roughdata=FindUserData(Rough_name$,WsTurnRough)
	IF NOT ValidGeometry(Roughdata) THEN
       		 Roughdata=ReadUserData(WsTurnRough,Rough_name$,Datastr$)
	ELSE
		 Stat= DeleteEntity(Roughdata)
       		 Roughdata=ReadUserData(WsTurnRough,Rough_name$,Datastr$)
	END IF

	Stat= AssocUserData(Rough_path,Roughdata,WsTurnRough)
	IF Tool_assoc THEN Stat= AssociateTool(Rough_path,Tool)

	ActivateUndo
        Stat= AdvanceUndoNumber("lathe rough")
        Stat= AddItemToUndo(WsUBCreateMode,Roughdata)
        Stat= AddGroupToUndo(WsUBCreateMode,Rough_path,1,WsTrue)

END IF

GOTO Pick_loop

Get_tool_data:!
	Tnose_rad=NamedVarVal("tool nose radius",WsLatheVars,.031)
	Stat=SetInputFormat("n4=","")
	Stat=SetRadiusEquals(Tnose_rad)
	Tnose_rad= InputRadius("TOOL NOSE RADIUS")
	IF ExitScript(Destination$) THEN Leave
	Tnose_rad=ABS(Tnose_rad)

	Quad=NamedVarVal("tool quadrant",WsLatheVars,1.)
	Stat=SetInputFormat("n4=","")
	Unum=Quad
	Stat=SetEqualsValue(Unum)
	Quad=GetValue("TOOL QUADRANT",Answer$)
	IF ExitScript(Destination$) THEN Leave

	Uncut_angle=NamedVarVal("tool clearance angle",WsLatheVars,180.)
	Stat=SetInputFormat("n4=","")
	Stat=SetAngleEquals(Uncut_angle)
	Uncut_angle=InputAngle("TOOL CLEARANCE ANGLE")
	IF ExitScript(Destination$) THEN Leave
	Uncut_angle=Uncut_angle MOD 360
RETURN

Leave:!
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
Deletepaths:SUB Deletepaths(Path)
IF ValidGeometry(Path) THEN 
	Stat= DeleteGroup(Path,1)
	Stat= DeleteEntity(Path)
END IF
SUBEND


