
! script name: offsets.d/multiislands
! topic: generate multiple cutter offsets for contour and islands

!A
!A NAME:
!A
!A 	Islands: Multiple Island Offsets
!A
!A DESCRIPTION:
!A
!A 	This script generates multiple cutter offsets for a contour
!A 	and islands.  That is, it generates a range of individual
!A 	offset paths for a contour and islands selected.
!A
!B INSTRUCTIONS:
!B
!B 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	STARTING OFFSET: Enter the starting distance from the contour
!B 		for the paths to be generated.
!B
!B 	ENDING OFFSET: Enter the ending distance from the contour for
!B 		the paths to be generated.
!B
!B 	WIDTH OF CUT: Enter the actual width of cut (the distance
!B 		between successive paths.)  This value is equalized
!B 		between the starting and ending offsets.  The starting
!B 		distance is not changed.
!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, None: Pick the contour that will be
!B 		used as a boundary for the paths generated.
!B
!B 	PICK WHICH SIDE: If the contour is closed, then the side must
!B 		be picked for the paths.
!B
!B 	NUMBER OF ISLANDS: Enter the number of islands to be included
!B 		in the offset paths generated.
!B
!B 	PICK ISLAND # : Pick the islands to be included in the offset
!B 		paths to be generated.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: multiple island offsets ")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[4096]
DIM Boundary_name$[256]
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)

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 Start
		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

Start:!
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)

Stat=SetInputFormat("n4","")
Start_offset=GetValue("STARTING OFFSET",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Start_offset=ABS(Start_offset)

End_offset=GetValue("ENDING OFFSET",Answer$)
IF (ExitScript(Destination$)) THEN Leave
End_offset=ABS(End_offset)

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)

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)
Stat=SetInputFormat("a1",",N,")
Contour=PickEntity("PICK EXTERIOR CONTOUR, None",Answer$)
IF (ExitScript(Destination$)) THEN Leave

IF Answer$="N" OR Answer$="n" THEN
	No_exterior=1
ELSE
	IF (NOT ValidGeometry(Contour)) THEN Pick_loop
	No_exterior=0
	Closed=IsContClosed(Contour)
	Dist=Offset
	Offset_sign=1
	IF NOT Closed THEN
		Pt=InputFreePoint("PICK WHICH SIDE")
		       IF (ExitScript(Destination$)) THEN Leave
		Side=OffsetLeftRight(Contour,Pt)
		Offset_sign= -Side
		Direction_sign=MillDirection(Contour,Pt)
		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)
	END IF
END IF

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)

IF No_exterior THEN

Path_group=MultiIsland(0,Islands,Start_offset,End_offset,Width_cut,Offset_sign,Mill_dir,Direction,WsFullLook,WsCircleMoves,Pen_color,WsDirArrowOn)

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

ELSE

Path_group=MultiIsland(Contour,Islands,Start_offset,End_offset,Width_cut,Offset_sign,Mill_dir,Direction,WsFullLook,WsCircleMoves,Pen_color,WsDirArrowOn)

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

END IF


if ValidGeometry(Path_group) THEN

        Datastr$=" OFFSET BOUNDARY "&CHR$(10)
        Datastr$=Datastr$&" "&CHR$(10)
        stat= GroupName(Path_group,Boundary_name$)
        Datastr$=Datastr$&"NAME: "&Boundary_name$&CHR$(10)
        Datastr$=Datastr$&"TYPE:  MULTI ISLAND"&CHR$(10)
        IF NOT No_exterior THEN
                group= ParentGroup(Contour,0)
                stat= GroupName(group,Name$)
                Datastr$=Datastr$&"EXTERIOR: "&Name$&CHR$(10)
        END IF
        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
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
	Datastr$=Datastr$&"START OFFSET: "&VAL$(Start_offset)&CHR$(10)
        Datastr$=Datastr$&"END OFFSET: "&VAL$(End_offset)&CHR$(10)
        Datastr$=Datastr$&"WIDTH CUT: "&VAL$(Width_cut)&CHR$(10)
        Datastr$=Datastr$&"LOOK AHEAD:  FULL"&CHR$(10)
        Datastr$=Datastr$&"MILL DIRECTION: "
        Datastr$=Datastr$&RPT$("Conventional",Mill_dir=WsConventional)
        Datastr$=Datastr$&RPT$("Climb",Mill_dir=WsClimb)&CHR$(10)
        Datastr$=Datastr$&"CONTOUR DIRECTION: "&VAL$(Direction)&CHR$(10)
        Datastr$=Datastr$&"SIDE: "&VAL$(Side)&CHR$(10)

        OffsetBdy=FindUserData(Boundary_name$,WsOffsetBdy)
        IF (NOT (ValidGeometry(OffsetBdy))) THEN
                 OffsetBdy=ReadUserData(WsOffsetBdy,Boundary_name$,Datastr$)
        ELSE
                 Stat= DeleteEntity(OffsetBdy)
                 OffsetBdy=ReadUserData(WsOffsetBdy,Boundary_name$,Datastr$)
        END IF

        stat= AssocUserData(Path_group,OffsetBdy,WsOffsetBdy)
	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 offset")
	Stat= AddItemToUndo(WsUBCreateMode,OffsetBdy)
	Stat= AddGroupToUndo(WsUBCreateMode,Path_group,1,WsTrue)
END IF


Stat=DeleteEntity(Islands)
GOTO Pick_loop

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=DeleteEntity(Pt)
Stat=SetCurrentColor(Current_pen)
Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(Pt)
LowerSelectBox
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END
