
! script name: offsets.d/calcoffset
! topic: recalculate offset

!A
!A NAME:
!A
!A 	Recalculate
!A
!A DESCRIPTION:
!A
!A 	This script recalculates the offset path after
!A 	the offset data has been edited.
!A

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

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Offset_name$[256]
DIM Holes$[256]
DIM Contour$[256]
DIM Type$[256]
DIM Island$[256]

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

Current_pen=CurrentColor

Pick_loop:!
Vname= FindNamedVar("offset name",WsSystemVars)
IF ValidGeometry(Vname) THEN
        stat= GetVariableStr(Vname,Offset_name$)
        Path_group=FindNamedGrp(Offset_name$)
        IF NOT ValidGeometry(Path_group) THEN Leave
END IF

IF  ValidGeometry(Path_group) THEN 
	Stat=GroupName(Path_group,Offset_name$)
	Stat=Highlight(Path_group,WsPickPen,WsDirArrowOff)
	Offset_bdy=GetAssocEnt(Path_group,WsOffsetBdy)
	IF NOT ValidGeometry(Offset_bdy) THEN Leave
	multi_paths=1
	Tool=GetAssocTool(Path_group)
ELSE
	Stat=GroupName(Path,Offset_name$)
	Stat=Highlight(Path,WsPickPen,WsDirArrowOff)
	Offset_bdy=GetAssocEnt(Path,WsOffsetBdy)
	IF NOT ValidGeometry(Offset_bdy) THEN Leave
	multi_paths=0
	Tool=GetAssocTool(Path)
END IF
IF Multi_paths THEN
        IF ValidGeometry(Path_group) THEN
                Stat=DeleteGroup(Path_group,1)
        END IF
ELSE
        IF ValidGeometry(Path) THEN
                Stat=DeleteGroup(Path,1)
        END IF
END IF

Edit_offset:!
stat= SetWorkplane(Offset_bdy)

Stat=GetUserString(Offset_bdy,"TYPE:",Type$)
SELECT Type$
CASE "OFFSET"
	GOSUB Offset_contour
CASE "MULTI"
	GOSUB Multi_offset
CASE "ISLAND"
	GOSUB Island_offset
CASE "MULTI ISLAND"
	GOSUB Multi_island
END SELECT
GOTO Leave

Offset_contour:! OFFSET OF A SINGLE CONTOUR
Stat=GetUserString(Offset_bdy,"CONTOUR:",Contour$)
Contour=FindNamedGrp(Contour$)
IF NOT ValidGeometry(Contour) THEN RETURN

Cutter_dia=GetUserValue(Offset_bdy,"CUTTER DIA:")
Finish_allow=GetUserValue(Offset_bdy,"FINISH ALLOWANCE:")
Offset= ABS(0.5*Cutter_dia+ Finish_allow)
GOSUB Get_mill_dir
GOSUB Get_look_ahead

Dist= -Offset*Side
Path=OffsetContour(Contour,Dist,Direction,Look_ahead,WsCircleMoves,Pen_color,WsDirArrowOn)
IF ValidGeometry(Path) THEN
	Stat=ChangeGrpName(Path,Offset_name$)
	Stat=AssocUserData(Path,Offset_bdy,WsOffsetBdy)
 	IF ValidGeometry(Tool) THEN Stat= AssociateTool(Path,Tool)
END IF
RETURN



Island_offset:!
No_exterior= 0
Stat=GetUserString(Offset_bdy,"EXTERIOR:",Contour$)
if Stat= 1 THEN
        No_exterior= 1
	Side=1
ELSE
        Contour=FindNamedGrp(Contour$)
        IF NOT ValidGeometry(Contour) THEN No_exterior= 1
END IF
GOSUB Get_islands
Cutter_dia=GetUserValue(Offset_bdy,"CUTTER DIA:")
Finish_allow=GetUserValue(Offset_bdy,"FINISH ALLOWANCE:")
Offset= ABS(0.5*Cutter_dia+ Finish_allow)
GOSUB Get_mill_dir
GOSUB Get_look_ahead
Stat=SetCurrentColor(Pen_color)
IF No_exterior THEN
	Offset_sign= -Side
	Dist=Offset*Offset_sign
        Path_group=IslandOffset(0,Islands,Dist,Mill_dir,Direction,WsFullLook,WsCircleMoves,Pen_color,WsDirArrowOn)
