Creating objects - SmartPlant Foundation - IM Update 44 - Customization & Programming - Hexagon

SmartPlant Foundation Customization

Language
English
Product
SmartPlant Foundation
Search by Category
Customization & Programming
SmartPlant Foundation / SDx Version
10

In the SPF.Utilities namespace, there is a class named GeneralUtilities that has a number of static methods to help with creating objects:

Public Shared Function InstantiateObject(ByVal pstrClassDef As String, ByVal pstrName As String, ByVal pstrDesc As String, ByVal pstrDomainUID As String) As IObject

Public Shared Function InstantiateObject(ByVal pstrClassDef As String, ByVal pstrName As String, ByVal pstrDesc As String, ByVal pstrDomainUID As String, ByVal pblnInstantiateOnly As Boolean) As IObject

Public Shared Function InstantiateObject(ByVal pstrClassDef As String, ByVal pstrName As String, ByVal pstrDesc As String, ByVal pstrDomainUID As String, ByVal pstrUID As String, ByVal pblnInstantiateOnly As Boolean) As IObject

Public Shared Function InstantiateObject(ByVal pnodObjectXml As System.Xml.XmlNode) As IObject

Public Shared Function InstantiateObject(ByVal pnodObjectXml As System.Xml.XmlNode, ByVal pblnInstantiateOnly As Boolean) As Iobject

The first three overrides all take a ClassDefUID, a Name, a Description, and a DomainUID to create an object. The second override adds an InstantiateOnly parameter that will only create the object in memory if True. The default is to add the object to the database.

The fourth and fifth overrides are low level versions called by the others. There will probably be little reason to use those.

After calling InstantiateObject and setting any properties you must call the FinishCreate method. FinishCreate will ensure that the obid's are set on the object, the domain, config, creation and termination properties are set. This is also the case for new relationships, finish create will ensure that all the internal properties are set correctly.

The following is an example of using this function to create an SPF host object:

Dim lobjHostIObject As IObject = SPF.Server.Utilities.GeneralUtilities.InstantiateObject("SPFHost", "New host", "Default host for xxx", "ADMIN", False)

This returns an IObject. From there, you can use ToInterface to get a more specialized interface and use that to complete the object:

Dim lobjHostISPFHost As ISPFHost = CType(lobjHostIObject.ToInterface("ISPFHost"), ISPFHost)

lobjHostISPFHost.SPFIsSecure = False

lobjHostISPFHost.SPFAdminOwner = "adminuser"

lobjHostISPFHost.GetClassDefinition.FinishCreate(lobjHostISPFHost)

You could actually use InstantiateObject and ToInterface in the same line of code to return a new object as the specialized interface. Here is the entire code for this example:

' Create a host object to match the vault.

Dim lobjHostIObject As IObject = SPF.Server.Utilities.GeneralUtilities.InstantiateObject("SPFHost", "New host", "Default host for xxx", "ADMIN", False)

Dim lobjHostISPFHost As ISPFHost = CType(lobjHostIObject.ToInterface("ISPFHost"), ISPFHost)

lobjHostISPFHost.SPFIsSecure = False

lobjHostISPFHost.SPFAdminOwner = "adminuser"

lobjHostISPFHost.GetClassDefinition.FinishCreate(lobjHostISPFHost)