
! script name: 3dfillet.d/varplnfillet
! 3dtopic: variable plane fillet two surfaces

!A
!A NAME:
!A
!A 	Variable Plane Fillet Surfaces
!A
!A DESCRIPTION:
!A
!A 	This script fillets two surfaces using the Plane fillet algorithm.
!A
!B INSTRUCTIONS:
!B
!B 	STARTING RADIUS: Enter the first radius.
!B
!B 	ENDING RADIUS: Enter the second radius.
!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 can affect the fillet generated.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick the side of surface #1 .
!B 		The normal vectors will be displayed to allow the side to be
!B 		picked.
!B
!B 	PICK SURFACE #2: Pick the other surface to be used for the fillet.
!B 		The pick location can affect the fillet generated.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick the side of surface #2 .
!B 		The normal vectors will be displayed to allow the side to be
!B 		picked.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: variable plane 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 Flow Fillets",Ws3DSystemVars,1.0)
Trim_surf1=Trim_on
Trim_surf2=Trim_on
Forward_color=3
Reverse_color=4

Start:!

Ask_fillet1: Stat=SetInputFormat("n4=","")
Start_radius=InputRadius("STARTING RADIUS")
IF ExitScript(Destination$) THEN Leave
IF Start_radius< 0 THEN Ask_fillet1

Ask_fillet2: Stat=SetInputFormat("n4=","")
End_radius=InputRadius("ENDING RADIUS")
IF ExitScript(Destination$) THEN Leave
IF End_radius< 0 THEN Ask_fillet2

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


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

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

Stat=SetPickMask(WsPickGeometry)
GOSUB Orient_normal
Surface1=Surface
Normal_sign1=norm_sign

Stat=SetPickMask(WsPickGeometry)
GOSUB UorV_direction


Pick_surf2:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
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)

Smooth= WsFalse
IF Direction= WsForward THEN
        R1= Start_radius
        R2= End_radius
ELSE
        R2= Start_radius
        R1= End_radius
END IF

ActivateUndo
Stat= AdvanceUndoNumber("plane fillet")

Fsurf=VarPlaneFillet(R1,R2,Surface1,Normal_sign1,UVpoint1,Surface2,Normal_sign2,UVpoint2,Patch_width,Tol,Uorv,Smooth)

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

ShowUVDirection:!
        U_linef= SurfFlowLine(Surface,U,V,Normal_len,1)
        Puf=ParameterPt(U_linef,1.0)
        U_liner= SurfFlowLine(Surface,U,V,-Normal_len,1)
        Pur=ParameterPt(U_liner,1.0)

        V_linef= SurfFlowLine(Surface,U,V,Normal_len,0)
        Pvf=ParameterPt(V_linef,1.0)

        V_liner= SurfFlowLine(Surface,U,V,-Normal_len,0)
        Pvr=ParameterPt(V_liner,1.0)

	Stat= SetForceVisible(U_linef,WsTrue)
	Stat= SetForceVisible(U_liner,WsTrue)
	Stat= SetForceVisible(V_linef,WsTrue)
	Stat= SetForceVisible(V_liner,WsTrue)
	Stat= SetForceVisible(Puf,WsTrue)
	Stat= SetForceVisible(Pur,WsTrue)
	Stat= SetForceVisible(Pvf,WsTrue)
	Stat= SetForceVisible(Pvr,WsTrue)

        Stat=SetColor(U_linef,Forward_color)
        Stat=SetColor(Puf,Forward_color)
        Stat=SetColor(U_liner,Forward_color)
        Stat=SetColor(Pur,Forward_color)
        Stat=SetColor(V_linef,Reverse_color)
        Stat=SetColor(Pvf,Reverse_color)
        Stat=SetColor(V_liner,Reverse_color)
        Stat=SetColor(Pvr,Reverse_color)
RETURN

UorV_direction:!
        GOSUB ShowUVDirection
        Stat=SetInputFormat("n4","")
Pick_uorv: L1=PickSpecificEnt("PICK PROPAGATION DIRECTION ",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Pick_uorv
        Pt=ParameterPt(L1,1.0)
        Du1=DistTwoPts(Pt,Puf)
        Dv1=DistTwoPts(Pt,Pvf)
        Du2=DistTwoPts(Pt,Pur)
        Dv2=DistTwoPts(Pt,Pvr)
        Uorv=1
        Direction= WsForward
        IF (Du1<= Dv1) THEN
                IF (Du1<= Du2) THEN
                        IF (Du1<= Dv2) THEN
                                Uorv= 1
                                Direction= WsForward
                        ELSE
                                Uorv= 0
                                Direction= WsReverse
                        END IF
                ELSE
                        IF(Du2<= Dv2) THEN
                                Uorv= 1
                                Direction= WsReverse
                        ELSE
                                Uorv= 0
                                Direction= WsReverse
                        END IF
                END IF
        ELSE
               IF (Dv1<= Du2) THEN
                        IF (Dv1<= Dv2) THEN
                                Uorv= 0
                                Direction= WsForward
                        ELSE
                                Uorv= 0
                                Direction= WsReverse
                        END IF
                ELSE
                        IF(Du2<= Dv2) THEN
                                Uorv= 1
                                Direction= WsReverse
                        ELSE
                                Uorv= 0
                                Direction= WsReverse
                        END IF
                END IF
        END IF
        Stat=DeleteEntity(U_linef)
        Stat=DeleteEntity(U_liner)
        Stat=DeleteEntity(V_linef)
        Stat=DeleteEntity(V_liner)
        Stat=DeleteEntity(Puf)
        Stat=DeleteEntity(Pur)
        Stat=DeleteEntity(Pvf)
        Stat=DeleteEntity(Pvr)

        Stat=DeleteEntity(Pt)
RETURN

Leave:!
Stat=SetInputFormat("n4","")
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)
Stat=DeleteEntity(U_linef)
Stat=DeleteEntity(U_liner)
Stat=DeleteEntity(V_linef)
Stat=DeleteEntity(V_liner)
Stat=DeleteEntity(Puf)
Stat=DeleteEntity(Pur)
Stat=DeleteEntity(Pvf)
Stat=DeleteEntity(Pvr)
Stat=DeleteEntity(Pt)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
