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
接下來請看第2頁精彩內容:http://www.linuxidc.com/Linux/2013-10/90981p2.htm