
! script name: lathe.d/undercut
! topic: roughing/undercutting

!A
!A NAME:
!A
!A      Undercut: undercut roughing cycle
!A
!A
!A DESCRIPTION:
!A
!A      The contour is made into an undercut by connecting the endpoints.
!A      The depth of cut is determined by the amount of material to
!A      remove and the number of cuts requested.
!A
!A
!B INSTRUCTIONS:
!B
!B	SELECT TOOL: Select the undercut tool. 
!B
!B 	NUMBER OF CUTS: Enter the number of 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: Select the contour.
!B
!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
!C NOTES:
!C
!C 	This routine can fail if it cannot locate an undercut on the
!C 	contour.
!C 	The undercut contour needs elements at the ends to form an
!C 	undercut. This can be accomplished by a small linear move
!C 	or a corner radius at the ends of the contour.
!C
!C 	lathe.d/setvals sets user definable constants.
!C
!# see also - lathe.d/dialenfin.hlp
!# see also - lathe.d/tooltiptan.hlp

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

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

Rough$=ARGV(1)
LOAD "lathe.d/setvals:undercut"
Restart:!
ActivateUndo

Animate_tool=1
Current_pen= CurrentColor
Work= CurrentWorkplane
StdWorkplane
Id_contour= 0
Reverse=(Rough$[LEN(Rough$)]="r")
Long=(Rough$[2;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",Answer$)
		IF (ExitScript(Destination$)) THEN Leave
		IF Answer$= "CANCEL" THEN
			LowerSelectBox
			Tool_assoc=0
			GOTO Start
		END IF
		Tool=FindUserData(Answer$,WsToolData)
		Tnose_rad=GetUserValue(Tool,"Tool nose radius:")
		Quad=GetUserValue(Tool,"Tool quadrant:")
		LowerSelectBox
	END IF
END IF
Ent=CreateVariable("tool nose radius",WsLatheVars,Tnose_rad)
Ent=CreateVariable("tool quadrant",WsLatheVars,Quad)


Stat=SetInputFormat("n4=","")
Ncuts=NamedVarVal("number of cuts",WsLatheVars,4)
Stat=SetEqualsValue(Ncuts)
Ncuts=GetValue("NUMBER OF CUTS",Answer$)
IF ExitScript(Destination$) THEN Leave
Ent=CreateVariable("number of cuts",WsLatheVars,Ncuts)

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 PATH",0)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Contour) THEN Pick_loop
Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)


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=WsZigzagCutPen
ELSE
	Pen_color=WsStHolesPen
END IF
Stat= SetCurrentColor(Pen_color)

PlotOff
Shift_type= 1*(Long)+ 2*(NOT Long)
Path= LatheToolPtShift(Contour,Tnose_rad,Dia_allow,Len_allow,Shift_type,Id_contour,Quad,Reverse)
PlotOn
Stat= PlotEntity(Path)
Ps= ContStartPt(Path)
Pe= ContEndPt(Path)
Tline= LineTwoPoints(Ps,Pe)
Uncut_angle= (LineAngle(Tline)+720) MOD 360
Stat= DeleteEntity(Ps)
Stat= DeleteEntity(Pe)
Stat= DeleteEntity(Tline)
Dp_code=Depth_code
Cut_type$= VAL$(Quad)
Upath_num=1
Rough_path= TurnRough(Path,Upath_num,Xs,Ys,Ncuts,ABS(Tnose_rad),Uncut_angle,Id_contour,Dp_code,Cut_type$,Rough$)
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$)&"_MAN"&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$(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]="NUMBER OF CUTS: "&VAL$(Ncuts)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="UNDERCUT NUMBER: "&VAL$(Upath_num)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="DIA ALLOWANCE: "&VAL$(Dia_allow)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="LEN ALLOWANCE: "&VAL$(Len_allow)&CHR$(10)

	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 undercut")
        Stat= AddItemToUndo(WsUBCreateMode,Roughdata)
        Stat= AddGroupToUndo(WsUBCreateMode,Rough_path,1,WsTrue)

END IF
Deletepaths(Path)

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.)
	Unum=Quad
	Stat=SetEqualsValue(Unum)
	Stat=SetInputFormat("n4=","")
	Quad=GetValue("TOOL QUADRANT",Answer$)
	IF ExitScript(Destination$) THEN Leave
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

