
! script name: groups.d/makegroupname
! topic: make and name group

!A
!A NAME:
!A
!A 	MAKE AND NAME A GROUP
!A
!A DESCRIPTION:
!A
!A 	This script allows a group to be made and named, or a group that
!A 	has already been made and named may have elements added to it.
!A 	The group may be created in one of three ways:
!A
!A 		1) SELECTION BOX
!A 		2) PICK INDIVIDUAL ELEMENTS
!A
!A 	The mode may be changed by editing the named variable "pick type".
!A 	To edit a named variable select the "EDIT USER DATA" option in the
!A 	"Edit" subdirectory, then select the "NAMED VARIABLES" option.  The
!A 	variable "pick type" should be one of the choices.  ( The numbers
!A 	above are the ones associated with the variable.)
!A
!B INSTRUCTIONS:
!B
!B 	GROUP NAME, PROCEED: Enter the name to be associated with the group
!B 		or enter PROCEED to exit.
!B
!B 	GROUP EXISTS: Add to group or New name: This question is dependent
!B 		on whether or not the named group already exists.  If the
!B 		group exists, the options available are: Add to the existing
!B 		group or return to the first question and select a new name
!B 		for the group.
!B
!B 	SELECT VIEW FOR SELECTION BOX: Pick the view for the elements to be
!B 		included in the group.  This question is for SELECTION BOX
!B 		mode only.
!B
!B 	PICK ELEMENT, PROCEED, CANCEL: Pick one of the elements to be included
!B 		in the group.  This question is for PICK INDIVIDUAL ELEMENTS
!B 		mode only.  The question will repeat until "PROCEED" or
!B 		"CANCEL" is selected.  All elements picked will be included in
!B 		the group.  If "CANCEL" is selected no group is made, however,
!B 		if "Add to group" was selected the original group is still
!B 		there.
!B
!B 	PICK GROUP TO BE NAMED: Pick an element on the group to be named.
!B 		This question is for PICK A GROUP mode only.
!B
!B NOTES:
!B
!B 	When a group is made and named any entities included in this group
!B 	are unlinked.  No element may remain in more than one permanent
!B 	group.  If a group has been named and an existing named group is
!B 	selected, the new group will override the existing named group.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: make and name a group ")

DIM Answer$[256]
DIM Destination$[256]
DEG

ActivateUndo
Start: !

Add_to_group=0
stat= SetInputFormat("n4","")

Stat= SetInputFormat("a99",",PROCEED,")
Stat= GetValue("GROUP NAME, PROCEED ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="PROCEED" THEN Leave
Group= FindNamedGrp(Answer$)
IF NOT ValidGeometry(Group) THEN New_group
Stat= SetInputFormat("a1",",A,N,")
Stat= GetValue("GROUP EXISTS: Add to group or New name",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Stat=SetInputFormat("n4","")
Add_to_group=(Answer$="A")
IF Answer$="N" THEN Start
IF Answer$="A" THEN GOTO Cont_group

New_group: !
Group=CreateGroup(Answer$)

Cont_group: !
Pick_type=NamedVarVal("pick type",WsSystemVars,1.0)

SELECT Pick_type

  CASE 1,3    ! PICK BOX

    Vnum= LastPickView
    IF Add_to_group THEN
       Grp_tmp=CreateGroup("TEMP")
       Stat= PickBox(Grp_tmp,Vnum,WsPickPen)
       IF (ExitScript(Destination$)) THEN Leave

	ActivateUndo
	Stat= AdvanceUndoNumber("add to group")
	Stat= AddItemToUndo(WsUBModifyMode,Group)
        Stat= AddGroupToUndo(WsUBModifyMode,Grp_tmp,0,WsFalse)

       Stat=UnLink(Grp_tmp)
       Stat=CombineGroups(Group,Grp_tmp)
       Stat=DeleteEntity(Grp_tmp)
    ELSE
       Grp_tmp=CreateGroup("TEMP")
       Stat= PickBox(Grp_tmp,Vnum,WsPickPen)
       IF (ExitScript(Destination$)) THEN Leave

	ActivateUndo
        Stat= AdvanceUndoNumber("create group")
        Stat= AddItemToUndo(WsUBCreateMode,Group)
        Stat= AddGroupToUndo(WsUBModifyMode,Grp_tmp,0,WsFalse)

       Stat=UnLink(Grp_tmp)
       Stat=CombineGroups(Group,Grp_tmp)
       Stat=DeleteEntity(Grp_tmp)
    END IF

  CASE 2    ! PICK ELEMENTS

    Grp_tmp= CreateGroup("TEMP")

    LOOP
      Stat=SetInputFormat("a7",",PROCEED,CANCEL,")
      E1= PickEntity("PICK ELEMENTS, PROCEED, CANCEL ",Answer$)
      IF (ExitScript(Destination$)) THEN Leave
      Stat=SetInputFormat("n4","")
    EXIT IF Answer$="PROCEED" OR Answer$="CANCEL"

      Stat=UnLink(E1)
      Stat=AddToGroup(Grp_tmp,E1)
    END LOOP

    IF Answer$="PROCEED" THEN

	IF Add_to_group THEN
		ActivateUndo
        	Stat= AdvanceUndoNumber("add to group")
        	Stat= AddItemToUndo(WsUBModifyMode,Group)
        	Stat= AddGroupToUndo(WsUBModifyMode,Grp_tmp,0,WsFalse)
	ELSE
		ActivateUndo
        	Stat= AdvanceUndoNumber("create group")
        	Stat= AddItemToUndo(WsUBCreateMode,Group)
        	Stat= AddGroupToUndo(WsUBModifyMode,Grp_tmp,0,WsFalse)
	END IF

       Stat=CombineGroups(Group,Grp_tmp)
    END IF

    Stat=DeleteEntity(Grp_tmp)

  CASE ELSE

    Pick_type=1
    GOTO Start

END SELECT

IF Answer$="QUIT" THEN Leave


GOTO Start

Leave:!
Stat= SetInputFormat("n4","")
IF ValidGeometry(Group) THEN
   N1= GroupCount(Group)
   IF NOT N1 THEN
      Stat=DeleteEntity(Group)
   END IF
END IF

IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
