
! script name: delete.d/deletecontour
! topic: delete contour

!A
!A NAME:
!A
!A 	Delete Contour
!A
!A DESCRIPTION:
!A
!A 	This script allows a contour to be deleted.  The contour selected will
!A 	be highlighted, the default color is red.  If an element is selected
!A 	rather than a contour, the element is highlighted in the pick color.
!A 	This script will not permanently delete a contour until "Delete" is
!A 	selected or the script is exited, by selection of another script.
!A
!B INSTRUCTIONS:
!B
!B 	DELETE WHICH CONTOUR, Undo, Restore, Delete: Pick an element on the
!B 		contour to be deleted.  NOTE: The Undo, Restore, and Procced
!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 contours will be
!B 		    undeleted in the reverse order in which they were selected.
!B 		R - Restore means that the highlighted contours will not be
!B 		    deleted and the script will start over.  If Delete has been
!B 		    used, those elements are still gone.
!B 		D - Delete finalizes the deletion process for all the contours
!B 		    presently highlighted and remains in the script.  Restore
!B 		    will not undo the Delete.
!B

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

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"

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

Highlight_color=2
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

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

  ActivateUndo
  G1=PickGroupEnt("DELETE WHICH CONTOUR")

ELSE

  FadeUndoButtons
  Stat=SetInputFormat("a1",",U,R,D,")
  G1=PickGroupEnt("DELETE WHICH CONTOUR, Undo, Restore, Delete",0,Answer$)

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

SELECT Choice$

   CASE "U"

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

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

   CASE "D"

      IF ValidGeometry(Group) THEN
	

	 ActivateUndo
         Stat= AdvanceUndoNumber("delete")

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

            stat= DeleteEntity(Group)
            Stat=Unlink(Hold_group)
            Stat=DeleteGroup(Hold_group,1)
         END IF
      END IF

      stat= DeleteEntity(Group)

      GOTO Start

   CASE ELSE

      IF NOT ValidGeometry(G1) THEN Ploop

      IF Ask_undo$="N" THEN

         ActivateUndo
         Stat= AdvanceUndoNumber("delete")
	 Stat= SetSelectedFlag(G1,WsFalse)
         Stat= AddGroupToUndo(WsUBDeleteMode,G1,1,WsTrue)


         Stat=UnLink(G1)
         Stat=DeleteGroup(G1,1)
         Stat=DeleteEntity(G1)
         Stat=DeleteEntity(Group)
         GOTO Start

      ELSE
	 IF IsEntVariational(G1) THEN Ploop
	 IF IsEntReferenced(G1) THEN Ploop

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

      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
