
! script name: 3dcpaths.d/autosurfgrp
! 3dtopic: auto surface group

!A
!A NAME:
!A
!A 	Group: Auto
!A
!A DESCRIPTION:
!A
!A 	This script will auto group surfaces.
!A
!B INSTRUCTIONS:
!B
!B 	SURFACE GROUP NAME: Enter the name for the
!B 		group of surfaces.
!B
!B 	SELECT VIEW FOR SELECTION BOX: Select the
!B 		view to be used.
!B
!B 	PICK FIRST CORNER OF PICK ZONE: Pick the
!B 		first corner of the selection box.
!B
!B 	PICK SECOND CORNER OF PICK ZONE: Pick the
!B 		second corner of the selection box.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: auto 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)
Normal_color=3
Wide_line=10

Start:!

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

E1= FindNamedGrp(Name$)
IF ValidGeometry(E1) THEN
        Stat=SetInputFormat("a1",",P,")
        Stat=GetValue("NAME IN USE,  Proceed",Answer$)
        IF ExitScript(Destination$) THEN Leave
        IF Answer$="P" THEN Start
END IF

Part_group=CreateGroup(Name$,WsSurfaceGroup)

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

Vnum= LastPickView
Stat=SetPickMask(WsPickSurfOnly)
Stat=PickBox(Part_group,Vnum,WsPickPen)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Part_group) THEN Pick_loop

Num_surfs=GroupCount(Part_group)
IF Num_surfs THEN
   FOR I=1 TO Num_surfs
   	surf1=GroupElement(Part_group,I)
   	IF ValidGeometry(Surf1) THEN
           PlotOff
           Uvpoint= ValidUVPoint(Surf1,0.5,0.5)
           U= XPointValue(Uvpoint)
           V= YPointValue(Uvpoint)
           Stat= DeleteEntity(Uvpoint)
           PlotOn
	   Dir1= AlignSurfNormal(Surf1,U,V)
   	END IF
   NEXT I
   Stat= ShowGroupNormals(Part_group,Normal_len,Normal_color,Wide_line)
ELSE
   Stat= DeleteEntity(Part_group)
END IF
GOTO Leave

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