You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Enterprise Architect:程序化填充枚举成员却按字母排序,非输入顺序

解决EA中程序化添加枚举成员顺序混乱的问题

问题根源在于EA的枚举成员排序逻辑:手动添加时EA会按创建顺序自动记录Position属性,而程序化添加时默认按成员名称的字母顺序排序——你设置的Default仅存储枚举的数值,和显示顺序无关。

要强制保持添加顺序,需要给每个枚举成员设置Position属性,这个属性直接决定EA中枚举成员的显示顺序。修改后的代码如下:

set newStateEnum = newComponent.Elements.AddNew(fileName & "_state_t", "Enumeration")
dim tempState, initialValue
dim tempAttribute as EA.Attribute
initialValue = 0

for each tempState in stateParts
    if InStr(tempState, "=") > 0 then
        set tempAttribute = newStateEnum.Attributes.AddNew(Split(tempState, "=")(0), "enum")
    else
        set tempAttribute = newStateEnum.Attributes.AddNew(Split(tempState, ",")(0), "enum")
    end if
    tempAttribute.Default = initialValue
    ' 新增:设置Position属性,强制保持添加顺序
    tempAttribute.Position = initialValue
    initialValue = initialValue + 1
    tempAttribute.Update()
next

newStateEnum.Update()

关键说明

  • Position属性从0开始递增,与成员添加顺序一一对应,EA会按该数值的升序排列枚举成员。
  • 无需修改枚举的全局排序设置,只要每个成员的Position设置正确,就能维持预期顺序。

内容的提问来源于stack exchange,提问作者shortnr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 07:24:22