
! script name: offsets.d/boundaryrough
! 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
!B INSTRUCTIONS:
!B
!B 	STARTING OFFSET: Enter the starting distance from the contour for
!B 		the path to be generated.
!B
!B 	ENDING OFFSET: Enter the ending distance from the contour for the
!B 		path to be generated.
!B
!B 	WIDTH OF CUT: Enter the actual width of cut (the distance between
!B 		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 will be used as
!B 		a boundary for the roughing path generated.
!B
!B 	NUMBER OF INTERIOR CONTOURS: Enter the number of contours within
!B 		the enclosed area to be used for the roughing path.
!B
!B 	PICK INTERIOR CONTOUR # : Pick the contours to be used in the
!B 		roughing path to be generated.
!B

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

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

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:!
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
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=SetInputFormat("n4","")
Stat=SetPickMask(WsPickNoSurf)
Contour=PickEntity("PICK EXTERIOR BOUNDARY")
IF (ExitScript(Destination$)) THEN Leave

IF (NOT ValidGeometry(Contour)) THEN Pick_loop
Closed=IsContClosed(Contour)
IF NOT Closed THEN GOTO Pick_loop

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

Isle_loop: Stat=SetInputFormat("n0","")
Nisles=GetValue("NUMBER OF INTERIOR CONTOURS",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Nisles<1 THEN Isle_loop

Inner=CreateGroup("ISLANDS1")
FOR I=1 TO Nisles

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

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

NEXT I
Stat=SetPickMask(WsPickGeometry)

Outer=CreateGroup("ISLANDS2")
Stat=SetCurrentColor(Pen_color)

Path_group=BoundaryRough(Contour,Outer,Inner,Start_offset,End_offset,Width_cut,Mill_dir,WsFullLook,WsCircleMoves)

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

IF (ValidGeometry(Path_group)) THEN
        Stat=Highlight(Path_group,Pen_color,WsDirArrowOn)
END IF


if ValidGeometry(Path_group) THEN

        Datastr$=" POCKET "&CHR$(10)
        Datastr$=Datastr$&" "&CHR$(10)
        stat= GroupName(Path_group,Boundary_name$)
        Datastr$=Datastr$&"NAME: "&Boundary_name$&CHR$(10)
        Datastr$=Datastr$&"TYPE:  BOUNDARY ROUGH"&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(Inner)
        Datastr$=Datastr$&"NUMBER ISLANDS: "&VAL$(Nisles)&CHR$(10)
        FOR I=1 TO Nisles
                isle= GroupElement(Inner,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$&"MILL DIRECTION: "
        Datastr$=Datastr$&RPT$("Conventional",Mill_dir=WsConventional)
        Datastr$=Datastr$&RPT$("Climb",Mill_dir=WsClimb)&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)
	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("boundary rough")
	Stat= AddItemToUndo(WsUBCreateMode,PocketBdy)
	Stat= AddGroupToUndo(WsUBCreateMode,Path_group,1,WsTrue)

END IF


Stat=DeleteEntity(Outer)
Stat=DeleteEntity(Inner)
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:!
Stat=SetPickMask(WsPickGeometry)
IF ValidGeometry(Contour) THEN
        Stat= SetSelectedFlag(Contour,WsFalse)
        Stat=PlotEntity(Contour)
END IF
IF ValidGeometry(Inner) THEN
        Stat= SetSelectedFlag(Inner,WsFalse)
        Stat=PlotEntity(Inner)
        Stat=DeleteEntity(Inner)
END IF
LowerSelectBox
Stat=DeleteEntity(Outer)
Stat=DeleteEntity(Inner)
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
