Chapter 3 Getting Started with Objects Manipulation Using Scripts


Creating an Object Using Scripts

It is recommended to create an object directly from the collection to which it belongs in order to directly obtain a valid state for the object. When you do so, you only create the object but not its graphical symbol.

You can also use the following method: CreateObject(ByVal Kind As Long, ByVal ParentCol As String = "", ByVal Pos As Long = -1, ByVal Init As Boolean = -1) As BaseObject

Creating an object in a model

If not ExistingModel is Nothing Then
' Call the CreateNew() method on the collection that owns the object
   Dim MyClass
   Set MyClass = ExistingModel.Classes.CreateNew()
   If MyClass is nothing Then
      msgbox "Fail to create a class", vbOkOnly, "Error"    ' Display an error message box
   Else
      output "The class objects has been successfully created"    ' Display a message in the application output window
     ' Initialize its name and code using a specific method
      ' that ensures naming conventions (Uppercase or lowercase constraints,
      ' invalid characters...) are respected and that the name and code 
      ' are unique inside the model
      MyClass.SetNameAndCode "Customer", "cust"
      ' Initialize other properties directly
      MyClass.Comment = "Created by script"
      MyClass.Stereotype = "MyStereotype"
      MyClass.Final = true
      ' Create an attribute inside the class
      Dim MyAttr
      Set MyAttr = MyClass.Attributes.CreateNew()
      If not MyAttr is nothing Then
         output "The class attribute has been successfully created"
         MyAttr.SetNameAndCode "Name", "custName"
         MyAttr.DataType = "String"
         Set MyAttr = Nothing
      End If
      ' Reset the variable in order to avoid memory leaks
      Set MyClass = Nothing
   End If
End If

Creating an object in a package

If not ExistingModel is Nothing Then
   ' Create a package first
   Dim MyPckg
   Set MyPckg = ExistingModel.Packages.CreateNew()
   If not MyPckg is Nothing then
      output "The package has been successfully created"
      MyPckg.SetNameAndCode "All interfaces", "intf"
      ' Create an interface object inside the package
      Dim MyIntf
      Set MyIntf = MyPckg.Interfaces.CreateNew()
      If not MyIntf is Nothing then
         output "The interface object has been successfully created inside the package"
         MyIntf.SetNameAndCode "Customer Interface", "custIntf"
         Set MyIntf = Nothing
      End If
      Set MyPckg = Nothing
   End If
End If

 


Copyright (C) 2006. Sybase Inc. All rights reserved.