Creating Objects (Scripting)

You should generally create objects via the collection under the parent object using the CreateNew() method. The CreateObject(kind) method is also available on model objects.

This script creates a class in an OOM, sets some of its properties, and then creates an attribute under the class, in each case creating the objects inside collections:
Dim MyModel
Set MyModel = ActiveModel
Dim MyClass
' Create a class
Set MyClass = MyModel.Classes.CreateNew()
If MyClass is nothing Then
  ' Display an error message box
   msgbox "Fail to create a class", vbOkOnly, "Error" 
Else
  output "The class has been created."  
  ' Set Name, Code, Comment, Stereotype and Final attributes
  MyClass.SetNameAndCode "Customer", "cust"
  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 attribute has been created."
   MyAttr.SetNameAndCode "Name", "custName"
   MyAttr.DataType = "String"
  ' Reset the variable in order to avoid memory leaks
  End If
 End If
You can also create objects using the CreateObject(kind) method. This script creates a class inside an OOM and sets some of its properties:
Dim MyModel
Set MyModel = ActiveModel
Dim MyClass
' Create a class
Set MyClass = MyModel.CreateObject(cls_Class)
MyClass.SetNameAndCode "Another Class", "Class2"
MyClass.Comment = "Created by CreateObject"
When creating a link object, you must define its extremities. This script creates two classes and joins them by an association link:
Dim MyModel
Set MyModel = ActiveModel
Dim MyFirstClass, MySecondClass, MyAssociation
' Create classes
Set MyFirstClass = MyModel.Classes.CreateNew()
MyFirstClass.SetNameAndCode "Class1", "C1"
Set MySecondClass = MyModel.Classes.CreateNew()
  MySecondClass.SetNameAndCode "Class2", "C2"
' Create association
Set MyAssociation = MyModel.Associations.CreateNew()
MyAssociation.Name = "A1"
' Define its extremities
Set MyAssociation.Object1 = MyFirstClass
Set MyAssociation.Object2 = MySecondClass