
! script name: delete.d/delcomponent
! topic: Sample - delete by component

!A
!A NAME:
!A
!A 	Delete By component
!A
!A DESCRIPTION:
!A
!A 	This script allows the deletion of everything in a specified component.
!A 	The entities that have been assigned to the component selected will be
!A 	highlighted, the default color is red.  This script will not
!A 	permanently delete the entities on a particular component until "Delete"
!A 	is selected.
!A
!B INSTRUCTIONS:
!B
!B 	DELETE COMPONENT #, Undo, Restore, Delete: Enter the number 
!B		of the component to be deleted or pick an element 
!B		on the component 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
!B 		U - Undo works in a recursive manner.  The component will be
!B 		    undeleted in the reverse order in which they were selected.
!B 		R - Restore means that the selected component will 
!B		    not be deleted and the script will start over.
!B 		D - Delete finalizes the deletion process for all the components
!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
!C NOTE:
!C
!C 	This is an unsupported sample script.
!C

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

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"

DeActivateUndo

Start: !
Group=CreateGroup("TEMPGROUP")
Highlight_color=2

Group_loop:!

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

   stat= SetInputFormat("n0","")
   Component=InputComponent("DELETE COMPONENT #",Answer$)

ELSE

   stat= SetInputFormat("n0a1",",U,R,D,")
   Component=InputComponent("DELETE COMPONENT #, 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=Highlight(Temp_group,1,0)
    Stat=RemoveFromGrp(Group,Group_num)
    Delete_count=GroupCount(Group)
    IF Delete_count>0 THEN
       Stat=Highlight(Group,Highlight_color,0)
    END IF
    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=UnLink(Hold_group)
          Stat=DeleteGroup(Hold_group,1)
       END IF
    END IF

    Stat=DeleteEntity(Group)

    TransformCleanUp

    GOTO Start

  CASE ELSE

    Group_component=GrpByComponent(Component)
    IF (NOT ValidGeometry(Group_component)) THEN Group_loop

    IF Ask_undo$="N" THEN

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

    ELSE

       N1=ElementGrpNum(Group,Group_component)
       IF N1=0 THEN
          Stat=AddToGroup(Group,Group_component)
       END IF
       Stat=Highlight(Group,Highlight_color,0)

    END IF

END SELECT


GOTO Group_loop

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

Leave:!

GOSUB Free_temp

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