
! script name: var_fillet.d/surfacefillet
! var_topic: generate a fillet between two surfaces

!A
!A NAME:
!A
!A 	Fillet Surfaces		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will generate a fillet between two surfaces.
!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.
!B
!B 	PICK SURFACE #1: Pick the first surface to be used for the surface
!B 		fillet.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick which side of SURFACE 1 the fillet
!B 		is to be constructed on.
!B
!B 	PICK SURFACE #2: Pick the other surface to be used for the surface
!B 		fillet.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick which side of SURFACE 2 the fillet
!B 		is to be constructed on.
!B
!C NOTES:
!C
!C	The fillet surface is generated in such a way as to propagate
!C	in the same direction as the u-curves in the first surface picked.
!C	This is done to insure consistant generation for vdm.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: surface 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)
Forward_color= 3
Reverse_color= 4

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$)
GOSUB Orient_normal
Surface1= Surface
Normal_sign1= norm_sign


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$)
GOSUB Orient_normal
Surface2= Surface
Normal_sign2= norm_sign

Stat= SetPickMask(WsPickGeometry)


Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("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)
IF No_radius_var THEN
	V1= FreeParameter("Radius",Radius)
	G1= FilletSurfaces(V1,Name1$,V2,Name2$,V3,V4,V5)
ELSE
	G1= FilletSurfaces(Radius_var$,Name1$,V2,Name2$,V3,V4,V5)
END IF
RecordFunctionOff
IF ValidGeometry(G1) THEN
	RecordFunctionOn
	Stat= AutoNameGroup(G1,Name$)
	ActivateUndo
	Stat= AdvanceUndoNumber("fillet surfaces")
	Stat= AddGroupToUndo(WsUBCreateMode,G1,0,WsTrue)
	stat= DeleteEntity(G1)
	RecordFunctionOff
	No_fillet=0
ELSE
	No_fillet=1
END IF
RecordFunctionOn
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF No_fillet THEN
        DelLastVarRecord
ELSE
	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:!
	PlotOff
        Uvpoint= ValidUVPoint(Surface,0.5,0.5)
        U= XPointValue(Uvpoint)
        V= YPointValue(Uvpoint)
        Stat= DeleteEntity(Uvpoint)
        PlotOn
        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= SetInputFormat("n4","")
Stat= SetPickMask(WsPickGeometry)
stat= DeleteEntity(Forward_normal)
stat= DeleteEntity(Reverse_normal)
stat= DeleteEntity(Pt)
stat= DeleteEntity(Pforward)
stat= DeleteEntity(Preverse)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
