
! script name: var_fillet.d/uvarfillet
! var_topic: uniform variable fillet

!A
!A NAME:
!A
!A 	Variable Radius Fillet: Uniform		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script creates a variable fillet between two surfaces.  The fillet
!A 	radii are specified at points of equal arc length along the generating
!A 	curve.
!A
!B INSTRUCTIONS:
!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
!B 	HOW MANY RADII ARE TO BE SPECIFIED: Enter the number of radii changes
!B 		to be specified along the fillet.	
!B
!B 	RADIUS # : Enter the value of radius #.  The radii should be specified
!B 		in order along the drive curve.
!B
!C NOTES:
!C
!C 	The fillet radius are assigned by breaking the drive curve into equal
!C 	length segments and assigning a radius at each segment. Between the
!C 	prescribed radii the values are splined as a function of arc length
!C 	along the drive curve.
!C	
!C 	The drive curve must lie on both surfaces. The best curves to use as
!C 	drive curves are intersect curves.
!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
!C 	be allowed space to flow between the radii changes.  The various radii
!C 	must be able to fit into each surface at all the points along the drive
!C 	curves.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: uniform 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]
REAL F_radii(100)

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:!
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)


Stat=SetInputFormat("n0=","")
Stat=SetEqualsValue(3)
Num_radii=GetValue("HOW MANY RADII ARE TO BE SPECIFIED ",Answer$)
IF ExitScript(Destination$) THEN Leave

Cgroup=CreateGroup("Circles")
Rcount=1
Pick_fillet_loop:!

Ask_fillet: Stat=SetInputFormat("n4=","")
Radius1=InputRadius("RADIUS #"&VAL$(Rcount))
IF ExitScript(Destination$) THEN Leave
Radius1=ABS(Radius1)
IF Radius1<=0 THEN Ask_fillet
F_radii(Rcount)= Radius1
IF Rcount=Num_radii THEN General_fillet
Rcount=Rcount+1

GOTO Pick_fillet_loop

General_fillet:!
Stat=SetPickMask(WsPickGeometry)


ActivateUndo
Stat= AdvanceUndoNumber("variable fillet")

Stat= GenerateVarName(WsCubicSurfEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("VARIABLE FILLET: Uniform")
Stat= SetWorkplane
UVpoint1= PointXYZ(Uvalue1,Vvalue1,0)
UVpoint2= PointXYZ(Uvalue2,Vvalue2,0)
Cgroup= CreateGroup("FILLET CIRCLES")
RecordFunctionOff
FOR I=1 TO Num_radii
	Radius= F_radii(I)
	A$= "Radius "&VAL$(I)
RecordFunctionOn
        V1= FreeParameter(A$,Radius)
	Pc= PointXYZ(0.0,0.0,0.0)
	C1= RadiusCenter(V1,Pc)
	Stat= AddToGroup(Cgroup,C1)
	Stat= DeleteEntity(Pc)
RecordFunctionOff
NEXT I
RecordFunctionOn
V3= FreeParameter("Sign1",Normal_sign1)
V4= FreeParameter("Sign2",Normal_sign2)
V5= FreeParameter("Flat start",Flat_start)
V6= FreeParameter("Flat end",Flat_end)
IF No_patch_var THEN
        V7= FreeParameter("Patch width",Patch_width)
ELSE
        V7= FreeParameter("Patch width",Patch_var$)
END IF
V8= FreeParameter("Tolerance",Tol)
S1=UniformVarFillet(Cgroup,Contour$,Name1$,V3,Name2$,V4,V7,V8,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,V8)
END IF
IF Trim_surf2 THEN
	Stat= AddItemToUndo(WsUBModifyMode,Name2$)
        Stat=TrimSurfToEdge(Name2$,UVpoint2,S1,2,V8)
END IF
RecordFunctionOff
END IF
RecordFunctionOn
Stat=DeleteEntityList(UVpoint1,UVpoint2)
Stat=DeleteGroup(Cgroup,1)
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:!
IF ValidGeometry(Cgroup) THEN
	Stat=DeleteGroup(Cgroup,1)
END IF
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
