
! script name: offsets.d/pocket
! topic: follow pocket with full look ahead

!A
!A NAME:
!A
!A 	Pocket Mill
!A
!A DESCRIPTION:
!A
!A 	This script creates successive offsets within an enclosed area.
!A 	The offsets start in the center and work there way to the selected
!A 	exterior contour.
!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 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

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

DIM Destination$[256]
DIM Answer$[256]
DIM Boundary_name$[256]
DIM 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)
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)
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=SetCurrentColor(Pen_color)

Cutter_rad=0.5*Cutter_dia

Islands=CreateGroup("ISLANDS")

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

Stat=DeleteEntity(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)
	stat= GroupName(Start_holes,Name$)
	Datastr$=Datastr$&"NUMBER ISLANDS:  0"&CHR$(10)
	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("pocket")
	Stat= AddItemToUndo(WsUBCreateMode,PocketBdy)
	Stat= AddGroupToUndo(WsUBCreateMode,Path_group,1,WsTrue)
	Stat= AddGroupToUndo(WsUBCreateMode,Start_holes,1,WsTrue)
END IF

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