---
title: "PowerDesigner 中name与code、comment的自动转换脚本"
author: "oldbig"
date: "2016-03-18T17:05:35Z"
tags: []
categories: ["数据库"]
slug: "powerdesigner-name-code-comment"
draft: false
lastmod: "2016-03-18T17:05:35Z"
id: 2284622
url: "https://oldbig.iteye.com/blog/2284622"
---

PowerDesign？是不是感觉很古老，用惯了而已。

以下代码参考网络整理而成，经本人优化并亲测通过。

**==&gt;name转code**   **name格式： 中文#code**

.set\_value(\_First, true, new)  
.foreach\_part(%Name%, "'#'")  
.if (%\_First%)  
.delete(%CurrentPart%)  
.set\_value(\_First, false, update)  
.else  
%CurrentPart%  
.endif

.next

在PowerDesigner中使用方法为：  PowerDesigner-&gt;Tools-&gt;Model Options-&gt;NamingConvention-&gt;name 【Enable name/code Conventions】-&gt;name to code

 

**==&gt;name转comment**   **name格式： 中文#code**

将table name的#号之前的文字变为table的comment，将column name的#号之前的文字变为column的comment.

原来的代码是针对一个folder（CDM或PDM）下所有表（还有其他view等）进行操作，改为可以只针对特定表进行操作，如果tabname有值，则就匹配tabname值，否则如果tabname为空，则对所有表进行操作。

在PowerDesigner中使用方法为：  PowerDesigner-&gt;Tools-&gt;Execute Commands-&gt;Edit/Run Scripts

\-----------------------------------------------------------------------------

Option   Explicit

ValidationMode   =   True  
InteractiveMode   =   im\_Batch  
Dim tabname  
Dim   mdl   '   the   current   model

'如果tabname留空，则对所有表进行操作  
tabname = "应答推荐"  
'   get   the   current   active   model  
Set   mdl   =   ActiveModel  
If   (mdl   Is   Nothing)   Then  
      MsgBox   "There   is   no   current   Model "  
ElseIf   Not   mdl.IsKindOf(PdPDM.cls\_Model)   Then  
      MsgBox   "The   current   model   is   not   an   Physical   Data   model. "  
Else  
      ProcessFolder   mdl  
End   If

'   This   routine   copy   name   into   comment   for   each   table,   each   column   and   each   view  
'   of   the   current   folder  
Private   sub   ProcessFolder(folder)  
      Dim   Tab   'running     table  
      for   each   Tab   in   folder.tables  
            dim cando  
            cando = 1  
            if tabname&lt;&gt;"" and Instr(tab.name, tabname) = 0 then  
               cando = 0  
            end if  
            'msgBox tab.name  
            'msgBox cando  
            if   not   tab.isShortcut and cando=1   then

                  msgBox "即将对如下表操作："&tab.name

                  tab.comment   =  split(tab.name ,"#")(0)  

                  Dim   col   '   running   column  
                  for   each   col   in   tab.columns  
                        col.comment=   split(col.name,"#")(0)  
                        msgBox col.comment  
                  next  
            end   if  
      next

      Dim   view   'running   view  
      for   each   view   in   folder.Views  
            if   not   view.isShortcut   then  
                  view.comment   =   split(view.name,"#")(0)  
            end   if  
      next

      '   go   into   the   sub-packages  
      Dim   f   '   running   folder  
      For   Each   f   In   folder.Packages  
            if   not   f.IsShortcut   then  
                  ProcessFolder   f  
            end   if  
      Next

 

end   sub