
! script name: items.d/unlinkitem
! topic: unlink an item flag

!A
!A NAME:
!A
!A 	Unlink an Item Flag
!A
!A DESCRIPTION:
!A
!A 	This script unlinks an item flag, removing it from VDM.
!A
!B INSTRUCTIONS:
!B
!B 	PICK ENTITY, Unlink, Backup, Cancel:  Pick the entity in the item flag
!B 	that you want unlinked. If the entity is contained in more than one
!B 	item flag, a selection box will appear giving you the choice of items
!B 	you want unlinked.  The selected item will be highlighted and placed
!B 	in the unlink buffer list.
!B
!B 		U - Unlink will unlink the highlighted items in the unlink list.
!B
!B 		B - Backup will back up through the unlink list.
!B
!B 		C - Cancel will clear the unlink list.
!B
!C NOTES:
!C
!C 	Unlinking an item will remove only the item reference selected.
!C 	Any other item flag references on the entities will be 
!C 	maintained. Once an item flag has been removed it can be
!C 	re-established by using the "Attach Item" scripts.
!C
!# see also - htdocs/help.d/items.htm
!# see also - htdocs/help.d/property.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: Unlink 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:!
Unlink_group= CreateGroup("")
Group= GrpOfUserItems
IF NOT GroupCount(Group) THEN Leave
Stat= DeleteEntity(Group)

Pick_entity:!
Stat= SetInputFormat("n4a1",",B,U,C,E,")
E1=PickEntity("PICK ENTITY, Unlink, Backup, Cancel, Exit ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "E","e"
        GOTO Leave
CASE "U", "u"
        GOSUB Unlink_items
        GOTO Start
CASE "C", "c"
	Stat= SetSelectedFlag(Unlink_group,WsFalse)
	Stat= PlotEntity(Unlink_group)
        Stat= DeleteEntity(Unlink_group)
        GOTO Start
CASE "B","b"
        N1= GroupCount(Unlink_group)
	IF N1 THEN
	Item_ent= GroupElement(Unlink_group,N1)
        Stat= SetSelectedFlag(Item_ent,WsFalse)
	Stat= PlotEntity(Item_ent)
	IF  ValidGeometry(Item_ent) THEN 
        	Stat=RemoveFromGrp(Unlink_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(Unlink_group,Item_ent)
IF NOT N1 THEN
	Stat= SetSelectedFlag(Item_ent,WsTrue)
	Stat= PlotEntity(Item_ent)
        Stat= AddToGroup(Unlink_group,Item_ent)
END IF
END IF
GOTO Pick_entity

Unlink_items:!
Stat= SetSelectedFlag(Unlink_group,WsFalse)
Stat= PlotEntity(Unlink_group)
FOR I= GroupCount(Unlink_group) TO 1 STEP -1
	Item_ent= GroupElement(Unlink_group,I)
	Id_number=UserDataType(Item_ent)
        Stat= UnAssocUserData(Item_ent,Id_number)
        Stat= DeleteEntity(Item_ent)
NEXT I
Stat= DeleteEntity(Unlink_group)
Stat= DeleteEntity(Group)
RETURN

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

