
! script name: delete.d/deletenamedgrp
! topic: delete named group

!A
!A NAME:
!A
!A 	Delete Named Group
!A
!A DESCRIPTION:
!A
!A 	This script allows a group that has a name associated with it to be
!A 	deleted.  The group selected will be highlighted, the default color
!A 	is red.  If an element is selected rather than a group, the element
!A 	is highlighted in the pick color.  This script will not permanently
!A 	delete a group until "Delete" is selected or the script is exited,
!A 	by selection of another script.
!A
!B INSTRUCTIONS:
!B
!B 	DELETE WHICH NAMED GROUP, Undo, Restore, Delete: The selection box with
!B 		the available list of named groups will automatically appear
!B 		for a selection to be made.  NOTE: Undo, Restore, and Delete
!B 		options do not appear the first time, and will only be present
!B 		if they are applicable.
!B
!B		U - Undo works in a recursive manner.  The groups will be
!B 		    undeleted in the reverse order.
!B 		R - Restore means that the highlighted groups will NOT be
!B 		    deleted and the script will start over.  If Delete has been
!B 		    used, those groups are still gone.
!B 		D - Delete finalizes the deletion process for all the groups
!B 		    presently highlighted and remains in the script. Restore
!B 		    will not undo the Delete.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: delete named group ")

DIM Destination$[256],Answer$[256]
DIM Name$[256]
DIM Listdata$[3000]
DIM Choice$[256]

!C 	If Ask_undo$="Y" the "Undo, Restore, Delete" options will be present.
!C 	If Ask_undo$="N" the script will automatically delete the elements
!C 	when they are picked.  NO CHANCE to change the selection.

  Ask_undo$="Y"

ActivateUndo
Start:!
Del_group=CreateGroup("TEMPGROUP")
Highlight_color= 2
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

Ploop: !
Delete_count=GroupCount(Del_group)
Choice$=""
Name$=""

Stat=ListGrpNames("GROUP NAMES","selected contour","")
IF Stat=1 THEN Leave

IF Delete_count=0 THEN

   Stat=SetInputFormat("a99","")
   Stat=GetValue("DELETE WHICH NAMED GROUP",Name$)

ELSE

   Stat=SetInputFormat("a99","")
   Stat=GetValue("DELETE WHICH NAMED GROUP, Undo , Restore , Delete ",Name$)

END IF
IF (ExitScript(Destination$)) THEN Leave
IF Name$="CANCEL" THEN Leave
IF Name$="TEMPGROUP" THEN Ploop
Choice$=UPC$(Name$)

SELECT Choice$

   CASE "U","UNDO"

      Group_num=GroupCount(Del_group)
      Temp_group=GroupElement(Del_group,Group_num)
      IF NOT ValidGeometry(Temp_group) THEN Ploop
      Stat= SetSelectedFlag(Temp_group,WsFalse)
      Stat=PlotEntity(Temp_group)
      Stat=RemoveFromGrp(Del_group,Group_num)

   CASE "R","RESTORE"

      IF ValidGeometry(Del_group) THEN
      	Stat= SetSelectedFlag(Del_group,WsFalse)
      	Stat= PlotEntity(Del_group)
      	Stat= DeleteEntity(Del_group)
      END IF
      GOTO Start

   CASE "D","DELETE"

     IF ValidGeometry(Del_group) THEN

	
	ActivateUndo
        Stat= AdvanceUndoNumber("delete contour")

        Stat= SetSelectedFlag(Del_group,WsFalse)
        N1=GroupCount(Del_group)
        IF N1 THEN
           Hold_group=CreateGroup("HOLDING GROUP")
           FOR I= 1 TO N1
              G1= GroupElement(Del_group,I)
              Stat= AddGroupToUndo(WsUBDeleteMode,G1,1,WsTrue)
              Stat= CombineGroups(Hold_group,G1)
	      Stat= DeleteEntity(G1)
           NEXT I
           stat=DeleteEntity(Del_group)
           Stat=UnLink(Hold_group)
           Stat=DeleteGroup(Hold_group,1)
        END IF
     END IF

     stat= DeleteEntity(Del_group)

     ! TransformCleanUp

     GOTO Start

   CASE ELSE

      Group=FindNamedGrp(Name$)
      IF NOT ValidGeometry(Group) THEN Ploop

      IF Ask_undo$="N" THEN

	 ActivateUndo
         Stat= AdvanceUndoNumber("delete")
         Stat= SetSelectedFlag(Del_group,WsFalse)
         Stat= AddGroupToUndo(WsUBDeleteMode,Del_group,WsFalse,WsFalse)

         Stat=UnLink(Del_group)
         Stat=DeleteGroup(Del_group,1)
         Stat=DeleteEntity(Del_group)
         ! TransformCleanUp
         GOTO Start

      ELSE

         N1=ElementGrpNum(Del_group,Group)
         IF N1=0 THEN
	    Stat= SetSelectedFlag(Group,WsTrue)
            Stat=PlotEntity(Group)
            Stat=AddToGroup(Del_group,Group)
         END IF
      END IF

END SELECT

GOTO Ploop

Leave: !
IF ValidGeometry(Del_group) THEN
     Stat= SetSelectedFlag(Del_group,WsFalse)
     Stat= PlotEntity(Del_group)
     Stat= DeleteEntity(Del_group)
END IF
stat=SetInputFormat("n4","")
LowerSelectBox

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