Access a view's child components using viewPath

Hi all,

I'm looking to develop a feature for filtering and highlighting assets, allowing users to easily search and locate them on a screen displaying a list of all assets.

The feature involves a screen where a 'viewPath' parameter is input, linking to an embedded view. I plan to use scripting to fetch the view objects using this 'viewPath', enabling access to all components within the embedded view. This approach will separate the code, allowing any generic view to be used.

It's similar to employing self.parent.getChildren() in a view to obtain all page components.

My question is: do I need to manually read the view's JSON file from the file system, or is there an existing system function that can perform this task?

Okay so after playing around , I ended up using using this.

viewPath=''

filePath = 'C:\Program Files\Inductive Automation\Ignition\data\projects\<ProjectName>\com.inductiveautomation.perspective\\views\{}'.format(viewPath.replace('/','\\'))
pageData=system.util.jsonDecode(system.file.readFileAsString(filePath))
return pageData

This seems a bit dodgy. I'd be inclined to have the asset information in a database and generate the views from that, if possible.

Here is how it turned out.

2 Likes

If you're going to do this, at least avoid the insane performance hit of calling into the local filesystem each time.

You could install Ignition Extensions (or search on this forum for access to the IgnitionGateway class, then retrieve the ProjectManager from it).

Once you've got a ProjectManager, you can ask it for a given project by name, which you can then ask for a specific resource by ResourcePath. You then ask the resource for its data under the view.json key.

This will all work off the in-memory representation of the project and should be dramatically faster than reading from the filesystem each time.

There's possibly also a better way to do this without the raw project resource involved at all, but it's also possible such a thing isn't actually exposed to you in scripting.

1 Like

Okay I tried what you suggested but got stuck, I there is no data returned on the resourceData Class Object.

from com.inductiveautomation.ignition.common.project.resource import ResourcePath,ResourceType
rtype=ResourceType('com.inductiveautomation.perspective','views')
resourcePathObject=ResourcePath(rtype,'1_Templates/3_UI/Alarms/highlight')
resourceData =system.project.getProject().getResource(resourcePathObject)
print resourceData.get().getData()

Okay finally figured it out

	from com.inductiveautomation.ignition.common.project.resource import ResourcePath,ResourceType
	rtype=ResourceType('com.inductiveautomation.perspective','views')
	resourcePathObject=ResourcePath(rtype,value)
	resourceData =system.project.getProject().getResource(resourcePathObject)
	jsonString=resourceData.get().getData('view.json').tostring()
	return system.util.jsonDecode(jsonString)
1 Like