
! script name: delete.d/singledeletes
! topic: Sample - delete single element, undo and restore available

!A
!A NAME:
!A
!A 	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: Pick the element to be
!B 		deleted.  NOTE: The Undo, Restore, and Delete options do not
!B 		appear the first time, and will only be present if they are
!B 		applicable.
!B
!B 		U - Undo works in a recursive manner.  The elements will be
!B 		    undeleted in the reverse order in which  they were selected.
!B 		R - Restore means that the highlighted elements will not be
!B 		    deleted and the script will start over.
!B 		D - 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
!C NOTE:
!C
!C 	If an element in a contour or a group is deleted,
!C 	the group is automatically unlinked.
!C
!C 	This is an unsupported sample script.
!C

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$="N"

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

Highlight_color= 2
Highlight_color2= 5

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=PlotEntity(Temp_e1)
      Stat=RemoveFromGrp(Group,Element_num)
      Delete_count=GroupCount(Group)
      IF Delete_count>0 THEN
         Stat=Highlight(Group,Highlight_color,0)
      END IF

   CASE "R"

      Delete_count=GroupCount(Group)
      IF Delete_count THEN
      	       PlotAll
      END IF
      Stat=DeleteEntity(Group)
      GOTO Start
 
   CASE "D"

      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

   CASE ELSE

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

      IF Ask_undo$="N" THEN

         IF ValidGeometry(Group) THEN
            N1=GroupCount(Group)

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

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

         Stat=DeleteEntity(Group)

         GOTO Start

      ELSE

	IF GetColor(E1)<>Highlight_color THEN
         Stat=PlotEntity(E1,Highlight_color)
	ELSE
         Stat=PlotEntity(E1,Highlight_color2)
	END IF

      END IF

END SELECT

GOTO Ploop

Leave:!

Delete_count=GroupCount(Group)
IF Delete_count THEN
   PlotAll
END IF
Stat=DeleteEntity(Group)

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