
! script name: 3dcpaths.d/mansurfgrp
! 3dtopic: manual surface grouping

!A
!A NAME:
!A
!A 	Group: Manual
!A
!A DESCRIPTION:
!A
!A 	This script allows the manual creation of a surface group.
!A
!B INSTRUCTIONS:
!B
!B 	SURFACE GROUP NAME: Name of surface group.
!B
!B 	PICK SURFACE, Proceed: Pick surface in group.
!B
!B 	NORMALS: Forward or Reverse: Align normal direction.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: manual surface group ")

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

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

Start:!

Stat=SetInputFormat("a99","",WsFalse)
Stat=GetValue("SURFACE GROUP NAME",Name$)
IF ExitScript(Destination$) THEN Leave
IF Name$="CANCEL" THEN Leave

Part_group= CreateGroup(Name$,WsSurfaceGroup)

Pick_loop:!

Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("a1",",P,")
Surface=PickEntity("PICK SURFACE, Proceed ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="P" THEN 
	Num_surfs= GroupCount(Part_group)
	IF Num_surfs=0 THEN Stat= DeleteEntity(Part_group)
END IF
IF NOT ValidGeometry(Surface) THEN Pick_loop
N1= ElementGrpNum(Part_group,Surface)
IF N1 THEN Pick_loop

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
IF Norm_sign=1 THEN
        Stat=SetNormalDir(Surface,WsForward)
ELSE
        Stat=SetNormalDir(Surface,WsReverse)
END IF
Stat=AddToGroup(Part_group,Surface)


GOTO Pick_loop

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)

        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)
        Stat=SetColor(Pforward,Forward_color)
        Stat=SetColor(Reverse_normal,Reverse_color)
        Stat=SetColor(Preverse,Reverse_color)
RETURN



Leave:!
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
