
! script name: lathe.d/roughgrcont
! topic: given a contour, produce a groove roughing path (radial style)

!A
!A NAME:
!A
!A 	Rough groove a contour with a stock contour.
!A
!A DESCRIPTION:
!A
!A 	This script creates a pathgroup to rough cut a contour with a
!A 	grooving tool. The stock contour is required and limits
!A	the cutting motion to between the stock contour plus clearance,
!A	and the groove contour. 
!A
!A~ image location - images/rghcontgrv.gif
!A
!B INSTRUCTIONS:
!B
!B	SELECT TOOL: Select the groove roughing tool.
!B
!B 	DEPTH OF CUT: Enter the depth of cut (this distance will be
!B	used on successive passes).
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to remain.
!B
!B	PICK CONTOUR: Select the groove contour.
!B
!B	STOCK CONTOUR: Select the stock contour.
!B
!B	If the tool is not selected the following questions will 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
!B 	WIDTH OF CUT: Enter the width of cut.
!B
!C NOTES:
!C
!C	There are two methods of cutting the groove:
!C
!C		Cut_type= WsSequentialCuts The cuts are made in a right to
!C		                left or left to right direction.
!C
!C		Cut_type= WsAlternateCuts  The cuts alternate from side
!C					   to side.
!C
!C	The groove contour must be "groove like". If the contour has
!C	bumps or indentations which deviate from a "groove like" shape
!C	then this proceedure may fail. 
!C	At each cut location this routine calculates the 
!C	first intersection of the leading and trailing edge of a 
!C	standard shaped grooving tool. It will then generate the cut 
!C	to that location.
!C	The cuts are generated in such a way as to insure cuts along
!C	walls which are vertical to the groove.
!C
!C
!# see also - lathe.d/tooltiptan.hlp

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

DIM Destination$[256],Answer$[256],Datastr$[4096],Roughname$[256],Contour_name$[256],Stock_name$[256]
Mode$= ARGV(1)

ActivateUndo
Current_pen= CurrentColor
Work= CurrentWorkplane
StdWorkplane
DEG

! Stat= SetXyzInput(WsLatheInput)

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

Animate_tool=1
Rapid_linetype= 8
Pen_roughpath=23
!   Cut_type= WsSequentialCuts
Cut_type= WsAlternateCuts

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

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")

Set_vals:!

Depth_of_cut:Stat=SetInputFormat("n4=","")
Depth_of_cut=NamedVarVal("depth of cut",WsLatheVars,Depth_of_cut_def)
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)

Allow=NamedVarVal("finish allowance",WsSystemVars,Allow_def)
Stat=SetEqualsValue(Allow)
Allow=GetValue("FINISH ALLOWANCE",Answer$)
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
END IF
IF NOT ValidGeometry(Contour) THEN Pick_loop
Closed=IsContClosed(Contour)
IF Closed THEN Pick_loop

Spick_loop:!
Stat= SetInputFormat("n4","")
Stock_contour= PickGroupEnt("PICK STOCK CONTOUR",0)
IF (ExitScript(Destination$)) THEN 
	GOTO Leave
END IF
IF NOT ValidGeometry(Stock_contour) THEN Spick_loop
Closed=IsContClosed(Stock_contour)
IF Closed THEN Pick_loop

Rough_it:!
Stat=SetCurrentColor(Pen_roughpath)
PlotOff
Neg_dia= NegDiaCheck(Face_groove,Id_groove,Quad)
Grvdef= GrooveDefinition(WsChamferGroove,Face_groove,Id_groove,Neg_dia,0.0,0.0,0.0,0.0,0.0)
Stat= GrooveDefCutInfo(Grvdef,Cut_type,Quad,ABS(Tlwd),ABS(Tnose_rad),Cutwidth,Depth_of_cut,Allow,Clearance,Chip_clear)
Path_group= GrooveRoughCont(Grvdef,Contour,Stock_contour)
Stat= DeleteEntity(Grvdef)
PlotOn
Stat= PlotEntity(Path_group)
Stat= AssociateTool(Path_group,Tool)
P1= LthToolInterfere(Tool,Path_group,Contour)
IF ValidGeometry(P1) THEN
	Stat= DeleteEntity(P1)
ELSE
	IF Animate_tool THEN
		Work_trans= CurrentWorkplane
		Stat= AnimateTool(Tool,Path_group,Work_trans)
	END IF
END IF

Record_groove:!
Stat= GroupName(Path_group,Roughname$)
Stat= GroupName(Contour,Contour_name$)
Stat= GroupName(Stock_contour,Stock_name$)
Roughdata=FindUserData(Roughname$,WsGrooveData)
IF ValidGeometry(Roughdata) THEN Stat= DeleteEntity(Roughdata)
Datastr$=" GROOVE DATA "&CHR$(10)&" "&CHR$(10)
Datastr$[LEN(Datastr$)+1]="TYPE:  roughcontour-"&Mode$&CHR$(10)
Datastr$[LEN(Datastr$)+1]="CALCULATE TYPE:  "&Mode$&CHR$(10)
Roughdata=ReadUserData(WsGrooveData,Roughname$,Datastr$)

Stat= PutUserString(Roughdata,"NAME:",Roughname$)
Stat= PutUserString(Roughdata,"CONTOUR NAME:",Contour_name$)
Stat= PutUserString(Roughdata,"STOCK NAME:",Stock_name$)
Stat= PutUserValue(Roughdata,"TOOL NOSE RAD:",Tnose_rad)
Stat= PutUserValue(Roughdata,"TOOL QUADRANT:",Quad)
Stat= PutUserValue(Roughdata,"TOOL WIDTH:",Tlwd)
Stat= PutUserValue(Roughdata,"FINISH ALLOWANCE:",Allow)
Stat= PutUserValue(Roughdata,"WIDTH OF CUT:",Cutwidth)
Stat= PutUserValue(Roughdata,"DEPTH OF CUT:",Depth_of_cut)
Stat= PutUserValue(Roughdata,"CUT METHOD:",Cut_type)
Stat= PutUserValue(Roughdata,"CLEARANCE:",Clearance)
Stat= PutUserValue(Roughdata,"CHIP CLEARANCE:",Chip_clear)

ActivateUndo
Stat= AdvanceUndoNumber("rough groove")
Stat= AddItemToUndo(WsUBCreateMode,Roughdata)
Stat= AddGroupToUndo(WsUBModifyMode,Contour,1,WsTrue)

Stat= AssocUserData(Path_group,Roughdata,WsGrooveData)
Stat= AssocUserData(Contour,Roughdata,WsGrooveData)

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

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

