
! script name: 3dintersect.d/surfnormline
! 3dtopic: 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]

ActivateUndo
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

Start:!
Intersect2:StdWorkplane

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


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

Pick_point:Stat= SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n4","")
Ploop:P1=XyzInput("INPUT POINT ON SURFACE",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
Stat=SetLastPoint(P1,WsXyzInput)
UVPoint= PointOnSurface(P1,Surface,Tol,WsReturnUVPoint)
IF NOT ValidGeometry(UVpoint) THEN Pick_point
U= XPointValue(UVpoint)
V= YPointValue(UVpoint)
Stat= DeleteEntity(UVPoint)
Stat= DeleteEntity(P1)

GOSUB Orient_normal
IF Norm_sign= 1 THEN
	Normal_line=SurfNormalLine(Surface,U,V,Normal_len,WsForward)
ELSE
	Normal_line=SurfNormalLine(Surface,U,V,Normal_len,WsReverse)
END IF

ActivateUndo
Stat= AdvanceUndoNumber("normal line")
Stat= AddItemToUndo(WsUBCreateMode,Normal_line)

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,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,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
