
! script name: wscturn.d/roughgrvcont
! var_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
!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]
DIM Tool_name$[256]
DIM Destination$[256]
DIM Answer$[256]
DIM Toolid$[256]
DIM Contour$[256]
DIM SContour$[256]
DIM Finish_var$[256]
DIM Dcut_var$[256]
DIM Name$[256]
INTEGER Id_contour


Current_pen= CurrentColor
Work= CurrentWorkplane
!   StdWorkplane
DEG

ActivateUndo
Var_object= CurrentVarObject
IF NOT ValidGeometry(Var_object) THEN
        Var_object=OpenVarObject("Program")
END IF
! Var_object= CheckVarObject
! IF (ExitScript(Destination$)) THEN Leave

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

Animate_tool=0
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
Stat=SetXyzInput(WsLatheInput)

IF ARGC>1 THEN
        Answer$= ARGV(1)
        GOTO Have_tool
END IF

Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllGroovers)
IF Stat THEN
  LOAD "wscturn.d/readlathetool:wscturn.d/roughgrvcont"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("GROOVING TOOL, New_tool ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
        LowerSelectBox
        GOTO Leave
END IF
IF Answer$="New_tool" THEN
        LowerSelectBox
        LOAD "wscturn.d/readlathetool:wscturn.d/roughgrvcont"
END IF

Have_tool:!
Tool_name$= Answer$


Tool=FindUserData(Tool_name$,WsToolData)
IF NOT ValidGeometry(Tool) THEN Start
Stat=GetUserString(Tool,"Tool type:",Toolid$)
Tnose_rad=ToolNoseRadius(Tool)
Quad=ToolQuadrant(Tool)
Tlwd=ToolWidth(Tool)
Cutwidth=ToolCutWidth(Tool)
IF NOT Cutwidth THEN Cutwidth= Cutwidth_def
IF NOT Tlwd THEN Tlwd= Tlwd_def

Ask_mode:!
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")

Depth_of_cut=NamedVarVal("depth of cut",WsLatheVars,Depth_of_cut_def)
Stat=SetInputFormat("n4=","")
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)
No_dcut_var= LastVariableInput(Dcut_var$)
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
No_finish_var= LastVariableInput(Finish_var$)
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 Restart
END IF
IF NOT ValidDependency(Contour) THEN Pick_loop
Stat= EntityNameCheck(Contour,Contour$)
Closed=IsContClosed(Contour)
IF Closed THEN Pick_loop

Spick_loop:!
Stat= SetInputFormat("a1",",N,")
Stock_contour= PickGroupEnt("STOCK CONTOUR, None ",0,Answer$)
IF (ExitScript(Destination$)) THEN 
	GOTO Leave
END IF
IF Answer$= "N" THEN
	Has_stock=0
ELSE
	IF NOT ValidDependency(Stock_contour) THEN Spick_loop
	Stat= EntityNameCheck(Stock_contour,SContour$)
	Closed=IsContClosed(Stock_contour)
	IF Closed THEN Pick_loop
	Has_stock=1
END IF
Stat=SetInputFormat("n4","")

Rough_it:!
Stat=SetCurrentColor(Pen_roughpath)

Groove_cycle:!
PlotOff
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("LATHE CONTOUR GROOVE: Rough "&Mode$)
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
IF Has_stock THEN
	Stat= NullReference(SContour$)
END IF
Tool=FindUserData(Tool_name$,WsToolData)
V1= FreeParameter("Tool nose radius",ToolNoseRadius(Tool))
V2= FreeParameter("Tool quadrant",ToolQuadrant(Tool))
V3= FreeParameter("Face groove",Face_groove)
V4= FreeParameter("Id groove",Id_groove)
IF No_dcut_var THEN
         V5= FreeParameter("Depth of cut",Depth_of_cut)
ELSE
         V5= FreeParameter("Depth of cut",Dcut_var$)
END IF
IF No_finish_var THEN
         V6= FreeParameter("Finish allowance",Allow)
ELSE
         V6= FreeParameter("Finish allowance",Finish_var$)
END IF
V7= FreeParameter("Width of cut",Cutwidth)
V8= FreeParameter("Cut type",Cut_type)
V9= FreeParameter("Chip clearance",Chip_clear)
V10= FreeParameter("Tool width",ToolWidth(Tool))
V11= FreeParameter("Tool clearance",Clearance)
V14= FreeParameter("Negative diameter",NegDiaCheck(V3,V4,V2))
V13= FreeParameter("Rough path color",Pen_roughpath)
V15= FreeParameter("Collision point color",7)
Gdef= GrooveDefinition(WsChamferGroove,V3,V4,V14,0.0,0.0,0.0,0.0,0.0)
Stat= GrooveDefCutInfo(Gdef,V8,V2,V10,V1,V7,V5,V6,V11,V9)
IF Has_stock THEN
	Path= GrooveRoughCont(Gdef,Contour$,Scontour$)
ELSE
	Path= GrooveRoughCont(Gdef,Contour$)
END IF
Stat= DeleteEntity(Gdef)
Stat= SetColor(Path,V13)
Stat= AutoNameGroup(Path,Name$)
Stat= AssociateTool(Path,Tool)
P1= LthToolInterfere(Tool,Path,Contour$)
Stat= SetColor(P1,V15)
Stat= SetLineType(P1,11)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
PlotOn
IF NOT ValidGeometry(Path) THEN
      DelLastVarRecord
      Stat= MessageStringBox("NO PATH CREATED: TOOL DOES NOT FIT","ATTENTION")
ELSE
        ActivateUndo
        Stat= AdvanceUndoNumber("rough cont groove")
        Stat= AddGroupToUndo(WsUBCreateMode,Path,1,WsTrue)
        Stat= AddItemToUndo(WsUBCreateMode,P1)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)

        Stat= PlotEntity(Contour)
        Stat= PlotEntity(Stock_contour)
        Stat= PlotEntity(Path)
        Stat= PlotEntity(P1)
        IF Animate_tool AND NOT ValidGeometry(P1) THEN
                 Work_trans= CurrentWorkplane
                 Stat= AnimateTool(Tool,Path)
        END IF
END IF

GOTO Pick_loop

Leave:!
Stat=SetInputFormat("n4","")
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

