Use Connection indexes to connect shapes
Contents
[
Hide
]
Add new Connections on the shape and use Connection indexes to connect shapes
Aspose.Diagram for Java API helps developers to add new connecting points on the shape, and developers can now connect shapes with connection indexes.
Use Connection indexes to connect shapes
The connectShapesViaConnectorIndex member exposed by the Page class can be used to connect shapes using connection indexes. The following code shows how to connect shapes:
- Initialize a new drawing.
- Place four rectangle shapes
- Add two additional connection points, so that there would be three connection points on the bottom border line
- Connect first shape from each bottom connection to other three rectangle shapes from Top with dynamic connectors
- Save drawing
use connection indexes to connect shapes Programming Sample
Use the following code in your Java application to connect shapes with connection indexes with Aspose.Diagram for Java API.
Java
// initialize a new drawing
Diagram diagram = new Diagram();
// get page by index
Page page = diagram.getPages().get(0);
// add masters
String connectorMaster = "Dynamic connector", rectangle = "Rectangle";
int pageNumber = 0;
double width = 2, height = 2, pinX = 4.25, pinY = 9.5;
diagram.addMaster("C:\\temp\\Basic Shapes.vss", rectangle);
diagram.addMaster("C:\\temp\\Basic Shapes.vss", connectorMaster);
// add shapes
long shape1_ID = diagram.addShape(4.5, 7, rectangle, pageNumber);
long shape2_ID = diagram.addShape(2.25, 4.5, rectangle, pageNumber);
long shape3_ID = diagram.addShape(4.5, 4.5, rectangle, pageNumber);
long shape4_ID = diagram.addShape(6.75, 4.5, rectangle, pageNumber);
// get shapes by ID
Shape shape1 = page.getShapes().getShape(shape1_ID);
Shape shape2 = page.getShapes().getShape(shape2_ID);
Shape shape3 = page.getShapes().getShape(shape3_ID);
Shape shape4 = page.getShapes().getShape(shape4_ID);
// add two more connection points
Connection connection1 = new Connection();
connection1.getX().getUfe().setF("Width*0.33");
connection1.getY().getUfe().setF("Height*0");
Connection connection3 = new Connection();
connection3.getX().getUfe().setF("Width*0.66");
connection3.getY().getUfe().setF("Height*0");
connection1.setIX(shape1.getConnections().add(connection1));
connection3.setIX( shape1.getConnections().add(connection3));
// add connector shapes
Shape connector1 = new Shape();
Shape connector2 = new Shape();
Shape connector3 = new Shape();
long connecter1Id = diagram.addShape(connector1, connectorMaster, 0);
long connecter2Id = diagram.addShape(connector2, connectorMaster, 0);
long connecter3Id = diagram.addShape(connector3, connectorMaster, 0);
// connect shapes by index of conneecting points
page.connectShapesViaConnectorIndex(shape1.getID(), 6, shape2.getID(), 3, connecter1Id);
page.connectShapesViaConnectorIndex(shape1.getID(), 1, shape3.getID(), 3, connecter2Id);
// save drawing
diagram.save("C:\\temp\\Drawing1_out.vsdx", SaveFileFormat.VSDX);