Chapter 7 Scripting PowerDesigner
PowerDesigner ships with a set of script samples, that you can use as a basis to create your own scripts. They are located in the VB Scripts folder of the PowerDesigner installation directory.
These scripts are intended to show you a range of the type of actions you can do over PowerDesigner objects using VBScript and also to help you in the code writing of your own scripts as you can easily copy/paste some code pieces from the sample into your script.
It is always recommended to make a backup copy of the sample file for it to remain intact.
The following example illustrates a script with a loop that browses a model and its sub-packages to display objects information:
' Scan CDM Model and display objects information
' going down each package
Option Explicit
ValidationMode = True
InteractiveMode = im_Batch
' get the current active model
Dim mdl ' the current model
Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "There is no Active Model"
Else
Dim fldr
Set Fldr = ActiveDiagram.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 children package
' of the current package
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 example illustrates a script that creates a new OOM model:
Option Explicit
' Initialization
' Set interactive mode to Batch
InteractiveMode = im_Batch
' 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
The previous script gives the following result in the interface:
| Copyright (C) 2008. Sybase Inc. All rights reserved. |
| |