Hello everyone,
i want to create an MaterialClass and a MaterialDefinition in the same script. When i run the script once it gives an error. But when i run the same script for the second time it works. Can someone help figure out why it fails on the first run?
The script:
className = 'Finished Goods'
matName ='Nuts 8oz'
addToRoot = True
objClassLink, retVal = Material.CreateClass(className, addToRoot)
print retVal
objClassLinks, retVal = Material.CreateDef(matName, objClassLink)
#print retVal
the Functions
# MaterialClass
def CreateClass(name, addToRoot):
# This function is passed a name for a Material Class and will create it if it doesn't already exist.
# If addToRoot is True, then this class will be added to the Material Root, allowing any material
# definitions in this class to be used by The OEE Material Manager
# Inputs - name: material class as string
# Inputs - addToRoot: boolean
# Outputs - link: MESObjectLink to the Material Class
# Outputs - RetVal: String status
filter = system.mes.object.filter.createFilter()
filter.setMESObjectTypeName('MaterialClass')
filter.setMESObjectNamePattern(name)
list = system.mes.searchMESObjects(filter)
if list.size() == 0:
retVal = "Creating Material Class " + name + "."
obj = system.mes.createMESObject('MaterialClass')
obj.setPropertyValue('Name', name)
link = system.mes.object.link.create(obj)
else:
link = list.get(0)
obj = list.get(0).getMESObject()
retVal = "Material Class " + name + " already exists."
if addToRoot:
rootFolder = 'Material Root'
parentList = obj.getParentCollection().getList()
if parentList.findByName(rootFolder) is None:
retVal = retVal + " Added to " + rootFolder
obj.addParent(system.mes.loadMESObject(rootFolder, 'MaterialRoot'))
else:
retVal = retVal + ' It is already a child of ' + rootFolder
system.mes.saveMESObject(obj)
return link, retVal
# MaterialDef
def CreateDef(name, objClassLink):
#This function will check if the passed Material definition exists and if not creates it.
#It will then check to see if it is a member of the given Material class and if not will add it.
#Inputs - name: material def as String
#Inputs - objClassLink: MaterialClass
#Outputs - link: MESObjectLink to the Material Definition
#Outputs - RetVal: String status
filter = system.mes.object.filter.createFilter()
filter.setMESObjectTypeName('MaterialDef')
filter.setMESObjectNamePattern(name)
list = system.mes.searchMESObjects(filter)
if list.size() == 0:
obj = system.mes.createMESObject('MaterialDef')
obj.setPropertyValue('Name', name)
retVal = "Created Material Definition " + name + "."
else:
retVal = "Material Definition " + name + " already exists."
obj = list.get(0).getMESObject()
objLink = system.mes.object.link.create(obj)
if (objClassLink is not None):
clsName = objClassLink.getName()
parentList = obj.getParentCollection().getList()
if parentList.findByName(clsName) is None:
obj.addParent(objClassLink)
retVal = retVal + " Adding to Material Class " + clsName
else:
retVal = retVal + " It is already a child of the Material Class"
print retVal
system.mes.saveMESObject(obj)
return objLink, retVal
Output run 1 with only the most important part of the error
Creating Material Class Finished Goods. Added to Material Root
Material Definition Nuts 8oz already exists. Adding to Material Class Finished Goods
Java Traceback:
Traceback (most recent call last):
File "<input>", line 10, in <module>
File "<module:Material>", line 73, in CreateDef
at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.newGatewayException(GatewayInterface.java:360)
at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.sendMessage(GatewayInterface.java:334)
at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.sendMessage(GatewayInterface.java:287)
at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.moduleInvokeSafe(GatewayInterface.java:930)
at com.inductiveautomation.ignition.client.gateway_interface.ModuleRPCFactory$DynamicRPCHandler.invoke(ModuleRPCFactory.java:53)
at jdk.proxy2/jdk.proxy2.$Proxy41.saveMESObjects(Unknown Source)
at jdk.internal.reflect.GeneratedMethodAccessor107.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at com.sepasoft.production.rpc.ProductionRPCProxy$1.invoke(ProductionRPCProxy.java:28)
at jdk.proxy2/jdk.proxy2.$Proxy41.saveMESObjects(Unknown Source)
at com.sepasoft.production.script.MESClientScript.saveMESObject(MESClientScript.java:204)
at jdk.internal.reflect.GeneratedMethodAccessor106.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
com.inductiveautomation.ignition.client.gateway_interface.GatewayException: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: Cannot save object (MaterialClass, Finished Goods, 80bffea1-dd26-4363-ab4c-b064942ee9be) without a version when the object already exists
output run 2
Material Class Finished Goods already exists. It is already a child of Material Root
Material Definition Nuts 8oz already exists. Adding to Material Class Finished Goods