EDIT: This code now works. All i did was step away for a bit, ignition had to restart due to computer going to sleep. I have no idea why it wouldn't have worked in the first place. Any insight on this would be great.
I updated my code to mimic the parameter property change. Even this does not update the Root Node UDT definition itself.
I have looked at other posts going back to 2020 and haven't found any updates on scripts to update a UDT definition's root node per say. Some places are:
- Edit UDT Definition in script
- Scripted editing of UDT definitions - modifying alarm names - I was able to do this to the UDT definition members, yet the Root node I have not figured out a way to script any property/parameter changes
My new code, does the same thing as my previous post but in a different way. No matter if I use a custom made Dict or just the value of "test" the UDT does not seem to update the Root Node UDT definition.
def updateTagParms(newParmsmap):
"""Add new perameters to UDT definitions"""
parms = {}
for key in newParmsmap:
parms[key] = {
"dataType": "String",
"value": ""
}
return parms
system.perspective.print("Proccess started")
# Variables
# merge, modifying values that are specified in the definition, without impacting values that aren't defined in the definition. Use this when you want to apply a slight change to tags, without having to build a complete configuration object.
collisionPolicy = "m"
# Parameters to add: TODO: need to take oput spaces in entries to be sure there are no duplicate add ons.
newParms = str(self.getSibling("ParmsToAdd").props.text).split(",")
newParmsmap = map(str.strip, newParms)
parms = updateTagParms(newParmsmap)
#grab MAin Folder path to UDT Instances
UDTDefinitionPath = self.getSibling("DefinitionPath").props.text
UDTconfigs = system.tag.getConfiguration(UDTDefinitionPath, True)
basePath = UDTconfigs[0]['path']
# itterate through each UDT configs
for udt in UDTconfigs[0]['tags']:
# Make sure we are only getting teh UDT definition and not any Atomic tags
if str(udt['tagType']) == "UdtType":
# itterate through each element in teh dict to add to the UDT
for key, value in parms.items():
# updates teh current parameters
udt['parameters'][key] = value
# sends new ipdates to ignition to make teh changes in the system
system.tag.configure(basePath, udt, collisionPolicy)
system.perspective.print("Proccess Finished")
UPDATED CODE USING CHAT GPT - Also added the browse function this morning, this will allow use to start at a root folder and work our way down to a UDT definition or tagType = UdtType:
def runAction(self, event):
try:
system.perspective.print("Proccess started")
# Variables
# merge, modifying values that are specified in the definition, without impacting values that aren't defined in the definition. Use this when you want to apply a slight change to tags, without having to build a complete configuration object.
collisionPolicy = "m"
# Parameters to add: Strips all white space from entries and splits each entry into a list
newParms = [param.strip() for param in str(self.getSibling("ParmsToAdd").props.text).split(",")]
for element in newParms:
for char in element:
if ord(char) > 127:
raise ValueError("ASCII chars are presents in parameters.")
if newParms[0] == "":
raise ValueError("No parameters were provided.")
# Creates a dict based off teh above list. Each Item in teh list is a key and teh value is "dataType": "String", "value": ""
parms = {key: {"dataType": "String", "value": ""} for key in map(str.strip,newParms)}
#grab MAin Folder path to UDT Instances
UDTDefinitionPath = self.getSibling("DefinitionPath").props.text
# need to use browse- Due to if we want to start at teh base folder and drill down this is where we need to start
# If we start with teh dirct tag/folder we can start with getconfigs function instead
UDTBrowse = system.tag.browse(UDTDefinitionPath, filter = {"tagType": "UdtType", "recursive":True})
for udt in UDTBrowse:
# grab the full path of teh UDT definition
UDTPath = udt['fullPath']
# we need to have the folder this UDT is in to update teh configs
basePath = str(udt['fullPath']).replace("/"+ str(udt['name']), "")
# we just need the configs of teh UDT definition and not the tags uinder them - So we use False as the recursion value.
UDTconfigs = system.tag.getConfiguration(UDTPath, False)
# # itterate through each UDT configs
for udtConfig in UDTconfigs:#[0]['tags']:
# Make sure we are only getting the UDT definition and not any Atomic tags
if str(udtConfig['tagType']) == "UdtType":
# itterate through each element in the dict to add to the UDT
for key, value in parms.items():
# updates teh current parameters
udtConfig['parameters'][key] = value
# sends new ipdates to ignition to make teh changes in the system
system.tag.configure(basePath, udtConfig, collisionPolicy)
system.perspective.print("Proccess Finished")
except Exception as e:
system.perspective.print("Process failed with error: " + str(e))