
! script name: vsolids.d/sphere3surfs
! var_topic: sphere tangent to three surfaces

!A
!A NAME:
!A
!A 	Sphere tangent to 3 surfaces. VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will generate a sphere of known radius tangent
!A 	to three surfaces.
!A
!B INSTRUCTIONS:
!B
!B 	PICK SURFACE #1: Pick the first surface.  Note: The pick location is
!B 		important.
!B
!B 	PICK SURFACE #2: Pick the the second surface.  Note: The pick location
!B 		is important.
!B
!B 	PICK SURFACE #3: Pick the third surface.  Note: The pick location is
!B 		important.
!B
!C NOTES:
!C
!C 	The pick location should be near the desired location, failure
!C 	to do this may result in no point generated.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: sphere tangent 3 surfaces ")

DIM Destination$[256]
DIM Answer$[256]
DIM Radius$[256]
DIM name$[256]
DIM name1$[256]
DIM name2$[256]
DIM name3$[256]

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave


ActivateUndo
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Tol=NamedVarVal("3D Fillet Tol",Ws3DSystemVars,0.005)
Forward_color=3
Reverse_color=4
Units= CurrentUnits
IF Units= WsMetric THEN
        Radius= 25
ELSE
        Radius= 1
END IF


Start:!
Stat=SetInputFormat("n4=","")
Stat=SetRadiusEquals(Radius)
Radius=InputRadius("RADIUS")
IF ExitScript(Destination$) THEN Leave
No_radius_var= LastVariableInput(Radius_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Cloop
        No_radius_var=0
        Stat= EntityNameCheck(C1,Radius_var$)
END IF



Intersect2:!
Pick_surf1:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface1=PickEntity("PICK SURFACE #1",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Surface1) THEN Pick_surf1
Stat= EntityNameCheck(Surface1,Name1$)
UVpoint1=LastUVSurfPick(Surface1)
IF NOT ValidGeometry(UVpoint1) THEN Pick_surf1
U=XPointValue(UVpoint1)
V=YPointValue(UVpoint1)
Surface= Surface1
GOSUB Orient_normal
Normal_sign1=norm_sign


Pick_surf2:!
Surface2=PickEntity("PICK SURFACE #2",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Surface2) THEN Pick_surf2
Stat= EntityNameCheck(Surface2,Name2$)
UVpoint2=LastUVSurfPick(Surface2)
IF NOT ValidGeometry(UVpoint2) THEN Pick_surf2
U=XPointValue(UVpoint2)
V=YPointValue(UVpoint2)
Surface= Surface2
GOSUB Orient_normal
Normal_sign2=norm_sign


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


Stat=SetPickMask(WsPickGeometry)
Uvalue1=XPointValue(UVpoint1)
Vvalue1=YPointValue(UVpoint1)
Uvalue2=XPointValue(UVpoint2)
Vvalue2=YPointValue(UVpoint2)
Uvalue3=XPointValue(UVpoint3)
Vvalue3=YPointValue(UVpoint3)
Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)
Stat=DeleteEntity(UVpoint3)


Stat= GenerateVarName(WsSphereSolidEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("SPHERE: Tangent to 3 surfaces")
Stat= SetRecDelLastExec
IF No_radius_var THEN
        V1= FreeParameter("Radius",Radius)
ELSE
        V1= FreeParameter("Radius",Radius_var$)
END IF
V2= FreeParameter("Tolerance",Tol)
V3= FreeParameter("Side 1",Normal_sign1)
V4= FreeParameter("Side 2",Normal_sign2)
V5= FreeParameter("Side 3",Normal_sign3)
UVpoint1= PointXYZ(Uvalue1,Vvalue1,0)
UVpoint2= PointXYZ(Uvalue2,Vvalue2,0)
UVpoint3= PointXYZ(Uvalue3,Vvalue3,0)
S1=Sphere3Tangents(V1,Name1$,UVpoint1,V3,Name2$,UVpoint2,V4,name3$,UVpoint3,V5,V2)

Stat= NameEntity(S1,Name$)
stat= DeleteEntityList(UVpoint1,UVpoint2,UVpoint3)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(S1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("sphere")
	Stat= AddItemToUndo(WsUBCreateMode,S1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Start

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)
        Stat=SetColor(Forward_normal,Forward_color)
        Stat=SetColor(Pforward,Forward_color)

        Reverse_normal=SurfNormalLine(Surface,U,v,Normal_len,WsReverse)
        Preverse=ParameterPt(Reverse_normal,1.0)
        Stat=SetColor(Reverse_normal,Reverse_color)
        Stat=SetColor(Preverse,Reverse_color)
RETURN


Leave:!
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)
Stat=DeleteEntity(UVpoint3)
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
