How to get project object using script

How to get project object using script

What I want to do is using the project object, get “templates” name on drop down, so user can select the template clicking on drop down

There’s a way to get the project’s windows ( system.gui.getWindowNames() ), but unfortunately there is no function to get all of the template names. You could just use a static list of your templates in a dropdown component.

I dug into the Ignition and Java objects and found a way to get a list of all the template paths that exist in a project.

The following function can be used in an event handler to get a list of all the templates in the current project. It works in the client and designer:

def getProjectTemplates(event):
	import system
	comp = event.source
	if system.util.getSystemFlags() & system.util.DESIGNER_FLAG:
		from com.inductiveautomation.ignition.designer import IgnitionDesigner
		while not isinstance(comp,IgnitionDesigner):		
			comp = comp.parent
		context = comp.getContext()
	else:
		from com.inductiveautomation.factorypmi.application.runtime import ClientPanel
		while not isinstance(comp,ClientPanel):
			comp = comp.parent
		context = comp.getClientContext()
	project = context.getProject()
	templates = project.getResourcesOfType("fpmi","component-template")	
	templatePaths = [project.getFolderPath(template.getResourceId()) for template in templates]
	return templatePaths

l = []
templatePaths = getProjectTemplates(event)
for path in templatePaths:
	l.append([path])  

data = system.dataset.toDataSet(["Value"], l)

event.source.getComponent('Dropdown').data = data

After the function definition the code shows how to make a dataset out of the list of template paths and assigns the dataset to a dropdown.

By binding a template’s templatePath property to the dropdown’s selectedStringValue it is possible to switch a template in a window to any template that exists in the project by selecting different template paths in the dropdown.

Nick

Note that there is no guarantee that this code will continue to work after any upgrade, and is not officially supported. I highly recommend against using it unless you are familiar with the SDK.

As Robert already mentioned, this code unfortunately isn’t working anymore under Ignition 8 you have to pass a ressourceType object. Creating this object with “fpmi”,“component-template” will give back an empty list.
Since I didn’t find the API documentation for Ignition 8 yet I can’t get this to work. I guess the only thing that is missing is the correct type identifier for templates in Ignition 8.
Any ideas what that could be?

1 Like