
! script name: var_fillet.d/cornerblend
! var_topic: corner blend 

!A
!A NAME:
!A
!A 	Corner Blend: Blending Three Radii at a corner. VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script is used to blend Three different radii together
!A	at a corner.
!A
!B INSTRUCTIONS:
!B
!B 	PICK LARGEST FILLET SURFACE: Pick the largest fillet coming to the
!B 		corner.  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 	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 OPPOSITE SURFACE: Pick the surface opposite the largest 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	This routine is used to generate a surface in a corner where
!C	three fillets come together.
!C	The following procedure must be followed.
!C
!C		1: First generate the fillet surfaces.
!C		2: The largest fillet to generate the blend.
!C		3: Pick the largest fillet on the side of the starting radius.
!C		4: Pick the surface opposite the largest fillet.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: corner blend ")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface_dir$[80]
DIM Name1$[256]
DIM Name2$[256]
DIM Name$[256]
DIM Rad1$[256]
DIM Rad2$[256]
DIM Sradius_var$[256]
DIM Eradius_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)
Forward_color=3
Reverse_color=4

Patch_width= Patch_width/2

Start:!

Ask_fillet1: Stat=SetInputFormat("n4=","")
Start_radius=InputRadius("STARTING RADIUS")
IF ExitScript(Destination$) THEN Leave
IF Start_radius< 0 THEN Ask_fillet1
No_sradius_var= LastVariableInput(Sradius_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Ask_fillet1
        No_sradius_var=0
        Stat= EntityNameCheck(C1,Sradius_var$)
END IF

Ask_fillet2: Stat=SetInputFormat("n4=","")
End_radius=InputRadius("ENDING RADIUS")
IF ExitScript(Destination$) THEN Leave
IF End_radius< 0 THEN Ask_fillet2
No_eradius_var= LastVariableInput(Eradius_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Ask_fillet2
        No_eradius_var=0
        Stat= EntityNameCheck(C1,Eradius_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 LARGEST FILLET SURFACE",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)
Uorv= 1
Direction= WsForward
IF (U> 0.5) THEN Direction= WsReverse


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


Smooth= WsTrue
IF Direction= WsForward THEN
        R1= Start_radius
        Rad1$= Sradius_var$
        no_r1_var= no_sradius_var
        R2= End_radius
        Rad2$= Eradius_var$
        no_r2_var= no_eradius_var
ELSE
        R2= Start_radius
        Rad2$= Sradius_var$
        no_r2_var= no_sradius_var
        R1= End_radius
        Rad1$= Eradius_var$
        no_r1_var= no_eradius_var
END IF

Stat= GenerateVarName(WsCubicSurfEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("FILLET: Three different radii ")
V1= FreeParameter("Sign1",Normal_sign1)
V2= FreeParameter("Sign2",Normal_sign2)
V3= FreeParameter("Smooth",Smooth)
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_r1_var THEN
        Vr1= FreeParameter("R1",R1)
        IF No_r2_var THEN
          Vr2= FreeParameter("R2",R2)
          Surf=VarFlowFillet(Vr1,Vr2,Name1$,V1,UVpoint1,Name2$,V2,UVpoint2,V4,V5,V6,V3,WsTrue)
        ELSE
          Surf=VarFlowFillet(Vr1,Rad2$,Name1$,V1,UVpoint1,Name2$,V2,UVpoint2,V4,V5,V6,V3,WsTrue)
        END IF
ELSE
        IF No_r2_var THEN
          Vr2= FreeParameter("R2",R2)
          Surf=VarFlowFillet(Rad1$,Vr2,Name1$,V1,UVpoint1,Name2$,V2,UVpoint2,V4,V5,V6,V3,WsTrue)
        ELSE
          Surf=VarFlowFillet(Rad1$,Rad2$,Name1$,V1,UVpoint1,Name2$,V2,UVpoint2,V4,V5,V6,V3,WsTrue)
        END IF
END IF
Stat= NameEntity(Surf,Name$)
Stat=DeleteEntityList(UVpoint1,UVpoint2)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Surf) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("corner blend")
	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_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


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
