
! script name: items.d/deleteitem
! topic: delete an item flag

!A
!A NAME:
!A
!A 	Delete an Item and its Associated Geometry
!A
!A DESCRIPTION:
!A
!A 	This script deletes an item flag and its associated geometry.
!A
!B INSTRUCTIONS:
!B
!B 	PICK ENTITY, Delete, Backup, Cancel: Select the entity in the item
!B 	that you want deleted.  If the entity you select is contained in more
!B 	than one item, a selection box will appear, letting you choose which
!B 	items to delete.  The item will be highlighted and placed in the
!B 	delete buffer list.
!B
!B 		D - Delete will unlink the highlighted items in the delete list.
!B 		B - Backup will back up through the delete list.
!B 		C - Cancel will clear the delete list.
!B
!# see also - htdocs/help.d/items.htm
!# see also - htdocs/help.d/property.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: Delete items ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM A$[256]
DIM B$[256]

DeActivateUndo
Highlight_color= 2
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

Start:!
Delete_group= CreateGroup("")
Group= GrpOfUserItems
IF NOT GroupCount(Group) THEN Leave
Stat= DeleteEntity(Group)

Pick_entity:!
Stat= SetInputFormat("n4a1",",D,U,C,E,")
E1=PickEntity("PICK ENTITY, Delete, Undo, Cancel Exit ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "D", "d"
        GOSUB Delete_items
        GOTO Start
CASE "C", "c"
	Stat= SetSelectedFlag(Delete_group,WsFalse)
	Stat= PlotEntity(Delete_group)
        Stat= DeleteEntity(Delete_group)
        GOTO Start
CASE "E","e"
	GOTO Leave
CASE "U","u"
        N1= GroupCount(Delete_group)
	IF N1 THEN
	Item_ent= GroupElement(Delete_group,N1)
        Stat= SetSelectedFlag(Item_ent,WsFalse)
	Stat= PlotEntity(Item_ent)
	IF  ValidGeometry(Item_ent) THEN 
        	Stat=RemoveFromGrp(Delete_group,N1)
	END IF
	END IF
        GOTO Pick_entity
END SELECT
IF NOT ValidGeometry(E1) THEN Leave

Group= EntityItemGroup(E1)
IF NOT ValidGeometry(Group) THEN Pick_entity
Nitems= GroupCount(Group)
IF Nitems= 0 THEN Leave
IF Nitems=1 THEN
        Item_ent= GroupElement(Group,1)
        Id_number= UserDataType(Item_ent)
        Stat= DeleteEntity(Group)
ELSE
Existing_items:!
Stat=UserSelectBox("EXISTING ITEMS","Item selected",0,WsPropGroupSelect,Group)

Pick_item:!
        Stat=SetInputFormat("a99","")
        Stat=GetValue("INPUT ITEM ID NAME",Name$)
        IF (ExitScript(Destination$)) THEN Leave
        SELECT Name$
        CASE "CANCEL"
                 Stat= DeleteEntity(Group)
                 GOTO Start
        END SELECT

	Num1= NumericPrefix(Name$)
	IF NOT Num1 THEN Pick_item
        Item_ent= GroupElement(Group,Num1)
        IF NOT ValidGeometry(Item_ent) THEN Pick_item
        Stat= DeleteEntity(Group)
        LowerSelectBox
        Id_number= UserDataType(Item_ent)
END IF
IF ValidGeometry(Item_ent) THEN
N1= ElementGrpNum(Delete_group,Item_ent)
IF NOT N1 THEN
	Stat= SetSelectedFlag(Item_ent,WsTrue)
	Stat= PlotEntity(Item_ent)
        Stat= AddToGroup(Delete_group,Item_ent)
END IF
END IF
GOTO Pick_entity

Delete_items:!
FOR I= GroupCount(Delete_group) TO 1 STEP -1
	Item_ent= GroupElement(Delete_group,I)
	stat= DeleteItem(Item_ent)
NEXT I
Stat= DeleteEntity(Delete_group)
Stat= DeleteEntity(Group)
RETURN

Leave:!
Stat=SetInputFormat("n4","")
Stat= SetSelectedFlag(Delete_group,WsFalse)
Stat= PlotEntity(Delete_group)
Stat= DeleteEntity(Group)
Stat= DeleteEntity(Delete_group)
LowerSelectBox
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
