
! script name: delete.d/deletebylayer
! topic: delete by layer

!A
!A NAME:
!A
!A 	Delete By Layer
!A
!A DESCRIPTION:
!A
!A 	This script allows the deletion of everything on a specified layer.
!A 	The entities that have been assigned to the layer selected will be
!A 	highlighted, the default color is red.  This script will not
!A 	permanently delete the entities on a particular layer until "Delete"
!A 	is selected.
!A
!B INSTRUCTIONS:
!B
!B 	DELETE LAYER #, Undo, Restore, Delete: Enter the number of the layer
!B 		to be deleted or pick an element on the layer to be deleted.
!B 		NOTE: The Undo, Restore, and Delete options do not appear the
!B 		first time, and will only
!B                 be present if they are applicable.
!B 		U - Undo works in a recursive manner.  The layers will be
!B 		    undeleted in the reverse order in which they were selected.
!B 		R - Restore means that the selected layers will not be deleted
!B 		    and the script will start over.
!B 		D - Delete finalizes the deletion process for all the layers
!B 		    selected and remains in the script.  Restore will not undo
!B 		    the Delete.  NOTE: The "Delete" option must be selected
!B 		    to finalize the delete process.
!B

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

DIM Answer$[256]
DIM Destination$[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
Start: !
Group=CreateGroup("TEMPGROUP")
Highlight_color= 2
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

Group_loop:!

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

   stat= SetInputFormat("n0","")
   Layer=InputLayer("DELETE LAYER #",Answer$)

ELSE

   stat= SetInputFormat("n0a1",",U,R,D,")
   Layer=InputLayer("DELETE LAYER #, Undo, Restore, Delete ",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 Group_loop
    Stat= SetSelectedFlag(Temp_group,WsFalse)
    Stat=PlotEntity(Temp_group)
    Stat=RemoveFromGrp(Group,Group_num)
    Stat=DeleteEntity(Temp_group)

  CASE "R"

    GOSUB Free_temp
    GOTO Start

  CASE "D"

    IF ValidGeometry(Group) THEN
       N1=GroupCount(Group)
       IF N1 THEN
          Hold_group=CreateGroup("HOLDING GROUP")
          FOR I=1 TO N1
             G1= GroupElement(Group,I)
             Stat=CombineGroups(Hold_group,G1)
          NEXT I
          Stat=DeleteEntity(Group)

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

          Stat=UnLink(Hold_group)
          Stat=DeleteGroup(Hold_group,1)
       END IF
    END IF

    Stat=DeleteEntity(Group)

    ! TransformCleanUp

    GOTO Start

  CASE ELSE

    Group_layer=GrpByLayer(Layer)
    Stat= RemoveVarEntities(Group_layer)
    Stat= RemoveRefEntities(Group_layer)
    IF (NOT ValidGeometry(Group_layer)) THEN Group_loop

    IF Ask_undo$="N" THEN

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

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

    ELSE

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

    END IF

END SELECT


GOTO Group_loop

Free_temp:!
IF ValidGeometry(Group) THEN
        Stat= SetSelectedFlag(Group,WsFalse)
        Stat= PlotEntity(Group)
        Delete_count=GroupCount(Group)
        IF Delete_count THEN
                 FOR I= 1 TO Delete_count
                         G1= GroupElement(Group,I)
                         Stat=DeleteEntity(G1)
                 NEXT I
        END IF
        Stat=DeleteEntity(Group)
END IF
RETURN

Leave:!

GOSUB Free_temp

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