
! script name: vlathe.d/offset
! topic: offset - generate an offset path for a lathe tool

!A
!A NAME:
!A
!A 	LATHE OFFSET  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script generates a cutter path with full, minimum or no look
!A 	ahead.  
!A 	The Minimum or None look ahead	options should be tried first.  
!A	If the path is unacceptable then use FULL LOOK ahead.
!A
!B INSTRUCTIONS:
!B
!B      LATHE Forward OR Reverse:
!B 		F - Forward will generate a path which has
!B 		    the same direction as the contour.
!B 		R - Reverse will generate a path which is in
!B 		    the opposite direction of the contour.
!B
!B 	TOOL NOSE RAD: Enter the toolnose radius
!B 		of the cutter to be used.
!B
!B 	FINISH ALLOWANCE: Enter the diameter and
!B 		length amount of material to remain.
!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
!B 		contour the path should be generated.
!B
!# see also - lathe.d/dialenfin.hlp
!# see also - lathe.d/tooltiptan.hlp

Stat=ChangeLabelStr("ScriptNameStatus","Function: Lathe offset ")
DIM Destination$[256], Answer$[256]
DIM Toolname$[256]
DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Dfinish$[256]
DIM Zfinish$[256]
DIM Name$[256]
DIM Type$[18]
INTEGER Id_contour


Type$=UPC$(ARGV(1))
Restart:!
Animate_tool=1


Current_pen=CurrentColor
Work= CurrentWorkplane
!   StdWorkplane

ActivateUndo
Restart:!
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

Stat=ToolSelectBox("TOOLS","selected tool",WsAllTurn)
IF Stat THEN 
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("PATH TOOL",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
                LowerSelectBox
                GOTO Leave
END IF
Toolname$= Answer$
LowerSelectBox

Tool=FindUserData(Toolname$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
Tnose_rad=ToolNoseRadius(Tool)
Quad=ToolQuadrant(Tool)

Ent=CreateVariable("tool quadrant",WsLatheVars,Quad)
Ent=CreateVariable("tool nose radius",WsLatheVars,Tnose_rad)

Stat=SetInputFormat("a1=",",F,R,")
Stat=NamedVarStr("lathe direction",WsLatheVars,"F",Answer$)
Stat=SetEqualsValue(0,Answer$)
Stat=GetValue("Forward OR Reverse",Answer$)
IF ExitScript(Destination$) THEN Leave
Ent= CreateVariable("lathe direction",WsLatheVars,Answer$)
IF (Answer$="F") THEN
	Direction=WsForward
	Pen_color=WsConvCutPen
ELSE
	Direction=WsReverse
	Pen_color=WsClimbCutPen
END IF


Fin_allow:!
Stat= SetInputFormat("n4=","")
Dia_allow=NamedVarVal("diameter fin allowance",WsLatheVars,.04)
Stat=SetEqualsValue(Dia_allow)
Dia_allow=GetValue("DIAMETER FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
No_dfinish_var= LastVariableInput(Dfinish$)
Ent=CreateVariable("diameter fin allowance",WsLatheVars,Dia_allow)

Len_allow=NamedVarVal("length fin allowance",WsLatheVars,.02)
Stat=SetEqualsValue(Len_allow)
Len_allow=GetValue("LENGTH FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
No_zfinish_var= LastVariableInput(Zfinish$)
Ent=CreateVariable("length fin allowance",WsLatheVars,Len_allow)


Pick_loop:!
E1=PickEntity("PICK AN ELEMENT OF CONTOUR TO BE OFFSET")
IF ExitScript(Destination$) THEN Leave
Contour= ParentGroup(E1,0)
IF (NOT ValidDependency(Contour)) THEN Pick_loop
Stat= EntityNameCheck(Contour,Contour$)
IF NOT LEN(Contour$) THEN pick_loop

Pt=InputFreePoint("PICK WHICH SIDE")
IF ExitScript(Destination$) THEN Leave
Side=OffsetLeftRight(E1,Pt)
Stat= DeleteEntity(Pt)
Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)

SELECT Type$
CASE "NONE"
	look_ahead= WsNoLook
CASE "MIN"
	look_ahead= WsMinLook
CASE "FULL"
	look_ahead= WsFullLook
CASE ELSE
	look_ahead= WsMinLook
END SELECT

Stat= SetCurrentColor(Pen_color)
PlotOff
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("LATHE OFFSET: "&Answer$)
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
Tool=FindUserData(Toolname$,WsToolData)
V1= FreeParameter("Side",Side)
V2= FreeParameter("Direction",Direction)
V3= FreeParameter("Tool nose radius",ToolNoseRadius(Tool))
V4= FreeParameter("Tool quadrant",ToolQuadrant(Tool))
IF No_dfinish_var THEN
        V5= FreeParameter("Diameter finish",Dia_allow)
ELSE
        V5= FreeParameter("Diameter finish",Dfinish$)
END IF
IF No_zfinish_var THEN
        V6= FreeParameter("Length finish",Len_allow)
ELSE
        V6= FreeParameter("Length finish",Zfinish$)
END IF
V7= FreeParameter("Offset",0.0)
V8= FreeParameter("Look ahead",look_ahead)
V9= FreeParameter("Collision point color",7)
Path= LatheOffsetContour(Contour$,V3,V7,V5,V6,V1,V4,V2,V8)
Stat= AutoNameGroup(Path,Name$)
Stat= AssociateTool(Path,Tool)
P1= LthToolInterfere(Tool,Path,Contour$)
Stat= SetColor(P1,V9)
Stat= SetLineType(P1,11)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
PlotOn
IF NOT ValidGeometry(Path) THEN
        DelLastVarRecord
ELSE
        ActivateUndo
        Stat= AdvanceUndoNumber("lathe offset")
        Stat= AddGroupToUndo(WsUBCreateMode,Path,1,WsTrue)
        Stat= AddItemToUndo(WsUBCreateMode,P1)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
        Stat= PlotEntity(Path)
        Stat= PlotEntity(P1)
        IF Animate_tool AND NOT ValidGeometry(P1) THEN
                 Work_trans= CurrentWorkplane
                 Stat= AnimateTool(Tool,Path)
        END IF
END IF
GOTO Pick_loop



Leave:!
PlotOn
Stat=SetCurrentColor(Current_pen)
IF ValidGeometry(Work) THEN Stat=SetWorkplane(Work)
Stat=SetInputFormat("n4","")
LowerSelectBox
IF NOT LEN(Destination$) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END
