
! script name: var_fillet.d/lvarfillet
! var_topic: linear variable fillet

!A
!A NAME:
!A
!A 	Variable Radius Fillet: Linear (R1 - R2)   VARIATIONAL
!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]
DIM Name1$[256]
DIM Name2$[256]
DIM Name$[256]
DIM Radius1_var$[256]
DIM Radius2_var$[256]
DIM Contour$[256]
DIM Patch_var$[256]

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

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
IF Trim_on THEN
	Tol= (Tol <= Trim_tol)*Tol+ Trim_tol*(Tol> Trim_tol)
END IF

Start:!

Ask_fillet: Stat=SetInputFormat("n4=","")
Radius1=InputRadius("START RADIUS")
IF ExitScript(Destination$) THEN Leave
Radius1=ABS(Radius1)
IF Radius1<=0 THEN Ask_fillet
No_radius1_var= LastVariableInput(Radius1_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Ask_fillet
        No_radius1_var=0
        Stat= EntityNameCheck(C1,Radius1_var$)
END IF

Radius2=InputRadius("END RADIUS")
IF ExitScript(Destination$) THEN Leave
Radius2=ABS(Radius2)
IF Radius2<=0 THEN Ask_fillet
No_radius2_var= LastVariableInput(Radius2_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Ask_fillet
        No_radius2_var=0
        Stat= EntityNameCheck(C1,Radius2_var$)
END IF

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

Intersect2:!

Pick_loop:!

Pick_surf1:!
Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE #1",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Surface) THEN Pick_surf1
Stat= EntityNameCheck(Surface,Name1$)
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)
stat= SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE #2",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Surface) THEN Pick_surf2
Stat= EntityNameCheck(Surface,Name2$)

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 ValidDependency(Contour) THEN Pick_drive
Stat= EntityNameCheck(Contour,Contour$)
Stat=Highlight(Contour,WsPickPen,WsDirArrowOn)
Stat=SetPickMask(WsPickGeometry)

Uvalue1=XPointValue(UVpoint1)
Vvalue1=YPointValue(UVpoint1)
Uvalue2=XPointValue(UVpoint2)
Vvalue2=YPointValue(UVpoint2)
Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)

ActivateUndo
Stat= AdvanceUndoNumber("variable fillet")

Stat= GenerateVarName(WsCubicSurfEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("VARIABLE FILLET: Linear")
UVpoint1= PointXYZ(Uvalue1,Vvalue1,0)
UVpoint2= PointXYZ(Uvalue2,Vvalue2,0)
IF No_radius1_var THEN
        V1= FreeParameter("Radius 1",Radius1)
ELSE
        V1= FreeParameter("Radius 1",Radius1_var$)
END IF
IF No_radius2_var THEN
        V2= FreeParameter("Radius 2",Radius2)
ELSE
        V2= FreeParameter("Radius 2",Radius2_var$)
END IF
V3= FreeParameter("Sign1",Normal_sign1)
V4= FreeParameter("Sign2",Normal_sign2)
IF No_patch_var THEN
        V5= FreeParameter("Patch width",Patch_width)
ELSE
        V5= FreeParameter("Patch width",Patch_var$)
END IF
V6= FreeParameter("Tolerance",Tol)
S1=LinearVarFillet(V1,V2,Contour$,Name1$,V3,Name2$,V4,V5,V6)
Stat= NameEntity(S1,Name$)
RecordFunctionOff
IF ValidGeometry(S1) THEN
RecordFunctionOn
IF Trim_surf1 THEN
	Stat= AddItemToUndo(WsUBModifyMode,Name1$)
        Stat=TrimSurfToEdge(Name1$,UVpoint1,S1,4,V6)
END IF
IF Trim_surf2 THEN
	Stat= AddItemToUndo(WsUBModifyMode,Name2$)
        Stat=TrimSurfToEdge(Name2$,UVpoint2,S1,2,V6)
END IF
RecordFunctionOff
END IF
RecordFunctionOn
Stat=DeleteEntityList(UVpoint1,UVpoint2)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(S1) THEN
        DelLastVarRecord
ELSE
	Stat= AddItemToUndo(WsUBCreateMode,S1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

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
