
! script name: offsets.d/islandpocket
! topic: create successive offsets within an enclosed area with islands

!A
!A NAME:
!A
!A 	Pocket Mill with Islands
!A
!A DESCRIPTION:
!A
!A 	This script creates successive offsets within an enclosed
!A 	area with islands.  The offsets start near the center,
!A 	depending on location of islands, and work their way to the
!A 	selected exterior contour, and going around the islands.
!A
!B INSTRUCTIONS:
!B
!B 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	WIDTH OF CUT: Enter the actual width of cut (the distance
!B 		between successive offsets).
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for
!B 		a finish pass.
!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 CONTOUR: Pick the contour that will be
!B 		the boundary.
!B
!B 	NUMBER OF ISLANDS: Enter the number of islands to be
!B 		included in the offset paths to be generated for the pocket.
!B
!B 	PICK ISLAND # : Pick the islands to be included in the offset
!B 		paths to be generated for the pocket.
!B
!C NOTES:
!C
!C 	The cutter offset path is calculated from the cutter diameter
!C 	and the finish allowance.
!C
!C 	( offset=.5*cutter_diameter + finish_allowance)
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: island follow pocket ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Boundary_Name$[256]
DIM Datastr$[4096]
DIM Tool_name$[256]

ActivateUndo
Current_pen=CurrentColor
Tool_assoc=0
Tool_val=FindNamedVar("tool association",WsToolVars)
IF ValidGeometry(Tool_val) THEN Tool_assoc=GetVariableVal(Tool_val)

Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)
Min_pickups=NamedVarVal("Minimum pickups",WsSystemVars,0.0)
V1=CreateVariable("Minimum pickups",WsSystemVars,Min_pickups)

Start:!

IF NOT Tool_assoc THEN
	GOSUB Get_cutter_dia
ELSE
	Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
	IF Stat THEN
		GOSUB Get_cutter_dia
		Tool_assoc=0
	ELSE
Tool_ques:!
		Stat=SetInputFormat("a99","")
		Stat=GetValue("PATH TOOL",Answer$)
		IF (ExitScript(Destination$)) THEN Leave
		IF Answer$= "CANCEL" THEN
			LowerSelectBox
			GOSUB Get_cutter_dia
			Tool_assoc=0
			GOTO Width_
		END IF
		Tool=FindUserData(Answer$,WsToolData)

		Stat=GetUserString(Tool,"Tool type:",Tool_name$)
		IF NOT POS(Tool_name$, "MILL") THEN Tool_ques
		Tool_name$=Answer$
		Cutter_dia=GetUserValue(Tool,"Diameter:")
		LowerSelectBox
	END IF
END IF
Width_:!
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)

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)

Finish_allow=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_allow)
Stat=SetInputFormat("n4=","")
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finish_allow)

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
ELSE
	Mill_dir=WsClimb
	Pen_color=WsClimbCutPen
END IF

Stat=SetInputFormat("n4","")

Pick_loop:!

