Enterprise Architect: Populating enum programmatically, but they're being alphabetized and not in the order they're entered

Viewed 56

I'm writing a VBScript script to assist in laying out the architecture of some software I'm developing at work. I'm parsing the header files for defined enumerations, structs, function definitions, and some auto-generated commented stuff related to the categorical structure of our modules.

It's all going swimmingly, and I can create an appropriate package (we categorize our modules by layer level, i.e. drivers vs application level, etc), add a Component, then populate the Component with a blank Diagram, Enumerations for state and error, an Interface for methods, and a struct for the data structure.

My problem is coming in when I try to populate the Enumerations with Attributes. When I do it by hand, they're listed in the proper order (where they would resolve to 0, 1, etc.), but when I populate them while parsing the header file, they get entered alphabetically, which is obviously a problem.

I tried modifying the Default member of each Attribute with it's corresponding numerical value, but it still alphabetized them (and added the numerical value, too).

Here's a snippet of my code. Hopefully it's sufficient!

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
    initialValue = initialValue + 1
    tempAttribute.Update()
next
    
newStateEnum.Update()
1 Answers

You can use EA.Attribute.Pos to set a custom ordering.

So something like:

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
    initialValue = initialValue + 1
    tempAttribute.Pos = initialValue 'set the custom POS here
    tempAttribute.Update()
next

Make sure you allow custom attribute ordering in the settings, or this won't have any visible effect either.

Related