2011-06-06 167 views
6

我有以下任務。我正在爲Studio 2010中的C#編寫Visio 2010加載項。 假設我打開了一個圖。我在這個圖中有任何一種形狀(讓我們試着管理一個形狀的開始)。問題是我如何閱讀這個形狀的任何屬性?我應該使用哪種API?如何閱讀Visio中Shape的屬性

基本算法:

  1. 掃描打開的文檔的形狀
  2. 如果有任何形狀的文件,然後返回所有形狀的數組(或列表)(空是沒有形狀的情況下返回當前文檔中)
  3. 超過形狀陣列運行並讀取每個元素的任何屬性(這將是巨大的,有機會寫入/修改屬性)

(代碼例子將是一個很大ppreciated)

回答

8

我假設通過屬性引用形狀數據,它曾經在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); 
      } 
     } 
    } 
} 
+0

您好帕特 感謝您詳細回答。這給了我一些想法。 我在您的幫助和幾本書的幫助下完成了我的任務。 我計劃在接下來的一兩個月內與Visio密切合作。我是否可以將您添加到LinkedIn的聯繫人中,因爲我可能需要您的幫助和建議? (我已發出邀請) 再次感謝。 Dan – 2011-06-09 04:11:36

+0

Daniil,如果您有與軟件開發相關的問題,請向他們詢問Stack Overflow。如果您需要私下與我聯繫,請在我的個人資料中使用我的電子郵件地址。 - Pat – 2011-06-09 15:15:28

4

我寫a library that makes this a easier

要爲多個形狀檢索性能:

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

存儲在道具返回值將是一個字典列表。每個字典對應於指定形狀的屬性。屬性的名稱是字典中的鍵。

要獲得第一個形狀的「Foo」屬性...

props[0]["Foo"] 

以檢索其中包含式&結果屬性的這些方面的目的:

  • 日曆
  • 格式
  • 隱形
  • 標籤
  • LANGID
  • 提示
  • SORTKEY
  • 類型
  • 價值
  • 驗證
+0

Hello Saveenr。我感謝你願意幫助我。非常感謝你。 – 2011-06-09 04:13:14

+0

偉大的圖書館。謝謝! – 2011-08-04 10:14:10

0

對所有這些,誰還會需要在以後對這個問題的代碼幫助:

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