Replace Model Element with Open API

In Visual Paradigm the models you created are in repository basis, where a single model element can appear on multiple diagrams, or even multiple occurrence in a single diagram. Each occurrence (we call the view) on diagram is a visual presentation of the model element. In some case you might want to change the underlying model element of a view to another model element without affecting the diagram. In this article we will show you how to change the underlying model element of a view using Open API.

Assume we would like to replace the class model in active diagram to a specific class, we first loop through all elements in the diagram to check is it underlying model element is a class. If it is a class model then we replace it with the class we specified using the setModelElement method.

// Create a class model to be use as the new underlying model for the views
IClass classA = IModelElementFactory.instance().createClass();
classA.setName("ClassA");

// Obtain the active diagram, and retrieve all elements in diagram into array
IDiagramUIModel diagram = ApplicationManager.instance().getDiagramManager().getActiveDiagram();
IDiagramElement[] diagramElements = diagram.toDiagramElementArray();
for (IDiagramElement diagramElement : diagramElements) {
  // Obtain the model element from the view
  IModelElement model = diagramElement.getModelElement();
  // If it is a class model and not the same class we created
  if (model != null && model instanceof IClass) {
    if (!model.equals(classA)) {
      // then we replace it with our class
      diagramElement.setModelElement(classA);
    }
  }
}

Related Know-how

Related Link