How to read Shape properties in Visio

I have the following task. I am writing an add-on for Visio 2010 in C # in Studio 2010. Let's say I have a chart open. And I have a form of any kind in this diagram (let him try to control one form for a start). The question is, how can I read any properties of this form? Which API should I use?

The main algorithm:

  • Scan an open document for shapes
  • If there are any shapes in the document, return an array (or list) of all shapes (null is returned if there are no shapes in the current document)
  • Run an array of shapes and read any property for each element (which would be great to be able to write / modify a property)

(Sample code would be greatly appreciated)

+6
source share
3 answers

I assume that by the properties you refer to the Shape Data, which used to be called user properties in the user interface, and still know that name in the API.

If you are not familiar with ShapeSheet, you must first view the custom property form in the ShapeSheet to see how the properties are defined. See “ What happened to ShapeSheet? ” For how to open a form in Visio 2010.

In the following sample program, you should start. This example assumes that your PC has Visio Primary Interop Assembly installed and that you have included the referee in 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 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); } } } } 
+8
source

I wrote a library that simplifies

To get properties for multiple shapes:

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

The return value stored in the details will be a list of dictionaries. Each dictionary corresponds to the properties of the indicated figures. Property names are keys in the dictionary.

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

 props[0]["Foo"] 

which retrieves an object that contains the Formulas and results for these aspects of the property:

  • The calendar
  • Format
  • Invisible
  • Label
  • LANGID
  • Tell me
  • Sortkey
  • A type
  • Value
  • Make sure
+4
source

For all those who need help with a code later on this issue:

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

Source: https://habr.com/ru/post/889896/


All Articles