
! script name: voffsets.d/contourplungemill
! var_topic: plunge mill along contour.

!A
!A NAME:
!A
!A 	Plunge Mill		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This function will plunge mill around the periphery of a contour. This
!A	routine will offset the contour the radius of the plunge mill plus the
!A	finish allowance. The plunge cuts will then be located at the width of
!A	cut distance along the contour.
!A
!A~ image location - images/contourplunge.jpg
!B INSTRUCTIONS:
!B
!B	MILL CLimb OR Conventional: Milling direction around contour.
!B
!B	FINISH ALLOWANCE: Allowance remaining for finishing.
!B
!B	FINISH DEPTH: Depth to plunge to.
!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")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Boundary_name$[256]
DIM Tool_name$[256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Finish_var$[256]
DIM Depth_var$[256]
DIM Name$[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/contourplungemill"
  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/contourplungemill"
END IF

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

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


Mill_direction:!
Stat=SetInputFormat("a2=",",CL,C,")
Stat=NamedVarStr("mill direction",WsSystemVars,"CL",Answer$)
Stat=SetEqualsValue(0,Answer$)
Mill_dir=GetValue("MILL CLimb OR Conventional",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF (Answer$="C") THEN
	Mill_dir=WsConventional
	Pen_color=WsConvCutPen
	Dcomp_dir= WsRight
ELSE
	Mill_dir=WsClimb
	Pen_color=WsClimbCutPen
	Dcomp_dir= WsLeft
END IF
Stat=SetCurrentColor(Pen_color)

Fin_allow:!
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)
Stat=SetInputFormat("n4=","")
Finish_allow=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_allow)
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finish_allow)
No_finish_var= LastVariableInput(Finish_var$)

Pen_color= CurrentColor
Stat=SetCurrentColor(Pen_color)



Pick_loop:!
Stat=SetPickMask(WsPickNoSurf)
E1=PickEntity("PICK AN ELEMENT OF CONTOUR TO BE OFFSET")
IF (ExitScript(Destination$)) THEN Leave
Contour= ParentGroup(E1,0)
IF NOT ValidDependency(Contour) THEN Pick_loop
Stat= EntityNameCheck(Contour,Contour$)


Pt=InputFreePoint("PICK WHICH SIDE")
IF (ExitScript(Destination$)) THEN Leave

Side=OffsetLeftRight(E1,Pt)
Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)

Direction_sign=MillDirection(E1,Pt)
Dist=-Side*Offset

Direction=WsForward
IF (Direction_sign=1)AND(Mill_dir=WsConventional) THEN
	Direction=WsReverse
END IF
IF (Direction_sign=-1)AND(Mill_dir=WsClimb) THEN
	Direction=WsReverse
END IF
Stat=DeleteEntity(Pt)

Finish_depth:!
Finish_depth=NamedVarVal("finish depth",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_depth)
Stat=SetInputFormat("n4=","")
Finish_depth=GetValue("FINISH DEPTH",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_depth_var= LastVariableInput(Depth_var$)
Vdepth=CreateVariable("finish depth",WsSystemVars,Finish_depth)

Stat=SetCurrentColor(Pen_color)
Rough_stock= 0.0
Width_cut= Cutter_dia/5
Side1= -Side

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("MILL: Plunge rough "&Tool_name$)
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
V0= FreeParameter("Bottom allowance",0)
IF No_finish_var THEN
	V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
	V1= FreeParameter("Finish allowance",Finish_var$)
END IF
V2= FreeParameter("Rough stock",Rough_stock)
V3= FreeParameter("Width cut",Width_cut)
V4= FreeParameter("Side",Side1)
IF No_depth_var THEN
	V5= FreeParameter("Finish depth",Finish_depth)
ELSE
	V5= FreeParameter("Finish depth",Depth_var$)
END IF
V7= FreeParameter("Zstart",0.100)
V8= FreeParameter("Leadout",Leadout_dist)
V9= FreeParameter("Method",1)
Tool=FindUserData(Tool_name$,WsToolData)
IF Mill_dir= WsConventional THEN
	Mdef1= ToolMillingDef(Tool,WsConventional)
ELSE
	Mdef1= ToolMillingDef(Tool,WsClimb)
END IF
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,V4,V3,V1,V2,1)
Stat= MillDefZInfo(Mdef1,1,V5,0,V0)
Paths=MillContour(Contour$,Mdef1,WsFullLook,WsCircleMoves)
Plunge_cpaths= PlungeMill(Paths,Mdef1,V7,V8,WsForward,V9)
Stat=AssociateTool(Plunge_cpaths,Tool)
Stat= DeleteGroup(Paths,WsTrue)
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 Pick_loop

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

