2020/03/12
代码中获取Codelist长短描述的方法
S3D中Codelist类型的属性无法直接获取到他们的长短描述,我们可以将这个过程封装成标准的提取方法,方便使用。
获取短描述,我们是要先获取到Codelist属性,然后要知道这个Codelist在后台数据库中的表名称和表空间类型(这两项可以在 Metadata Browser 中查询),一起传递到方法内,即可取到短描述。
Public Function GetCodelistShortDesc(ByVal oPropCodeList As PropertyValueCodelist, ByVal sCodeListName As String, ByVal sCodeListSpace As String) As String
Dim oMetaMgr As MetadataManager = MiddleServiceProvider.SiteMgr.ActiveSite.ActivePlant.PlantModel.MetadataMgr
Dim oCodelistInfo As CodelistInformation
Dim oCodelistItem As CodelistItem
GetCodelistShortDesc = ""
Try
oCodelistInfo = oMetaMgr.GetCodelistInfo(sCodeListName, sCodeListSpace)
oCodelistItem = oCodelistInfo.GetCodelistItem(oPropCodeList.PropValue)
GetCodelistShortDesc = oCodelistItem.Name
Catch ex As Exception
MsgBox("GetCodelistShortDesc函数异常: " & ex.Message)
End Try
End Function
获取长描述比较简单,我们就代替原有的GetProperty方法,直接传递对象、接口,属性进来,就能获取到长描述了。当然,获取短描述其实也可以参考类似方法,但要传递的参数就多了,看你们自己喜欢。
Public Function GetCodelistLongDesc(ByVal oBO As BusinessObject, ByVal sInterfaceName As String, ByVal sAttributeName As String) As String
Dim sDesc As String = ""
Dim oPropCodelist As PropertyValueCodelist
Try
oPropCodelist = oBO.GetPropertyValue(sInterfaceName, sAttributeName)
sDesc = oPropCodelist.PropertyInfo.CodeListInfo.GetCodelistItem(oPropCodelist.PropValue).DisplayName
Catch ex As Exception
End Try
Return sDesc
End Function
后记:某些直接通过封装好的方法获取的Codelist属性可以直接后缀加.ToString,直接得到它的短描述了。但是并不是普适的方法,上述获取方法是比较严谨的途径。