Offset_sign=1
Stat=SetPickMask(WsPickNoSurf)
Contour=PickEntity("PICK EXTERIOR CONTOUR",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF (NOT ValidGeometry(Contour)) THEN Pick_loop

Closed=IsContClosed(Contour)
IF NOT Closed THEN Pick_loop

Stat= SetSelectedFlag(Contour,WsTrue)
Stat=PlotEntity(Contour)

Stat=SetInputFormat("n0","")
Nisles=GetValue("NUMBER OF ISLANDS",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Islands=CreateGroup("ISLANDS")
FOR I=1 TO Nisles

	Stat=SetPickMask(WsPickNoSurf)
	Isle=PickEntity("PICK ISLAND #"&VAL$(I))
	IF (ExitScript(Destination$)) THEN Leave

	Stat=AppendToGroup(Islands,Isle)
	Stat= SetSelectedFlag(Isle,WsTrue)
	Stat=PlotEntity(Isle)

NEXT I

Stat=SetCurrentColor(Pen_color)

Cutter_rad=0.5*Cutter_dia

Path_group=IslandPocket(Contour,Islands,Cutter_rad,Width_cut,Finish_allow,Mill_dir,WsCircleMoves,Pen_color,WsDirArrowOn,Min_pickups)

Stat= SetSelectedFlag(Contour,WsFalse)
Stat=PlotEntity(Contour)
Stat= SetSelectedFlag(Islands,WsFalse)
Stat=PlotEntity(Islands)

if ValidGeometry(Path_group) THEN
        Stat=SetCurrentColor(WsStHolesPen)

        Start_holes=PocketStHoles(Path_group,Cutter_dia,Depth)


        Datastr$=" POCKET BOUNDARY "&CHR$(10)
        Datastr$=Datastr$&" "&CHR$(10)
        stat= GroupName(Path_group,Boundary_name$)
        Datastr$=Datastr$&"NAME: "&Boundary_name$&CHR$(10)
	Datastr$=Datastr$&"TYPE:  FOLLOW"&CHR$(10)
        group= ParentGroup(Contour,0)
        stat= GroupName(group,Name$)
        Datastr$=Datastr$&"EXTERIOR: "&Name$&CHR$(10)
	Nisles= GroupCount(Islands)
        Datastr$=Datastr$&"NUMBER ISLANDS: "&VAL$(Nisles)&CHR$(10)
	FOR I=1 TO Nisles
		isle= GroupElement(Islands,I)
        	group= ParentGroup(Isle,0)
        	stat= GroupName(group,Name$)
        	Datastr$=Datastr$&"ISLAND"&VAL$(I)&": "&Name$&CHR$(10)
	NEXT I

        stat= GroupName(Start_holes,Name$)
        Datastr$=Datastr$&"START HOLES: "&Name$&CHR$(10)
        Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
        Datastr$=Datastr$&"WIDTH CUT: "&VAL$(Width_cut)&CHR$(10)
        Datastr$=Datastr$&"FINISH ALLOWANCE: "&VAL$(Finish_allow)&CHR$(10)
        Datastr$=Datastr$&"MILL DIRECTION: "
        Datastr$=Datastr$&RPT$("Conventional",Mill_dir=WsConventional)
        Datastr$=Datastr$&RPT$("Climb",Mill_dir=WsClimb)&CHR$(10)
        Datastr$=Datastr$&"MINIMUM PICKUPS: "&VAL$(Min_pickups)&CHR$(10)

        PocketBdy=FindUserData(Boundary_name$,WsPocketBdy)
        IF (NOT (ValidGeometry(PocketBdy))) THEN
                 PocketBdy=ReadUserData(WsPocketBdy,Boundary_Name$,Datastr$)
        ELSE
                 Stat= DeleteEntity(PocketBdy)
                 PocketBdy=ReadUserData(WsPocketBdy,Boundary_Name$,Datastr$)
        END IF

        stat= AssocUserData(Path_group,PocketBdy,WsPocketBdy)
        stat= AssocUserData(Start_holes,PocketBdy,WsPocketBdy)
	IF Tool_assoc THEN
		Stat= AssociateTool(Path_group,Tool)
		Number= GroupCount(Path_group)
		FOR I= 1 TO Number
			GrpTool=GroupElement(Path_group,I)
			Stat= AssociateTool(GrpTool,Tool)
		NEXT I
	END IF

	ActivateUndo
	Stat= AdvanceUndoNumber("island pocket")
	Stat= AddItemToUndo(WsUBCreateMode,PocketBdy)
	Stat= AddGroupToUndo(WsUBCreateMode,Path_group,1,WsTrue)
	Stat= AddGroupToUndo(WsUBCreateMode,Start_holes,1,WsTrue)
END IF

Stat=DeleteEntity(Islands)

GOTO Start

Get_cutter_dia:!
	Cutter_dia=NamedVarVal("cutter diameter",WsSystemVars,0.0)
	Stat=SetInputFormat("n4=","")
	Stat=SetRadiusEquals(Cutter_dia/2)
	Cutter_dia=InputRadius("CUTTER DIAMETER",WsDiaInput)
	IF (ExitScript(Destination$)) THEN Leave
	Cutter_dia=ABS(Cutter_dia)
RETURN

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