
! script name: offsets.d/offset
! topic: cutter offset with minimum look ahead

!A
!A NAME:
!A
!A 	Minimum: Cutter Offset - minimum look ahead
!A
!A DESCRIPTION:
!A
!A 	This script generates a cutter path with a minimum look ahead.
!A 	The LOOK AHEAD's function is to find where PATH elements are
!A 	eliminated, and then to determine whether that part must be
!A 	aborted, or can continue past the eliminated element.  The
!A 	MINIMUM look ahead looks ahead three elements, and is suitable
!A 	for most contours.  It performs three calculations for each
!A 	PATH element.  If the path is unacceptable, e.g., crosses over
!A 	itself, or stops short of completion, redo the path with a full
!A 	look ahead.
!A
!B INSTRUCTIONS:
!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 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for
!B 		a finish pass.
!B
!B 	PICK AN ELEMENT OF CONTOUR TO BE OFFSET: Pick an element on
!B 		the contour to be offset.
!B
!B 	PICK WHICH SIDE: Pick which side of the contour the path should
!B 		be generated.
!B
!C NOTES:
!C
!C 	The cutter offset path is calculated from the cutter diameter and
!B 	the finish allowance.  (offset=.5*cutter_diameter + finish_allowance)
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: cutter offset (min look) ")

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

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

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

Fin_allow:!
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)

Stat=SetInputFormat("n4=","")
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)

Pick_loop:!

Offset=ABS(0.5*Cutter_dia+ Finish_allow)

Stat=SetPickMask(WsPickNoSurf)
Contour=PickEntity("PICK AN ELEMENT OF CONTOUR TO BE OFFSET")
IF (ExitScript(Destination$)) THEN Leave


Pt=InputFreePoint("PICK WHICH SIDE")
IF (ExitScript(Destination$)) THEN Leave

Side=OffsetLeftRight(Contour,Pt)
Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)

Direction_sign=MillDirection(Contour,Pt)
Dist=-Side*Offset

Direction=WsForward
IF (Direction_sign=1)AND(Mill_dir=WsConventional) THEN
	Direction=WsReverse
END IF
IF (Direction_sign=-1)AND(Mill_dir=WsClimb) THEN
	Direction=WsReverse
END IF

Stat=SetCurrentColor(Pen_color)
Path=OffsetContour(Contour,Dist,Direction,WsMinLook,WsCircleMoves,Pen_color,WsDirArrowOn)

if ValidGeometry(Path) THEN
        Datastr$=" OFFSET BOUNDARY "&CHR$(10)
        Datastr$=Datastr$&" "&CHR$(10)
        stat= GroupName(Path,Boundary_name$)
        Datastr$=Datastr$&"NAME: "&Boundary_name$&CHR$(10)
        Datastr$=Datastr$&"TYPE:  OFFSET"&CHR$(10)
        group= ParentGroup(Contour,0)
        stat= GroupName(group,Name$)
        Datastr$=Datastr$&"CONTOUR: "&Name$&CHR$(10)
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
	Datastr$=Datastr$&"FINISH ALLOWANCE: "&VAL$(Finish_allow)&CHR$(10)
        Datastr$=Datastr$&"LOOK AHEAD:  MIN"&CHR$(10)
	Datastr$=Datastr$&"CONTOUR DIRECTION: "&VAL$(Direction)&CHR$(10)
        Datastr$=Datastr$&"MILL DIRECTION: "
        Datastr$=Datastr$&RPT$("Conventional",Mill_dir=WsConventional)
        Datastr$=Datastr$&RPT$("Climb",Mill_dir=WsClimb)&CHR$(10)
        Datastr$=Datastr$&"SIDE: "&VAL$(Side)&CHR$(10)

        OffsetBdy=FindUserData(Boundary_name$,WsOffsetBdy)
        IF (NOT (ValidGeometry(OffsetBdy))) THEN
                 OffsetBdy=ReadUserData(WsOffsetBdy,Boundary_name$,Datastr$)
        ELSE
                 Stat= DeleteEntity(OffsetBdy)
                 OffsetBdy=ReadUserData(WsOffsetBdy,Boundary_name$,Datastr$)
        END IF

        stat= AssocUserData(Path,OffsetBdy,WsOffsetBdy)
	IF Tool_assoc THEN Stat= AssociateTool(Path,Tool)

	ActivateUndo
	Stat= AdvanceUndoNumber("mill offset")
	Stat= AddItemToUndo(WsUBCreateMode,OffsetBdy)
	Stat= AddGroupToUndo(WsUBCreateMode,Path,1,WsTrue)
END IF


Stat=DeleteEntity(Pt)

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=SetCurrentColor(Current_pen)
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(Pt)
LowerSelectBox
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END
