Scripting PowerDesigner

When working with large or multiple models, it can be tedious to perform repetitive tasks, such as modifying objects using global rules, importing or generating new formats, or checking models. Such operations can be automated through scripts.

You can access and modify any PowerDesigner object using Java, VBScript, C#, or many other languages. In this chapter, we focus primarily on writing VBScript to execute in PowerDesigner's Edit/Run Script dialog, but you can also call add-ins from PowerDesigner menus (see Launching Scripts and Add-Ins from Menus) or script the PowerDesigner application via OLE automation (see OLE Automation and Add-Ins).

The following script illustrates the basic syntax of VBScript applied to manipulating PowerDesigner models and objects, including:
' This is a VBScript comment. 
Dim var ' Declaration of a local variable
var = 1 ' Value assignment for simple type
Set var = ActiveModel ' Value assignment for an object. ActiveModel is a PowerDesigner global property
If not var is Nothing Then ' Condition on an object, testing if it is 'null'
   Dim objt ' Declaration of another local variable
   For Each objt In ActiveModel.Children ' Loop on the Children object collection
      DescribeObject objt ' Procedure call with objt as a parameter (without parentheses). The procedure is defined below.      
   Next
Else
   output "There is no active model" ' Output is a PowerDesigner procedure that writes text to the Output window
End If

' This is a procedure - a method that does not return a value
Sub DescribeObject(objt)
   Dim desc ' A variable declaration inside the procedure
   desc = ComputeObjectLabel(objt) ' A function call with objt as parameter (with parentheses). The function is defined below. 
                                   ' We retrieve the value returned by the function in the variable desc
   output desc ' Displays the object description in the output
End Sub

' This is a function - a method that returns a value
Function ComputeObjectLabel(objt)
   Dim label ' Declare a local variable to store the object label
   label = "" ' Initialize the label variable with a default value
   If objt is nothing then
      label = "There is no object"
   ElseIf objt.IsShortcut() then ' IsShortcut is a PowerDesigner function available on objects
      label = objt.Name & " (shortcut)" ' Concatenation of two strings
   Else
      On Error Goto 0 ' Disables script execution abort on error
      label = objt.Name ' Assigns the object's Name property to the local variable
      On Error Resume Next ' Reactivates script execution error
   End If
   ComputeObjectLabel = label ' The value is returned by assigning an implicit variable with same name than the function
End Function
Note: VBScript can also be used to create custom checks, event handlers, transformations, and methods in an extension file (see Extension Files) and embedded in or called from GTL templates (see .execute_vbscript Macro and .vbscript Macro).
The examples in this chapter are intended to introduce the basic concepts and techniques for controlling PowerDesigner by script. For complete documentation of the PowerDesigner metamodel, select Help > Metamodel Objects Help. For full documentation of VBScript, see the Microsoft MSDN site.