
! script name: vproperty.d/turbodata
! var_topic: attach TurboPost variable data a contour

!A
!A NAME:
!A
!A 	Attach an TurboPost Data to a group	--- VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script attaches TurboPost variable data to a specified
!A 	contour.  It will output TurboPost Data to the command_file
!A	when the contour is ready to be output.  The contour must
!A	have this packet AND a tool associated with it in order to be
!A	output.
!A
!B INSTRUCTIONS:
!B
!B	INPUT PACKET ID NAME.  Pick an existing packet to assign.
!B		Choose CANCEL to read in a new one.
!B
!B	INFO PACKET FILE NAME.  Pick which packet to read and assign.
!B
!B	EDIT WHICH DATA FIELD, Continue.  Edit the data fields before
!B		they are assigned to the group.  Hit Continue when
!B		done editing or if you don't need to edit.
!B
!B	PICK GROUP, New_prop .  Pick the group to associate the
!B		TurboPost data.  Pick New_prop to choose another
!B		packet.
!B
!C NOTES:
!C
!C 	1) In VDM, info packets must be defined variationally.
!C
!C 	2) You can attach an entity to any info 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 within another object is imported again. 
!C 	The system will only optimize packet occurrences within objects.
!C
!C 	4) Duplicate info packets may be resolved by running cleanup.
!C
!C 	This script creates two variational records. The first record
!C 	creates an info packet. The second record attaches the info packet to
!C 	the appropriate data.
!C
!# see also - htdocs/help.d/property.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: attach a TurboPost 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_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]
DIM Namelist$[4096],A$[256],B$[256],C$[256]

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

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

Run_mode= 3

Start:!
Existing_props:!
IF Run_mode<3 THEN New_prop
Group1= GrpOfUserProps(WsTurboData,WsTurboData)
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_group

New_prop:!
Stat= DeleteEntity(Temp_ent)
Class_dir$= "TurboData.d"
D_label$= "TurboPost DATA"
Choice$= "TurboPost DATA SELECTED"
Stat= DirListBox(FileNameString(Prop_dir$,Class_dir$),"*",D_label$,Choice$,WsAscending)

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

Name$=ExtUserDataStr(Filename$,"Name:")
Class_name$=ExtUserDataStr(Filename$,"Class name:")
LowerSelectBox
Temp_ent= ReadUserData(WsTurboData,"temp",Filename$,WsTrue)

Edit_data:!
Namelist$= DataFieldListStr(Temp_ent,":",",",WsNoOrder,WsTrue)

Stat=GeneralListBox("DATA FIELDS","DATA FIELD SELECTED",Namelist$)

Data_field:Stat= SetInputFormat("a99","",WsFalse)
Stat= GetValue("EDIT WHICH DATA FIELD, Continue ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT LEN(Answer$) THEN Data_field
LowerSelectBox
SELECT UPC$(Answer$)
CASE "CANCEL"
	Stat= DeleteEntity(Temp_ent)
	GOTO New_prop
CASE "CONTINUE","C","CONT"
	GOTO Make_prop_record
END SELECT

IF POS(Answer$,"=") THEN
	A$= DataFieldStr(Answer$,"=",1)
	B$= DataFieldStr(Answer$,"=",2)
	B$= TRIM$(B$)
ELSE
	A$= Answer$
	B$=""
END IF

New_value:!
IF LEN(B$) THEN
	Stat= AddBackSlashChars(B$,C$)
	Stat= SetInputFormat("a99=","",WsFalse)
	Stat= SetEqualsValue(0,C$)
	Stat=SetEditString(C$)
ELSE
	Stat= SetInputFormat("a99","",WsFalse)
END IF
Stat= GetValue(A$&" = ",Answer$)
IF ExitScript(Destination$) THEN Leave
Answer$= TRIM$(Answer$)
IF NOT LEN(Answer$) THEN New_value
Stat= BackSlashChars(Answer$,B$)
Stat= PutUserString(Temp_ent,A$&":",B$)
GOTO Edit_data

Make_prop_record:!
Name$=UserDataStr(Temp_ent,"Name:")

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(WsTurboData,V2,V5,WsTrue)

RecordFunctionOff
Loop_var= 1
LOOP
Stat= StringFieldStr(Namelist$,",",Loop_var,A$)
EXIT IF NOT LEN(A$)
A$= DataFieldStr(A$,"=",1)
IF A$="Class id number" THEN End_loop
IF A$="Class name" THEN End_loop
C$=A$&":"
B$= UserDataStr(Temp_ent,C$)
IF IsStringNumeric(B$) THEN
	Value= VAL(B$)
	RecordFunctionOn
	V1=StringConstant(C$)
	V2=FreeParameter(A$,Value)
	Stat= PutUserValue(Prop_ent,V1,V2)
	RecordFunctionOff
ELSE
	RecordFunctionOn
	V1=StringConstant(C$)
	V2=StringConstant(B$)
	Stat= PutUserString(Prop_ent,V1,V2)
	RecordFunctionOff
END IF
End_loop:Loop_var= Loop_var+1
END LOOP
RecordFunctionOn

Stat= NameEntity(Prop_ent,Prop_name$)
RecordFunctionOff
CloseVarRecord
Stat= DeleteEntity(Temp_ent)

IF NOT ValidGeometry(Prop_ent) THEN
        DelLastVarRecord
	GOTO Start
END IF

Pick_group:!
Stat= DeleteEntity(Temp_ent)
IF Run_mode<2 THEN Start
Stat= SetInputFormat("a1",",N,")
Group=PickGroupEnt("PICK GROUP, New_prop ",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN Start
IF NOT ValidDependency(Group) THEN Pick_group
Stat= EntityNameCheck(Group,Element$)
Stat=Highlight(Group,WsPickPen,WsDirArrowOff)

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$)
Stat=AssocUserData(Element$,Prop_name$,WsTurboData)
RecordFunctionOff
CloseVarRecord

GOTO Pick_group

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

