 
! script name: lathe.d/roughgroove
! topic: create groove of specified width and depth and cut it

!A
!A NAME:
!A
!A 	Rough Groove
!A
!A DESCRIPTION:
!A
!A 	This script creates a path to rough cut a groove with specified
!A 	width and depth.
!A
!A~ image location - images/roughgrv.gif
!A
!B INSTRUCTIONS:
!B
!B	SELECT TOOL: Select the finish grooving tool.
!B
!B 	GROOVE WIDTH: Enter the width of the groove to be created.
!B
!B 	GROOVE DEPTH: Enter the depth of the groove to be created.
!B
!B 	DEPTH OF CUT: Enter the depth of cut (this distance will be
!B 		used on successive passes).
!B
!B 	GROOVE START POINT ABS X,Y: Enter the X,Y coordinates
!B 		for the start point of the groove.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to remain.
!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
!B 	WIDTH OF CUT: Enter the width between cuts.
!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
!# see also - points.d/xyzinput.hlp
!# see also - lathe.d/tooltiptan.hlp


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

DIM Destination$[256],Answer$[256],Datastr$[1024],Roughname$[80],Outline_name$[80]
True_mode$= ARGV(1)

ActivateUndo
Animate_tool=1
Current_pen= CurrentColor
Work= CurrentWorkplane
StdWorkplane
DEG

! Stat= SetXyzInput(WsLatheInput)

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

Picked_it=0
Pen_groove=20
Pen_roughpath=23
Chamfer_corners=1
Cut_type= WsSequentialCuts

Top_corner_rad= .03*Conv
Bottom_rad_def= .02*Conv
Chamfer_length_def= .05*Conv
Cutwidth_def= .15*Conv
Tlwd_def= .5*Conv
Grwd_def= .5*Conv
Depth_def= .5*Conv
Depth_of_cut_def= .2*Conv
Tnose_rad_def= .031*Conv
Allow_def= .04*Conv

Overcut=.25          !percent of tool width to cut on both sides of groove.
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=ToolNoseRadius(Tool)
		Quad=ToolQuadrant(Tool)
		Tlwd=ToolWidth(Tool)
		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)
Outside_cut= MAX(Tlwd*Overcut, Chamfer_length*1.1)

Ask_width:!
PlotOn
Picked_it=0
Groove_width=NamedVarVal("groove width",WsLatheVars,Grwd_def)
Stat=SetInputFormat("a99=","")
Stat=SetEqualsValue(0,VAL$(Groove_width))
Pick_loop:!
Rough_ent=PickEntity("PICK GROOVE, GROOVE WIDTH",Answer$)
IF ExitScript(Destination$) THEN Leave
IF ValidGeometry(Rough_ent) THEN
	Groovedata= GetAssocEnt(Rough_ent,WsGrooveData)
	IF ValidGeometry(Groovedata) THEN 
		Picked_it=1
		Groove_width=GetUserValue(Groovedata,"GROOVE WIDTH:")
		Groove_depth=GetUserValue(Groovedata,"GROOVE DEPTH:")
		Chamfer_corners=GetUserValue(Groovedata,"CHAMFER CORNERS:")
		Bottom_rad=GetUserValue(Groovedata,"BOTTOM RAD:")
		Top_rad=GetUserValue(Groovedata,"TOP RAD:")
		Chamfer_length=GetUserValue(Groovedata,"CHAMFER LENGTH:")
		GOSUB Get_xy
		Stat=GetUserString(Groovedata,"OUTLINE NAME:",Outline_name$)
		Outline_contour=FindNamedGrp(Outline_name$)
		IF NOT ValidGeometry(Outline_contour) THEN Picked_it=0
		Made_outline= 0
		GOTO Depth_of_cut
	END IF
	Groove_width=LineLength(Rough_ent)
	GOTO Set_vals
	
END IF

IF NOT LEN(Answer$) THEN Pick_loop
Groove_width= VAL("0"&TRIM$(Answer$))

Set_vals:IF (Groove_width<Tlwd) THEN Pick_loop
Ent=CreateVariable("groove width",WsLatheVars,Groove_width)

Stat=SetInputFormat("a99=","")
Groove_depth=NamedVarVal("groove depth",WsLatheVars,Depth_def)
Stat=SetEqualsValue(0,VAL$(Groove_depth))
Rough_ent=PickEntity("GROOVE DEPTH",Answer$)
IF ExitScript(Destination$) THEN Leave
IF ValidGeometry(Rough_ent) THEN
	Groove_depth=LineLength(Rough_ent)
