【问题标题】:How to read Shape's properties in Visio如何在 Visio 中读取 Shape 的属性
【发布时间】:2011-06-06 19:27:23
【问题描述】:

我有以下任务。我正在 Studio 2010 的 C# 上为 Visio 2010 编写加载项。 假设我打开了一个图表。在这张图中,我有任何一种形状(让我们一开始就尝试管理一个形状)。问题是我怎样才能读取这个形状的任何属性?我应该使用哪个 API?

基本算法:

  1. 扫描打开的文档中的形状
  2. 如果文档中有任何形状,则返回所有形状的数组(或列表)(如果当前文档中没有形状,则返回 null)
  3. 遍历形状数组并读取每个元素的任何属性(如果有机会编写/修改属性会很棒)

(非常感谢代码示例)

【问题讨论】:

    标签: c# visio shape


    【解决方案1】:

    我假设您所指的属性指的是形状数据,过去在 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);
                }
            }
        }
    }
    

    【讨论】:

    • 你好帕特谢谢你的详细回答。这给了我一些想法。在您的帮助和几本书的帮助下,我设法完成了我的任务。我计划在接下来的一两个月内与 Visio 密切合作。我可以将您添加到我在 LinkedIn 中的联系人中,因为我可能需要您的帮助和建议? (我已发送邀请)再次感谢。丹
    • Daniil,如果您有与软件开发相关的问题,请在 Stack Overflow 上提问。如果您需要私下联系我,请在我的个人资料中使用我的电子邮件地址。 - 帕特
    【解决方案2】:

    我写了a library that makes this a easier

    要检索多个形状的属性:

    var shapes = new[] {s1, s2};
    var props = VA.CustomProperties.CustomPropertyHelper.GetCustomProperties(page, shapes);
    

    props 中存储的返回值将是一个字典列表。每个字典对应于指定形状的属性。属性的名称是字典中的键。

    要获取第一个形状的“Foo”属性...

    props[0]["Foo"] 
    

    检索包含属性这些方面的公式和结果的对象:

    • 日历
    • 格式
    • 不可见
    • 标签
    • 语言标识
    • 提示
    • 排序键
    • 类型
    • 价值
    • 验证

    【讨论】:

    • 你好 Saveenr。我很感激你愿意帮助我。非常感谢。
    【解决方案3】:

    致所有在这个问题上需要代码帮助的人:

    ...
    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);
                }
             }
    ....
    

    【讨论】:

      猜你喜欢
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      相关资源
      最近更新 更多