
! script name: 3dfillet.d/lvarfillet
! 3dtopic: linear variable fillet

!A
!A NAME:
!A
!A 	Variable Radius Fillet: Linear (R1 - R2)
!A
!A DESCRIPTION:
!A
!A 	This script creates a variable fillet between two surfaces.  The fillet
!A 	varies between r1 and r2 in a linear fashion along the drive curve.
!A
!B INSTRUCTIONS:
!B
!B 	START RADIUS: Enter the starting radius of the fillet to be constructed.
!B
!B 	END RADIUS: Enter the ending radius of the fillet to be constructed.
!B
!B 	PATCH WIDTH: Enter the width of the patch to be used.  The patch width
!B 		is along the length of the fillet and the radii that will be
!B 		encountered should be used to determine the patch width.
!B
!B 	PICK SURFACE #1: Pick one of the surfaces to be used for the fillet.
!B 		The pick location will determine the fillet generated.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick which side of surface #1 the fillet is
!B 		on.  The normal vectors will be displayed to allow the side to
!B 		be picked.
!B
!B 	PICK SURFACE #2: Pick the other surface to be used for the fillet.
!B 		The pick location will determine the fillet generated.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick which side of surface #2 the fillet is
!B 		on.  The normal vectors will be displayed to allow the side to
!B 		be picked.
!B
!B 	PICK DRIVE CURVE: Pick the curve generating the variable fillet.  
!B
!C NOTES:
!C
!C 	The drive curve must lie on both surfaces. The best curves to use as
!C 	drive curves are intersect curves. The variable fillet will assume
!C 	radius r1 at the start of the curve and will assume radius r2 at the
!C 	end. Otherwise it varys as a linear function of the arc length of the
!C 	drive curve. 
!C
!C 	When we say that the fillet assumes a radius along the curve we mean
!C 	that if a plane is constructed normal to the drive curve at a given
!C 	point, the circle that is in this plane and tangent to the two surfaces
!C 	has the specified radius. The variable fillet surface is the locus of
!C 	all such circles constructed along the drive curve with the varying
!C 	radii.
!C
!C 	CAUTION: It may not always be possible to generate a smooth fillet
!C 	surface matching the radius data. The variable fillet surface must be
!C 	allowed space to flow between the radii changes. The various radii must
!C 	be able to fit into each surface at all the points along the drive
!C 	curves.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: linear variable fillet ")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface_dir$[80]

ActivateUndo
Patch_width=NamedVarVal("3D Default Patch",Ws3DSystemVars,0.25)
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Tol=NamedVarVal("3D Fillet Tol",Ws3DSystemVars,0.005)
Trim_tol=NamedVarVal("3D Trimming Tol",Ws3DSystemVars,0.005)
Trim_on=NamedVarVal("3D Trim Var Fillets",Ws3DSystemVars,1.0)
Trim_surf1=Trim_on
Trim_surf2=Trim_on
Forward_color=3
Reverse_color=4

Start:!
StdWorkplane

Ask_fillet: Stat=SetInputFormat("n4=","")
Radius1=InputRadius("START RADIUS")
IF ExitScript(Destination$) THEN Leave
Radius1=ABS(Radius1)
IF Radius1<=0 THEN Ask_fillet

Radius2=InputRadius("END RADIUS")
IF ExitScript(Destination$) THEN Leave
Radius2=ABS(Radius2)
IF Radius2<=0 THEN Ask_fillet

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Patch_width)
Patch_width=GetValue("PATCH WIDTH",Answer$)
IF ExitScript(Destination$) THEN Leave

Intersect2: StdWorkplane

Pick_loop:!

Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE #1",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Pick_loop

UVpoint1=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint1) THEN Pick_loop
U=XPointValue(UVpoint1)
V=YPointValue(UVpoint1)

GOSUB Orient_normal
Surface1=Surface
Normal_sign1=norm_sign

Pick_surf2:!
Stat=SetPickMask(WsPickSurfOnly)
Surface=PickEntity("PICK SURFACE #2",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Pick_surf2

UVpoint2=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint2) THEN Pick_surf2
U=XPointValue(UVpoint2)
V=YPointValue(UVpoint2)

GOSUB Orient_normal
Surface2=Surface
Normal_sign2=norm_sign

Stat=SetPickMask(WsPickGeometry)

Pick_Drive: Stat=SetPickMask(WsPickNoSurf)
Contour=PickGroupEnt("PICK DRIVE CURVE")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Contour) THEN Pick_Drive
Stat=Highlight(Contour,WsPickPen,WsDirArrowOn)
Stat=SetPickMask(WsPickGeometry)

Fsurf=LinearVarFillet(Radius1,Radius2,Contour,Surface1,Normal_sign1,Surface2,Normal_sign2,Patch_width,Tol)

ActivateUndo
Stat= AdvanceUndoNumber("var fillet")
Stat= AddItemToUndo(WsUBCreateMode,Fsurf)

IF ValidGeometry(Fsurf) THEN 
	IF Trim_surf1 THEN
		Which1=4
		Stat= AddItemToUndo(WsUBModifyMode,Surface1)
		Stat=TrimSurfToEdge(Surface1,UVpoint1,Fsurf,Which1,Trim_tol)
	END IF

	IF Trim_surf2 THEN
		Which2=2
		Stat= AddItemToUndo(WsUBModifyMode,Surface2)
		Stat=TrimSurfToEdge(Surface2,UVpoint2,Fsurf,Which2,Trim_tol)
	END IF
END IF

Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)

GOTO Intersect2

Orient_normal:!
	GOSUB ShowNormalDir
Pick_normal:L1=PickSpecificEnt("PICK WHICH SIDE OF SURFACE",WsPickLines)
	IF ExitScript(Destination$) THEN Leave
	IF NOT ValidGeometry(L1) THEN Line_loop
	Norm_sign=0
	Pt=ParameterPt(L1,1.0)
	D1=DistTwoPts(Pt,Pforward)
	D2=DistTwoPts(Pt,Preverse)
	Norm_sign=1
	IF D2<D1 THEN Norm_sign=-1
	IF NOT Norm_sign THEN Pick_normal
	Stat=DeleteEntity(Forward_normal)
	Stat=DeleteEntity(Reverse_normal)
	Stat=DeleteEntity(Pt)
	Stat=DeleteEntity(Pforward)
	Stat=DeleteEntity(Preverse)
RETURN


ShowNormalDir:!
	Forward_normal=SurfNormalLine(Surface,U,V,Normal_len,WsForward)
	Pforward=ParameterPt(Forward_normal,1.0)

	Reverse_normal=SurfNormalLine(Surface,U,V,Normal_len,WsReverse)
	Preverse=ParameterPt(Reverse_normal,1.0)

	Stat= SetForceVisible(Forward_normal,WsTrue)
        Stat= SetForceVisible(Pforward,WsTrue)
        Stat= SetForceVisible(Reverse_normal,WsTrue)
        Stat= SetForceVisible(Preverse,WsTrue)

        Stat=SetColor(Forward_normal,Forward_color)
        Stat=SetColor(Pforward,Forward_color)
        Stat=SetColor(Reverse_normal,Reverse_color)
        Stat=SetColor(Preverse,Reverse_color)
	
RETURN


Leave:!
Stat=SetPickMask(WsPickGeometry)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
