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

!A
!A NAME:
!A
!A 	LATHE OFFSET
!A
!A DESCRIPTION:
!A
!A 	This script generates a cutter path with full, minimum or no look
!A 	ahead.  It will also change the path geometry to eliminate undercuts
!A 	based on the tool clearance angle.  The Minimum or None look ahead
!A	options should be tried first.  If the path is unacceptable then
!A	use FULL LOOK ahead.
!A
!B INSTRUCTIONS:
!B
!B      LATHE Forward OR Reverse:
!B 		F - Forward will generate a path which has the
!B 		    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 of the
!B 		cutter to be used.
!B
!B 	FINISH ALLOWANCE: Enter the diameter and length
!B 		amount of material to remain.
!B
!B 	PICK AN ELEMENT OF CONTOUR TO BE OFFSET: Pick
!B 		an 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
!# see also - lathe.d/dialenfin.hlp
!# see also - lathe.d/tooltiptan.hlp

Stat=ChangeLabelStr("ScriptNameStatus","Function: Lathe offset ")

COM /Rough/ Allow_delta,Conv,Depth_code,Cut_type$[18],Rough$[18],Stock$[18]
DIM Answer$[256],Boundary_name$[256],Datastr$[1256],Destination$[256],Type$[256],Utype$[256]
INTEGER Id_contour,Upath_num

Rough$=UPC$(ARGV(1))
LOAD "lathe.d/setvals:offset"
Restart:!
ActivateUndo
Animate_tool=1
Type$= Rough$

Current_pen=CurrentColor
Work= CurrentWorkplane
StdWorkplane

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

IF Type$="USEANGLE" OR Type$="UNDERCUT" THEN 
	Stat= SetInputFormat("a1=",",L,R,")
	Stat= NamedVarStr("under type",WsLatheVars,"L",Utype$)
	Stat= SetEqualsValue(0,Utype$)
	Stat= GetValue("Longitudinal or Radial undercut",Utype$)
	IF ExitScript(Destination$) THEN Leave
	Ent= CreateVariable("under type",WsLatheVars,Utype$)
	Long= (Utype$="L")

	Id_contour=NamedVarVal("contour type",WsLatheVars,0)
	Stat= SetEqualsValue(0,RPT$("I",Id_contour)&RPT$("O",NOT Id_contour))
	Stat= SetInputFormat("a1=",",I,O,")
	Stat= GetValue("Id or Od cycle",Answer$)
	IF ExitScript(Destination$) THEN Leave
	Id_contour= (Answer$="I")
	Unum= Id_contour
	Ent= CreateVariable("contour type",WsLatheVars,Unum)
	IF Id_contour THEN
		Pen_color=WsConvCutPen+1
	ELSE
		Pen_color=WsClimbCutPen+1
	END IF

	IF Type$="UNDERCUT" THEN 
		Pen_color=Pen_color+3
		Stat=SetInputFormat("n0=","")
		Upath_num=NamedVarVal("undercut number",WsLatheVars,1.)
		Stat=SetEqualsValue(Upath_num)
		Upath_num=GetValue("UNDERCUT NUMBER, (0=All)",Answer$)
		IF ExitScript(Destination$) THEN Leave
		Ent=CreateVariable("undercut number",WsLatheVars,Upath_num)
	ELSE
		Upath_num=0
	END IF
END IF

Tool_assoc=0
Tool_val=FindNamedVar("tool association",WsToolVars)
IF ValidGeometry(Tool_val) THEN Tool_assoc=GetVariableVal(Tool_val)

Start:!
IF NOT Tool_assoc THEN
	GOSUB Get_tool_data
ELSE
	Stat=ToolSelectBox("TOOLS","selected tool",WsAllTurn)
	IF Stat THEN
		Tool_assoc=0
		GOTO Start
	ELSE
