0
votes

Since I'm a fairly new user when it comes to the Enterprise Architect, I'm stuck at this problem.

I need to write a script to transform each item marked as <<property>> in the class diagram, into an attribute of that particular class.

So far, I've come to this and I am not sure how can I change each property into an attribute.

When I try to add a new attribute like this:

selectedObject.Attributes.AddNew("example", "int");

It crashes with an error that this collection is null or not an object.

Even when I add an attribute to the class manually and then try to access it with the method selectedObject.Attributes.GetAt(0); it still crashes with the error: Script.script 'Attributes' is null or not an object, Line: 39.

Below is my full code:

var currentDiagram as EA.Diagram;
currentDiagram = Repository.GetCurrentDiagram();

if ( currentDiagram != null )
{
    // Get a reference to any selected connector/objects
    var selectedObjects as EA.Collection;
    selectedObjects = currentDiagram.SelectedObjects();

    if ( selectedObjects.Count > 0 )
    {
        // One or more diagram objects are selected
        var selectedObject as EA.Element;

        for(var i = 0; i < selectedObjects.Count; ++i)
        {
            selectedObject = selectedObjects.GetAt(i);

            Session.Output(selectedObject.Attributes.GetAt(0));

            selectedObject.Attributes.AddNew("example", "int");
        }

        Session.Output(selectedObject.Name);
    }
1
Please indicate the language you are using (with the appropriate tag), the exact error message you are getting and a minimal complete example of the codeGeert Bellekens

1 Answers

0
votes
selectedObjects = currentDiagram.SelectedObjects();

returns diagram elements. You need to retrieve the element first.

elemId = selectedObjects.getat(0).elementId:
element = Repository.GetElementByID(elemId);

will return the first element of selected diagram elements.


Previous bug

Actually your code uses

selectedObject.Attributes.AddNew();

which of course won't work since you need (as you mentioned above) to supply two parameters: the name and the (eventually blank) type. Note that the name must be unique.

So it should be

attr = selectedObject.Attributes.AddNew("theName", "someType");
attr.update();

(Sorry, I don't know how to declare a new variable attr in the language you use.)