
! script name: voffsets.d/plungemillalongcurve
! var_topic: plunge mill along curve.

!A
!A NAME:
!A
!A 	Plunge Mill along Curve		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This function will plunge mill along a curve. The curve may be a contour
!A	or a 3d cutterpath. It is assumed that the tool offset is built into
!A     	the curve with an appropriate finish allowance.
!A	The curve may be generated by 3d cutting routines.
!A
!B INSTRUCTIONS:
!B
!B
!C NOTES:
!C
!C	There are three methods available to this cycle:
!C		Method=1 : Tool pulls away to left or right side normal to contour.
!C		Method=2 : Tool plunges at slight angle then retracts.
!C		Method=3 : Tool pulls away along contour tangent.
!C


stat=ChangeLabelStr("ScriptNameStatus","Function: plunge mill along curve")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Zstart_var$[256]
DIM Width_var$[256]
DIM Name$[256]
DIM Cpath$[256]

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

ActivateUndo
Current_pen=CurrentColor

Leadout_dist= 0.100*(25.4^(CurrentUnits=WsMetric))
IF ARGC>1 THEN
	Answer$= ARGV(1)
	GOTO Have_tool
END IF

Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:voffset.d/plungemillalongcurve"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("PATH 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 "vtools.d/readmilltool:voffset.d/plungemillalongcurve"
END IF

Have_tool:!
Tool_name$= Answer$
Tool=FindUserData(Answer$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Toolid$)
Cutter_dia=GetUserValue(Tool,"Diameter:")
LowerSelectBox


Width_cut:!
Width_cut= 0
Width_val=FindNamedVar("factor for width of cut",WsToolVars)
IF ValidGeometry(Width_val) THEN
	Width_factor=GetVariableVal(Width_val)
	Width_cut=Cutter_dia*Width_factor
END IF
IF NOT Width_cut THEN Width_cut=NamedVarVal("width of cut",WsSystemVars,0.0)

Stat=SetEqualsValue(Width_cut)
Stat=SetInputFormat("n4=","")
Width_cut=GetValue("WIDTH OF CUT",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Width_cut=ABS(Width_cut)
Vwidth=CreateVariable("width of cut",WsSystemVars,Width_cut)
No_width_var= LastVariableInput(Width_var$)

Pull_away:!
Stat=SetEqualsValue(0,"L")
Stat=SetInputFormat("a1=",",L,R,T,")
Stat=GetValue("PULL AWAY: Right, Left, Tangent ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "L"
	Side=-1
	Method=1
CASE "R"
	Side=1
	Method=1
CASE "T"
	Side=0
	Method=3
END SELECT

Pick_loop:!
E1=PickEntity("PICK AN ELEMENT OF CURVE")
IF (ExitScript(Destination$)) THEN Leave
Etype1= EntityType(E1)
IF Etype1= WsCPathFileEnt THEN
   IF NOT ValidDependency(E1) THEN Pick_loop
   Stat= EntityNameCheck(E1,Cpath$)
   Along_cpath=1
ELSE
   Contour= ParentGroup(E1,0)
   IF NOT ValidDependency(Contour) THEN Pick_loop
   Stat= EntityNameCheck(Contour,Contour$)
   Along_cpath=0
END IF


Zstart:!
Stat=SetInputFormat("n4=","")
Zstart=GetValue("STARTING Z FOR PLUNGE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_z_var= LastVariableInput(Zstart_var$)


Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("MILL: Plunge along curve "&Tool_name$)
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
IF No_width_var THEN
	V3= FreeParameter("Width cut",Width_cut)
ELSE
	V3= FreeParameter("Width cut",Width_var$)
END IF
IF No_z_var THEN
	V7= FreeParameter("Z start",Zstart)
ELSE
	V7= FreeParameter("Z start",Zstart_var$)
END IF
V8= FreeParameter("Leadout",Leadout_dist)
V4= FreeParameter("Side",Side)
V9= FreeParameter("Method",method)
V10= FreeParameter("Direction",1)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,0)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,V4,V3,0,0,1)
Stat= MillDefZInfo(Mdef1,1,0,0,0)
IF Along_cpath THEN
Plunge_cpaths= PlungeMillCPath(Cpath$,Mdef1,V7,V8,V10,V9)
ELSE
Plunge_cpaths= PlungeMill(Contour$,Mdef1,V7,V8,V10,V9)
END IF
Stat=AssociateTool(Plunge_cpaths,Tool)
Stat= NameEntity(Plunge_cpaths,Name$)
Stat= UnAssocMachDef(Plunge_cpaths,Mdef1)
Stat=DeleteEntity(Mdef1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Plunge_cpaths) THEN 
	DelLastVarRecord
ELSE
	Stat= AnimateTool(Tool,Plunge_cpaths)
	ActivateUndo
	Stat= AdvanceUndoNumber("Plunge mill")
	Stat= AddItemToUndo(WsUBCreateMode,Plunge_cpaths)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Pull_away

Leave:!
Stat=SetPickMask(WsPickGeometry)
LowerSelectBox
Stat=DeleteEntity(Pt)
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(Pt)
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END

