Skip to end of metadata
Go to start of metadata

You want to use a Script to Delete 
1) all Equipment State(s) of Code X in ALL Classes
2) Equipment State of Code X in ONE Class

Step-by-step guide

Use the Script attached or upload the attached Window for Ignition 7.9.12 / MES 2.0

please note that the Equipment Manager MUST BE RESTARTED for the change to be visible


def getChildLinks(mesObject):
	return mesObject.getChildCollection().getList()

def isType(link, typeStr):
	return link.getMESObjectType().getName() == typeStr

def isEquipmentStateClass(link):
	return isType(link, 'EquipmentStateClass')

def isEquipmentState(link):
	return isType(link, 'EquipmentState')

def remove(mesObject):
	mesObject.setEnabled(False)
	system.mes.saveMESObject(mesObject)

def logInspectMessage(mesObject, depth):
	print '%sInspecting %s object (%s, %s)...' \
		% (' ' * depth * 4, mesObject.getMESObjectTypeName(), mesObject.getUUID(), mesObject.getName())

def logDeleteMessage(state, depth):
	print '%sDeleting %s object (%s, %s) with state code: %s (any substates will also be deleted).' \
		% (' ' * depth * 4, state.getMESObjectTypeName(), state.getUUID(), state.getName(), state.getStateCode())

def deleteStatesInClassWithCodes(stateClass, codes, deleted, logging, depth = 0):
	if logging:
		logInspectMessage(stateClass, depth)
	for childLink in getChildLinks(stateClass):
		if isEquipmentStateClass(childLink):
			childStateClass = childLink.getMESObject()
			deleteStatesInClassWithCodes(childStateClass, codes, deleted, logging, depth + 1)
		elif isEquipmentState(childLink):
			childState = childLink.getMESObject()
			deleteStatesInStateWithCodes(childState, codes, deleted, logging, depth + 1)

def deleteStatesInStateWithCodes(state, codes, deleted, logging, depth = 0):
	if logging:
		logInspectMessage(state, depth)
	for childLink in getChildLinks(state):
		if isEquipmentState(childLink):
			subState = childLink.getMESObject()
			deleteStatesInStateWithCodes(subState, codes, deleted, logging, depth + 1)

	code = state.getStateCode()
	if code in codes:
		if logging:
			logDeleteMessage(state, depth)
		remove(state)
		deleted.add(code)

# Deletes all equipment state objects that match a code in the passed in 'codes' list.
# Example usage: deleteAllStatesWithMatchingCodes([200, 300, 400])
#     The above will delete all equipment states with state code: 200, 300, or 400.
#     Note that this means in all classes, so if you have a state with code 200
#     in 3 different state classes, all 3 will be deleted.
def deleteAllStatesInAllClassesWithMatchingCodes(codes, logging = True):
	root = system.mes.loadMESObject('Equipment States', 'EquipmentStateRoot')
	deleted = set()
	for stateClassLink in getChildLinks(root):
		stateClass = stateClassLink.getMESObject()
		deleteStatesInClassWithCodes(stateClass, codes, deleted, logging)

	if logging:
		print
		if set(codes) - deleted:
			print 'Could not find states with the following codes: %s\n' % codes

# Deletes all equipment state objects that match the passed in code.
# Example usage: deleteStateWithMatchingCode(500)
#     The above will delete all equipment states with state code 500.
def deleteStatesInAllClassesWithMatchingCode(code, logging = True):
	deleteAllStatesInAllClassesWithMatchingCodes([code], logging)

# Deletes equipment state objects that match a code in the passed in 'codes' list,
# if they are also in the passed in state class (nested to any level beneath it).
# Example usage: deleteStatesInClassWithMatchingCodes('MyStateClass', [200, 300, 400])
#     The above will delete all equipment states in 'MyStateClass' that have
#     a state code of: 200, 300, or 400.
def deleteStatesInClassWithMatchingCodes(stateClassName, codes, logging = True):
	stateClass = system.mes.loadMESObject(stateClassName, 'EquipmentStateClass')
	deleted = set()
	deleteStatesInClassWithCodes(stateClass, codes, deleted, logging)
	
	if logging:
		print
		if set(codes) - deleted:
			print 'Could not find states with the following codes: %s\n' % codes

# Deletes the equipment state object that matches the passed in code and state class
def deleteStatesInClassWithMatchingCode(stateClassName, code, logging = True):
	deleteStatesInClassWithMatchingCodes(stateClassName, [code], logging)

# Example calls:

# Deleting a list of state codes from ALL state classes where they appear
# Logging can be turned off by passing 'logging = False'
deleteAllStatesInAllClassesWithMatchingCodes([200, 300, 5000], logging = False)

# Deleting a state code from ALL state classes where it appears
deleteStatesInAllClassesWithMatchingCode(333)

# Deleting a list of state codes from a specific state class
deleteStatesInClassWithMatchingCodes('MyStateClass', [200, 300, 5000])

# Deleting a state code from a specific state class
deleteStatesInClassWithMatchingCode('MyStateClass', 333)



Delete EqState Scripted 7912.proj


The following Script is a modified version that would display in the Script Console, the same information. 
To make it quite obvious all "delete" have been replaced with "display" and the former "remove" function is not only doing a print
and other print are formatted to provide clear information.


