
! script name: offsets.d/minofflth

!A
!A NAME:
!A
!A 	LATHE OFFSET (min look)
!A
!A DESCRIPTION:
!A
!A 	This script generates a cutter path with a minimum
!A 	look ahead.  The LOOK AHEAD's function is to find
!A 	where PATH elements are eliminated, and then to
!A 	determine whether that part must be aborted, or
!A 	can continue past the eliminated element.  The
!A 	MINIMUM look ahead looks ahead three elements, and
!A 	is suitable for most contours.  It performs three
!A 	calculations for each PATH element.  If the path
!A 	is unacceptable, e.g., crosses over itself, or stops
!A 	short of completion, redo the path with a full look
!A 	ahead.
!A
!B INSTRUCTIONS:
!B
!B      LATHE Forward OR Reverse:
!B 		F - Forward will generate a path which
!B 		has the same direction as the contour.
!B 		R - Reverse will generate a path which
!B 		is in the opposite direction of the contour.
!B
!B 	TOOL NOSE RAD: Enter the toolnose rad. of the cutter
!B 		to be used.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be
!B 		left for a finish pass.
!B
!B 	PICK AN ELEMENT OF CONTOUR TO BE OFFSET: Pick an
!B 		element on the contour to be offset.
!B
!B 	PICK WHICH SIDE: Pick which side of the contour
!B 		the path should be generated.
!B
!C NOTES:
!C
!C      The cutter offset path is calculated from the
!C      toolnose rad. and the finish allowance.
!C      ( offset=Toolnoserad + finish_allowance)
!C

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

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Boundary_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=",",F,R,")
Stat=NamedVarStr("lathe direction",WsLatheVars,"F",Answer$)
Stat=SetEqualsValue(0,Answer$)
Stat=GetValue("Forward OR Reverse",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF (Answer$="F") THEN
	Direction=WsForward
	Pen_color=WsConvCutPen
ELSE
	Direction=WsReverse
	Pen_color=WsClimbCutPen
END IF

IF NOT Tool_assoc THEN
	GOSUB Get_cutter_dia
ELSE
	Stat=ToolSelectBox("TOOLS","selected tool",WsAllTurn)
	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)

		Toolnoserad=GetUserValue(Tool,"Tool nose radius:")
		LowerSelectBox
	END IF
END IF

Fin_allow:!
Vcutter=CreateVariable("tool nose radius",WsLatheVars,Toolnoserad)

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(Toolnoserad + 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


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$&"TOOL NOSE RAD: "&VAL$(Toolnoserad)&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$&"LATHE DIRECTION: "
        Datastr$=Datastr$&RPT$("Forward",Direction=WsForward)
        Datastr$=Datastr$&RPT$("Reverse",Direction=WsReverse)&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("minimum offset")
	Stat= AddItemToUndo(WsUBCreateMode,OffsetBdy)
	Stat= AddGroupToUndo(WsUBCreateMode,Path,1,WsTrue)
END IF


Stat=DeleteEntity(Pt)

GOTO Pick_loop

Get_cutter_dia:!
	Toolnoserad=NamedVarVal("tool nose radius",WsLatheVars,0.0)
	Stat=SetInputFormat("n4=","")
	Stat=SetRadiusEquals(Toolnoserad)
	Toolnoserad=InputRadius("TOOL NOSE RAD",WsRadInput)
	IF (ExitScript(Destination$)) THEN Leave
	Toolnoserad=ABS(Toolnoserad)
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
