How to retrieve model element in your project using Open API

tomcat-thumbVisual Paradigm’s Open API allow user to access the model data in your project in programmatic way by creating plugin. With Open API you can create new diagram or model elements to your project, also retrieve the diagram or model elements form your project and manipulate it. When dealing with model element there are 2 approach we can use to retrieve it from your project. In this article we will teach you how to obtain the model element via Open API.

Method 1 – obtain the model element from model repository

Model elements can be obtained directly from project’s model repository. The model element retrieve directly from project is not limited to a particular scope. They can be model elements in different diagrams of your project. To do this we first have to obtain the reference of your current opening project from ApplicationManager.

IProject project = ApplicationManager.instance().getProjectManager().getProject();

Once you obtained the project you can use the toXXArray method to obtain the model element into an array. There are six toXXArray method available.

project.toModelElementArray() Obtain model elements (not limiting to any model type) on the root level of the project.
project.toModelElementArray(model type) Obtain particular type of model element on the root level of the project
project.toModelElementArray(model types[]) Obtain the selected types of model element on the root level of the project
project.toAllLevelModelElementArray() Obtain all model elements in the project (not limiting the type and level)
project.toAllLevelModelElementArray(model type) Obtain particular type of model element in the project (not limiting the level)
project.toAllLevelModelElementArray(model types[]) Obtain the select types of model element in the project (not limiting the level)

 

e.g. IModelElement[] modelElements = project.toAllLevelModelElementArray();

After you obtained the model elements into an array, you can then walk the array to obtain the model element and to do whatever you want.

for (IModelElement modelElement : modelElements) {
  // do what you want on this model element
}

Method 2- obtain the model element form diagram

Besides obtain model element from project’s model repository, you can also retrieve it from the diagram. To do this we first have to obtain the diagram you look for. Please reference to the article How to retrieve a diagram in your project using Open API about how to obtain the diagram via Open API. Once you obtained the diagram, you can then make use of IDiagramUIModel.toDiagramElementArray to retrieve all diagram element in your diagram into an array.

IDiagramElement[] elements = diagram.toDiagramElementArray();

As long as the array is not null you can then walk through the array to obtain the diagram element you looking for.

if (elements != null) {
  for (IDiagramElement element : elements) {
    // do what you want on this model element
  }
}

Once you obtained the diagram element, you can then use the IDiagramElement.getMetaModelElement() to obtain the underlying model element.

element. getMetaModelElement();

Related Know-how

Related Link

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply