如何通过VBS为PowerDesigner数据项添加扩展属性
解决PowerDesigner VBS给数据项添加扩展属性的问题
首先,咱们先理清PowerDesigner对象模型中扩展属性的处理逻辑:所有支持扩展属性的对象(比如DataItem)都有一个ExtendedAttributes集合,要给对象添加扩展属性,需要先确保对应的扩展属性定义在模型中存在,然后通过这个集合给属性赋值。
另外,我注意到你当前代码里有个小bug:CreateAttributes函数里重新创建了一个DataItem对象(t=MyModel.dataitems.CreateNew()),这会覆盖你之前循环中创建的t,导致之前的DataItem没有被正确添加属性,这个得先修正。
步骤1:修正CreateAttributes函数
把函数里多余的Set t=MyModel.dataitems.CreateNew()去掉,直接对传入的t对象操作:
Function CreateAttributes(t) Dim attr ' 给传入的DataItem添加ID属性 Set attr = t.CreateObject(Pdcdm.cls_Attribute) attr.Name = "ID" attr.Code = "ID" attr.DataType = "int" attr.Persistent = True attr.PersistentCode = "ID" attr.PersistentDataType = "I" attr.PrimaryIdentifier = True ' 添加Name属性 Set attr = t.CreateObject(Pdcdm.cls_Attribute) attr.Name = "Name" attr.Code = "Name" attr.DataType = "String" attr.Persistent = True attr.PersistentCode = "NAME" attr.PersistentDataType = "A30" CreateAttributes = True End Function
步骤2:添加扩展属性处理逻辑
接下来,我们需要在创建DataItem后,把Excel中的扩展字段(比如Stewards、LongDescription等)赋值给DataItem的扩展属性。这里分两种情况:
情况A:模型中已经定义好所需的扩展属性
如果你的PowerDesigner模型已经提前创建了Stewards、LongDescription等扩展属性定义,直接在循环中添加以下代码:
Do Until objExcel.Cells(intRow,1).Value = "" ' ... 之前读取Excel字段的代码保持不变 ... Set t=MyModel.dataitems.CreateNew() t.Name = Name ' 这里直接用之前读取的Name变量,不用重复读Excel CreateAttributes t ' ------------------- 新增:添加扩展属性 ------------------- ' 给Stewards赋值 Set extAttr = t.ExtendedAttributes.ItemByName("Stewards") If Not extAttr Is Nothing Then extAttr.Value = Stewards End If ' 给LongDescription赋值 Set extAttr = t.ExtendedAttributes.ItemByName("LongDescription") If Not extAttr Is Nothing Then extAttr.Value = LongDescription End If ' 其他扩展属性同理,比如ParentCategory、DomainSubCategory等 Set extAttr = t.ExtendedAttributes.ItemByName("ParentCategory") If Not extAttr Is Nothing Then extAttr.Value = ParentCategory End If Set extAttr = t.ExtendedAttributes.ItemByName("DomainSubCategory") If Not extAttr Is Nothing Then extAttr.Value = DomainSubCategory End If ' ... 继续添加其他需要的扩展属性 ... ' -------------------------------------------------------- Logger LogFile, Year(Date) & "-" & Month(Date) & "-" & Day(Date) & " " & Time & " - Object: " & name intRow = intRow + 1 loop
情况B:需要动态创建扩展属性定义
如果你的模型还没有这些扩展属性定义,可以在脚本开头先批量创建它们,或者在赋值时判断不存在则创建:
' 先定义一个辅助函数,获取或创建扩展属性定义 Function GetOrCreateExtAttrDef(model, attrName) Dim extDef Set extDef = model.ExtendedAttributeDefinitions.ItemByName(attrName) If extDef Is Nothing Then Set extDef = model.ExtendedAttributeDefinitions.CreateNew() extDef.Name = attrName extDef.Code = attrName ' 通常Code和Name一致,可根据需求调整 extDef.Description = "Auto-created extended attribute: " & attrName End If Set GetOrCreateExtAttrDef = extDef End Function
然后在循环中使用这个函数:
' 给Stewards赋值 Dim extDef Set extDef = GetOrCreateExtAttrDef(MyModel, "Stewards") Set extAttr = t.ExtendedAttributes.Add(extDef) extAttr.Value = Stewards ' 给LongDescription赋值 Set extDef = GetOrCreateExtAttrDef(MyModel, "LongDescription") Set extAttr = t.ExtendedAttributes.Add(extDef) extAttr.Value = LongDescription ' 其他扩展属性同理...
额外注意事项
- 确保Excel中的字段名和扩展属性的Name/Code完全匹配(PD的
ItemByName是区分大小写的,注意大小写一致) - 扩展属性的值如果是多行文本,直接赋值即可,PD会自动处理
- 如果需要批量处理所有扩展属性,可以把字段名和对应的变量放到数组里,用循环来处理,避免重复代码
比如,用数组批量处理:
' 定义扩展属性名和对应的变量数组 Dim extAttrs(15,1) extAttrs(0,0) = "Stewards" : extAttrs(0,1) = Stewards extAttrs(1,0) = "LongDescription" : extAttrs(1,1) = LongDescription extAttrs(2,0) = "ParentCategory" : extAttrs(2,1) = ParentCategory extAttrs(3,0) = "DomainSubCategory" : extAttrs(3,1) = DomainSubCategory ' ... 继续添加其他属性 ... ' 循环批量赋值 Dim i For i = 0 To UBound(extAttrs, 1) Dim attrName, attrValue attrName = extAttrs(i,0) attrValue = extAttrs(i,1) If attrValue <> "" Then ' 只赋值非空的属性 Set extDef = GetOrCreateExtAttrDef(MyModel, attrName) Set extAttr = t.ExtendedAttributes.Add(extDef) extAttr.Value = attrValue End If Next
这样代码会更简洁,也更容易维护。
内容的提问来源于stack exchange,提问作者Jext
相关产品推荐
相关产品推荐

