如何阅读Visio中Shape的属性
我有以下任务。我正在为Studio 2010中的C#编写Visio 2010加载项。 假设我打开了一个图。我在这个图中有任何一种形状(让我们试着管理一个形状的开始)。问题是我如何阅读这个形状的任何属性?我应该使用哪种API?如何阅读Visio中Shape的属性
基本算法:
- 扫描打开的文档的形状
- 如果有任何形状的文件,然后返回所有形状的数组(或列表)(空是没有形状的情况下返回当前文档中)
- 超过形状阵列运行并读取每个元素的任何属性(这将是巨大的,有机会写入/修改属性)
(代码例子将是一个很大ppreciated)
我假设通过属性引用形状数据,它曾经在UI中被称为自定义属性,并且仍然在API中被该名称所了解。
如果您不熟悉ShapeSheet,则应首先查看ShapeSheet中具有自定义属性的形状,以查看属性是如何定义的。请参阅“What happened to the ShapeSheet?”以了解如何在Visio 2010中打开Shapesheet。
以下示例程序应该让您开始。这个例子假设你在PC上安装了Visio Primary Interop Assembly,并且你的项目中包含了一个裁判给Microsoft.Office.Interop.Visio。
namespace VisioEventsExample
{
using System;
using Microsoft.Office.Interop.Visio;
class Program
{
public static void Main(string[] args)
{
// Open up one of Visio's sample drawings.
Application app = new Application();
Document doc = app.Documents.Open(
@"C:\Program Files\Microsoft Office\Office14\visio content\1033\ASTMGT_U.VST");
// Get the first page in the sample drawing.
Page page = doc.Pages[1];
// Start with the collection of shapes on the page and
// print the properties we find,
printProperties(page.Shapes);
}
/* This function will travel recursively through a collection of
* shapes and print the custom properties in each shape.
*
* The reason I don't simply look at the shapes in Page.Shapes is
* that when you use the Group command the shapes you group become
* child shapes of the group shape and are no longer one of the
* items in Page.Shapes.
*
* This function will not recursive into shapes which have a Master.
* This means that shapes which were created by dropping from stencils
* will have their properties printed but properties of child shapes
* inside them will be ignored. I do this because such properties are
* not typically shown to the user and are often used to implement
* features of the shapes such as data graphics.
*
* An alternative halting condition for the recursion which may be
* sensible for many drawing types would be to stop when you
* find a shape with custom properties.
*/
public static void printProperties(Shapes shapes)
{
// Look at each shape in the collection.
foreach (Shape shape in shapes)
{
// Use this index to look at each row in the properties
// section.
short iRow = (short) VisRowIndices.visRowFirst;
// While there are stil rows to look at.
while (shape.get_CellsSRCExists(
(short) VisSectionIndices.visSectionProp,
iRow,
(short) VisCellIndices.visCustPropsValue,
(short) 0) != 0)
{
// Get the label and value of the current property.
string label = shape.get_CellsSRC(
(short) VisSectionIndices.visSectionProp,
iRow,
(short) VisCellIndices.visCustPropsLabel
).get_ResultStr(VisUnitCodes.visNoCast);
string value = shape.get_CellsSRC(
(short) VisSectionIndices.visSectionProp,
iRow,
(short) VisCellIndices.visCustPropsValue
).get_ResultStr(VisUnitCodes.visNoCast);
// Print the results.
Console.WriteLine(string.Format(
"Shape={0} Label={1} Value={2}",
shape.Name, label, value));
// Move to the next row in the properties section.
iRow++;
}
// Now look at child shapes in the collection.
if (shape.Master == null && shape.Shapes.Count > 0)
printProperties(shape.Shapes);
}
}
}
}
我写a library that makes this a easier
要为多个形状检索性能:
var shapes = new[] {s1, s2};
var props = VA.CustomProperties.CustomPropertyHelper.GetCustomProperties(page, shapes);
存储在道具返回值将是一个字典列表。每个字典对应于指定形状的属性。属性的名称是字典中的键。
要获得第一个形状的“Foo”属性...
props[0]["Foo"]
以检索其中包含式&结果属性的这些方面的目的:
- 日历
- 格式
- 隐形
- 标签
- LANGID
- 提示
- SORTKEY
- 类型
- 价值
- 验证
Hello Saveenr。我感谢你愿意帮助我。非常感谢你。 – 2011-06-09 04:13:14
伟大的图书馆。谢谢! – 2011-08-04 10:14:10
对所有这些,谁还会需要在以后对这个问题的代码帮助:
...
using Visio = Microsoft.Office.Interop.Visio;
namespace RibbonCustomization
{
[ComVisible(true)]
public class Ribbon1 : Office.IRibbonExtensibility
{
public void ReadShapes(Microsoft.Office.Core.IRibbonControl control)
{
ExportElement exportElement;
ArrayList exportElements = new ArrayList();
Visio.Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
Visio.Pages Pages = currentDocument.Pages;
Visio.Shapes Shapes;
foreach(Visio.Page Page in Pages)
{
Shapes = Page.Shapes;
foreach (Visio.Shape Shape in Shapes)
{
exportElement = new ExportElement();
exportElement.Name = Shape.Master.NameU;
exportElement.ID = Shape.ID;
exportElement.Text = Shape.Text;
...
// and any other properties you'd like
exportElements.Add(exportElement);
}
}
....
您好帕特 感谢您详细回答。这给了我一些想法。 我在您的帮助和几本书的帮助下完成了我的任务。 我计划在接下来的一两个月内与Visio密切合作。我是否可以将您添加到LinkedIn的联系人中,因为我可能需要您的帮助和建议? (我已发出邀请) 再次感谢。 Dan – 2011-06-09 04:11:36
Daniil,如果您有与软件开发相关的问题,请向他们询问Stack Overflow。如果您需要私下与我联系,请在我的个人资料中使用我的电子邮件地址。 - Pat – 2011-06-09 15:15:28