
! script name: vpksheets.d/vpktangentplane
! var_solids: tangent plane sheet body  to a surface

!A
!A NAME:
!A
!A 	SHEET: Tangent Plane	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will create a plane tangent to a surface, at a point on
!A 	the surface.
!A
!B INSTRUCTIONS:
!B
!B 	PLANE LENGTH: Enter the length of the plane.
!B
!B 	PICK SURFACE: Pick the surface that will be tangent to the constructed
!B 		plane.
!B
!B 	PICK POINT ON SURFACE: Pick the point on the surface that will
!B 		determine the tangent point of the plane and the surface.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: tangent plane ")

DIM Destination$[256]
DIM Answer$[256]
DIM XLength_var$[256]
DIM Name$[256]
DIM Element$[256]
DIM Center$[256]

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

ActivateUndo
Units= CurrentUnits
IF Units= WsMetric THEN
	Xlength= 25
	Ylength= 25
ELSE
	Xlength= 1
	Ylength= 1
END IF


Tol=NamedVarVal("3D Intersect Tol",Ws3DSystemVars,0.005)

Stat= GLPlotOn
Start:!

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Xlength)
XLength=GetValue("PLANE LENGTH")
IF ExitScript(Destination$) THEN Leave
No_xlength_var= LastVariableInput(xlength_var$)
Ylength=Xlength

Intersect2:!

Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n4","")
Surface=PickEntity("PICK SURFACE ")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Surface) THEN Intersect2
Stat= EntityNameCheck(Surface,Element$)
Stat=SetPickMask(WsPickNoSurf)

Pick_point:Stat=SetInputFormat("n4=","")
E1=PickEntity("PICK POINT ON SURFACE")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E1) THEN Pick_point
T= LastPickPara
Pick_return= CurrPickReturn
Stat= EntityNameCheck(E1,Center$)
Pc= ParameterReturnPt(E1,T,Pick_return)
Stat=SetEqualsValue(Radius)
Stat=SetLastPoint(Pc)
Stat= DeleteEntity(Pc)

Stat=SetPickMask(WsPickAll)

WPKClearErrorFlag
Stat= GenerateVarName("Body",Name$)
RecordFunctionOn
Stat= RecordDescr("SHEET: Tangent plane to surface")
SELECT Pick_return
CASE WsGeometryEnd
        Pc= ParameterReturnPt(Center$,T,WsGeometryEnd)
CASE WsGeometryMid
        Pc= ParameterReturnPt(Center$,T,WsGeometryMid)
CASE WsGeomCenter
        Pc= ParameterReturnPt(Center$,T,WsGeomCenter)
END SELECT
IF No_xlength_var THEN
        V1= FreeParameter("Length",Xlength)
ELSE
        V1= FreeParameter("Length",Xlength_var$)
END IF
V2= FreeParameter("Tolerance",Tol)
Stat=Set3dInterOffset(0.0)
P1=TangentPlane(WsPlaneSolidEnt,Element$,Pc,V1,V2)
RecordFunctionOff
IF ValidGeometry(P1) THEN
        Make_record= WsTrue
        RecordFunctionOn
        Pkbody=WPKBasicSolid(P1)
        Stat= WPKNameBody(Pkbody,Name$)
        Stat= WPKSetBodyParent(Pkbody)
        Stat= DeleteEntity(P1,WsTrue)
        RecordFunctionOff
ELSE
        Make_record= WsFalse
END IF
RecordFunctionOn
Stat=DeleteEntity(Pc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF Make_record= WsFalse THEN
        DelLastVarRecord
ELSE
        Stat= WPKSolidRecordOn(Curr_record)
        ActivateUndo
        Stat= AdvanceUndoNumber("disk sheet")
        Stat= AddItemToUndo(WsUBPKSessionMark)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
        WPKErrorCheck
END IF
GOTO Intersect2

Leave:!
Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickAll)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
