
! script name: 3dfillet.d/pvarfillet
! 3dtopic: variable fillet from radius profile curve

!A
!A NAME:
!A
!A 	Variable Radius Fillet: Rad Profile Curve
!A
!A DESCRIPTION:
!A
!A 	This script creates a variable fillet between two surfaces.
!A	The fillet radii are by a radius profile 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 	PICK RADIUS PROFILE CURVE: Pick the curve which describes the radii
!B 		as a function of the arc length along the drive curve.  
!B
!C NOTES:
!C
!C 	The drive curve must lie on both surfaces.
!C
!C	The radius profile curve must be a smooth curve in the XY plane.  This
!C 	curve is to be constructed with the X axis representing the arc length
!C 	along the drive curve. The Y axis values are the radii to be assigned
!C 	to the variable fillet. The curve must extend exactly the length of the
!C 	drive curve along the x axis.
!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: variable fillet from profile curve")

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)

Pick_profile: Profile=PickGroupEnt("PICK RADIUS PROFILE CURVE")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Profile) THEN Pick_Profile
Stat=Highlight(Profile,WsPickPen,WsDirArrowOff)

Stat=SetPickMask(WsPickGeometry)

General_fillet:!

Fsurf=ProfileVarFillet(Profile,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
