Visual Paradigm’s Open API allows you to create all kind of our supported connectors in diagram. They are not necessary to be in simple straight line form. You can create complex connectors with multiple turning points just like those you created in diagram with your mouse. This article will teach you how to create connectors with turning points using Open API.
Defining turning points
The turning points are defined with java.awt.Point. You can define multiple points by specifying the X & Y locations. In our example we will use the position of the shapes on both ends to create couple of turning points in between.
Point lStartPoint = new Point(lClassShape.getX()+lClassShape.getWidth(), lClassShape.getY()+(lClassShape.getHeight()/2)); Point lEndPoint = new Point(lAnotherClassShape.getX(), lAnotherClassShape.getY()+(lAnotherClassShape.getHeight()/2)); Point lMidPoint1 = new Point(lEndPoint.x+(lAnotherClassShape.getWidth()/2), lStartPoint.y); //.....Create other points
After we created all turning points we needed, we can then put it into an array.
Collection lPointCollection = new ArrayList(); lPointCollection.add(lStartPoint);lPointCollection.add(lMidPoint1); //.....add other pointsPoint points = new Point[lPointCollection.size()];lPointCollection.toArray(points);
The connector is created based on the underlying model, also from and to shapes. Before we create the connector shapes, we first need to create the model. We use association as an example.
IAssociation association = IModelElementFactory.instance().createAssociation();
Once the model is ready, we can then create the connector shape. But instead of creating a simple straight line connector, we can create it with turning point into it by passing in an array of Points as parameter. The connector shape can be created by calling
DiagramManager.createConnector(diagram, association, classShape, anotherClassShape, points);
You can also specify the turning point after creating the connector by calling addPoints(java.awt.Point) method.
This sample plugin demonstrate how to create association with turning points between 2 classes using Open API. Once you have deployed the plugin into VP Application, you will find the “Create Association (with points)” from the popup menu of class diagram.
You can use this menu to create classes with a zigzag association between classes.
Download Sample Plugin
You can download the sample plugins at here.