
! script name: v3dinter.d/surfnormline
! var_topic: create surface normal line.

!A
!A NAME:
!A
!A 	Surface Normal Line
!A
!A DESCRIPTION:
!A
!A 	This script makes a normal line to a surface.
!A
!B INSTRUCTIONS:
!B
!B	NORMAL LINE LENGTH: Length of the normal line.
!B
!B 	PICK SURFACE: Pick the surface.
!B
!B 	INPUT POINT ON SURFACE: Pick a point on the surface.
!B
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: surface normal line ")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface$[256]
DIM Point$[256]
DIM Name$[256]
DIM Length_var$[256]

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

Tol=NamedVarVal("3D Trimming Tol",Ws3DSystemVars,0.005)
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

ActivateUndo
Start:!
Intersect2:!

Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Normal_len)
Normal_len=GetValue("NORMAL LINE LENGTH")
IF ExitScript(Destination$) THEN Leave
No_length_var= LastVariableInput(Length_var$)


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 Pick_point
GOSUB Orient_normal
Stat= DeleteEntity(UVPoint)
Stat= DeleteEntity(P1)

Stat= GenerateVarName(WsLineEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("LINE: Surface normal line")
Stat= SetRecDelLastExec
Stat= NullReference(Surface$)
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)
UVPoint= PointOnSurface(Pt,Surface$,V1,WsReturnUVPoint)
RecordFunctionOff
IF NOT ValidGeometry(UVPoint) THEN
        Stat= DeleteEntity(UVPoint)
        Stat= DeleteEntity(Pt)
        CloseVarRecord
        DelLastVarRecord
        GOTO Pick_point
END IF
RecordFunctionOn
IF No_length_var THEN
	V2= FreeParameter("Normal length",Normal_len)
ELSE
	V2= FreeParameter("Normal length",Length_var$)
END IF
V3= FreeParameter("Normal sign",Norm_sign)
L1=SurfNormalLine(Surface$,UVPoint,V2,V3)
Stat= NameEntity(L1,Name$)
Stat= DeleteEntityList(UVPoint,Pt)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(L1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("normal line")
	Stat= AddItemToUndo(WsUBCreateMode,L1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Pick_point


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,UVPoint,Normal_len,WsForward)
	Pforward=ParameterPt(Forward_normal,1.0)

	Reverse_normal=SurfNormalLine(Surface,UVPoint,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,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=DeleteEntity(UVPoint)
Stat=DeleteEntity(P1)
stat= SetInputFormat("n4","")
Stat= SetPickMask(WsPickGeometry)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
