We can use programming language C#, C++ ,Visual Basic and JAVA to automate PowerDesigner. But these languages are very complicated. It may take
a professional programmer days or weeks to get job down. In addition, we have to install Microsoft Visual Studio or Eclipse as development tool, which
is not always available to everyone.
Microsoft Powershell, by contrast, is simple, powerful and available to almost all Windows users. Powershell 2.0 is already installed on Windows 7.
Plus a lot of automation solutions are already available in Powershell community.
I'm going to separate the topic in three parts:
Part one Create a model
Part two Emulate mouse and keyboard to control Powerdesigner GUI
Part three Automate Powerdesigner Portal Server login process
In this article, I'm going to work on part one.
I'm on Windows 7 64bit with PowerDesigner 16.5.4 64bit. I have not tested 32bit PowerDesigner.
First of all, for those who are unfamiliar with Powershell, go to Internet, search for Powershell, you'll get tons of information and tutorials.
Here we only need learn three commands:
new-object this command allows us create a new object
add-type we use this command to load external assembly files.
get-member given an object, list all its member functions
Windows 7 64bit has both 32bit and 64bit Powershell installed. But by default, it runs 64bit version.
Let's start Powershell 64bit.
Click Start button. search for Windows PowerShell . Click the Windows PowerShell tile
OR
Click Start button. Click Run... Type powershell. Click OK.
Powershell window pops up.
At command prompt, type or paste the following code and hit Return
$PowerDesigner = new-object -com powerdesigner.application
Powerdesigner starts.
In command prompt, type or paste the following code and hit Return
$Powerdesigner | Get-Member
The output lists all functions $Powerdesigner COM object owned.
TypeName: System.__ComObject#{57c91ea5-68ef-4877-bb3e-757b12ecae55}
Name MemberType Definition
---- ---------- ----------
BeginTransaction Method void BeginTransaction ()
CancelTransaction Method void CancelTransaction ()
ConvertToUTF16 Method void ConvertToUTF16 (string, string)
ConvertToUTF8 Method void ConvertToUTF8 (string, string)
CreateModel Method BaseObject CreateModel (int, string, OpenModelFlags)
CreateModelFromTemplate Method BaseObject CreateModelFromTemplate (string, OpenModelFlags)
CreateModelWithDialog Method BaseObject CreateModelWithDialog (int, string, OpenModelFlags)
EndTransaction Method void EndTransaction ()
EvaluateNamedPath Method string EvaluateNamedPath (string, bool, bool)
ExecuteCommand Method string ExecuteCommand (string, string, CommandExecutionMode)
FileImport Method BaseObject FileImport (string, string, int, string, OpenModelFlags)
GetModuleForModelFile Method BaseObject GetModuleForModelFile (string)
IsKindOf Method bool IsKindOf (int, int)
MapToNamedPath Method string MapToNamedPath (string)
NewGUID Method string NewGUID ()
newPoint Method APoint newPoint (int, int)
NewPtList Method PtList NewPtList ()
NewRect Method ARect NewRect (int, int, int, int)
OpenModel Method BaseObject OpenModel (string, OpenModelFlags)
Output Method void Output (string)
ProfileSnap Method void ProfileSnap (string, bool, int)
Progress Method BaseObject Progress (string, bool)
Rtf2Ascii Method string Rtf2Ascii (string)
Rtf2Html Method string Rtf2Html (string)
ShowHelp Method void ShowHelp (int)
ActiveDiagram Property BaseObject ActiveDiagram () {get}
ActiveGlossary Property BaseObject ActiveGlossary () {get}
ActiveModel Property BaseObject ActiveModel () {get}
ActivePackage Property BaseObject ActivePackage () {get}
ActiveSelection Property ObjectSet ActiveSelection () {get}
ActiveWorkspace Property BaseObject ActiveWorkspace () {get}
CheckPermissionsMode Property bool CheckPermissionsMode () {get} {set}
EclipseWorkspace Property string EclipseWorkspace () {get}
ExternalClientAdapter Property IDispatch ExternalClientAdapter () {get}
HomeDirectory Property string HomeDirectory () {get}
InteractiveMode Property InteractiveModeValue InteractiveMode () {get} {set}
LicenceParameters Property string LicenceParameters () {get}
LicenceStatus Property string LicenceStatus () {get}
Locked Property bool Locked () {get} {set}
MainWindowHandle Property LONG_PTR MainWindowHandle () {get}
MetaModel Property BaseObject MetaModel () {get}
Models Property ObjectSet Models () {get}
PicturesTransparentColor Property OLE_COLOR PicturesTransparentColor () {get} {set}
RegistryHome Property string RegistryHome () {get}
RepositoryConnection Property BaseObject RepositoryConnection () {get}
ScriptInputArray Property Variant ScriptInputArray () {get}
ScriptInputParameters Property string ScriptInputParameters () {get}
ScriptResult Property string ScriptResult () {get} {set}
ShowMode Property bool ShowMode () {get} {set}
UserDataDictionary Property IStringDataDictionary UserDataDictionary () {get}
UserName Property string UserName () {get}
ValidationMode Property bool ValidationMode () {get} {set}
Version Property string Version () {get}
Viewer Property bool Viewer () {get}
The list is very helpful. It tells user which function is available and for what purpose.
Next, in order to create a PDM we need load two Powerdesigner COM assemblies
Interop.PdCommon.dll
Interop.PdPDM.dll
By default, all PowerDesigner COM assemblies are located at PowerDesigner Home folder.
But you can move them to other place. In my case, I put them in D:\PD folder.
Type or paste the following code and hit Return:
add-type -path "D:\PD\Interop.PdCommon.dll"
add-type -path "D:\PD\Interop.PdPDM.dll"
The assemblies are loaded.
We want to create a PDM, which contains a table with a column.
Type or paste the code below. Don't forget hit Return.
$model=$PowerDesigner.createmodel([PdPDM.PdPDM_Classes]::cls_Model,"|DBMS=ORACLE Version 11g",0)
if ($model -eq $null )
{
[Windows.Forms.MessageBox]::Show("Cannot create PDM")
}
else
{
$model.name ="PDMexample"
$model.code ="PDMexample"
$diagram=$model.PhysicalDiagrams.Item(0)
$tbl=$model.createobject([PdPDM.PdPDM_Classes]::cls_Table,"" , -1, $false)
$tbl.Name="Customer"
$tbl.Code="Customer"
$tbl.Comment="Customer table"
$tbl.Description="The customer table stores customer data"
$coln=$tbl.Columns.createnew([PdPDM.PdPDM_Classes]::cls_Column)
$coln.Name="ID"
$coln.Code="ID"
$coln.DataType="integer"
$coln.Primary=$true
$diagram.AttachObject($tbl)
}
A PDM with a table is created. It uses Oracle 11g as DBMS.
You can copy above code in notepad and save as, say CreatePDM.ps1(ps1 means Powershell script). In PowerShell prompt, go to the same folder where
CreatePDM.ps1 located, type
.\CreatePDM.ps1
The program will do its job.
Attached is CreatePDM.ps1.
Enjoy coding!