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

!A
!A NAME:
!A
!A 	Autoundercut: undercut roughing cycle
!A
!A DESCRIPTION:
!A
!A 	The tool clearance angle is used to determine uncutable
!A 	areas of the starting contour, called undercuts.  These
!A	undercuts are roughed out by successive paths where the
!A	depth of cut is determined by the amount of material to
!A	remove and the number of cuts requested.
!A
!A~ image location - images/undercuts.gif
!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 PATH: Two possibilities.
!B
!B 		1) Pick the contour to be roughed.  This will force a tool
!B 		   clearance angle question to define the undercuts.
!B 		2) Pick a previously defined roughing path.  This will use the
!B 		   data associated with the rough path to determine undercuts.
!B
!B 	TOOL CLEARANCE ANGLE: Enter the cutting edge clearance
!B 		angle of the tool that left the undercut.
!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 	lathe.d/setvals sets user definable constants.
!C
!# see also - lathe.d/dialenfin.hlp
!# see also - lathe.d/tooltiptan.hlp

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

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:autoundercut"

ActivateUndo
Restart:!

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)

Roughdata= GetAssocEnt(Contour,WsTurnRough)
IF ValidGeometry(Roughdata) THEN
	Uncut_angle=GetUserValue(Roughdata,"CLEARANCE ANGLE:")
	Stat=GetUserString(Roughdata,"CONTOUR:",Contour$)
	Contour=FindNamedGrp(Contour$)
	IF NOT ValidGeometry(Contour) THEN
!  error condition
		GOTO Pick_loop
	END IF
ELSE
	GOSUB Tclr
END IF
Ent=CreateVariable("tool clearance angle",WsLatheVars,Uncut_angle)

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,Uncut_angle)
PlotOn
Stat= PlotEntity(Path)

Upath= PathNoUnders(Path,Uncut_angle,Id_contour,Rough$)

Dp_code=Depth_code
Cut_type$= VAL$(Quad)
Upath_num=0
LOOP
	Upath_num= 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

	ActivateUndo
        Stat= AdvanceUndoNumber("auto undercut")

EXIT IF NOT ValidGeometry(Rough_path)

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

	Stat= AddItemToUndo(WsUBCreateMode,Roughdata)
	Stat= AddGroupToUndo(WsUBCreateMode,Rough_path,1,WsTrue)

END LOOP
Deletepaths(Path)
Deletepaths(Upath)

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

Tclr:!
	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=ABS(Uncut_angle)
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

