Adding Elements to Diagram Layers using Open API

tomcat-thumbVisual Paradigm support create diagram with multiple layers. The layer function allow you to assign different shapes into different layers. It help in hiding unnecessary information in your diagram. The layer information can also being access via Open API. In this article we will show you how to work with layers using Open API, including create layers and assign the shapes and connectors in your diagram to different layers.

Obtain the active diagram

First we obtain the diagram currently opening:

IDiagramUIModel diagram = ApplicationManager.instance().getDiagramManager().getActiveDiagram();

Create layers to diagram

Now we can create new layers to the diagram. Let’s create 2 layers, one for the shapes and one for the connectors.

IDiagramLayer shapeLayer = diagram.createDiagramLayer("Shapes");
IDiagramLayer connectorLayer = diagram.createDiagramLayer("Connectors");

Walk through the elements and assign to layers

// retrieve all diagram elements on the diagram
IDiagramElement[] elements = diagram.toDiagramElementArray();
if (elements != null) {
  for (int i = 0; i < elements.length; i++) {
    IDiagramElement element = elements[i];
    if (element instanceof IConnectorUIModel) {
      // add to connector layer
      connectorLayer.addDiagramElement(element);
    } else {
      // add the shape layer
      shapeLayer.addDiagramElement(element);
    }
  }
}

And finally we display a message indicate the process is done.

ApplicationManager.instance().getViewManager().showMessage("Finish create diagram layer");

Sample Plugin

The sample plugin attached in the article demonstrate how to create layers in active diagram and assign the shapes into various layer. After you deploy the plugin into Visual Paradigm, you can then click the plugin button in the application toolbar to trigger it.

Trigger sample plugin

Trigger sample plugin

Download Sample Plugin

You can click this link to download the sample plugin.

Related Know-how

Related Links

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply