
! script name: voffset.d/boundarymill
! var_topic: create path for roughing area around contour within enclosed area

!A
!A NAME:
!A
!A 	Boundary Rough
!A
!A DESCRIPTION:
!A
!A 	This script creates a path for roughing an area around a contour(s)
!A 	within an enclosed area.  An exterior boundary must be selected.
!A
!A~ image location - images/boundaryroughmill.jpg
!A~ image location - images/boundaryroughmill2.jpg
!B INSTRUCTIONS:
!B
!B 	ROUGH STOCK: Enter the amount of rough stock.
!B
!B 	WIDTH OF CUT: Enter the actual width of cut,
!B 		the distance between successive offsets.
!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 	PICK EXTERIOR BOUNDARY: Pick the contour that
!B 		will be used as a boundary for the roughing
!B 		path generated.
!B
!B 	NUMBER OF INTERIOR CONTOURS: Enter the number
!B 		of contours within the enclosed area to be used
!B 		for the roughing path.
!B
!B 	PICK INTERIOR CONTOUR # : Pick the contours
!B 		to be used in the roughing path to be generated.
!B

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

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 Depthcut_var$[256]
DIM Rough_var$[256]
DIM Width_var$[256]
DIM Iname$[256]
DIM Name$[256]


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

ActivateUndo
Current_pen=CurrentColor

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/boundarymill"
  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/boundarymill"
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)

Rough_stock:!
Stat=SetInputFormat("n4=","")
Rough_stock=NamedVarVal("rough stock",WsSystemVars,0.0)
Stat=SetEqualsValue(Rough_stock)
Rough_stock=GetValue("ROUGH STOCK",Answer$)
IF (ExitScript(Destination$)) THEN Leave
VStock=CreateVariable("rough stock",WsSystemVars,Rough_stock)
No_rough_var= LastVariableInput(Rough_var$)


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$)


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)
Contour=PickGroupEnt("PICK EXTERIOR BOUNDARY")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Contour) THEN Pick_loop
Stat= EntityNameCheck(Contour,Contour$)
Closed=IsContClosed(Contour)
IF NOT Closed THEN Pick_loop

Pocket_boundary= FindBoundary(Contour)
IF (ValidDependency(Pocket_boundary)) THEN 
	Stat= EntityNameCheck(Pocket_boundary,Boundary$)
	Stat= SetSelectedFlag(Pocket_boundary,WsTrue)
	Stat= PlotEntity(Pocket_boundary)
	Picked_boundary=1
ELSE
	Picked_boundary=0
END IF


Islands:!
IF NOT Picked_boundary THEN
Stat=SetInputFormat("n0=","")
Dtat= SetEqualsValue(0)
Nisles=GetValue("NUMBER OF INTERIOR CONTOURS",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Stat=SetInputFormat("n4","")

Islands=CreateGroup("ISLANDS")
FOR I=1 TO Nisles
Stat=SetPickMask(WsPickNoSurf)
Pick_isle:Isle=PickGroupEnt("PICK INTERIOR CONTOURS #"&VAL$(I))
        IF (ExitScript(Destination$)) THEN Leave
        IF (NOT ValidDependency(Isle)) THEN Pick_isle
        Stat= EntityNameCheck(Isle,Iname$)
        IF NOT LEN(Iname$) THEN Pick_isle
        Stat=AppendToGroup(Islands,Isle)
NEXT I
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)


Depth_cut:!
Depth_cut=NamedVarVal("depth of cut",WsSystemVars,0.0)
Stat=SetEqualsValue(Depth_cut)
Stat=SetInputFormat("n4=","")
Depth_cut=GetValue("DEPTH OF CUT",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Depth_cut=ABS(Depth_cut)
Vdepth=CreateVariable("depth of cut",WsSystemVars,Depth_cut)
No_depthcut_var= LastVariableInput(Depthcut_var$)

Stat=SetCurrentColor(Pen_color)
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("MILL: Boundary Rough "&Tool_name$)
Stat= SetRecDelLastExec

RecordFunctionOff
IF NOT Picked_boundary THEN
	RecordFunctionOn
	Isles=CreateGroup("ISLANDS")
	RecordFunctionOff
	FOR I= 1 TO Nisles
	        Isle= GroupElement(Islands,I)
	        Stat= EntityNameCheck(Isle,Iname$)
	        RecordFunctionOn
	        Stat= AppendToGroup(Isles,Iname$)
	        RecordFunctionOff
	NEXT I
END IF
RecordFunctionOn

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
IF No_rough_var THEN
	V2= FreeParameter("Rough stock",Rough_stock)
ELSE
	V2= FreeParameter("Rough stock",Rough_var$)
END IF
IF No_width_var THEN
	V3= FreeParameter("Width cut",Width_cut)
ELSE
	V3= FreeParameter("Width cut",Width_var$)
END IF
IF No_depth_var THEN
	V4= FreeParameter("Finish depth",Finish_depth)
ELSE
	V4= FreeParameter("Finish depth",Depth_var$)
END IF
IF No_depthcut_var THEN
	V5= FreeParameter("Depth cut",Depth_cut)
ELSE
	V5= FreeParameter("Depth cut",Depthcut_var$)
END IF
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,1,V3,V1,V2,1)
Stat= MillDefZInfo(Mdef1,1,V4,V5,V0)
IF NOT Picked_boundary THEN
	Paths=BoundaryMilling(Contour$,0,Isles,Mdef1,WsFullLook,WsCircleMoves)
	Stat= DeleteEntity(Isles)
ELSE
	Ext_cont=BoundaryExterior(Boundary$)
	Isles=BoundaryInteriors(Boundary$)
	Paths=BoundaryMilling(Ext_cont,0,Isles,Mdef1,WsFullLook,WsCircleMoves)
END IF
Stat= AutoNameGroup(Paths,Name$)
V7= FreeParameter("Dia comp dir (+1 left  -1 right)",Dcomp_dir)
Stat= SetDCompDirection(Paths,V7)
Stat= UnAssocMachDef(Paths,Mdef1)
Stat= DeleteEntity(Mdef1)

RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Paths) THEN 
	DelLastVarRecord
ELSE
	Stat= AnimateTool(Tool,Paths)
	ActivateUndo
	Stat= AdvanceUndoNumber("boundary rough")
	Stat= AddGroupToUndo(WsUBCreateMode,Paths,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

IF Picked_boundary THEN
Stat= SetSelectedFlag(Pocket_boundary,WsFalse)
END IF
Stat= DeleteEntity(Islands)
GOTO Pick_loop

Leave:!
Stat=SetPickMask(WsPickGeometry)
LowerSelectBox
Stat= SetSelectedFlag(Pocket_boundary,WsFalse)
Stat= DeleteEntity(Islands)
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

