1.说明
PowerDesigner支持自定义一些命令与操作,
通过编写VBS(Visual Basic Script)脚本,
可以扩展出更多的功能。
下面开发一个自定义的VBS脚本,
实现复制Name到Comment的功能,
即表的名称复制到注释中,
字段的名称复制到注释中,
这样在设计完表之后,
就不用一个一个复制名称到注释中了。
2.运行脚本
菜单:
Tools -> Execute Commands -> Edit/Run Script
快捷键:
Ctrl+Shift+X
打开运行脚本的界面
然后选择需要执行的VBS脚本,
点击Run即可运行:

3.编写脚本
可以使用PowerDesigner内置的编辑器,
也可以使用Notepad++编辑器,
新建名称为CDM_Name2Comment.vbs的文件:
Option Explicit
ValidationMode = True
InteractiveMode = im_Batch
' 当前模型的变量
Dim mdl
' 获取当前活动的模型
Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "没有选择数据模型,请打开一个模型再执行脚本"
ElseIf Not mdl.IsKindOf(PdCDM.cls_Model) Then
MsgBox "当前选择的不是一个概念数据模型(CDM) "
Else
ProcessFolder mdl
End If
' 轮询当前模型下的所有实体表entity,将表和字段的名称Name复制给注释Comment
Private Sub ProcessFolder(folder)
Dim entity
For Each entity In folder.entities
If Not entity.isShortcut Then
'如果表没有注释,则把name复制给comment
If Trim(entity.comment) = "" Then
entity.comment = entity.name
End If
Dim attribute
For Each attribute In entity.attributes
'如果字段没有注释,则把name复制给comment
If Trim(attribute.comment) = "" Then
attribute.comment = attribute.name
End If
Next
End If
Next
'递归处理子包中的表,否则只能取到第一个模型图内的表
Dim f
For Each f In folder.Packages
If Not f.IsShortcut Then
ProcessFolder f
End If
Next
End Sub
4.运行结果
脚本运行后,
可以发现表和字段的Comment和Name一样了:


注意要打开一个概念数据模型(CDM),
该脚本会操作当前模型下的所有表和字段。
5.复制Comment到Name
上面脚本实现复制Name到Comment的功能,
如果要实现复制Comment到Name的功能,
修改脚本以下关键语句:
entity.comment = entity.name
attribute.comment = attribute.name
修改为:
entity.name = entity.comment
attribute.name = attribute.comment
6.支持物理数据模型PDM
上面脚本只支持操作概念数据模型CDM,
如果要支持操作物理数据模型PDM,
修改脚本以下关键语句:
ElseIf Not mdl.IsKindOf(PdCDM.cls_Model) Then
For Each entity In folder.entities
For Each attribute In entity.attributes
修改为:
ElseIf Not mdl.IsKindOf(PdPDM.cls_Model) Then
For Each table In folder.tables
For Each column In table.column
7.预置脚本
在PowerDesigner安装目录下的VB Scripts目录,
预置了很多VBS脚本,
可以直接拿来使用,
也可以作为开发自定义脚本的参考:
AccessRTFDescription.vbs
BPM-Activity.vbs
BPM-CDM.vbs
BuildModelDependencies.vbs
CheckModel.vbs
code2name.vbs
Command Execution.vbs
CommunicationToSequence.vbs
Convert_class_to_interface.vbs
Create OOM.vbs
Create_ProjectCDM.vbs
CreatePersistentSelection.vbs
CreateViews.vbs
Delete Private attributes.vbs
Export_model_To_Excel.vbs
FromDirectoryToModel.vbs
FromDirectoryToRepository.vbs
FromDirectoryToWorkspace.vbs
FromRepositoryToDirectory.vbs
GenerateDatabase.vbs
GenerateDatabaseUsingSelections.vbs
GenerateDBWithSetting.vbs
Interactive Mode.vbs
List Classes.vbs
List Classifiers.vbs
List Metamodel Resources.vbs
List Processes.vbs
List Tables.vbs
ManipulatingObjects.vbs
MeasurePDMComplexity.vbs
MetaModelBrowse.vbs
name2code.vbs
Norm_ClassModel.vbs
ODBCConnect.vbs
prefix.vbs
read_models.vbs
ReadFile.vbs
ReverseEngineerDatabase.vbs
ReverseEngineerSQLScript.vbs
SequenceToCollaboration.vbs
VBSreadme.txt
WorkspaceBrowse.vbs
WriteFile.vbs
8.帮助文档
在运行脚本页面,
可以打开两个帮助文档:

上面的?点击后,
打开元数据模型帮助文档:
Sybase PowerDesigner Metamodel Objects help.

下面的Help点击后,
打开PowerDesigner自带的帮助文档,
并且跳转到VBS脚本相关章节:

9.参考
PowerDesigner中常用的脚本
PowerDesigner为表字段添加Comment注释,让name等于Comment
网友评论