Ignition scripting function for component creation

Is there a system function to create a component in a window with a parameter dict in vision?

Not that I'm aware of, but adding components with scripting can easily be done. Just import the component class, create an instance of the class using the class's constructor, and add the resultant component to the desired container with the container's addComponent method. Finally, use system.gui.transform to properly position and size the component.

2 Likes

Can you expand on this? I haven’t been able to find anything on importing a component class.

Welcome to the forum, the search here is pretty good…

https://forum.inductiveautomation.com/search?q=import%20component%20class

3 Likes

Sure; I can do a quick tutorial on this. You're wanting to add a component at runtime with scripting? Can you give me an example of what you're actually trying to accomplish, so I can tailor my advice to your use case?

I’ve inherited an Ignition project that uses a column of “buttons” on the left of each window to navigate. I say “button” because it’s really a template with a button component on it (plus some labels and custom properties).

The setup works just fine, but it’s a cumbersome process to add another one of these Navigation Buttons. We’re still in the process of building out this project so whenever we add a window we have to go through the process of reorganizing every other Navigation Button by hand and make sure they line up, that sort of thing. More often than not, we don’t bother and clunkily add a button on a different (often unrelated) window so we don’t mess with the formatting of our navigation bar.

I want to make this process easier so my idea is to have a dataset where each row represents a Navigation Button and whatever properties are needed. For example: text to display, path of the window to switch to, associated tag paths, to name a few. I’d have a script reference this and create each Navigation Button and assign the properties, and then use system.gui.transform to place each Navigation Button in the window.

If it's not possible to do this because a user-generated template doesn't have a class that can be imported with Python, I'd still be curious how to do with with another component like a button or label.

Thanks!

Sounds like a job for a Template Repeater. Don't re-invent the wheel.

I explored the Template Repeater, but our windows are organized in categories so I wanted to be able to indent them and dynamically show/hide the Navigation Buttons. For example, if a user clicks on Category 2, it'll hide the associated windows in the other categories:

  • Home
  • Category 1
  • Category 2
    • SubWindow 1
    • SubWindow 2
  • Cagetory 3

We're quickly adding more windows than we can display Navigation Buttons for all at once.

Use a tree view?

It would be possible, technically, to this with a more complex GUI using the template canvas, but I'd just use a tree view.

That is, in order of complexity and maintainability I'd recommend:

  1. A tree view
  2. A tree view with custom scripting to change the way it renders to get appearance closer to what you want
  3. A template canvas where you'd be in charge of managing selection state synchronization yourself
  4. Adding custom components at runtime
1 Like