
! script name: vdelete.d/vmultdelete
! var_topic: delete elements with undo and restore options

!A
!A NAME:
!A
!A 	Multiple Delete   VARIATIONAL
!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 NOTES:
!B
!B 	This is a variational delete function. The delete operation becomes
!B 	part of the variational object. This function should only be used when
!B 	entities which are created variationally are no longer needed and there
!B 	presence in the variational program is not desireable. Normally extra
!B 	elements should just be hidden.
!B

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

DIM Destination$[256]
DIM Answer$[256]
DIM Choice$[256]
DIM Element$[256]

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

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

Highlight_color= 2
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

Ploop:!
Delete_count=GroupCount(Group)
Answer$=""
Choice$=""
IF Delete_count=0 THEN
  E1=PickEntity("DELETE WHICH ELEMENT")
ELSE
  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"
      GOTO Make_record
   CASE ELSE
      IF NOT ValidGeometry(E1) THEN Ploop
      Var_record=VarRecordParent(E1)
      IF NOT ValidGeometry(Var_record) THEN Ploop
      Stat= SetSelectedFlag(E1,WsTrue)
      N1= ElementGrpNum(Group,E1)
      IF N1 = 0 THEN 
      	      Stat=AddToGroup(Group,E1)
      END IF
      Stat=PlotEntity(E1)
END SELECT
GOTO Ploop

Make_record:!

ActivateUndo
Stat= AdvanceUndoNumber("delete entities")

Num_elements= GroupCount(Group)
RecordFunctionOn
Stat=RecordDescr("DELETE: Entities")
FOR I=1 TO Num_elements
        RecordFunctionOff
        E1= GroupElement(Group,I)
        Stat= EntityNameCheck(E1,Element$)
        IF ValidDependency(E1) THEN
                RecordFunctionOn
		Stat= AddItemToUndo(WsUBDeleteMode,Element$)
                Stat= DeleteEntity(Element$)
                RecordFunctionOff
        END IF
NEXT I
RecordFunctionOn
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
Stat= DeleteEntity(Group)
GOTO Start


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
