
! script name: delete.d/delete
! topic: delete single element with undo and restore options

!A
!A NAME:
!A
!A 	Multiple Delete
!A
!A DESCRIPTION:
!A
!A 	This script allows the deletion of a single element.  The elements
!A 	selected will be highlighted, the default is red.  This script will
!A 	not permanently delete an element until "Delete" is selected.
!A
!B INSTRUCTIONS:
!B
!B 	DELETE WHICH ELEMENT, Undo, Restore, Delete:
!B 		Pick the element to be deleted.
!B
!B 		NOTE: The Undo, Restore, and Delete options do not appear
!B 		    the first time, and will only be present if they are
!B 		    applicable.
!B 		Undo works in a recursive manner.  The elements will be
!B 		   undeleted in the reverse order in which they were selected.
!B 		Restore means that the highlighted elements will not be
!B 		   deleted and the script will start over.
!B 		Delete finalizes the deletion process for all the elements
!B 		   presently highlighted and remains in the script.  Restore
!B 		   will not undo the Delete.  NOTE: The "Delete" option must
!B 		   be selected to finalize the delete process.
!B
!B NOTE:
!B
!B 	If an element in a contour or a group is deleted, the group is
!B 	automatically unlinked.
!B

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

DIM Destination$[256]
DIM Answer$[256]
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"

Start: !
ActivateUndo
FadeUndoButtons
Group=CreateGroup("TEMPGROUP")

Highlight_color= 2
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

Ploop:!
Delete_count=GroupCount(Group)
Answer$=""
Choice$=""
IF Delete_count=0 THEN

  ActivateUndo
  E1=PickEntity("DELETE WHICH ELEMENT")

ELSE

  FadeUndoButtons
  Stat=SetInputFormat("a1",",U,R,D,")
  E1=PickEntity("DELETE WHICH ELEMENT, Undo, Restore, Delete",Answer$)

END IF

IF (ExitScript(Destination$)) THEN Leave
Choice$=UPC$(Answer$)

SELECT Choice$

   CASE "U"

      Element_num=GroupCount(Group)
      Temp_e1=GroupElement(Group,Element_num)
      IF NOT ValidGeometry(Temp_e1) THEN Ploop
      Stat= SetSelectedFlag(Temp_e1,WsFalse)
      Stat=PlotEntity(Temp_e1)
      Stat=RemoveFromGrp(Group,Element_num)

   CASE "R"

      Stat= SetSelectedFlag(Group,WsFalse)
      Stat= PlotEntity(Group)
      Stat=DeleteEntity(Group)
      GOTO Start
 
   CASE "D"

      IF ValidGeometry(Group) THEN

         N1=GroupCount(Group)
         IF N1 THEN
            ActivateUndo
            Stat= AdvanceUndoNumber("delete")
            Stat= AddGroupToUndo(WsUBDeleteMode,Group,0,0)
            Stat=UnLink(Group)
            Stat=DeleteGroup(Group,1)
         END IF
      END IF
      Stat=DeleteEntity(Group)

      GOTO Start

   CASE ELSE

      IF NOT ValidGeometry(E1) THEN Ploop
      IF IsEntVariational(E1) THEN Ploop
      IF IsEntReferenced(E1) THEN Ploop
      Stat= SetSelectedFlag(E1,WsTrue)
      N1= ElementGrpNum(Group,E1)
      IF N1 = 0 THEN 
      	      Stat=AddToGroup(Group,E1)
      END IF

      IF Ask_undo$="N" THEN

         IF ValidGeometry(Group) THEN

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

            N1=GroupCount(Group)
            IF N1 THEN
               Stat=UnLink(Group)
               Stat=DeleteGroup(Group,1)
            END IF
         END IF

         Stat=DeleteEntity(Group)

         GOTO Start

      ELSE
         Stat=PlotEntity(E1)
      END IF

END SELECT

GOTO Ploop

Leave:!
IF ValidGeometry(Group) THEN
        Stat= SetSelectedFlag(Group,WsFalse)
        stat= PlotEntity(Group)
        stat= DeleteEntity(Group)
END IF

Stat=SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
