使用连接索引连接形状
Contents
[
Hide
]
在形状上添加新的连接并使用连接索引连接形状
Aspose.Diagram for Java API 帮助开发者在形状上添加新的连接点,开发者现在可以使用连接索引连接形状。
使用连接索引连接形状
由公开的 connectShapesViaConnectorIndex 成员页类可用于使用连接索引连接形状。以下代码显示了如何连接形状:
- 初始化一个新绘图。
- 放置四个矩形
- 添加两个额外的连接点,以便在底部边框线上有三个连接点
- 使用动态连接器将每个底部连接的第一个形状连接到顶部的其他三个矩形形状
- 保存图纸
使用连接索引连接形状编程示例
在您的 Java 应用程序中使用以下代码将具有连接索引的形状与 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);