ELSE
	Groove_depth= VAL("0"&TRIM$(Answer$))
END IF
Ent=CreateVariable("groove depth",WsLatheVars,Groove_depth)

Bottom_rad=NamedVarVal("bottom rad",WsLatheVars,Bottom_rad_def)
Stat=SetInputFormat("n4=","")
Stat=SetRadiusEquals(Bottom_rad)
Bottom_rad= InputRadius("GROOVE BOTTOM CORNER RADIUS")
IF ExitScript(Destination$) THEN Leave
Bottom_rad=ABS(Bottom_rad)
Ent=CreateVariable("bottom rad",WsLatheVars,Bottom_rad)

IF Chamfer_corners THEN
	Chamfer_length=NamedVarVal("chamfer length",WsLatheVars,Chamfer_length_def)
	Stat=SetInputFormat("a99=","")
	Stat=SetEqualsValue(0,VAL$(Chamfer_length))
	Chamfer_line= PickEntity("CHAMFER LENGTH",Answer$)
	IF ExitScript(Destination$) THEN Leave
	IF ValidGeometry(Chamfer_line) THEN
		Chamfer_length=LineLength(Chamfer_line)
	ELSE
		Chamfer_length=VAL("0"&TRIM$(Answer$))
	END IF
	Chamfer_length=ABS(Chamfer_length)
	Ent=CreateVariable("chamfer length",WsLatheVars,Chamfer_length)
ELSE
	Chamfer_length=0
	Top_rad=NamedVarVal("top rad",WsLatheVars,Top_rad_def)
	Stat=SetInputFormat("n4=","")
	Stat=SetRadiusEquals(Top_rad)
	Bottom_rad= InputRadius("GROOVE TOP CORNER RADIUS")
	IF ExitScript(Destination$) THEN Leave
	Top_rad=ABS(Top_rad)
	Ent=CreateVariable("top rad",WsLatheVars,Top_rad)
END IF

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)
IF Groove_width-Tlwd<Allow THEN Allow= MAX(0,Groove_width-Tlwd)
Groove_depth=ABS(Groove_depth)
IF Picked_it THEN After_startpt

Start_pt:!
LOOP
	Mode$= True_mode$
	Pt=XyzInput("GROOVE START POINT",WsXyOnly)
	IF ExitScript(Destination$) THEN Leave

	Ent= LastPickEnt
	IF ValidGeometry(Ent) THEN
		Groovedata= GetAssocEnt(Ent,WsGrooveData)
		IF ValidGeometry(Groovedata) THEN
			GOSUB Get_xy
			Stat= DeleteEntity(Pt)
		ELSE
			Rough_path= ParentGroup(Ent,0)
			IF ValidGeometry(Rough_path) THEN
				Groovedata= GetAssocEnt(Rough_path,WsGrooveData)
				IF ValidGeometry(Groovedata) THEN
					GOSUB Get_xy
					Stat= DeleteEntity(Pt)
				ELSE
					Gstart_pt=Pt
				END IF
			ELSE
				Gstart_pt=Pt
			END IF
		END IF
	ELSE
		Gstart_pt=Pt
	END IF
After_startpt:!
	IF SGN(YPointValue(Gstart_pt))=-1 THEN 
		IF Mode$="id" THEN
			Mode$="od"
		ELSE
			Mode$="id"
		END IF
	END IF
	Id_groove= (Mode$="id")
	Face_groove=(Mode$="face")
	GOSUB Do_groove
	IF Picked_it THEN Ask_width
	Picked_it=0
END LOOP

Do_groove:!
IF Picked_it THEN Rough_it

Outline_groove:!
PlotOff
Stat= SetCurrentColor(Pen_groove)
IF Chamfer_corners THEN
	Grv_type= WsChamferGroove
ELSE
	Grv_type= WsRadiusGroove
	Chamfer_length= Top_rad
END IF
Neg_dia= NegDiaCheck(Face_groove,Id_groove,Quad)
Grvdef= GrooveDefinition(Grv_type,Face_groove,Id_groove,Neg_dia,Groove_width,Groove_depth,Bottom_rad,Chamfer_length,0)
Outline_contour= TurnGrooveOutline(Grvdef,Gstart_pt)
Stat= DeleteEntity(Grvdef)
Made_outline=1
PlotOn

Rough_it:!
PlotOff
Stat= SetCurrentColor(Pen_roughpath)
IF Chamfer_corners THEN
        Grv_type= WsChamferGroove
ELSE
        Grv_type= WsRadiusGroove
        Chamfer_length= Top_rad
