
! script name: 3dfillet.d/gvarfillet
! 3dtopic: general variable fillet

!A
!A NAME:
!A
!A 	Variable Radius Fillet: General
!A
!A DESCRIPTION:
!A
!A 	This script creates a variable fillet between two surfaces.  The fillet
!A 	radii are specified at various points along the generating 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 #2 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 	LOCATION OF RADIUS # : Enter the coordinates or pick the location along
!B 		the curve of the radius.  The radius locations must be specified
!B 		in order along the drive curve.
!B
!B 	RADIUS # : Enter the value of radius #.
!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 	the specified radii at the prescribed points.  Between the points the
!C 	radii are assigned by creating a spline curve relating the radii as a
!C 	function of arc length along the curve.  Since the spline curve
!C 	generating the radii is smooth it may be possible that the radii
!C 	generated between the specified radii will be smaller or larger
!C 	than the largest and smallest specified radius.
!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: general 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
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=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:!

Stat=SetInputFormat("n4=","")
Pc=XyzInput("LOCATION OF RADIUS #"&VAL$(Rcount)&"  ",WsXyzInput)
IF ExitScript(Destination$) THEN Leave

Ask_fillet: Stat=SetInputFormat("n4=","")
Radius1=InputRadius("RADIUS #"&VAL$(Rcount))
IF ExitScript(Destination$) THEN Leave

Radius1=ABS(Radius1)
IF Radius1<=0 THEN Ask_fillet

C1=RadiusCenter(Radius1,Pc)
Stat=HideEntity(C1)
Stat=AddToGroup(Cgroup,C1)
Stat=DeleteEntity(Pc)

IF Rcount=Num_radii THEN General_fillet
Rcount=Rcount+1

GOTO Pick_fillet_loop

General_fillet:!
Stat=SetPickMask(WsPickGeometry)

!  Flat_start and flat_end control start and end of radius profile 
Flat_start=1
Flat_end=1

Fsurf=GeneralVarFillet(Cgroup,Contour,Surface1,Normal_sign1,Surface2,Normal_sign2,Patch_width,Tol,Flat_start,Flat_end)

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)
Stat=DeleteGroup(Cgroup,1)

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
