How do I get the all the attributes from a class diagram?

Hi.

I am trying to create a plugin that exports all the hidden attributes from a diagram to a file.
What I have done so far is to get all the diagram elements, and I guessed that I could get the attributes from this, but I have troubles figuring out how - it is very likely that my method is wrong.

The code I have written so far is inserted below.

So my question is:
How do I get the all the attributes from a class diagram? Is my method so far wrong?

With respect to the visibility, I have noticed in the docs, that IAttribute have a method called isVisible() and I guessed that this does the job.

   @Override
    public void performAction(VPAction vpAction) {
        // Managers
        ApplicationManager am  = ApplicationManager.instance();
        DiagramManager dm = am.getDiagramManager();
        ViewManager vm = am.getViewManager();

        // Get the active diagram (ie. the diagram that is open and is being viewed by the client)
        IDiagramUIModel diagram = dm.getActiveDiagram();

        // Get class diagrams in active diagram
        IDiagramElement[] diagramElements = diagram.toDiagramElementArray(IShapeTypeConstants.SHAPE_TYPE_CLASS);

        // Loops through class diagrams to get invisible attributes
        for (IDiagramElement diagramElement : diagramElements ) {
            // Not sure if the modelElement is of any use - maybe delete
            IModelElement modelElement = diagramElement.getModelElement();

            // TODO: Get invisible attributes
            // ... ?
        }
    }

You can cast the modelElement to IClass, and then use the toAttributeArray() method to obtain all attributes from that class. This will retrieve all attributes no matter it is being showing up, or hide away in diagram. Hope this can help.

Hi Rain.

Thank you very much.

This was excatly what i did. But something rather peculiar happened - It gave me only the vissible attributes. I.e. all attributes that were hidden under presentation options were not included in this list. This is actually good enough for me, but Iā€™m just wondering why I do not get the hidden attributes as well. Do you have an answer for that?

I have inserted the new code below.

// Loops through class diagrams to get invisible attributes
        for (IDiagramElement diagramElement : diagramElements ) {
            // Not sure if the modelElement is of any use - maybe delete
            IModelElement modelElement = diagramElement.getModelElement();

            // IClass is a superinterface of modelElement - thus, modelElement can be casted to classElement
            IClass classElement = (IClass) modelElement;

            // Get attributes
            IAttribute[] attributes = classElement.toAttributeArray();

            // Prints only vissible attributes!!! 
            vm.showMessage("------ Attributes of class: "+modelElement.getName() + " ---------");
            if (attributes.length == 0) {
                vm.showMessage("No att");
            }
            else {
                for (IAttribute att : attributes) {
                    vm.showMessage("Attribute: " + att.getName());
                    if (att.isVisible()) {
                        vm.showMessage("--Visible--");
                    } else {
                        vm.showMessage("--Not visible--");
                    }
                }
            }
            
        }

I just tested your code and do list out all attributes even they are hidden from diagram. BTW the ā€œisVisible()ā€ operation under IAttribute is a non-using property and can cannot be use to determine is attribute visible or not (this information is store in the view but not in model element). Would you please send me your sample project so that we can test on it?

I just realised the same thing. Where exactly can I find information about if an attribute is hidden or not? Under IDiagramElement I found no such methods giving information about attributes nor under IDiagramUIModelā€¦

I also just realised I have been using the wrong term. I am actually not interested in the visibility of the attribute, but if it is hidden or shown in the view. I.e., if it is set to hidden or shown under Presentation Options > Attributes > Customized.

I am not sure if my organization allows me to share my sample project on an open forum.

I really appreciate your help!

Then in this case you should work on the class shape instead of class model. i.e. cast the diagramElement to IClassUIModel. Under IClassUIModel there are two methods can help:

toSorterAttributes(): This will return all attributes of that class, no matter it is hidden or not
getHiddenAttributeModels(): This will return attributes which set to hidden under Customized mode

By work on the two collections you can find out which attributes are showing, which are hide away in your current working diagram.

Hope this can help.

1 Like

Thank you Rain! That did the job :blush: