一、通过Power Designer 自带命令窗口执行VB脚本时的乱码问题:
Power Designer 支持vb脚本语言,在大批量数据建模时很方便,有时候需要读取外部文档资源替换数据模型中的某些标记。例如:数据模型的字段名都是英文,需要给其name赋为中文,可以通过vb脚本进行比对替换。读取外部txt文档时可能会出现乱码。由于未找到读取文档设置编码的方法,我们只能从根源处解决乱码问题,将txt文本转换为ANSI编码格式即可。
TXT文本示例:
EMP||员工表||EmpNo||员工号码||
EMP||员工表||EmpName||员工名||
DEPT||部门表||DEPTNO||部门号码||
DEPT||部门表||DEPTNAME||部门名称||
DEPT||部门表||DEPTNUM||部门员工数量||
VB脚本示例:
'******************************************************************************
'* Purpose: 通过txt文本替换表名、字段名
'* Title:
'* Category:
'* Author: Aikes On 2019-08-21
'* Created: 2019-08-21
'* Use: 打开PDM,运行本脚本(Ctrl+Shift+X)
'* txt 格式要求 按照||划分
'* Version: 2.0
'*
'* Comment: Modified By Aikes On 2019-08-21
'*
'******************************************************************************
Option Explicit
ValidationMode = True
InteractiveMode = im_Batch
' 读取文本文件
Dim NoteApp
set NoteApp =createobject("scripting.filesystemobject")
Dim NoteObj
set NoteObj=NoteApp.opentextfile("E:/1.txt",1,true)
' the current model
Dim mdl
' 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
Private sub ProcessFolder(folder)
On Error Resume Next
Dim Tab 'running table
Dim i
Dim flag
Dim TCode
do while NoteObj.atendofstream<>true
flag = false
' 读取文本文件一行的数据
TCode = NoteObj.readline
if TCode = "" then
' 如果读取的信息为空,则调出循环
Exit do
else
Dim tArr
Dim tLen
tArr = Split(TCode,"||")
tLen = ubound(tArr)
for each Tab in folder.tables
if not tab.isShortcut then
' 找到table对象
If LCase(Trim(tab.code)) = LCase(Trim(tArr(0))) Then
tab.code = tArr(0)
tab.name = tArr(1)
' 循环处理table下面的column
Dim col ' running column
for each col in tab.columns
If LCase(Trim(col.code)) = LCase(Trim(tArr(2))) Then
col.code = tArr(2)
col.name = tArr(3)
exit for
end if
next
Exit for
end if
end if
next
end if
loop
NoteObj.close
end sub