
! script name: vnormals.d/alignnormal
! var_topic: align surface normal vector

!A
!A NAME:
!A
!A 	Align Normal
!A
!A DESCRIPTION:
!A
!A      Aligns normal vector direction on a surface or solid.
!A
!B INSTRUCTIONS:
!B
!B 	PICK SURFACE, Proceed: Pick the surface to align surface normal vector.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick which side of the surface.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: align normal ")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface_dir$[256]
DIM Surface$[256]
DIM Name$[256]

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

DeActivateUndo
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Forward_color= 3
Reverse_color= 4
Start:!

Pick_surf:Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("a1",",P,")
Surface=PickEntity("PICK SURFACE, Proceed ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "P" THEN Leave
IF NOT ValidDependency(Surface) THEN Pick_surf
Stat= EntityNameCheck(Surface,Surface$)
Stat= SetPickMask(WsPickGeometry)

UVpoint= LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint) THEN Start
U= XPointValue(UVpoint)
V= YPointValue(UVpoint)
stat= DeleteEntity(UVPoint)


stat= SetNormalDir(Surface,WsForward)
GOSUB Orient_normal

RecordFunctionOn
Stat= RecordDescr("ALIGN NORMAL: Surface")
IF Norm_sign= 1 THEN
        stat= SetNormalDir(Surface$,WsForward)
ELSE
        stat= SetNormalDir(Surface$,WsReverse)
END IF
RecordFunctionOff
CloseVarRecord

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 Orient_normal
        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)

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






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