
! script name: vproperty.d/attachprop
! var_topic: attach an info packet to an entity

!A
!A NAME:
!A
!A 	Attach an Info Packet to an Entity	--- VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will attach an info packet to a single piece of Synergy 
!A 	geometry.
!A
!B INSTRUCTIONS:
!B
!B 	The existing info packets are displayed in a selection box.
!B 	The list shows the packets' class followed by the packets' name.
!B
!B 	If you select the "CANCEL," button, the Info Packet Class Menu is 
!B 	displayed and you may create a new packet class and name.
!B
!C NOTES:
!C
!C 	1) In VDM info packets must be defined variationally.
!C
!C 	2) A user may attach an entity to any packet previously defined 
!C 	in the variational program.
!C
!C 	3) Duplicate info packets may occur if a packet that has already
!C 	been imported in another object is imported again. 
!C 	The system will only optimize packet occurrences within objects.
!C
!C 	4) Duplicate packets can be resolved by running cleanup.
!C
!C 	This script creates two variational records. The first record
!C 	creates an info packet. The second attaches the packet to
!C 	the appropriate data.
!C
!# see also - htdocs/help.d/property.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: attach a packet")

DIM Destination$[512]
DIM Answer$[512]
DIM A$[512]
DIM B$[512]
DIM Name$[512]
DIM Element$[512]
DIM Prop_dir$[512]
DIM Prop_class$[512]
DIM Prop_name$[512]
DIM Class_dir$[512]
DIM Class_name$[512]
DIM Filename$[512]
DIM Prop_file$[512]
DIM D_label$[512]
DIM Choice$[512]
DIM Ques$[512]
DIM Current_dir$[512]

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

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

IF ARGC>1 THEN
	SELECT UPC$(ARGV(1))
	CASE "READ"
		Run_mode= 1
	CASE "CREATE"
		Run_mode= 2
	CASE ELSE
		Run_mode= 3
	END SELECT
ELSE
	Run_mode= 3
END IF

Start:!
Existing_props:!
IF Run_mode<3 THEN New_prop
Group1= GrpOfUserProps
Group= DependentSubGroup(Group1)
Stat= DeleteEntity(Group1)
IF NOT GroupCount(Group) THEN New_prop
Stat=UserSelectBox("EXISTING INFO PACKETS","Packet selected",0,WsPropGroupSelect,Group)


Pick_prop:!
Stat=SetInputFormat("a99","")
Stat=GetValue("INPUT PACKET 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 ValidDependency(Prop_ent) THEN Start
Stat= EntityNameCheck(Prop_ent,Prop_name$)
Stat= DeleteEntity(Group)
LowerSelectBox
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$=  BaseNamePath(Answer$,Prop_dir$)
Prop_class$= PathNameFile(Answer$)

Ask_file:!
D_label$= Prop_class$
Choice$= Prop_class$&" SELECTED"
Stat= DirListBox(FileNameString(Prop_dir$,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
Prop_file$= Answer$
Filename$= FileNameString(Prop_dir$,Class_dir$,Prop_file$)

Id_number=GetExtUserValue(Filename$,"Class id number:")
Name$=ExtUserDataStr(Filename$,"Name:")
Class_name$=ExtUserDataStr(Filename$,"Class name:")
LowerSelectBox

Make_prop_record:!
Stat= GenerateVarName(WsDataStringEnt,Prop_name$)
RecordFunctionOn
Stat=RecordDescr("PROPERTY:"&Class_name$&" : "&Name$)
V1= StringConstant(Prop_dir$)
V2= StringConstant(Name$)
V3= StringConstant(Class_dir$)
V4= StringConstant(Prop_file$)
V5= StringConstant(FileNameString(V1,V3,V4))
Prop_ent= ReadUserData(WsMiscData,V2,V5,WsTrue)
Stat= NameEntity(Prop_ent,Prop_name$)
RecordFunctionOff
CloseVarRecord
IF NOT ValidGeometry(Prop_ent) THEN
        DelLastVarRecord
	GOTO Start
END IF


Pick_entity:!
IF Run_mode<2 THEN Start
Stat= SetInputFormat("a1",",N,")
E1=PickEntity("PICK ENTITY, New prop",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN Start
IF NOT ValidDependency(E1) THEN Pick_entity
Stat= EntityNameCheck(E1,Element$)

Name$= UserDataStr(Prop_ent,"Name:")
Class_name$= UserDataStr(Prop_ent,"Class name:")
Prop_name$= EntityNameStr(Prop_ent)

Make_assoc_record:!
RecordFunctionOn
Stat=RecordDescr("ASSOCIATE:"&Class_name$&" : "&Name$)
V1= FreeParameter("Class id number",UserDataType(Prop_name$))
Stat=AssocUserData(Element$,Prop_name$,V1)
RecordFunctionOff
CloseVarRecord

GOTO Pick_entity

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


