PowerDesigner ships with a set of script samples, that you can use as a basis to create your own scripts, and which are located in the VB Scripts folder of the PowerDesigner installation directory. These scripts are intended to show you the range of tasks you can perform on PowerDesigner models using VBScript.
The following script browses any model, looping through any packages and listing the objects contained in them:
Option Explicit ' Forces each variable to be declared 'before assignment InteractiveMode = im_Batch ' Supresses the display of dialogs ' get the current active model Dim diag Set diag = ActiveDiagram ' the current diagram If (diag Is Nothing) Then MsgBox "There is no Active Diagram" Else Dim fldr Set Fldr = diag.Parent ListObjects(fldr) End If ' Sub procedure to scan current package and print information on ' objects from current package and call again the same sub procedure ' on all child packages Private Sub ListObjects(fldr) output "Scanning " & fldr.code Dim obj ' running object For Each obj In fldr.children ' Calling sub procedure to print out information on the object DescribeObject obj Next ' go into the sub-packages Dim f ' running folder For Each f In fldr.Packages 'calling sub procedure to scan children package ListObjects f Next End Sub ' Sub procedure to print information on current object in output Private Sub DescribeObject(CurrentObject) if CurrentObject.ClassName ="Association-Class link" then exit sub 'output "Found "+CurrentObject.ClassName output "Found "+CurrentObject.ClassName+" """+CurrentObject.Name+""", Created by "+CurrentObject.Creator+" On "+Cstr(CurrentObject.CreationDate) End Sub
The following script creates a new OOM model, then creates a class with attributes and operations:
ValidationMode = True 'Forces PowerDesigner to validate
' actions and return errors in the event of a forbidden action
InteractiveMode = im_Batch ' Supresses PowerDesigner dialogs
' Main function
' Create an OOM model with a class diagram
Dim Model
Set model = CreateModel(PdOOM.cls_Model, "|Diagram=ClassDiagram")
model.Name = "Customer Management"
model.Code = "CustomerManagement"
' Get the class diagram
Dim diagram
Set diagram = model.ClassDiagrams.Item(0)
' Create classes
CreateClasses model, diagram
' Create classes function
Function CreateClasses(model, diagram)
' Create a class
Dim cls
Set cls = model.CreateObject(PdOOM.cls_Class)
cls.Name = "Customer"
cls.Code = "Customer"
cls.Comment = "Customer class"
cls.Stereotype = "Class"
cls.Description = "The customer class defines the attributes and behaviors of a customer."
' Create attributes
CreateAttributes cls
' Create methods
CreateOperations cls
' Create a symbol for the class
Dim sym
Set sym = diagram.AttachObject(cls)
CreateClasses = True
End Function
' Create attributes function
Function CreateAttributes(cls)
Dim attr
Set attr = cls.CreateObject(PdOOM.cls_Attribute)
attr.Name = "ID"
attr.Code = "ID"
attr.DataType = "int"
attr.Persistent = True
attr.PersistentCode = "ID"
attr.PersistentDataType = "I"
attr.PrimaryIdentifier = True
Set attr = cls.CreateObject(PdOOM.cls_Attribute)
attr.Name = "Name"
attr.Code = "Name"
attr.DataType = "String"
attr.Persistent = True
attr.PersistentCode = "NAME"
attr.PersistentDataType = "A30"
Set attr = cls.CreateObject(PdOOM.cls_Attribute)
attr.Name = "Phone"
attr.Code = "Phone"
attr.DataType = "String"
attr.Persistent = True
attr.PersistentCode = "PHONE"
attr.PersistentDataType = "A20"
Set attr = cls.CreateObject(PdOOM.cls_Attribute)
attr.Name = "Email"
attr.Code = "Email"
attr.DataType = "String"
attr.Persistent = True
attr.PersistentCode = "EMAIL"
attr.PersistentDataType = "A30"
CreateAttributes = True
End Function
' Create operations function
Function CreateOperations(cls)
Dim oper
Set oper = cls.CreateObject(PdOOM.cls_Operation)
oper.Name = "GetName"
oper.Code = "GetName"
oper.ReturnType = "String"
Dim body
body = "{" + vbCrLf
body = body + " return Name;" + vbCrLf
body = body + "}"
oper.Body = body
Set oper = cls.CreateObject(PdOOM.cls_Operation)
oper.Name = "SetName"
oper.Code = "SetName"
oper.ReturnType = "void"
Dim param
Set param = oper.CreateObject(PdOOM.cls_Parameter)
param.Name = "newName"
param.Code = "newName"
param.DataType = "String"
body = "{" + vbCrLf
body = body + " Name = newName;" + vbCrLf
body = body + "}"
oper.Body = body
CreateOperations = True
End Function