Tool_ques:!
		Stat=SetInputFormat("a99","")
		Stat=GetValue("PATH TOOL",Answer$)
		IF ExitScript(Destination$) THEN Leave
		IF Answer$= "CANCEL" THEN
			LowerSelectBox
			Tool_assoc=0
			GOTO Start
		END IF
		Tool=FindUserData(Answer$,WsToolData)
		Tnose_rad=GetUserValue(Tool,"Tool nose radius:")
		Quad=GetUserValue(Tool,"Tool quadrant:")

		LowerSelectBox
		IF Type$="USEANGLE" THEN 
			IF Utype$="R" THEN
				Uncut_angle=ToolVCAngle(Tool)
			ELSE
				Uncut_angle=ToolHCAngle(Tool)
			END IF
			SELECT Quad
			CASE 1,2
				IF Uncut_angle<180 THEN Uncut_angle= Uncut_angle+180
			CASE 3,4
				IF Uncut_angle>180 THEN Uncut_angle= (Uncut_angle+180) MOD 360
			END SELECT
		END IF
	END IF
END IF

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

IF Type$="UNDERCUT" THEN 
	Stat=SetInputFormat("n4=","")
	Uncut_angle=NamedVarVal("tool clearance angle",WsLatheVars,180.)
	Stat=SetAngleEquals(Uncut_angle)
	Uncut_angle=InputAngle("TOOL CLEARANCE ANGLE")
	IF ExitScript(Destination$) THEN Leave
	Uncut_angle=Uncut_angle MOD 360
	Ent= CreateVariable("tool clearance angle",WsLatheVars,Uncut_angle)
END IF

