
! script name: lathe.d/finishgrvcont
! topic: given a contour, produce a groove finish path.

!A
!A NAME:
!A
!A 	Finish groove a contour.
!A
!A DESCRIPTION:
!A
!A 	This script creates a pathgroup to finish cut a contour with a
!A 	grooving tool. The contour must be "groove like", that is
!A	for an Od groove that the diameter decreases and then it
!A	increases.
!A
!A~ image location - images/fingrvcont.gif
!A
!B INSTRUCTIONS:
!B
!B	SELECT TOOL: Select the finish grooving tool.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to remain.
!B
!B 	PICK CONTOUR: Select the contour to be grooved.
!B
!B	SPLIT POINT:  Input or Pick the point that the tool will switch sides.  
!B		      	The split point plus an overlap distance determine
!B			where the bottom center of the groove tool will stop.
!B
!B	If the tool is not selected the following questions appear:
!B
!B	TOOL WIDTH: Enter the width of the grooving tool.
!B
!B	TOOL NOSE RADIUS: Enter the tool's corner radius.
!B
!B	TOOL QUADRANT: Enter the tool nose cutting quadrant.
!B
!C NOTE:
!C	
!C	The finish cut will start cutting the contour with one side of the 
!C	tool until it its center reaches the split point plus the overlap.
!C	It will then retract, go to the other end of the
!C	contour, and cut with the other side of the tool down to the
!C	where the last cut left off, plus the overlap.
!C	The grooving tool must fit in the groove. This cycle will not
!C	produce a correct path if the tool cannot fit in the location
!C	of the split point.
!C


Stat=ChangeLabelStr("ScriptNameStatus","Function: finish groove contour ")

DIM Destination$[256],Answer$[256],Datastr$[4096],Finishname$[80],Contour_name$[80],Stock_name$[80]

Mode$= ARGV(1)
Current_pen= CurrentColor
Work= CurrentWorkplane
StdWorkplane
DEG

ActivateUndo
Start_script:!

Animate_tool=1
Units= CurrentUnits
IF Units= WsMetric THEN
	Conv= 25.4
ELSE
	Conv= 1.
END IF

Rapid_linetype= 8
Pen_finpath=21

Cutwidth_def= .15*Conv
Tlwd_def= .5*Conv
Depth_of_cut_def= .2*Conv
Tnose_rad_def= .031*Conv
Allow_def= .04*Conv
Stock_allow= .04*Conv

Clearance= .05*Conv
Chip_clear= .01*Conv
Overlap= .02*Conv
Lead_dist= .05*Conv
Lead_in= 5  ! tangent lead into groove 

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("GROOVING 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:")
		Tlwd=GetUserValue(Tool,"Tool width:")
		Cutwidth=GetUserValue(Tool,"Width of cut:")
		IF NOT Cutwidth THEN Cutwidth= Cutwidth_def
		IF NOT Tlwd THEN Tlwd= Tlwd_def
		LowerSelectBox
	END IF
END IF

Ent=CreateVariable("tool nose radius",WsLatheVars,Tnose_rad)
Ent=CreateVariable("tool width",WsLatheVars,Tlwd)
Ent=CreateVariable("tool quadrant",WsLatheVars,Quad)
Ent=CreateVariable("cut width",WsLatheVars,Cutwidth)

IF (Mode$="") THEN
	Stat= SetEqualsValue(0,"O")
	Stat=SetInputFormat("a1=",",O,I,F,")
	Stat=GetValue("GROOVE TYPE Od, Id, Face",Mode$)
	IF (ExitScript(Destination$)) THEN Leave
Set_mode:SELECT Mode$
	CASE "O"
		Mode$="od"
	CASE "I"
		Mode$="id"
	CASE "F"
		Mode$="face"
	CASE ELSE
		Moder$=Mode$
		Mode$=UPC$(Mode$)
		GOTO Setmode
	END SELECT
END IF
Face_groove=(Mode$="face")
Id_groove= (Mode$= "id")
Face_test=(Mode$<>"face")
Side= (-1)^((Face_groove<>0) OR (Mode$="od"))
Id_test= (-1)^(Mode$="id")

Set_vals:!
Allow=NamedVarVal("finish allowance",WsSystemVars,Allow_def)
Stat= SetInputFormat("n4=","")
Stat=SetEqualsValue(Allow)
Allow=GetValue("FINISH ALLOWANCE")
IF ExitScript(Destination$) THEN Leave
Ent=CreateVariable("finish allowance",WsSystemVars,Allow)

