Aspose.Diagram for .NET允许你操作Visio图表,但在某些情况下,您需要添加新的图状到在您的图表中。在这种情况下,你可以通过Aspose.Diagram for .NET的API来创建新的形状,并将这些形状添加到图表中的形状集合中。本文主要介绍如何添加一个新的矩形到你的图中。
Aspose.Diagram 的详细介绍:请点这里
Aspose.Diagram 的下载地址:请点这里
添加新的形状需遵循以下步骤:
* 查找页面并添加新形状
* 为新的形状设置一个ID
* 设置主控形状
* 设置形状的属性
查找页面
每个图表包含了网页的集合。你可以循环访问图形页面的集合,并将所需要的页面储存到Page类对象。
[C#]
//Load a diagram
Diagram diagram = new Diagram(Server.MapPath("Drawing1.vdx"));
//Get first page
Page page0 = diagram.Pages[0];
[Visual Basic]
'Load a diagram
Dim diagram As New Diagram(Server.MapPath("Drawing1.vdx"))
'Get first page
Dim page0 As Page = diagram.Pages(0)
查找主控形状
每个图表包含了主控形状的集合。你可以循环访问主控形状的集合,并将所需要的主控形状储存到主类的对象。
[C#]
//Get the rectangle master
Master masterRectangle = null;
foreach (Master master in diagram.Masters)
if (master.Name == "Rectangle")
{
masterRectangle = master;
break;
}
[Visual Basic]
'Get the rectangle master
Dim masterRectangle As Master = Nothing
For Each master As Master In diagram.Masters
If master.Name = "Rectangle" Then
masterRectangle = master
Exit For
End If
Next master
计算下一个ID
你需要一个新的ID并将新图形添加到图表中。你可以循环访问每个页面的形状集合,以查找到最大的ID。在查找到在最大的ID后,你可以轻松地计算出下一个ID。
[C#]
//Get next shape ID
long nextID = -1L;
foreach (Page page in diagram.Pages)
foreach (Shape shape in page.Shapes)
{
long temp = GetMaxShapeID(shape);
if (temp > nextID)
nextID = temp;
}
nextID++;
[Visual Basic]
'Get next shape ID
Dim nextID As Long = -1L
For Each page As Page In diagram.Pages
For Each shape As Shape In page.Shapes
Dim temp As Long = GetMaxShapeID(shape)
If temp > nextID Then
nextID = temp
End If
Next shape
Next page
nextID += 1
GetMaxShapeID 方法的代码:
[C#]
private long GetMaxShapeID(Shape shape)
{
long max = shape.ID;
foreach (Shape child in shape.Shapes)
{
long temp = GetMaxShapeID(child);
if (temp > max)
max = temp;
}
return max;
}
[Visual Basic]
Private Function GetMaxShapeID(ByVal shape As Shape) As Long
Dim max As Long = shape.ID
For Each child As Shape In shape.Shapes
Dim temp As Long = GetMaxShapeID(child)
If temp > max Then
max = temp
End If
Next child
Return max
End Function