没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
原创|其它|编辑:郝浩|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
文章转载自:慧都控件网面对“数字中国”建设和中国制造2025战略实施的机遇期,中车信息公司紧跟时代的步伐,以“集约化、专业化、标准化、精益化、一体化、平台化”为工作目标,大力推进信息服务、工业软件等核心产品及业务的发展。在慧都3D解决方案的实施下,清软英泰建成了多模型来源的综合轻量化显示平台、实现文件不失真的百倍压缩比、针对模型中的大模型文件,在展示平台上进行流畅展示,提升工作效率,优化了使用体验。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号