
! script name: delete.d/deletebytool
! topic: delete by tool

!A
!A NAME:
!A
!A 	Delete By Tool
!A
!A DESCRIPTION:
!A
!A 	This script allows the geometry associated with a particular tool and
!A 	the tool to be deleted.  The geometry associated with the selected tool
!A 	will be highlighted, the default color is red.  This script will not
!A 	permanently delete the geometry and the tool until "Delete" is selected
!A 	or the script is exited, by selection of another script.
!A
!B INSTRUCTIONS:
!B
!B 	INPUT TOOL ID NAME, Undo, Restore, Delete : The selection box with the
!B 		tool names available will appear for a selection to be made.
!B 		NOTE: The Undo, Restore, and Delete options do not appear the
!B 		first time, and will only be present if they are applicable.
!B
!B 		U - Undo works in a recursive manner.  The geometry and the
!B 		    tool will be undeleted in the reverse order in which they
!B 		    were selected.
!B 		R - Restore means that the selected tool and associated 
!B 		    geometry will not be deleted and the script will start over.
!B 		D - Delete finalizes the deletion process for all the tools and
!B 		    associated geometry selected and remains in the script.
!B 		    Restore will not undo the Delete.
!B

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

DIM Answer$[256]
DIM Destination$[256]
DIM Tool$[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$=""

Stat=ToolSelectBox("DELETE BY TOOL","select tool for deletion")
IF Stat=1 THEN Leave

IF Delete_count=0 THEN

   Stat=SetInputFormat("a99","")
   Stat=GetValue("SELECT TOOL",Answer$)

ELSE

   Stat=SetInputFormat("a99","")
   Stat=GetValue("SELECT TOOL, Undo, Restore, Delete ",Answer$)

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

SELECT Choice$

  CASE "U","UNDO"

    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","RESTORE"

    GOSUB Free_temp
    GOTO Start

  CASE "D","DELETE"

    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

    Tool$=Answer$
    Tool=FindUserData(Tool$,WsToolData)
    IF NOT ValidGeometry(Tool) THEN Group_loop

    Group_tool=GrpByTool(Tool)
    Stat= RemoveVarEntities(Group_tool)
    Stat= RemoveRefEntities(Group_tool)
    IF NOT ValidGeometry(Group_tool) THEN Group_loop

    IF Ask_undo$="N" THEN

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

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

    ELSE

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

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

