
! script name: offsets.d/zigzagpocket
! topic: generate path for roughing area using zig-zag motion

!A
!A NAME:
!A
!A 	Zig-Zag Rough
!A
!A DESCRIPTION:
!A
!A 	This script generates a path for roughing an area using
!A 	a back and forth tool motion at some angle.
!A
!B INSTRUCTIONS:
!B
!B 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	WIDTH OF CUT: Enter the distance between successive passes.
!B 		The width of cut should not exceed the smallest X or
!B 		Y component of the most acute angle generated by the
!B 		zigzag routine, without the danger of stock being left.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for
!B 		a finish pass.
!B
!B 	ANGLE OF ZIGZAG LINES: Enter the angle at which the cutter will
!B 		travel as it goes back and forth across the area to be
!B 		roughed.
!B
!B 	PICK EXTERIOR CONTOUR: Pick the contour that will
!B 		be the boundary.
!B
!B 	NUMBER OF ISLANDS: Enter the number of islands to be
!B 		avoided by the zigzag path generated.
!B
!B 	PICK ISLAND #: Pick the islands to be avoided by the zigzag path.
!B
!C NOTES: 
!C
!C 	If the exterior contour is broken into component islands by the offset
!C 	required for the cutter and finish allowance the zigzag rough will only
!C 	use the island with the longest length as the exterior.  The other
!C 	components are not used. 
!C		

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

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

Angle=NamedVarVal("zigzag angle",WsSystemVars,0.0)
Stat=SetAngleEquals(Angle)
Stat=SetInputFormat("n4=","")
Angle=InputAngle("ANGLE OF ZIGZAG LINES")
IF (ExitScript(Destination$)) THEN Leave
Vangle=CreateVariable("zigzag angle",WsSystemVars,Angle)

Pen_color=WsZigzagCutPen

Pick_loop:!

Stat=SetInputFormat("n4","")

Pick_loop1:!

Stat=SetPickMask(WsPickNoSurf)
Contour=PickEntity("PICK EXTERIOR CONTOUR",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF (NOT ValidGeometry(Contour)) THEN Pick_loop1

Closed=IsContClosed(Contour)
IF NOT Closed THEN Pick_loop1

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)

Cutter_rad=0.5*Cutter_dia

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= SetSelectedFlag(Contour,WsFalse)
Stat=PlotEntity(Contour)
Stat= SetSelectedFlag(Islands,WsFalse)
Stat=PlotEntity(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:  ZIGZAG"&CHR$(10)
        group= ParentGroup(Contour,0)
        stat= GroupName(group,Name$)
        Datastr$=Datastr$&"EXTERIOR: "&Name$&CHR$(10)
	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

        stat= GroupName(Start_holes,Name$)
        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$&"ANGLE: "&VAL$(Angle)&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("zigzag rough")
	Stat= AddItemToUndo(WsUBCreateMode,PocketBdy)
	Stat= AddGroupToUndo(WsUBCreateMode,Path_group,1,WsTrue)
	Stat= AddGroupToUndo(WsUBCreateMode,Start_holes,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:!
Stat=SetPickMask(WsPickGeometry)
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(Islands)
Stat=SetCurrentColor(Current_pen)
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(Pt)
LowerSelectBox
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END
