
! script name: vzplanes.d/surfnormwork
! var_topic: workplane determined by a surface normal.

!A
!A NAME:
!A
!A 	Surface Normal Workplane  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script creates a workplane determined by the normal to a
!A	surface. The u curves determine the x-axis direction.
!A
!B INSTRUCTIONS:
!B
!B 	PICK POINT ON SURFACE: Pick an element on the desired workplane.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: pick workplane ")

DIM Answer$[256]
DIM Destination$[256]
DIM Ename$[256]


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

Tol=NamedVarVal("3D Trimming Tol",Ws3DSystemVars,0.005)
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Tol=NamedVarVal("3D Fillet Tol",Ws3DSystemVars,0.005)
Forward_color=3
Reverse_color=4

DeActivateUndo
stat= SetInputFormat("n4","")

Pick_surface:!
Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Surface) THEN Intersect2
Stat= EntityNameCheck(Surface,Surface$)

Pick_point:Stat= SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n4","")
E1=PickEntity("PICK POINT ON SURFACE")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(E1) THEN Pick_point
Stat= EntityNameCheck(E1,Point$)
Pick_para= LastPickPara
Pick_return= CurrPickReturn
P1= ParameterPt(E1,Pick_return)
UVPoint= PointOnSurface(P1,Surface,Tol,WsReturnUVPoint)
IF NOT ValidGeometry(UVpoint) THEN 
	GOTO Pick_point
	Stat= DeleteEntity(P1)
END IF
GOSUB Orient_normal
Stat= DeleteEntity(UVPoint)
Stat= DeleteEntity(P1)

RecordFunctionOn
Stat= RecordDescr("WORKPLANE: Normal to Surface")
Stat= SetRecDelLastExec
Stat= NullReference(Surface$)
Stat= SetWorkplane(0)
SELECT Pick_return
CASE WsGeometryEnd
     Pt= ParameterReturnPt(Point$,Pick_para,WsGeometryEnd)
CASE WsGeometryMid
     Pt= ParameterReturnPt(Point$,Pick_para,WsGeometryMid)
CASE WsGeomCenter
     Pt= ParameterReturnPt(Point$,Pick_para,WsGeomCenter)
END SELECT
V1= FreeParameter("Tolerance",Tol)
V2= FreeParameter("Direction",Norm_sign)
UVPoint= PointOnSurface(Pt,Surface$,V1,WsReturnUVPoint)
Trans1= SurfNormalTrans(Surface$,UVPoint,V2)
Stat= SetWorkplane(Trans1)
Stat= DeleteEntityList(UVPoint,Pt)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF IsRulerOn THEN PlotAll
GOTO Leave


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:!
	IF IsDirReversed(surface)= WsFalse THEN
           Forward_normal=SurfNormalLine(Surface,UVPoint,Normal_len,WsForward)
           Pforward=ParameterPt(Forward_normal,1.0)
           Reverse_normal=SurfNormalLine(Surface,UVPoint,Normal_len,WsReverse)
           Preverse=ParameterPt(Reverse_normal,1.0)
	ELSE
	   Forward_normal=SurfNormalLine(Surface,UVPoint,Normal_len,WsReverse)
           Pforward=ParameterPt(Forward_normal,1.0)
           Reverse_normal=SurfNormalLine(Surface,UVPoint,Normal_len,WsForward)
           Preverse=ParameterPt(Reverse_normal,1.0)
	END IF

        Stat= SetForceVisible(Forward_normal,WsTrue)
        Stat= SetForceVisible(Pforward,WsTrue)
        Stat= SetForceVisible(Reverse_normal,WsTrue)
        Stat= SetForceVisible(Preverse,WsTrue)

        Stat=SetColor(Forward_normal,Forward_color,WsTrue)
        Stat=SetColor(Pforward,Forward_color,WsTrue)
        Stat=SetColor(Reverse_normal,Reverse_color,WsTrue)
        Stat=SetColor(Preverse,Reverse_color,WsTrue)

RETURN



Leave:!
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
stat= SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END

