Get all from and to relationships - Open API

Hi Team

From open API code

Q. How do I get all the from and to relationships of a view?

Q. How do I get all the diagrams that a view appears in?

Q. How do I get the model or the diagram where the master view for a view was created?

Q. If i replace the model for a view ( see code below ), does it impact in any other diagrams where views are created? I am trying to replace the duplicate views and do not want to use merge here.

diagramElement.setModelElement( someOthermodel )

Q. Is there a way to replace 2 duplicate views in 1 diagram with just 1 combined view retaining all relationships from both into 1 single view in the diagram?

Thanks a lot for your help

Let me try to reply the first 3 questions first:

Q. How do I get all the from and to relationships of a view?

IDiagramElement view = ...
IConnectorUIModel[] fromConnectors = view.toFromConnectorArray(); // the connectors connected from this view
IConnectorUIModel[] toConnectors = view.toToConnectorArray(); // the connectors connected from this view

For example:
FromToConnectors

Q. How do I get all the diagrams that a view appears in?
Background:

  • 1 view must be appeared in 1 diagram.
  • But 1 model element may have multiple views.
  • Different views can be appeared in different diagrams.
IDiagramElement view = ...;
IModelElement modelElement = view.getModelElement(); // this view presents which model element
IDiagramElement[] views = modelElement.getDiagramElements(); // this model element is presented by which view(s).
for (IDiagramElement v : views) {
	IDiagramUIModel diagram = v.getDiagramUIModel(); // that view is shown on which diagram
	
}

Q. How do I get the model or the diagram where the master view for a view was created?

IDiagramElement view = ...;
boolean isMasterView = view.isMasterView();

IDiagramElement masterView = view.getModelElement().getMasterView();
IDiagramUIModel diagram = masterView.getDiagramUIModel();

About the last 2 questions:
First, let me try to explain the structure between ModelElement <> View <> Diagram

Q. If i replace the model for a view ( see code below ), does it impact in any other diagrams where views are created? I am trying to replace the duplicate views and do not want to use merge here.
It won’t impact other views. But you need to make sure, the view connected to correct connectors as the model.

Q. Is there a way to replace 2 duplicate views in 1 diagram with just 1 combined view retaining all relationships from both into 1 single view in the diagram?
You need to move the relationships+connectors from modelB+viewB to modelA+viewA

IModelElement modelA = ...;
IModelElement modelB = ...;
for (ISimpleRelationship relationship : modelB.toFromRelationshipArray()) {
	relationship.setFrom(modelA);
}
for (IRelationshipEnd relationshipEnd : modelB.toFromRelationshipEndArray()) {
	relationshipEnd.setModelElement(modelA);
}
for (ISimpleRelationship relationship : modelB.toToRelationshipArray()) {
	relationship.setTo(modelA);
}
for (IRelationshipEnd relationshipEnd : modelB.toToRelationshipEndArray()) {
	relationshipEnd.setModelElement(modelA);
}

IShapeUIModel viewA = ...;
IShapeUIModel viewB = ...;
for (IConnectorUIModel connector : viewB.toFromConnectorArray()) {
	connector.setFromShape(viewA);
}
for (IConnectorUIModel connector : viewB.toToConnectorArray()) {
	connector.setToShape(viewA);
}

Sample:

1 Like