def getChildLinks(mesObject):
	return mesObject.getChildCollection().getList()

def isType(link, typeStr):
	return link.getMESObjectType().getName() == typeStr

def isEquipmentStateClass(link):
	return isType(link, 'EquipmentStateClass')

def isEquipmentState(link):
	return isType(link, 'EquipmentState')

def show(mesObject):
	print "------------------------------------------------------------------------------------"
	print "!! mesObject for [%s] found [%s] !!" % (stateCode, mesObject)
	print "------------------------------------------------------------------------------------"

def logInspectMessage(mesObject, depth):
	print '1>[%s] | ObjectTypeName 2>[%s] | Object.getUUID 3>[%s] | Object.getName 4>[%s]' \
		% (' ' * depth * 4, mesObject.getMESObjectTypeName(), mesObject.getUUID(), mesObject.getName())



def logdisplayMessage(state, depth):
	#print '1[%s]Displaying 2[%s] object (3[%s], 4[%s]) with state code: 5[%s] (any substates will also be displayd).' \
	print '1>[%s] | ObjectTypeName 2>[%s] | state.getUUID 3>[%s] | state.getName 4>[%s] | state.getStateCode 5[%s]' \
		% (' ' * depth * 4, state.getMESObjectTypeName(), state.getUUID(), state.getName(), state.getStateCode())

def displayStatesInClassWithCodes(stateClass, codes, displayd, logging, depth = 0):
	if logging:
		logInspectMessage(stateClass, depth)
	for childLink in getChildLinks(stateClass):
		if isEquipmentStateClass(childLink):
			childStateClass = childLink.getMESObject()
			displayStatesInClassWithCodes(childStateClass, codes, displayd, logging, depth + 1)
		elif isEquipmentState(childLink):
			childState = childLink.getMESObject()
			displayStatesInStateWithCodes(childState, codes, displayd, logging, depth + 1)

def displayStatesInStateWithCodes(state, codes, displayd, logging, depth = 0):
	if logging:
		logInspectMessage(state, depth)
	for childLink in getChildLinks(state):
		if isEquipmentState(childLink):
			subState = childLink.getMESObject()
			displayStatesInStateWithCodes(subState, codes, displayd, logging, depth + 1)

	code = state.getStateCode()
	if code in codes:
		if logging:
			logdisplayMessage(state, depth)
		show(state)
		displayd.add(code)

# displays all equipment state objects that match a code in the passed in 'codes' list.
# Example usage: displayAllStatesWithMatchingCodes([200, 300, 400])
#     The above will display all equipment states with state code: 200, 300, or 400.
#     Note that this means in all classes, so if you have a state with code 200
#     in 3 different state classes, all 3 will be displayd.
def displayAllStatesInAllClassesWithMatchingCodes(codes, logging = True):
	root = system.mes.loadMESObject('Equipment States', 'EquipmentStateRoot')
	displayd = set()
	for stateClassLink in getChildLinks(root):
		stateClass = stateClassLink.getMESObject()
		displayStatesInClassWithCodes(stateClass, codes, displayd, logging)

	if logging:
		print
		if set(codes) - displayd:
			print 'Could not find states with the following codes: %s\n' % codes

# displays all equipment state objects that match the passed in code.
# Example usage: displayStateWithMatchingCode(500)
#     The above will display all equipment states with state code 500.
def displayStatesInAllClassesWithMatchingCode(code, logging = True):
	displayAllStatesInAllClassesWithMatchingCodes([code], logging)

# displays equipment state objects that match a code in the passed in 'codes' list,
# if they are also in the passed in state class (nested to any level beneath it).
# Example usage: displayStatesInClassWithMatchingCodes('MyStateClass', [200, 300, 400])
#     The above will display all equipment states in 'MyStateClass' that have
#     a state code of: 200, 300, or 400.
def displayStatesInClassWithMatchingCodes(stateClassName, codes, logging = True):
	stateClass = system.mes.loadMESObject(stateClassName, 'EquipmentStateClass')
	displayd = set()
	displayStatesInClassWithCodes(stateClass, codes, displayd, logging)
	
	if logging:
		print
		if set(codes) - displayd:
			print 'Could not find states with the following codes: %s\n' % codes

# displays the equipment state object that matches the passed in code and state class
def displayStatesInClassWithMatchingCode(stateClassName, code, logging = True):
	displayStatesInClassWithMatchingCodes(stateClassName, [code], logging)

# Example calls:

# Displaying a list of state codes from ALL state classes where they appear
# Logging can be turned off by passing 'logging = False'
#displayAllStatesInAllClassesWithMatchingCodes([200, 300, 5000], logging = False)
#displayAllStatesInAllClassesWithMatchingCodes([200, 300, 5000], logging)

# Displaying a state code from ALL state classes where it appears
if logging:
	displayStatesInAllClassesWithMatchingCode(stateCode)
else:
# Displaying a state code from a specific state class
	displayStatesInClassWithMatchingCode(stateClass, stateCode)

# Displaying a list of state codes from a specific state class
#displayStatesInClassWithMatchingCodes('MyStateClass', [200, 300, 5000])





Ignition 7.9.12
MES 2.0

MES Objects

Equipment States