Export all component text used in Ignition

This should work in a button for vision… pro tip dont try to understand this one xD There is a bucnh of things that dont get used or are not needed but… it works xd
(again dont do this to often, it may take long xd)

from com.inductiveautomation.factorypmi.application.binding.action import ActionAdapter

class ProjectResources:
	
	def __init__(self):

		import system
		from javax.swing import JFrame
		from java.awt.event import WindowEvent
		
		if system.util.getSystemFlags() & system.util.DESIGNER_FLAG:
			try:
				from com.inductiveautomation.ignition.designer import IgnitionDesigner
			except ImportError:
				pass
			try:
				self.context = IgnitionDesigner.getFrame().getContext()
			except AttributeError:
				pass
		else:
			from com.inductiveautomation.factorypmi.application.runtime import ClientPanel
			"""
			Attempt to resolve client context. By creating a frame it prevents the need to traverse the object heirarchy. i.e. parent.parent....
			"""
			projName = system.util.getProjectName()
			frame_name = 'ThrowAway'
			frm = JFrame(frame_name)
			frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
			windows = frm.getWindows()
			frm.dispose()
			for window in windows:
				try:
					if projName in window.getTitle():
						pane =  window.getContentPane()
						if isinstance(pane, ClientPanel):
							if hasattr(pane, 'getClientContext'):
								self.context = pane.getClientContext()
								break
				except AttributeError:
					pass
		if not hasattr(self.context, 'getProject'):
			raise Exception(self.getInvalidContextError(context))	

	def getAllProjectResources(self):
		return self.context.getProject().getResources()
	
	def getProjectResource(self, resourcepaths):
		list = []
		for resource in self.getAllProjectResources():
			if str(resource.getResourcePath().getFolderPath()) in resourcepaths :
				list.append(resource)
		return list	 	

	def getResourceInfo(self, resourcepath):
		for resource in self.getAllProjectResources():
			if str(resource.getResourcePath().getFolderPath()) == resourcepath :
				dresource = self.getDeSerializedResource(resource)
				if dresource != None:
					return {'Name' : dresource.getName(), 'Title' : dresource.getTitle() }
				else:
					return {}					
		return {}	 	

	def getAllComponentsForResource(self,resourcepaths):
		rows = []
		header = ['Project name', 'Resource Name','Resource Type', 'Component Type', 'Attribute', 'Name' ,'Value']
		resources = self.getProjectResource(resourcepaths)
		checkedResource=[]
		
		def resolveData(component):
			
			if component.getClass().getSimpleName() == u'TemplateHolder':
				if component.templatePath not in checkedResource:				
					checkedResource.append(component.templatePath)				
					tempResources = self.getProjectResource(component.templatePath)					
					for tempResource in tempResources:											
						dresource = self.getDeSerializedResource(tempResource)
						if dresource != None:
							rows.extend(resolveData(dresource))
							
							tempComponents = self.getAllComponentsForObject(dresource)
							for tempComponent in tempComponents:
								#print tempComponent.getName(), tempComponent.getClass().getSimpleName()								
								rows.extend(resolveData(tempComponent))
								
			#get resources
			arr = []
			if hasattr(component, 'text') and component.name != None and component.text:
				arr.append([resource.getProjectName(), resource.getResourceName(), resource.getResourceType().getTypeId(), component.getClass().getSimpleName(), 'Text', component.name, component.text])	
			if hasattr(component, 'toolTipText') and component.name != None and component.toolTipText:
				arr.append([resource.getProjectName(), resource.getResourceName(), resource.getResourceType().getTypeId(), component.getClass().getSimpleName(), 'ToolTip', component.name, component.toolTipText])	
			if hasattr(component, 'title') and component.name != None and component.title:
				arr.append([resource.getProjectName(), resource.getResourceName(), resource.getResourceType().getTypeId(), component.getClass().getSimpleName(), 'Title', component.name, component.title])	
			return arr			
		
			
		for resource in resources:
			dresource = self.getDeSerializedResource(resource)
			if dresource != None:
				
				rows.extend(resolveData(dresource))
				components = self.getAllComponentsForObject(dresource)
				for component in components:
					#print component.getName(), component.getClass().getSimpleName()					
					rows.extend(resolveData(component))
					
		return system.dataset.toDataSet(header, rows)		
		
	def getDeSerializedResource(self, resource):
		deserializer = self.context.createDeserializer()
		if not resource.isFolder():
			if str(resource.getResourceType().getTypeId()) == 'windows':
				try:				
					return deserializer.deserialize(resource.getData("window.bin")).getRootObjects()[0]
				except:
					pass
			elif str(resource.getResourceType().getTypeId()) == 'templates':
				try:				
					return deserializer.deserialize(resource.getData("template.bin")).getRootObjects()[0]
				except:
					pass
		return None

	def getAllComponentsForObject(self, object):
		arr = []
		try:
			for component in object.getComponents():
				arr.append(component)
				self.objectSearch(component, arr)
		except AttributeError:
			pass		
		return arr
		
	def objectSearch(self, object, array):
		try:
			for component in object.getComponents():
				array.append(component)
				self.objectSearch(component, array)
		except AttributeError:
			return		

allText = ProjectResources().getAllComponentsForResource(system.gui.getWindowNames())

for r in range(allText .getRowCount()):
	key = allText.getValueAt(r, 'Value')
	value = key 
	system.util.modifyTranslation(str(key), str(value), 'en')

1 Like