
! script name: 3dfillet.d/threecorner
! 3dtopic: three surface fillet

!A
!A NAME:
!A
!A 	Fillet: 3 Surfaces
!A
!A DESCRIPTION:
!A
!A 	This script will create a fillet tangent to three intersecting
!A 	surfaces.  The pick location on each surface will determine the
!A 	fillet generated.
!A
!B INSTRUCTIONS:
!B
!B	FILLET RADIUS: Enter the fillet radius.
!B
!B	PATCH WIDTH: Enter the patch width.
!B
!B 	PICK SURFACE #1: Pick the first surface.
!B 		Note: The pick location is important.
!B
!B 	PICK SURFACE #2: Pick the the second surface.
!B 		Note: The pick location is important.
!B
!B 	PICK SURFACE #3: Pick the third surface.
!B 		Note: The pick location is important.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: Three surface 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)
Forward_color=3
Reverse_color=4
Patch_width=0.5*Patch_width

Start:!
StdWorkplane

Stat=Setup3dCutter(WsNullTool,0.0,0.0,0.0,0.0,0.0)

Ask_fillet: Stat=SetInputFormat("n4=","")
Radius=InputRadius("FILLET RADIUS")
IF ExitScript(Destination$) THEN Leave

IF Radius<=0 THEN Ask_fillet

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

Intersect2: StdWorkplane

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

Pick_surf2:!
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

Pick_surf3:!
Surface=PickEntity("PICK SURFACE #3",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Pick_surf3
UVpoint3=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint3) THEN Pick_surf3
U=XPointValue(UVpoint3)
V=YPointValue(UVpoint3)
GOSUB Orient_normal
Surface3=Surface
Normal_sign3=norm_sign

Stat=SetPickMask(WsPickGeometry)

Fillet_surf=ThreeCornerFillet(Radius,Surface1,UVpoint1,Normal_sign1,Surface2,UVpoint2,Normal_sign2,Surface3,UVpoint3,Normal_sign3,Patch_width,Tol)

Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)
Stat=DeleteEntity(UVpoint3)

ActivateUndo
Stat= AdvanceUndoNumber("fillet")
Stat= AddItemToUndo(WsUBCreateMode,Fillet_surf)
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=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(UVpoint3)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
