Add templates to window via script

I have an array of settings tags in my PLC, which are a combination of strings/dints/floats. I have a few settings template objects. I want to write a script that browses PLC tags, then adds the appropriate template object to my Settings screen. Is this possible? I would want it to look something like this:

window = system.gui.getWindow("Settings")
tags = system.tag.browse("PLC/Settings",{})

for i in range(tags.count()):
    if tag.Type == 1:
        window.addTemplate("Setting_String", x, y)
    if tag.Type == 2:
        window.addTemplate("Setting_Int", x, y)

That's what the template canvas is for. (You cannot add components to windows at runtime.)

Welcome to the forum. Please see Wiki - how to post code on this forum. Then you can edit your post (pencil icon) to fix it. It may help someone else in future.

Is there no way to do this in the designer script window also then? I was thinking I could just script it while designing and return to it if I have UDT changes. I'm having a hard time with the template canvas. I can't figure out how to have a string/int setting object but hide visibility of one. I'm also trying to pass a tag through the template canvas to the template but the tag doesn't seem to be linking

Not reliably, no, and not supported. Not impossible, but you have to go way under the hood, and have to be comfortable with java Swing.

Can I modify the properties on objects from the scripting? So if I make a generic setting template, can I script through and set the template properties to the objects after I've added them to the window?

The final argument in the template canvas' dataset is a JSON string (because Vision lacks true multidimensional property support).

So I would say you should be constructing a json value for each row that looks like this:

{
"shouldBeVisible": true,
"tagPath": "path/to/tag"
}

Updating the template canvas dataset dynamically from the outside using whatever script you feel like.
Inside the template, use indirect tag bindings to reference actual tag values using the input tagPath parameter, and bind your template's overall visibility to the shouldBeVisible value. If you add hidemode 3 to your template canvas' overall "layout constraints" property, invisible components will not participate in layout.

1 Like

The indirect tags fixed this for me, thanks! I ended up just copy/pasting a single template object in multiple times, the canvas object didn't end up being necessary. For the curious, here was my final script. Still adding in the type verification from the PLC tag

window = system.gui.getWindow("Settings_Recipe/_StdSettings")
rc = window.getRootContainer()
cont = rc.getComponent("Settings")

for i in range(20):
	setting = "TN_Setting " + str(i)
	comp = cont.getComponent(setting)
	comp.setPropertyValue("TagPath", "[default]OvenPLC/HMI/Settings/Setting" + '{:03d}'.format(i))