
! script name: items.d/attachent
! topic: attach an item flag to an entity

!A
!A NAME:
!A
!A 	Attach Entity
!A
!A DESCRIPTION:
!A
!A 	This script attaches an item flag to an entity.
!A
!B INSTRUCTIONS:
!B
!B 	EXISTING ITEMS: Select an existing item flag.
!B
!B TO CREATE A NEW ITEM FLAG:
!B
!B 	PACKET CLASS DIRECTORY: Choose the info packet class directory.
!B
!B 	PACKET FILE NAME: Choose the info packet file you want to use as an
!B 	item.
!B
!B 	PICK ENTITY, New item: Pick the entity to be associated with the item.
!B
!C NOTES:
!C
!C 	Each selected entity will become part of the same item. To change
!C 	items, a new item flag must be selected from the directories.
!C 	If an existing item is picked, the entity is included in that item.
!C
!# see also - htdocs/help.d/items.htm
!# see also - htdocs/help.d/property.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: attach item flag")

DIM Destination$[512]
DIM Answer$[512]
DIM A$[512]
DIM B$[512]
DIM Name$[512]
DIM Prop_dir$[512]
DIM Prop_class$[512]
DIM Class_dir$[512]
DIM Class_name$[512]
DIM Filename$[512]
DIM Oldname$[512]
DIM Data$[32000]
DIM Namelist$[32000]
DIM D_label$[512]
DIM Sub_label$[512]
DIM F_label$[512]
DIM Choice$[512]
DIM Mask$[32]
DIM Current_dir$[512]
DIM Ques$[512]

Stat= NamedVarStr("properties dir",WsSystemVars,"/usr/weber/database/properties/",Prop_dir$)

DeActivateUndo
Start:!
Existing_props:!
Namelist$=""
Group= GrpOfUserItems
IF NOT GroupCount(Group) THEN New_prop
Stat=UserSelectBox("EXISTING ITEMS","Item selected",0,WsPropGroupSelect,Group)

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

Num1= NumericPrefix(Name$)
IF NOT Num1 THEN Pick_prop
Prop_ent= GroupElement(Group,Num1)
IF NOT ValidGeometry(Prop_ent) THEN Start
Stat= DeleteEntity(Group)
LowerSelectBox
Id_number= UserDataType(Prop_ent)
GOTO Pick_entity


New_prop:!
D_label$="PACKET CLASSES"
Choice$="CLASS SELECTED"
Ques$= "PACKET CLASS DIRECTORY  List  "
Answer$= SelectDirectory(Prop_dir$,"*",Ques$,D_label$,Choice$,"",Current_dir$)
IF (ExitScript(Destination$)) THEN Leave
IF  Answer$= "CANCEL" THEN Leave
Answer$= TRIM$(Answer$)
Class_dir$= Answer$
Prop_class$= PathNameFile(Answer$)

Ask_file:!
D_label$= Prop_class$
Choice$= Prop_class$&" SELECTED"
Stat=DirListBox(Class_dir$,"*",D_label$,Choice$,WsAscending)

File_name:stat= SetInputFormat("a99","")
stat= GetValue("PACKET FILE NAME ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Start
IF NOT LEN(Answer$) THEN File_name
Oldname$= Answer$
Filename$=FileNameString(Class_dir$,Oldname$)

LowerSelectBox
Read_item:!
Id_number= NextItemNumber
Stat=GetExtUserString(Filename$,"Name:",Name$)
Prop_ent= ReadUserData(Id_number,Name$,Filename$)

Edit_item:!
IF NOT ValidGeometry(Prop_ent) THEN Start
Stat= DisplayMessage(" ")
Input_stat= MultiEditDataStr(Prop_ent,":")
IF (ExitScript(Destination$)) THEN Leave_script
IF Input_stat=27 THEN Leave
IF Input_stat=1 THEN Edit_item
Stat=GetUserString(Prop_ent,"Name:",Name$)


Pick_entity:!
Stat= SetInputFormat("a1",",N,C,")
E1=PickEntity("PICK ENTITY, New item, Create new instance ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN Start
IF Answer$= "C" THEN 
	New_prop= NewItemInstance(Prop_ent)
	Prop_ent= New_prop
	GOTO Edit_item
END IF

Id_number= UserDataType(Prop_ent)
Stat=AssocUserData(E1,Prop_ent,Id_number)

GOTO Pick_entity

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

