Layout diagram using Open API
Visual Paradigm bundled with auto-layout engine which help user to perform auto layout to their diagrams. The auto-layout can triggered via user interface as well as via Open API. In this article we will teach you how to layout your diagram via Open API.
Obtain current opening diagram
In this sample we will change the layout of the active opening diagram. To do this first we have to retrieve the reference of the active diagram.
// Obtain DiagramManager from ApplicationManager DiagramManager diagramManager = ApplicationManager.instance().getDiagramManager(); IDiagramUIModel diagram = diagramManager.getActiveDiagram();
Create layout options
Next we can create different layout options for layout our diagram. In the sample we use Orthogonal Layout, Hierarchical Layout and Directed Tree Layout to layout our diagram. Different properties can apply to different layout options to control how the diagram being layout.
// Initialize different LayoutOptions LayoutOption$Orthogonal orthogonalLayoutOption = diagramManager.createOrthogonalLayoutOption(); orthogonalLayoutOption.setLayoutGridSize(100); LayoutOption$Hierarchical hierarchicalLayoutOption = diagramManager.createHierarchicalLayoutOption(); hierarchicalLayoutOption.setMinimumConnectorDistance(10); hierarchicalLayoutOption.setMinimumLayerDistance(20); hierarchicalLayoutOption.setMinimumShapeDistance(30); LayoutOption$DirectedTree directedTreeLayoutOption = diagramManager.createDirectedTreeLayoutOption(); directedTreeLayoutOption.setMinimumLaterDistance(50); directedTreeLayoutOption.setMinimumShapeDistance(50); directedTreeLayoutOption.setOrientation(LayoutOption$Orientation.TopToBottom);
Apply layout to diagram
Once the layout options are ready we can then apply layout to diagram. We generate a random number to control which layout will apply to diagram. The layout can be trigger using DiagramManager.openAndLayoutDiagram(IDiagramUIModel, LayoutOptions).
// Generate random number to perform different layout int min = 0; int max = 2; int range = max - min + 1; int rand = (int) (Math.random() * range) + min; System.out.println("rand: " + rand); switch (rand) { //passing different option to perform layout case 0: diagramManager.openAndLayoutDiagram(diagram, orthogonalLayoutOption); break; case 1: diagramManager.openAndLayoutDiagram(diagram, hierarchicalLayoutOption); break; case 2: diagramManager.openAndLayoutDiagram(diagram, directedTreeLayoutOption); break; }
Sample plugin
The sample plugin attached demonstrate how to change the layout of the active diagram. After you deploy the plugin into Visual Paradigm you can click the plugin button in application toolbar to change the diagram layout.
Download sample plugin
You can click this link to download the sample plugin.
Related Know-how |
Related Link |
Leave a Reply
Want to join the discussion?Feel free to contribute!