Referencing a Data Type structue in a script

Say I have a Data Type defined, “MyData”, which has three tags in it, Name, Date, Size.

I have an instance of that type called “MyComplexTag”.

Is it possible in a script to read the base tag and then reference the subtags within it?

e.g.:

myInfo = system.tag.read(“MyComplexTag”)
name = myInfo.Name
date = myInfo.Date
size = myInfo.Size

What I’ve tried doesn’t work and I don’t see this documented anywhere.

Thanks.


name = system.tag.read("MyComplexTag/name").value
date = system.tag.read("MyComplexTag/date").value
size = system.tag.read("MyComplexTag/size").value

I havent fooled with UDT’s but I think this would work. or you could use system.tag.readAll and grab all 3 values in one call.

While I’m certain that would work, I’m interested if they can be referenced as I described.

Thanks for the response.

If you have more than a few tags to read then system.tag.readAll() is much more efficient.

[code]udtPath = “[default]MyComplexTag”
udtTags = ["/Name", “/Date”, “/Size”]

#create tags to read based on the udtPath and udtTags
tagsToRead = []
for tag in udtTags:
tagsToRead.append(udtPath + tag)

#read tag values
tagValues = system.tag.readAll(tagsToRead)

#print values
for tagValue in tagValues:
print tagValue.value

#or you could do
print tagValues[0].value
print tagValues[1].value
print tagValues[2].value[/code]

[quote=“eric129”]While I’m certain that would work, I’m interested if they can be referenced as I described.

Thanks for the response.[/quote]

I dont think so, but you may be able to use the browseTags function to grab all of the available tagnames in your structure first and then use readAll to grab the values for the tags that browseTags finds.

I’m thinking about using a readall and converting the results to a dictionary so it would be flexible, but if the read (and write) of the structure did this internally it would be a lot cleaner.

You could create a function like this -

[code]#udtPath determines dictionary keys, pay attention to the final backslash
udtPath = “[myTagProvider]MyUdtFolder/”

#get tag paths for the ‘udt’
udtTagPaths = system.tag.browseTags(parentPath=udtPath, recursive=True, sort=“ASC”)

udtTags = []
for tag in udtTagPaths:
if not tag.isFolder() and not tag.isUDT():
udtTags.append(tag.fullPath)

tagValues = system.tag.readAll(udtTags)

udt = {}
for x in range(len(udtTags)):
#strip udtPath from fullPath and add key and value to dictionary
udt[udtTags[x].replace(udtPath, “”)] = tagValues[x]

print udt[‘Level’].value
print udt[‘Level’].quality
print udt[‘Level’].timestamp
#or
print udt[‘Level’][/code]