Stat=SetInputFormat("n4=","")
Dia_allow=NamedVarVal("diameter fin allowance",WsLatheVars,.02)
Stat=SetEqualsValue(Dia_allow)
Dia_allow=GetValue("DIAMETER FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
Ent=CreateVariable("diameter fin allowance",WsLatheVars,Dia_allow)

Len_allow=NamedVarVal("length fin allowance",WsLatheVars,.01)
Stat=SetEqualsValue(Len_allow)
Len_allow=GetValue("LENGTH FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
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 ValidGeometry(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)

Stat= SetCurrentColor(Pen_color)
SELECT Type$
CASE "NONE"
	Path= LatheOffsetContour(Contour,Tnose_rad,0.0,Dia_allow,Len_allow,Side,Quad,Direction,WsNoLook)
	GOSUB Datastr
	GOSUB Animate_tool
CASE "MIN"
	Path= LatheOffsetContour(Contour,Tnose_rad,0.0,Dia_allow,Len_allow,Side,Quad,Direction,WsMinLook)
	GOSUB Datastr
	GOSUB Animate_tool
CASE "FULL"
	Path= LatheOffsetContour(Contour,Tnose_rad,0.0,Dia_allow,Len_allow,Side,Quad,Direction,WsFullLook)
	GOSUB Datastr
	GOSUB Animate_tool
CASE "USEANGLE"
	Path= UndercutShift(Contour,Tnose_rad,0.0,Dia_allow,Len_allow,Uncut_angle,Long,Side,Quad,Direction,WsTrue,WsMinLook)
	Path1= PathNoUnders(Path,Uncut_angle,Id_contour,"u"&LWC$(Utype$),Upath_num)
	Deletepaths(Path)
	Path= Path1
	GOSUB Datastr
	GOSUB Animate_tool
CASE "UNDERCUT"
	Path= UndercutShift(Contour,Tnose_rad,0.0,Dia_allow,Len_allow,Uncut_angle,Long,Side,Quad,Direction,WsFalse,WsMinLook)
	IF Upath_num>0 THEN
		Path1= PathNoUnders(Path,Uncut_angle,Id_contour,"u"&LWC$(Utype$),Upath_num,Quad)
		Deletepaths(Path)
		Path= Path1
		GOSUB Datastr
		GOSUB Animate_tool
	ELSE
		Offset_path=Path
		Upath_num=0
		LOOP
			Upath_num= Upath_num+1
			Path= PathNoUnders(Offset_path,Uncut_angle,Id_contour,"u"&LWC$(Utype$),Upath_num,Quad)
		EXIT IF NOT ValidGeometry(Path)
			GOSUB Datastr
			GOSUB Animate_tool
		END LOOP
		Deletepaths(Offset_path)
	END IF
END SELECT

PlotAll


Stat=DeleteEntity(Pt)

GOTO Pick_loop

Animate_tool:!
P1= LthToolInterfere(Tool,Path,Contour)
IF ValidGeometry(P1) THEN
	Stat= DeleteEntity(P1)
ELSE
	IF Animate_tool THEN
		Work_trans= CurrentWorkplane
		Stat= AnimateTool(Tool,Path,Work_trans)
	END IF
END IF
RETURN

Offset_a:!
Path= LatheOffsetContour(Contour,Tnose_rad,0.0,Dia_allow,Len_allow,Side,Quad,Direction,Look)

RETURN


Datastr:!
IF ValidGeometry(Path) THEN
        Datastr$=" LATHE OFFSET "&CHR$(10)
        Datastr$=Datastr$&" "&CHR$(10)
        Stat= GroupName(Path,Boundary_name$)
        Datastr$=Datastr$&"NAME: "&Boundary_name$&CHR$(10)
        Datastr$=Datastr$&"TYPE:  OFFSET"&CHR$(10)
        Stat= GroupName(Contour,Name$)
        Datastr$[LEN(Datastr$)+1]="CONTOUR: "&Name$&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="CONTOUR TYPE: "&RPT$("OD",NOT Id_contour)&RPT$("ID",Id_contour)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="TOOL NOSE RAD: "&VAL$(Tnose_rad)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="TOOL QUADRANT: "&VAL$(Quad)&CHR$(10)
	IF Type$="USEANGLE" OR Type$="UNDERCUT" THEN 
		Datastr$[LEN(Datastr$)+1]="CLEARANCE ANGLE: "&VAL$(Uncut_angle)&CHR$(10)
		Datastr$[LEN(Datastr$)+1]="UNDERCUT TYPE: "&Utype$&CHR$(10)
		IF Type$="UNDERCUT" THEN Datastr$[LEN(Datastr$)+1]="UNDERCUT NUMBER: "&VAL$(Upath_num)&CHR$(10)
	END IF
	Datastr$[LEN(Datastr$)+1]="DIA ALLOWANCE: "&VAL$(Dia_allow)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="LEN ALLOWANCE: "&VAL$(Len_allow)&CHR$(10)
        Datastr$[LEN(Datastr$)+1]="LOOK AHEAD:  "&Type$&CHR$(10)
        Datastr$[LEN(Datastr$)+1]="LATHE DIRECTION: "&RPT$("Forward",Direction=WsForward)&RPT$("Reverse",Direction=WsReverse)&CHR$(10)
        Datastr$[LEN(Datastr$)+1]="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
RETURN


Get_tool_data:!
	Tnose_rad=NamedVarVal("tool nose radius",WsLatheVars,.031)
	Stat=SetInputFormat("n4=","")
	Stat=SetRadiusEquals(Tnose_rad)
	Tnose_rad= InputRadius("TOOL NOSE RADIUS")
	IF ExitScript(Destination$) THEN Leave
	Tnose_rad=ABS(Tnose_rad)

	Quad=NamedVarVal("tool quadrant",WsLatheVars,1.)
	Stat=SetEqualsValue(Quad)
	Quad=GetValue("TOOL QUADRANT",Answer$)
	IF ExitScript(Destination$) THEN Leave

	IF Type$="USEANGLE" THEN 
		Uncut_angle=NamedVarVal("tool clearance angle",WsLatheVars,180.)
		Stat=SetAngleEquals(Uncut_angle)
		Uncut_angle=InputAngle("TOOL CLEARANCE ANGLE")
		IF ExitScript(Destination$) THEN Leave
		Uncut_angle=Uncut_angle MOD 360
		Ent= CreateVariable("tool clearance angle",WsLatheVars,Uncut_angle)
	END IF
RETURN

Leave:!
Stat=SetCurrentColor(Current_pen)
IF ValidGeometry(Work) THEN Stat=SetWorkplane(Work)
Stat=SetInputFormat("n4","")
IF ValidGeometry(Pt) THEN Stat=DeleteEntity(Pt)
LowerSelectBox
IF NOT LEN(Destination$) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END
Deletepaths:SUB Deletepaths(Path)
IF ValidGeometry(Path) THEN
	Stat= DeleteGroup(Path,1)
	Stat= DeleteEntity(Path)
END IF
SUBEND
