如何在 Visio 中读取形状的属性

发布于 2024-11-14 03:17:38 字数 303 浏览 1 评论 0原文

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

基本算法:

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

(代码示例将不胜感激)

I have the following task. I'm writing an Add-In for Visio 2010 on C# in Studio 2010.
Let's say that I have a diagram opened. And I have a shape of any kind in this diagram (let's try to manage one shape for the begining). The question is how can I read any properties of this shape? Which API should I use?

Basic algorithm:

  1. Scan opened document for shapes
  2. If there are any shapes in document, then return an array (or a list) of all shapes (null is returned in case of no shapes in current document)
  3. Run over shapes array and read any property for each element (that would be great to have a chance to write/modify property)

(Code example would be greatly appreciated)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹏半生如梦愿梦如真 2024-11-21 03:17:38

我假设您所说的属性指的是形状数据,它曾经在 UI 中称为“自定义属性”,并且在 API 中仍然以该名称为人所知。

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

。以下示例程序应该可以帮助您入门。此示例假设您的电脑上安装了 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);
            }
        }
    }
}

I assume that by properties you are referring to Shape Data, which used to be called Custom Properties in the UI and is still know by that name in the API.

If you are not familiar with the ShapeSheet you should have look at a shape with custom properties in the ShapeSheet first to see how the properties are defined. See "What happened to the ShapeSheet?" to learn how to open the Shapesheet in Visio 2010.

The following example program should get you started. This example assumes that you have the Visio Primary Interop Assembly installed on you PC and that you have included a referee to Microsoft.Office.Interop.Visio in your project.

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);
            }
        }
    }
}
如歌彻婉言 2024-11-21 03:17:38

我编写了一个库,使这变得更容易

检索多个形状的属性:

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

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

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

props[0]["Foo"] 

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

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

I wrote a library that makes this a easier

To retrieve the properties for multiple shapes:

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

The return value stored in props will be a list of dictionaries. Each dictionary corresponds to the properties of the specified shapes. The names of the properties are the keys in the dictionary.

To get the "Foo" property for the first shape...

props[0]["Foo"] 

which retrieves an object which contains the Formulas & Results for these aspects of a property:

  • Calendar
  • Format
  • Invisible
  • Label
  • LangId
  • Prompt
  • SortKey
  • Type
  • Value
  • Verify
情深缘浅 2024-11-21 03:17:38

对于所有稍后在这个问题上需要代码帮助的人:

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

To all those, who will need code help later on this question:

...
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);
            }
         }
....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文