
! script name: var_fillet.d/flowfillet
! var_topic: flow fillet two surfaces

!A
!A NAME:
!A
!A 	Flow Fillet Surfaces	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script fillets two surfaces using the flow fillet algorithm.
!A
!B INSTRUCTIONS:
!B
!B 	FILLET RADIUS: Enter the 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 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
!C  NOTES:
!C
!C	In the flow curve fillet circles are created in a plane tangent
!C	to a surface1 flow curve in contact with the second surface.
!C	The flow curve fillet surface is swept out by these circles
!C	as the fillet propagates along the surface.

Stat=ChangeLabelStr("ScriptNameStatus","Function: flow curve fillet ")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface_dir$[80]
DIM Name1$[256]
DIM Name2$[256]
DIM Name$[256]
DIM Radius_var$[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 Flow 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=","")
Radius=InputRadius("FILLET RADIUS")
IF ExitScript(Destination$) THEN Leave
IF Radius<=0 THEN Ask_fillet
No_radius_var= LastVariableInput(Radius_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Ask_fillet
        No_radius_var=0
        Stat= EntityNameCheck(C1,Radius_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_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_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 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)
Uvalue1=XPointValue(UVpoint1)
Vvalue1=YPointValue(UVpoint1)
Uvalue2=XPointValue(UVpoint2)
Vvalue2=YPointValue(UVpoint2)
Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)


ActivateUndo
Stat= AdvanceUndoNumber("flow fillet")


Stat= GenerateVarName(WsCubicSurfEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("FLOW FILLET: Surfaces")
V2= FreeParameter("Sign1",Normal_sign1)
V3= FreeParameter("Sign2",Normal_sign2)
IF No_patch_var THEN
        V4= FreeParameter("Patch width",Patch_width)
ELSE
        V4= FreeParameter("Patch width",Patch_var$)
END IF
V5= FreeParameter("Tolerance",Tol)
V6= FreeParameter("UorV curves",Uorv)
UVpoint1= PointXYZ(Uvalue1,Vvalue1,0)
UVpoint2= PointXYZ(Uvalue2,Vvalue2,0)
IF No_radius_var THEN
    V1= FreeParameter("Radius",Radius)
    Surf=FlowFilletSurfs(V1,Name1$,V2,UVpoint1,Name2$,V3,UVpoint2,V4,V5,V6)
ELSE
    Surf=FlowFilletSurfs(Radius_var$,Name1$,V2,UVpoint1,Name2$,V3,UVpoint2,V4,V5,V6)
END IF
Stat= NameEntity(Surf,Name$)
RecordFunctionOff
IF ValidGeometry(Surf) THEN
RecordFunctionOn
IF Trim_surf1 THEN
	Stat= AddItemToUndo(WsUBModifyMode,Name1$)
        Stat=TrimSurfToEdge(Name1$,UVpoint1,Surf,4,V5)
END IF
IF Trim_surf2 THEN
	Stat= AddItemToUndo(WsUBModifyMode,Name2$)
        Stat=TrimSurfToEdge(Name2$,UVpoint2,Surf,2,V5)
END IF
RecordFunctionOff
END IF
RecordFunctionOn
Stat=DeleteEntityList(UVpoint1,UVpoint2)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Surf) THEN
        DelLastVarRecord
ELSE
	Stat= AddItemToUndo(WsUBCreateMode,Surf)
	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

ShowUVDirection:!
	U_line= SurfFlowLine(Surface,U,V,Normal_len,1)
	Pu=ParameterPt(U_line,1.0)
	V_line= SurfFlowLine(Surface,U,V,Normal_len,0)
	Pv=ParameterPt(V_line,1.0)

	Stat= SetForceVisible(U_line,WsTrue)
        Stat= SetForceVisible(V_line,WsTrue)
        Stat= SetForceVisible(Pu,WsTrue)
        Stat= SetForceVisible(Pv,WsTrue)

        Stat=SetColor(U_line,Forward_color)
        Stat=SetColor(Pu,Forward_color)
        Stat=SetColor(V_line,Reverse_color)
        Stat=SetColor(Pv,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)
        D1=DistTwoPts(Pt,Pu)
        D2=DistTwoPts(Pt,Pv)
        Uorv=1
        IF D2<D1 THEN Uorv= 0
	Stat=DeleteEntity(U_line)
	Stat=DeleteEntity(V_line)
	Stat=DeleteEntity(Pu)
	Stat=DeleteEntity(Pv)
	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_line)
Stat=DeleteEntity(V_line)
Stat=DeleteEntity(Pu)
Stat=DeleteEntity(Pv)
Stat=DeleteEntity(Pt)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