ELSE

	Closed=IsContClosed(Contour)
	IF Closed THEN
		Offset_sign= 1
	ELSE
		Offset_sign= -Side
	END IF
	Dist=Offset*Offset_sign
        Path_group=IslandOffset(Contour,Islands,Dist,Mill_dir,Direction,WsFullLook,WsCircleMoves,Pen_color,WsDirArrowOn)
END IF
IF ValidGeometry(Path_group) THEN
        Stat=ChangeGrpName(Path_group,Offset_name$)
        Stat=AssocUserData(Path_group,Offset_bdy,WsOffsetBdy)
	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
Stat= DeleteEntity(islands)
RETURN

Multi_offset:!
Stat=GetUserString(Offset_bdy,"CONTOUR:",Contour$)
Contour=FindNamedGrp(Contour$)
IF NOT ValidGeometry(Contour) THEN RETURN
Start_offset=GetUserValue(Offset_bdy,"START OFFSET:")
End_offset=GetUserValue(Offset_bdy,"END OFFSET:")
Width_cut=GetUserValue(Offset_bdy,"WIDTH CUT:")
GOSUB Get_mill_dir
GOSUB Get_look_ahead
Offset_sign= -Side
Path_group=MultiOffset(Contour,Start_offset,End_offset,Width_cut,Offset_sign,Direction,WsFullLook,WsCircleMoves,Pen_color,WsDirArrowOn)
IF ValidGeometry(Path_group) THEN
	Stat=ChangeGrpName(Path_group,Offset_name$)
	Stat=AssocUserData(Path_group,Offset_bdy,WsOffsetBdy)
	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


Multi_island:!
No_exterior= 0
Stat=GetUserString(Offset_bdy,"EXTERIOR:",Contour$)
if Stat= 1 THEN
	No_exterior= 1
ELSE
	Contour=FindNamedGrp(Contour$)
	IF NOT ValidGeometry(Contour) THEN No_exterior= 1
END IF
GOSUB Get_islands
Start_offset=GetUserValue(Offset_bdy,"START OFFSET:")
End_offset=GetUserValue(Offset_bdy,"END OFFSET:")
Width_cut=GetUserValue(Offset_bdy,"WIDTH CUT:")
GOSUB Get_mill_dir
GOSUB Get_look_ahead
Stat=SetCurrentColor(Pen_color)
IF No_exterior THEN
	Offset_sign= 1
	Path_group=MultiIsland(0,Islands,Start_offset,End_offset,Width_cut,Offset_sign,Mill_dir,Direction,WsFullLook,WsCircleMoves,Pen_color,WsDirArrowOn)
ELSE
 	Closed=IsContClosed(Contour)
        IF Closed THEN
                Offset_sign= 1
        ELSE
                Offset_sign= -Side
        END IF
	Path_group=MultiIsland(Contour,Islands,Start_offset,End_offset,Width_cut,Offset_sign,Mill_dir,Direction,WsFullLook,WsCircleMoves,Pen_color,WsDirArrowOn)

END IF

IF ValidGeometry(Path_group) THEN
        Stat=ChangeGrpName(Path_group,Offset_name$)
        Stat=AssocUserData(Path_group,Offset_bdy,WsOffsetBdy)
	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
Stat= DeleteEntity(islands)
RETURN


Get_look_ahead:!  READS LOOK AHEAD
Stat=GetUserString(Offset_bdy,"LOOK AHEAD:",Look$)
SELECT Look$
CASE "NONE"
        Look_ahead= WsNoLook
CASE "MIN"
        Look_ahead= WsMinLook
CASE "FULL"
        Look_ahead= WsFullLook
END SELECT
RETURN


Get_mill_dir:! READS MILL DIRECTION AND SETS PEN COLOR 
Side=GetUserValue(Offset_bdy,"SIDE:")
Direction=GetUserValue(Offset_bdy,"CONTOUR DIRECTION:")
Mill_dir=GetUserString(Offset_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)
RETURN


Get_islands: ! READS ISLAND GROUP
Islands=CreateGroup("ISLANDS")
Nisles=GetUserValue(Offset_bdy,"NUMBER ISLANDS:")
FOR I=1 TO Nisles
        Stat=GetUserString(Offset_bdy,"ISLAND"&VAL$(I)&":",Island$)
        Isle=FindNamedGrp(Island$)
        IF ValidGeometry(Isle) THEN
                Stat=AppendToGroup(Islands,Isle)
        END IF
NEXT I
RETURN




Leave:!
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF
Stat=SetCurrentColor(Current_pen)
IF (NOT LEN(Destination$)) THEN
	  LOAD "offsets.d/editoffset"
ELSE
	  LOAD Destination$
END IF
END
