如何使用Aspose.Diagram for .NET将矩形添加到图表中

原创|其它|编辑:郝浩|2011-09-28 11:48:35.000|阅读 792 次

概述:如何使用Aspose.Diagram for .NET将矩形添加到图表中

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  Aspose.Diagram for .NET允许你操作Visio图表,但在某些情况下,您需要添加新的图状到在您的图表中。在这种情况下,你可以通过Aspose.Diagram for .NET的API来创建新的形状,并将这些形状添加到图表中的形状集合中。本文主要介绍如何添加一个新的矩形到你的图中。

添加新的形状需遵循以下步骤:

* 查找页面并添加新形状
* 为新的形状设置一个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

创建并添加新的形状

  在查找到所需的页面,master ID和新ID后,您可以创建一个Shape类的新对象,设置其属性(颜色,位置,对齐方式,文本等),并将新对象添加到图形集合中。

[C#]


//Set shape properties and add it in the shapes' collection

Shape rectangle = new Shape();

rectangle.Master = masterRectangle;

rectangle.MasterShape = masterRectangle.Shapes[0];

rectangle.ID = nextID;

rectangle.XForm.PinX.Value = 5;

rectangle.XForm.PinY.Value = 5;

rectangle.Type = TypeValue.Shape;

rectangle.Text.Value.Add(new Txt("Aspose Diagram"));

rectangle.TextStyle = diagram.StyleSheets[3];

rectangle.Line.LineColor.Value = page0.Shapes[1].Fill.FillForegnd.Value;

rectangle.Line.LineWeight.Value = 0.03041666666666667;

rectangle.Line.Rounding.Value = 0.1;

rectangle.Fill.FillBkgnd.Value = page0.Shapes[0].Fill.FillBkgnd.Value;

rectangle.Fill.FillForegnd.Value = "#ebf8df";

page0.Shapes.Add(rectangle);

[Visual Basic]


'Set shape properties and add it in the shapes' collection

Dim rectangle As New Shape()

rectangle.Master = masterRectangle

rectangle.MasterShape = masterRectangle.Shapes(0)

rectangle.ID = nextID

rectangle.XForm.PinX.Value = 5

rectangle.XForm.PinY.Value = 5

rectangle.Type = TypeValue.Shape

rectangle.Text.Value.Add(New Txt("Aspose Diagram"))

rectangle.TextStyle = diagram.StyleSheets(3)

rectangle.Line.LineColor.Value = page0.Shapes(1).Fill.FillForegnd.Value

rectangle.Line.LineWeight.Value = 0.03041666666666667

rectangle.Line.Rounding.Value = 0.1

rectangle.Fill.FillBkgnd.Value = page0.Shapes(0).Fill.FillBkgnd.Value

rectangle.Fill.FillForegnd.Value = "#ebf8df"

page0.Shapes.Add(rectangle)

完整的源代码

[C#]


private void btnAddShape_Click(object sender, EventArgs e)

{

 

//Load adiagram

Diagram diagram = new Diagram(Application.StartupPath + "\\Drawing1.vdx");

 

//Get first page

if (diagram.Pages.Count == 0)

{

MessageBox.Show("The diagram does not contain pages.");

return;

}

Page page0 = diagram.Pages[0];

//Get the rectangle master

Master masterRectangle = null;

foreach (Master master in diagram.Masters)

if (master.Name == "Rectangle")

{

masterRectangle = master;

break;

}

if (masterRectangle == null)

{

MessageBox.Show("The diagram does not contain rectangle's master.");

return;

}

//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++;

 

//Set shape properties and add it in the shapes' collection

Shape rectangle = new Shape();

rectangle.Master = masterRectangle;

rectangle.MasterShape = masterRectangle.Shapes[0];

rectangle.ID = nextID;

rectangle.XForm.PinX.Value = 5;

rectangle.XForm.PinY.Value = 5;

rectangle.Type = TypeValue.Shape;

rectangle.Text.Value.Add(new Txt("Aspose Diagram"));

rectangle.TextStyle = diagram.StyleSheets[3];

rectangle.Line.LineColor.Value = page0.Shapes[1].Fill.FillForegnd.Value;

rectangle.Line.LineWeight.Value = 0.03041666666666667;

rectangle.Line.Rounding.Value = 0.1;

rectangle.Fill.FillBkgnd.Value = page0.Shapes[0].Fill.FillBkgnd.Value;

rectangle.Fill.FillForegnd.Value = "#ebf8df";

page0.Shapes.Add(rectangle);

 

diagram.Save(Application.StartupPath + "\\output.vdx", SaveFileFormat.VDX);

MessageBox.Show("Shape has been added.");

}

 

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 Sub btnAddShape_Click(ByVal sender As Object, ByVal e As EventArgs)

 

'Load adiagram

Dim diagram As New Diagram(Application.StartupPath & "\Drawing1.vdx")

'Get first page

If diagram.Pages.Count = 0 Then

MessageBox.Show("The diagram does not contain pages.")

Return

End If

Dim page0 As Page = diagram.Pages(0)

'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

If masterRectangle Is Nothing Then

MessageBox.Show("The diagram does not contain rectangle's master.")

Return

End If

'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

'Set shape properties and add it in the shapes' collection

Dim rectangle As New Shape()

rectangle.Master = masterRectangle

rectangle.MasterShape = masterRectangle.Shapes(0)

rectangle.ID = nextID

rectangle.XForm.PinX.Value = 5

rectangle.XForm.PinY.Value = 5

rectangle.Type = TypeValue.Shape

rectangle.Text.Value.Add(New Txt("Aspose Diagram"))

rectangle.TextStyle = diagram.StyleSheets(3)

rectangle.Line.LineColor.Value = page0.Shapes(1).Fill.FillForegnd.Value

rectangle.Line.LineWeight.Value = 0.03041666666666667

rectangle.Line.Rounding.Value = 0.1

rectangle.Fill.FillBkgnd.Value = page0.Shapes(0).Fill.FillBkgnd.Value

rectangle.Fill.FillForegnd.Value = "#ebf8df"

page0.Shapes.Add(rectangle)

 

diagram.Save(Application.StartupPath & "\output.vdx", SaveFileFormat.VDX)

MessageBox.Show("Shape has been added.")

End Sub

 

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

下图展示了添加了新形状后的输出结果:

总结:

  我们可以利用Aspose.Diagram for .NET的API查找到所需页面和主控形状等,并在现有的形状基础上添加一个新的形状。另外,通过Aspose.Diagram for .NET的API还可以对形状进行定制。


(慧都控件网版权所有,转载请注明出处,否则追究法律责任)
标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP