
! script name: offsets.d/fullofflth

!A
!A NAME:
!A
!A 	LATHE OFFSET (full look)
!A
!A DESCRIPTION:
!A
!A 	This script generates a cutter path which looks ahead all elements.
!A 	It will perform as many calculations per path element as there are
!A 	contour elements, which may take a long time.  The Minimum or None
!A 	look ahead options should be tried first.  If the path is unacceptable
!A 	then use FULL LOOK ahead.
!A
!B INSTRUCTIONS:
!B
!B      LATHE Forward OR Reverse:
!B 		F - Forward will generate a path which has the same direction
!B 		    as the contour.
!B 		R - Reverse will generate a path which is in the opposite
!B 		    direction of the contour.
!B
!B 	TOOL NOSE RAD: Enter the toolnose radius of the cutter to be used.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B 	PICK AN ELEMENT OF CONTOUR TO BE OFFSET: Pick an element on the
!B 		contour to be offset.
!B
!B 	PICK WHICH SIDE: Pick which side of the contour the path should be
!B 		generated.
!B
!C NOTES:
!C
!C 	The cutter offset path is calculated from the toolnose radius and 
!C 	the finish allowance.  ( offset=Toolnoserad + finish_allowance)
!C

stat=ChangeLabelStr("ScriptNameStatus","Function:Lathe cutter offset (full 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
Stat=SetPickMask(WsPickGeometry)


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,WsFullLook,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:  FULL"&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("lathe 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
