Very often you need to find out if some Element has a presentation element in a diagram. You also might need to collect all existing presentation elements for Element. There are different approaches for doing it. Choose the one which suites your needs.

Keep in mind that diagrams in MagicDraw uses lazy content loading approach. It means that content (presentation elements) are loaded only on demand. For example when diagram is opened.

 

Use SymbolElementMap

You can take this map using com.nomagic.magicdraw.core.Project.getSymbolElementMap(). Map has API for collecting all symbols in all diagrams for some specific Element.

Presentation elements can be collected only from loaded diagrams. If diagram is not loaded, presentation elements of this diagram are not registered in a map.

Load diagram using com.nomagic.magicdraw.uml.symbols.DiagramPresentationElement.ensureLoaded()


Find element usages in loaded and not loaded diagrams (do not load diagrams)

Use this approach if you are not sure if presentation element exists in a diagram. This approach checks existing of presentation elements without loading a diagram.

DiagramPresentationElement API provides very useful methods for that. This API works both in loaded and not loaded diagrams.

DiagramPresentationElement.getUsedModelElements(boolean) to get all displayed elements in the diagram

Use DiagramPresentationElement.isElementInDiagram(Element) to check if Element has a presentation element in a diagram

If you have not loaded diagram and you need to get presentation element of some symbol, you can do the following


Find presentation elements in diagrams and load them 

Use this approach only if you are sure that presentation element of Element exists in diagram, because diagram will be loaded always.

Use DiagramPresentationElement.findPresentationElement(Element, java.lang.Class) method

 

More information is available in javadoc