
! script name: vnormals.d/vectoraligngrp
! var_topic: align surface group normal vectors using a vector.

!A
!A NAME:
!A
!A 	Vector Align Normals
!A
!A DESCRIPTION:
!A
!A 	This script aligns normal vector direction on a surface.
!A
!B INSTRUCTIONS:
!B
!B 	PICK SURFACE GROUP,  Name, Proceed: Pick the surface group to be
!B 		aligned.
!B
!B 		P - Proceed may be used to exit the script.
!B	
!B	PICK DIRECTION LINE: Pick a line to determine the vector direction.
!B 		X+, Y+, Z+ are relative to the current workplane.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: vector align normal ")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface_dir$[256]
DIM Name$[256]
DIM Part_group$[256]
DIM Axis_line$[256]

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

DeActivateUndo
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Normal_color=3
Wide_line=10
Start:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n4a1",",N,")
Part_group=PickGroupEnt("PICK SURFACE GROUP, Name ",WsSurfaceGroup,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN
        GOSUB Get_named_group
END IF
IF NOT ValidDependency(Part_group) THEN Start
Stat= EntityNameCheck(Part_group,Part_group$)
Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)


Axis_line:!
Stat=SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("a1=",",X,Y,Z,")
Stat=SetEqualsValue(0,"Z")
Axis_line=PickEntity("PICK DIRECTION LINE, X+, Y+, Z+  ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "X"
	RecordFunctionOn
        Stat= RecordDescr("NORMALS: X+ align normals")
        Ps= PointXYZ(0,0,0)
        Pe= PointXYZ(1,0,0)
        Axis_line= LineTwoPoints(Ps,Pe)
        Stat= AlignGroupNormals(Part_group$,Axis_line)
        Stat= DeleteEntity(Ps)
        Stat= DeleteEntity(Pe)
        Stat= DeleteEntity(Axis_line)
        RecordFunctionOff
        CloseVarRecord
CASE "Y"
	RecordFunctionOn
        Stat= RecordDescr("NORMALS: Y+ align normals")
        Ps= PointXYZ(0,0,0)
        Pe= PointXYZ(0,1,0)
        Axis_line= LineTwoPoints(Ps,Pe)
        Stat= AlignGroupNormals(Part_group$,Axis_line)
        Stat= DeleteEntity(Ps)
        Stat= DeleteEntity(Pe)
        Stat= DeleteEntity(Axis_line)
        RecordFunctionOff
        CloseVarRecord
CASE "Z"
	RecordFunctionOn
        Stat= RecordDescr("NORMALS: Z+ align normals")
	Ps= PointXYZ(0,0,0)
	Pe= PointXYZ(0,0,1)
	Axis_line= LineTwoPoints(Ps,Pe)
        Stat= AlignGroupNormals(Part_group$,Axis_line)
        Stat= DeleteEntity(Ps)
        Stat= DeleteEntity(Pe)
        Stat= DeleteEntity(Axis_line)
        RecordFunctionOff
        CloseVarRecord
CASE ELSE
	IF NOT ValidDependency(Axis_line) THEN Start
	Stat= EntityNameCheck(Axis_line,Axis_line$)

	RecordFunctionOn
	Stat= RecordDescr("NORMALS: Vector align normals")
	Stat= AlignGroupNormals(Part_group$,Axis_line$)
	RecordFunctionOff
	CloseVarRecord
END SELECT

GOSUB Show_normals
GOTO Start


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

Show_normals:!
!Num_surfs=GroupCount(Part_group)
!FOR I=1 TO Num_surfs
!   surf1=GroupElement(Part_group,I)
!   IF ValidGeometry(Surf1) THEN
!        Sw= IsEntVisible(surf1)
!        IF Sw= WsTrue THEN
!             PlotOff
!             Uvpoint= ValidUVPoint(Surf1,0.5,0.5)
!             U= XPointValue(Uvpoint)
!             V= YPointValue(Uvpoint)
!             Stat= DeleteEntity(Uvpoint)
!             PlotOn
!             Stat= ShowSurfNormal(Surf1,U,V,Normal_len,Normal_color,Wide_line)
!        END IF
!   END IF
!NEXT I
PlotOn
Stat= ShowGroupNormals(Part_group,Normal_len,Normal_color,Wide_line)
RETURN


Get_named_group:!
Stat=ListGrpNames("SURFACE GROUPS","selected group","",WsSurfaceGroup)
IF Stat=1 THEN Leave_name

Stat=SetInputFormat("a99","")
Stat=GetValue("SURFACE GROUP NAME",Answer$)

IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave_name
Part_group=FindNamedGrp(Answer$)
IF NOT ValidGeometry(Part_group) THEN Get_named_group
Grp_type= GroupType(Part_group)
IF Grp_type <> WsSurfaceGroup THEN Get_named_group
LowerSelectBox

Leave_name:!
Stat=SetInputFormat("n4","")

RETURN
END
