
! script name: delete.d/deletebycolor
! topic: delete elements by color

!A
!A NAME:
!A
!A 	Delete By Color
!A
!A DESCRIPTION:
!A
!A 	This script allows the deletion of elements by color.  The
!A 	elements that have been assigned the particular color selected
!A 	will be highlighted, the default color is red.  This script will
!A 	not permanently delete the elements of a particular color until
!A 	"Delete" is selected.
!A
!B INSTRUCTIONS:
!B
!B 	DELETE WHICH COLOR, Undo, Restore, Delete: Enter the number of the
!B 		color to be deleted or pick an element of the color that is
!B 		to be deleted.  NOTE: The Undo, Restore, and Delete options
!B 		do not appear the first time, and will only be present if
!B 		they are applicable.
!B
!B 		U - Undo works in a recursive manner.  The colors will be
!B 		    undeleted in the reverse order in which they were selected.
!B 		R - Restore means that the selected colors will not be deleted
!B 		    and the script will start over.
!B 		D - Delete finalizes the deletion process for all the colors
!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 color ")

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","")
   Color1=InputColor("DELETE WHICH COLOR",Answer$)

ELSE

   stat= SetInputFormat("n0a1",",U,R,D,")
   Color1=InputColor("DELETE WHICH COLOR, 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)
             G2= GroupUnion(Hold_group,G1)
             Stat=DeleteEntity(Hold_group)
             Hold_group=G2
          NEXT I

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

          Stat=DeleteEntity(Group)
          Stat=UnLink(Hold_group)
          Stat=DeleteGroup(Hold_group,1)
          PlotAll
       END IF
    END IF
    Stat=DeleteEntity(Group)
    ! TransformCleanUp
    GOTO Start

  CASE ELSE

!   Group_color=GrpByColor(Color1)
    Group_color=GrpByColorVisible(Color1)
    Stat= RemoveVarEntities(Group_color)
    Stat= RemoveRefEntities(Group_color)
    IF (NOT ValidGeometry(Group_color)) THEN Group_loop

    IF Ask_undo$="N" THEN

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

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

    ELSE

       N1=ElementGrpNum(Group,Group_color)
       IF N1=0 THEN
	  Stat= SetSelectedFlag(Group_color,WsTrue)
          Stat=PlotEntity(Group_color)
          Stat=AddToGroup(Group,Group_color)
          Color_count=GroupCount(Group)
       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