Pick_loop:!
Stat= SetInputFormat("n4a1",",T,")
Contour= PickGroupEnt("PICK CONTOUR, Tool ",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "T" THEN 
	Mode$=""
	GOTO Start_script
END IF
IF NOT ValidGeometry(Contour) THEN Pick_loop
Closed=IsContClosed(Contour)
IF Closed THEN Pick_loop


PlotOff
Xs= XPointValue(Psplit)
Ys= YPointValue(Psplit)
Stat=SetCurrentColor(Pen_finpath)
Neg_dia= NegDiaCheck(Face_groove,Id_groove,Quad)
Grvdef= GrooveDefinition(WsChamferGroove,Face_groove,Id_groove,Neg_dia,0,0,0,0,0)
Stat= GrooveDefCutInfo(Grvdef,Cut_type,Quad,ABS(Tlwd),ABS(Tnose_rad),0,0,Allow,Clearance,Chip_clear)
Path= GrooveFinishCont(Grvdef,Contour,0,Overlap,Lead_in,Lead_dist)
PlotOn
Stat= DeleteEntity(Psplit)
Stat= DeleteEntity(Grvdef)
Stat= AssociateTool(Path,Tool)
Stat= PlotEntity(Path)
P1= LthToolInterfere(Tool,Path,Contour)
IF ValidGeometry(P1) THEN
	Stat= DeleteEntity(P1)
ELSE
	IF Animate_tool THEN
		Work_trans= CurrentWorkplane
		Stat= AnimateTool(Tool,Path,Work_trans)
	END IF
END IF

Record_groove:!
IF ValidGeometry(Path) THEN
Stat= GroupName(Path,Finishname$)
Stat= GroupName(Contour,Contour_name$)
Finishdata=FindUserData(Finishname$,WsGrooveData)
IF ValidGeometry(Finishdata) THEN Stat= DeleteEntity(Finishdata)
Datastr$=" GROOVE DATA "&CHR$(10)&" "&CHR$(10)
Datastr$[LEN(Datastr$)+1]="TYPE:  finishcontour-"&Mode$&CHR$(10)
Datastr$[LEN(Datastr$)+1]="CALCULATE TYPE:  "&Mode$&CHR$(10)
Finishdata=ReadUserData(WsGrooveData,Finishname$,Datastr$)

Stat= PutUserString(Finishdata,"NAME:",Finishname$)
Stat= PutUserString(Finishdata,"CONTOUR NAME:",Contour_name$)
Stat= PutUserValue(Finishdata,"TOOL NOSE RAD:",Tnose_rad)
Stat= PutUserValue(Finishdata,"TOOL QUADRANT:",Quad)
Stat= PutUserValue(Finishdata,"TOOL WIDTH:",Tlwd)
Stat= PutUserValue(Finishdata,"FINISH ALLOWANCE:",Allow)
Stat= PutUserValue(Finishdata,"WIDTH OF CUT:",Cutwidth)
Stat= PutUserValue(Finishdata,"DEPTH OF CUT:",Depth_of_cut)
Stat= PutUserValue(Finishdata,"CUT METHOD:",Cut_type)
Stat= PutUserValue(Finishdata,"OVERLAP CUTS:",Overlap)
Stat= PutUserValue(Finishdata,"CLEARANCE:",Clearance)
Stat= PutUserValue(Finishdata,"CHIP CLEARANCE:",Chip_clear)
Stat= PutUserValue(Finishdata,"LEAD IN TYPE:",Lead_in)
Stat= PutUserValue(Finishdata,"LEAD IN DIST:",Lead_dist)
Stat= PutUserValue(Finishdata,"STARTING X:",Xs)
Stat= PutUserValue(Finishdata,"STARTING Y:",Ys)


ActivateUndo
Stat= AdvanceUndoNumber("finish contour grv")
Stat= AddGroupToUndo(WsUBModifyMode,Contour,1,WsTrue)

Stat= AssocUserData(Path,Finishdata,WsGrooveData)
Stat= AssocUserData(Contour,Finishdata,WsGrooveData)

Stat= AddGroupToUndo(WsUBCreateMode,Path,1,WsTrue)


END IF

GOTO Pick_loop



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

	Tlwd=NamedVarVal("tool width",WsLatheVars,Tlwd_def)
	Stat=SetInputFormat("n4=","")
	Stat=SetEqualsValue(Tlwd)
	Tlwd=GetValue("TOOL WIDTH",Answer$)
	IF ExitScript(Destination$) THEN Leave

	Stat=SetInputFormat("a1=",",1,2,3,4,")
	Quad=NamedVarVal("tool quadrant",WsLatheVars,1+3*(Mode$="id"))
	Stat=SetEqualsValue(0,VAL$(Quad))
	Stat=GetValue("TOOL QUADRANT",Answer$)
	Quad=VAL(Answer$)
	IF ExitScript(Destination$) THEN Leave

	Stat=SetInputFormat("n4=","")
	Cutwidth=NamedVarVal("cut width",WsLatheVars,Cutwidth_def)
	Stat=SetEqualsValue(Cutwidth)
	Cutwidth=GetValue("WIDTH OF CUT",Answer$)
	IF ExitScript(Destination$) THEN Leave
	Cutwidth=ABS(Cutwidth)

RETURN

Leave:!
Stat=SetCurrentColor(Current_pen)
IF ValidGeometry(Work) THEN Stat=SetWorkplane(Work)

IF NOT LEN(Destination$) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END

