
! script name: voffsets.d/finishboundary
! var_topic: finish mill boundary contours with full look ahead and leadin move

!A
!A NAME:
!A
!A 	Finish Boundary		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script generates finish cuts along the contours of a boundary.
!A
!A~ image location - images/finishboundary.jpg
!B INSTRUCTIONS:
!B
!B 	MILL CLimb OR Conventional:
!B
!B 		C - Conventional mill will generate a path which is
!B 		    clockwise for an interior cut and counterclockwise
!B 		    for an exterior cut.
!B 		CL - Climb mill will generate a path which is clockwise for
!B 		    an exterior cut and counterclockwise for an interior cut.
!B
!B 	CUTTER DIAMETER: Enter the diameter of
!B 		the cutter to be used.
!B
!B 	FINISH ALLOWANCE: Enter the amount of
!B 		material to be left for a finish pass.
!B
!B 	PICK AN ELEMENT OF BOUNDARY: Pick an
!B 		element on the boundary.
!B
!C NOTES:
!C
!C 	The cutter offset path is calculated from the cutter diameter and
!C 	the finish allowance.  (offset=.5*cutter_diameter + finish_allowance)
!C
!C	The switch Ext_sw can be used to control which boundary contours
!C	are machined.
!C		Ext_sw= 0  Inside Islands Only
!C		Ext_sw= 1  Inside Islands and Outside Exterior
!C		Ext_sw= 2  Outside Exterior Only
!C		Ext_sw= 3  Inside Exterior and Outside Islands
!C		Ext_sw= 4  Outside Islands Only
!C		Ext_sw= 5  Inside Exterior Only
!C


stat=ChangeLabelStr("ScriptNameStatus","Function: finish boundary")

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]
REAL Leadintype
REAL Leadouttype

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

ActivateUndo
Units= CurrentUnits

IF Units= WsMetric THEN
        Dist= 12.5
        Radius= 6.25
ELSE
        Dist= 0.5
        Radius= 0.25
END IF

LeadinVarsType= 20011
Leadintype=NamedVarVal("leadin type",LeadinVarsType,1.0)
Leadouttype=NamedVarVal("leadout type",LeadinVarsType,1.0)
Leadindist=NamedVarVal("leadin dist",LeadinVarsType,Dist)
Leadoutdist=NamedVarVal("leadout dist",LeadinVarsType,leadindist)
Rin=NamedVarVal("leadin radius",LeadinVarsType,Radius)
Rout=NamedVarVal("leadout radius",LeadinVarsType,Radius)

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

Start:!
Current_pen=CurrentColor

Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:voffset.d/finishboundary"
  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/finishboundary"
END IF

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

Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques
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:!
Boundary_loop:!
Stat=SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n4","")
Contour=PickGroupEnt("PICK CONTOUR IN BOUNDARY")
IF (ExitScript(Destination$)) THEN Leave
Pocket_boundary= FindBoundary(Contour)
IF (NOT ValidDependency(Pocket_boundary)) THEN Start
Stat= EntityNameCheck(Pocket_boundary,Boundary$)
Stat= SetSelectedFlag(Pocket_boundary,WsTrue)
Stat= PlotEntity(Pocket_boundary)


Ext_sw=1
Leadinside= 1
Direction=WsForward
Direction_sign=1
Direction=WsForward
IF Mill_dir=WsConventional THEN
        Leadinside= -1
ELSE
        Leadinside= 1
END IF

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= 0.0
Side1= -Side

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("MILL: Finish with leadin "&Tool_name$)
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
V6= FreeParameter("Depth cut",Depth_cut)
V7= FreeParameter("Exterior sw",Ext_sw)
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,V6,V0)
Paths=MillBdyContours(Boundary$,Mdef1,WsFullLook,V7,WsCircleMoves)
Stat= UnAssocMachDef(Paths,Mdef1)
Stat=DeleteEntity(Mdef1)
V1= FreeParameter("Leadin radius",Rin)
V2= FreeParameter("Leadin distance",Leadindist)
V3= FreeParameter("Leadin type",Leadintype)
V4= FreeParameter("Leadout radius",Rout)
V5= FreeParameter("Leadout distance",Leadoutdist)
V6= FreeParameter("Leadout type",Leadouttype)
V7= FreeParameter("Leadin side",Leadinside)
Stat= PathGroupLeadIn(Paths,V1,V4,V2,V5,V3,V6,V7)
Stat= AutoNameGroup(Paths,Name$)
V8= FreeParameter("Dia comp dir (+1 left  -1 right)",Dcomp_dir)
Stat= SetDCompDirection(Paths,V8)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Paths) THEN 
	DelLastVarRecord
ELSE
	Stat= AnimateTool(Tool,Paths)
	ActivateUndo
	Stat= AdvanceUndoNumber("finish mill")
	Stat= AddGroupToUndo(WsUBCreateMode,Paths,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

Stat= SetSelectedFlag(Pocket_boundary,WsFalse)
Stat= PlotEntity(Pocket_boundary)


GOTO Pick_loop

Leave:!
LowerSelectBox
IF ValidGeometry(Pocket_boundary) THEN
        Stat= SetSelectedFlag(Pocket_boundary,WsFalse)
        Stat= PlotEntity(Pocket_boundary)
END IF
Vleadintype=CreateVariable("leadin type",LeadinVarsType,leadintype)
Vleadouttype=CreateVariable("leadout type",LeadinVarsType,leadouttype)
Vleadin=CreateVariable("leadin dist",LeadinVarsType,leadindist)
Vleadout=CreateVariable("leadout dist",LeadinVarsType,leadoutdist)
VRout=CreateVariable("leadin radius",LeadinVarsType,Rin)
VRin=CreateVariable("leadout radius",LeadinVarsType,Rout)
Stat=DeleteEntity(Pt)
Stat=SetCurrentColor(Current_pen)
Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(Pt)
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END