END IF
Neg_dia= NegDiaCheck(Face_groove,Id_groove,Quad)
Grvdef= GrooveDefinition(Grv_type,Face_groove,Id_groove,Neg_dia,Groove_width,Groove_depth,Bottom_rad,Chamfer_length,Outside_cut)

Stat= GrooveDefCutInfo(Grvdef,Cut_type,Quad,ABS(Tlwd),ABS(Tnose_rad),Cutwidth,Depth_of_cut,Allow,Clearance,Chip_clear)

Pgroup= RoughGroove(Grvdef,Gstart_pt)

Stat= DeleteEntity(Grvdef)
PlotOn
Stat= PlotEntity(Outline_contour)
Stat= PlotEntity(Pgroup)
P1= LthToolInterfere(Tool,Pgroup,Outline_contour)
IF ValidGeometry(P1) THEN
	Stat= DeleteEntity(P1)
ELSE
	IF Animate_tool THEN
		Work_trans= CurrentWorkplane
		Stat= AnimateTool(Tool,Pgroup,Work_trans)
	END IF
END IF
GOTO Record_groove


Record_groove:!
Stat= GroupName(Pgroup,Roughname$)
Stat= GroupName(Outline_contour,Outline_name$)
Roughdata=FindUserData(Roughname$,WsGrooveData)
IF ValidGeometry(Roughdata) THEN Stat= DeleteEntity(Roughdata)
Datastr$=" GROOVE DATA "&CHR$(10)&" "&CHR$(10)
Datastr$[LEN(Datastr$)+1]="TYPE:  rough-"&True_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,"OUTLINE NAME:",Outline_name$)
Stat= PutUserValue(Roughdata,"TOOL NOSE RAD:",Tnose_rad)
Stat= PutUserValue(Roughdata,"TOOL QUADRANT:",Quad)
Stat= PutUserValue(Roughdata,"TOOL WIDTH:",Tlwd)
Stat= PutUserValue(Roughdata,"CUT METHOD:",Cut_type)
Stat= PutUserValue(Roughdata,"CHAMFER CORNERS:",Chamfer_corners)
Stat= PutUserValue(Roughdata,"TOP RAD:",Top_rad)
Stat= PutUserValue(Roughdata,"BOTTOM RAD:",Bottom_rad)
Stat= PutUserValue(Roughdata,"CHAMFER LENGTH:",Chamfer_length)
Stat= PutUserValue(Roughdata,"FINISH ALLOWANCE:",Allow)
Stat= PutUserValue(Roughdata,"GROOVE WIDTH:",Groove_width)
Stat= PutUserValue(Roughdata,"GROOVE DEPTH:",Groove_depth)
Stat= PutUserValue(Roughdata,"WIDTH OF CUT:",Cutwidth)
Stat= PutUserValue(Roughdata,"DEPTH OF CUT:",Depth_of_cut)
Stat= PutUserValue(Roughdata,"CLEARANCE:",Clearance)
Stat= PutUserValue(Roughdata,"CHIP CLEARANCE:",Chip_clear)
Xs= XPointValue(Gstart_pt)
Ys= YPointValue(Gstart_pt)
Stat= PutUserValue(Roughdata,"STARTING X:",Xs)
Stat= PutUserValue(Roughdata,"STARTING Y:",Ys)

ActivateUndo
Stat= AdvanceUndoNumber("rough groove")
Stat= AddItemToUndo(WsUBCreateMode,Roughdata)
IF NOT Made_outline THEN
	Stat= AddGroupToUndo(WsUBModifyMode,Outline_contour,1,WsTrue)
END IF

Stat= AssocUserData(Pgroup,Roughdata,WsGrooveData)
Stat= AssocUserData(Outline_contour,Roughdata,WsGrooveData)
IF Tool_assoc THEN Stat= AssociateTool(Pgroup,Tool)

IF Made_outline THEN
	Stat= AddGroupToUndo(WsUBCreateMode,Outline_contour,1,WsTrue)
END IF
Stat= AddGroupToUndo(WsUBCreateMode,Pgroup,1,WsTrue)

Stat=DeleteEntity(Gstart_pt)
RETURN


Get_xy:!
Xs=GetUserValue(Groovedata,"STARTING X:")
Ys=GetUserValue(Groovedata,"STARTING Y:")
Gstart_pt=PointXYZ(Xs,Ys,0)
RETURN

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:!
PlotOn
Stat=SetCurrentColor(Current_pen)
IF ValidGeometry(Work) THEN Stat=SetWorkplane(Work)

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

