Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何阅读Shape';Visio中的属性_C#_Visio_Shape - Fatal编程技术网

C# 如何阅读Shape';Visio中的属性

C# 如何阅读Shape';Visio中的属性,c#,visio,shape,C#,Visio,Shape,我有以下任务。我正在为Visio2010的C#In Studio 2010编写一个外接程序。 假设我打开了一个图表。在这个图中,我有一个任何类型的形状(让我们从一开始尝试管理一个形状)。问题是如何读取此形状的任何属性?我应该使用哪种API 基本算法: 扫描打开的文档中的形状 如果文档中有任何形状,则返回所有形状的数组(或列表)(如果当前文档中没有形状,则返回null) 运行shapes数组并读取每个元素的任何属性(如果有机会写入/修改属性,那就太好了) (非常感谢代码示例)我假设通过属性,您指的

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

基本算法:

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

  • (非常感谢代码示例)

    我假设通过属性,您指的是形状数据,它过去在UI中被称为自定义属性,在API中仍然以该名称命名

    如果您不熟悉ShapeSheet,应该先查看ShapeSheet中具有自定义特性的形状,以了解特性是如何定义的。请参见“”,了解如何在Visio 2010中打开Shapesheet

    下面的示例程序将帮助您开始。本示例假设您的电脑上安装了,并且您的项目中包含了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);
    
    存储在道具中的返回值将是一个字典列表。每个字典对应于指定形状的属性。属性的名称是字典中的键

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

    props[0]["Foo"] 
    
    它检索一个对象,该对象包含属性的以下方面的公式和结果:

    • 历法
    • 格式
    • 无形的
    • 标签
    • 兰吉德
    • 提示
    • 索特基
    • 类型
    • 价值观
    • 核实

    对于所有需要代码帮助的人,请稍后回答此问题:

    ...
    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中的联系人中,因为我可能需要您的帮助和建议?(我已发出邀请)再次感谢。Dandanil,如果您有与软件开发相关的问题,请询问他们关于堆栈溢出的问题。如果你需要私下联系我,请在我的个人资料中使用我的电子邮件地址。-帕切罗·萨文尔。我感谢你愿意帮助我。非常感谢你。