Add template dynamically

Hi everybody,
I’m trying to dynamically add a template but without being able to change the size and content,
code is:


from com.inductiveautomation.factorypmi.application.components.template import TemplateHolder

template = TemplateHolder()
template.setName("test")
template.visible = True
template.setTemplatePath("ItemPopup/GENERIC_POPUP")  #here can't load right popup
template.setSize(400,400)  #here can't load right size
root = system.gui.getParentWindow(event).rootContainer
root.addComponent(template)

from com.inductiveautomation.factorypmi.application.components.template import TemplateHolder root = system.gui.getParentWindow(event).rootContainer newobj = TemplateHolder() newobj.initTemplate(root.getAppContext()) newobj.setTemplatePath("XXX") newobj.setName("DynTemplate") newobj.getLoadedTemplate().setEnableLayout(1) root.addComponent(newobj)
8)

After marioquark’s code is written you can resize and move the template instance around with the system.gui.moveComponent and system.gui.resizeComponent functions.

Example:

from com.inductiveautomation.factorypmi.application.components.template import TemplateHolder
root = system.gui.getParentWindow(event).rootContainer
newobj = TemplateHolder()
newobj.initTemplate(root.getAppContext())
newobj.setTemplatePath("test1")
newobj.setName("DynTemplate")
newobj.getLoadedTemplate().setEnableLayout(1)
root.addComponent(newobj)
system.gui.moveComponent(newobj, 100,100)
system.gui.resizeComponent(newobj, 400,400)

Unsupported by IA, but powerful and awesome code. I will always remember marioquark as wearing cool sunglasses.

Nick Mudge
Ignition Consultant
nickmudge.info

:laughing:

Hi,

I have only a small addition. If you want to use the dynamics within the template (animations, bindings, …) template should be initialized with one extra line:

newobj.getLoadedTemplate().startup()

Vita, this is great to know. How did you find this out?

Hi Nick,

I needed set a variable in my dynamically added template and it did not work. I try simple animation using through tag and it did not work. I realized that some init is missing. So because of poor programmer manual in Ignition I tried to find a solution myself (through class analyzing). Then I remembered that I saw the init function list in trace log when error occurred. And indeed there was “VisionTemplate.initialize” line :slight_smile:

So little reverse engineering and luck 8)

Gotcha, thanks!