如何阅读Shape';的属性

本文关键字:属性 何阅读 Shape | 更新日期: 2023-09-27 17:58:27

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

基本算法:

  1. 扫描打开的文档中的形状
  2. 如果文档中有任何形状,那么返回所有形状的数组(或列表)(如果当前文档中没有形状,则返回null)
  3. 运行shapes数组并读取每个元素的任何属性(如果有机会写入/修改属性,那就太好了)

(代码示例将不胜感激)

如何阅读Shape';的属性

我假设通过属性,您指的是Shape Data,它过去在UI中被称为Custom properties,但在API中仍然知道这个名称。

如果您不熟悉ShapeSheet,您应该先在ShapeSheed中查看具有自定义特性的形状,以了解特性是如何定义的。请参阅"形状表发生了什么?"了解如何在Visio 2010中打开形状表。

下面的示例程序应该让您开始学习。本例假设您的电脑上安装了Visio主互操作程序集,并且您的项目中包含了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);
            }
        }
    }
}

我写了一个库,使更容易

检索多个形状的属性:

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

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

要获取第一个形状的"Foo"属性。。。

props[0]["Foo"] 

其检索包含公式&属性的这些方面的结果:

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

对于所有稍后需要代码帮助的人:

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