
! script name: offsets.d/calcpocket
! topic: recalculate pocket

!A
!A NAME:
!A
!A 	Recalculate Pocket
!A
!A DESCRIPTION:
!A 	This script recalculates the roughing path after
!A 	the pocket boundary data has been edited.
!A

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

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Pocket_name$[256]
DIM Holes$[256]
DIM Exterior$[256]
DIM Island$[256]
DIM Type$[256]

DeActivateUndo
Active_workplane=0
Work=CurrentWorkplane
IF (ValidGeometry(Work)) THEN Active_workplane=1

Current_pen=CurrentColor
Pick_loop:!

Vname= FindNamedVar("pocket name",WsSystemVars)
IF ValidGeometry(Vname) THEN 
	stat= GetVariableStr(Vname,Pocket_name$)
	Path_group=FindNamedGrp(Pocket_name$)
	IF NOT ValidGeometry(Path_group) THEN Leave
	Tool=GetAssocTool(Path_group)
END IF

GetBoundary:Pocket_bdy=GetAssocEnt(Path_group,WsPocketBdy)
IF NOT ValidGeometry(Pocket_bdy) THEN Leave

stat= SetWorkplane(Pocket_bdy)

Getexterior:Stat=GetUserString(Pocket_bdy,"EXTERIOR:",Exterior$)
Contour=FindNamedGrp(Exterior$)
IF NOT ValidGeometry(Contour) THEN Leave

Getislands:Islands=CreateGroup("ISLANDS")
Nisles=GetUserValue(Pocket_bdy,"NUMBER ISLANDS:")
FOR I=1 TO Nisles
        Stat=GetUserString(Pocket_bdy,"ISLAND"&VAL$(I)&":",Island$)
        Isle=FindNamedGrp(Island$)
        IF ValidGeometry(Isle) THEN
                Stat=AddToGroup(Islands,Isle)
        END IF
NEXT I

Edit_pocket:!
Follow_pocket=0
zigzag_pocket= 0
boundary_rough= 0
Stat=GetUserString(Pocket_bdy,"TYPE:",Type$)
SELECT Type$
CASE "FOLLOW"
	follow_pocket= 1
	GOSUB Pocket_sub
CASE "ZIGZAG"
	zigzag_pocket= 1
	GOSUB Pocket_sub
CASE "BOUNDARY ROUGH"
	boundary_rough= 1
	GOSUB Boundary_sub
END SELECT
GOTO Leave


Pocket_sub:!	Follow or Zigzag Pocket
Cutter_dia=GetUserValue(Pocket_bdy,"CUTTER DIA:")
Width_cut=GetUserValue(Pocket_bdy,"WIDTH CUT:")
Finish_allow=GetUserValue(Pocket_bdy,"FINISH ALLOWANCE:")
Min_pickups=GetUserValue(Pocket_bdy,"MINIMUM PICKUPS:")
Cutter_rad=0.5*Cutter_dia

Getstartholes:Stat=GetUserString(Pocket_bdy,"START HOLES:",Holes$)
Start_holes=FindNamedGrp(Holes$)
IF ValidGeometry(Start_holes) THEN
	Stat=DeleteGroup(Start_holes)
END IF

DelPathGroup:IF ValidGeometry(Path_group) THEN
	Stat=DeleteGroup(Path_group,1)
END IF

IF Follow_pocket THEN
	Mill_dir=GetUserString(Pocket_bdy,"MILL DIRECTION:",Answer$)
	IF Answer$="Conventional" THEN
		Mill_dir=WsConventional
		Pen_color=WsConvCutPen
	ELSE
		Mill_dir=WsClimb
		Pen_color=WsClimbCutPen
	END IF
	Stat=SetCurrentColor(Pen_color)
	Path_group=IslandPocket(Contour,Islands,Cutter_rad,Width_cut,Finish_allow,Mill_dir,WsCircleMoves,Pen_color,WsDirArrowOn,Min_pickups)

END IF

IF Zigzag_pocket THEN
	Angle=GetUserValue(Pocket_bdy,"ANGLE:")
	Pen_color=WsZigzagCutPen
	Stat=SetCurrentColor(Pen_color)
	Extoffsw=0
	Isleoffsw=0
	Semifinsw=1
	Path_group=ZigZagPocket(Contour,Islands,Cutter_rad,Width_cut,Finish_allow,Angle,WsClimb,WsCircleMoves,Extoffsw,Isleoffsw,Semifinsw,Pen_color,WsDirArrowOn)
	Stat=DeleteEntity(Startpt)
	Stat=DeleteEntity(Endpt)
END IF

Stat=DeleteEntity(Islands)
IF ValidGeometry(Path_group) THEN
	Stat=SetCurrentColor(WsStHolesPen)
	Start_holes=PocketStHoles(Path_group,Cutter_dia,Depth)
	Stat=ChangeGrpName(Path_group,Pocket_name$)
	Stat=ChangeGrpName(Start_holes,Holes$)
	Stat=AssocUserData(Path_group,Pocket_bdy,WsPocketBdy)
	Stat=AssocUserData(Start_holes,Pocket_bdy,WsPocketBdy)
	IF ValidGeometry(Tool) 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
END IF
RETURN

Boundary_sub:!	Boundary rough
Cutter_dia=GetUserValue(Pocket_bdy,"CUTTER DIA:")
Start_offset=GetUserValue(Pocket_bdy,"START OFFSET:")
End_offset=GetUserValue(Pocket_bdy,"END OFFSET:")
Width_cut=GetUserValue(Pocket_bdy,"WIDTH CUT:")
Mill_dir=GetUserString(Pocket_bdy,"MILL DIRECTION:",Answer$)
IF Answer$="Conventional" THEN
	Mill_dir=WsConventional
        Pen_color=WsConvCutPen
ELSE
	Mill_dir=WsClimb
        Pen_color=WsClimbCutPen
END IF
Stat=SetCurrentColor(Pen_color)

DelPathGroup:IF ValidGeometry(Path_group) THEN
        Stat=DeleteGroup(Path_group,1)
END IF

PRINT Start_offset
PRINT End_offset
PRINT Width_cut

Outer=CreateGroup("ISLANDS2")

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

Stat=DeleteEntity(Outer)
Stat=DeleteEntity(Islands)
IF ValidGeometry(Path_group) THEN
        Stat=Highlight(Path_group,Pen_color,WsDirArrowOn)
        Stat=ChangeGrpName(Path_group,Pocket_name$)
        Stat=AssocUserData(Path_group,Pocket_bdy,WsPocketBdy)
	IF ValidGeometry(Tool) 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
END IF
RETURN


Leave:!
Stat=DeleteEntity(Islands)
Stat=DeleteEntity(Outer)
Stat=DeleteEntity(Startpt)
Stat=DeleteEntity(Endpt)
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF
Stat=SetCurrentColor(Current_pen)
IF (NOT LEN(Destination$)) THEN
	  LOAD "offsets.d/editpocket"
ELSE
	  LOAD Destination$
END IF
END
