Refactor Script from Vision to Perspective strategies

I´ve got some code from previous Ignition Vision project I´m trying to refactor into the new Ignition Perspective project.

¿What are some good strategies to refactor the code?

image

Vision Script example:

if event.propertyName == 'data':
	if (event.source.data.rowCount > 0) and (event.source.data.getValueAt(0, 0) != 0):
		TPyRowData = []
		for col in range(event.source.data.columnCount):
			TPyRowData.append("")
		
		event.source.data = system.dataset.addRow(event.source.data, 0, TPyRowData)
		
		TData = event.source.data	
		for row in range(1, TData.rowCount):
			nTotal = 0
			for col in range(2, TData.columnCount):
				nTotal += TData.getValueAt(row, col)	
			
			TData = system.dataset.setValue(TData, row, 1, int(nTotal))
		
		event.source.data = TData	
		
		graph = event.source.parent.getComponent('graphStats1')
		graph.data = system.dataset.deleteRows(graph.data, range(graph.data.getRowCount()))
		for row in range(1, TData.rowCount):
			graph.data = system.dataset.addRow(graph.data, [TData.getValueAt(row, 0), TData.getValueAt(row, 1)])
			
	
	if (event.source.data.rowCount > 0):
		event.source.RecordCount = event.source.data.rowCount - 1
	else:
		event.source.RecordCount = 0

	#Ajusta textos de cabecera
	listText = [ \
	'<html><p>Equipos de</p><p>PCI</p>', \
	'<html><p>Instalacion</p><p>electrica</p>', \
	'<html><p>Se&ntilde;alizacion</p><p>PCI</p>', \
	'<html><p>Iluminacion</p><p>emergencia</p>', \
	'<html><p>Salidas y</p><p>vias evac.</p>', \
	'<html><p>Se&ntilde;alizacion</p><p>evacuacion y</p><p>emergencia</p>', \
	'<html><p>Se&ntilde;alizacion</p><p>riesgo elect.</p>', \
	'<html><p>Caida de</p><p>personas</p><p>mismo nivel</p>', \
	'<html><p>Escalas</p><p>fijas</p>', \
	'<html><p>Escaleras</p><p>portatiles</p><p>de mano</p>', \
	'<html><p>Iluminacion</p><p>PVD</p>', \
	'<html><p>Iluminacion</p><p>resto</p><p>(no emerg.)</p>', \
	'<html><p>Climatiz.</p><p>(Temp.)</p>', \
	'<html><p>Climatiz.</p><p>(Hr)</p>', \
	'<html><p>Climatiz.</p><p>(CO2)</p>', \
	'<html><p>Caida de</p><p>objetos</p><p>desprendidos</p>', \
	'<html><p>Atropellos</p>', \
	'<html><p>Transito por</p><p>cubierta</p>', \
	'<html><p>Otros riesgos</p><p>detallar)</p>' \
	]
	
	tabla = event.source.parent.getComponent('tblStats')
	TData = tabla.columnAttributesData
	for row in range(TData.rowCount):
		if TData.getValueAt(row, 0)[1:7] == 'RIL':		
			TData = system.dataset.setValue(TData, row, 6, listText[int(TData.getValueAt(row, 0)[7:11]) - 1])
		
	tabla.columnAttributesData = TData
	
  1. Understand what it does.
  2. Understand the differences between Vision and Perspective.
  3. Write new code that does the same thing in Perspective.

Not be flippant, but there's no shortcut here.

A useful thing to between steps 1 and 2 might be "extract the common business logic that deals with basic Python/Ignition types into a project library script" - that way you can call the exact same code from both Vision and Perspective, and only deal with the UI layer to extract component properties.

3 Likes

Thanks for the advice PGriffith.