将 visio vba 方法转换为 c#

convert visio vba method into c#

您好,我想将此 vba 方法转换为 C#。我正在尝试获取选择的 ID 并将它们打印出来。特别是我在将 GetIDs() 方法(visio vba 中的内置方法)转换为 C# 时遇到了问题。

Public Sub getCapabilityRectIDs()

    Dim vsoSelection1 As Visio.Selection
    Dim selectionIDs() As Long


    Set vsoSelection1 = Application.ActiveWindow.Page.CreateSelection(visSelTypeByLayer, visSelModeSkipSuper, "Capability")
    Application.ActiveWindow.Selection = vsoSelection1
    Call vsoSelection1.GetIDs(selectionIDs)
    For i = 0 To UBound(selectionIDs)
        Debug.Print selectionIDs(i)
    Next i
End Sub

这是我目前在 C# 中的内容

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Visio = Microsoft.Office.Interop.Visio;


class Program
    {
        static void Main(string[] args)
        {
            //create the object that will do the drawing
             VisioDrawer Drawer = new VisioDrawer();
             Drawer.selectShpLayer("Capability");

        }
    }

    class VisioDrawer
    {
        public Visio.Application VisApp;
        public static Visio.Page acPage;
        public Visio.Selection LayerSelection;
        public VisioDrawer()
        {   

            //create the application
            VisApp = new Visio.Application();
            VisApp.Documents.Open(@"............. - abc.vsdm");
            ActiveDoc = VisApp.ActiveDocument;
            acPage = VisApp.ActivePage;
        }



         public void selectShpLayer (string layerName){
            Int i = 0;
            long[] lngRowIDs;
            //this selects the shapes of the selected layer
            LayerSelection = acPage.CreateSelection(Microsoft.Office.Interop.Visio.VisSelectionTypes.visSelTypeByLayer, Microsoft.Office.Interop.Visio.VisSelectMode.visSelModeOnlySuper,layerName);
            LayerSelection.GetIDs(lngRowIDs);
            for (i = 0; i < lngRowIDs.Length; i++)
            {
                Debug.Write(lngRowIDs[i]);
            }
        }
    }

提前致谢!

我创建了一个 visio 文档并绘制了 3 个简单的形状。然后我创建了一个名为 "cool" 的层,并向其中添加了这 3 个形状。

我对形状选择的了解不够,您有 visSelModeOnlySuper 这可能适合您,但不适用于我创建的案例。默认值为 visSelModeSkipSuper(对我有用)

这是描述如何使用 createSelection 的 API 页面: https://msdn.microsoft.com/en-us/library/office/ff765565.aspx

这是我的示例代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Visio = Microsoft.Office.Interop.Visio;


class Program
{
    static void Main(string[] args)
    {
        //create the object that will do the drawing
        VisioDrawer Drawer = new VisioDrawer();
        Drawer.selectShpLayer("cool");

    }
}

class VisioDrawer
{
    public Visio.Application VisApp;
    public static Visio.Page acPage;
    public Visio.Selection LayerSelection;
    public VisioDrawer()
    {

        //create the application
        VisApp = new Visio.Application();
        VisApp.Documents.Open(@"c:\temp\trial.vsdm");
        acPage = VisApp.ActivePage;
    }



    public void selectShpLayer(string layerName)
    {
        int i = 0;
        int[] lngRowIDs;
        Array lngRowIDArray;
        //this selects the shapes of the selected layer
        LayerSelection = acPage.CreateSelection(Microsoft.Office.Interop.Visio.VisSelectionTypes.visSelTypeByLayer, Microsoft.Office.Interop.Visio.VisSelectMode.visSelModeSkipSuper, layerName);
        LayerSelection.GetIDs(out lngRowIDArray);
        lngRowIDs = (int[])lngRowIDArray;
        for (i = 0; i < lngRowIDs.Length; i++)
        {
            Debug.Write("Object ID: " + lngRowIDs[i].ToString() + "\n");
        }
    }
}

它产生这个输出:

Object ID: 1
Object ID: 2
Object ID: 3

如果将调试行更改为:

Debug.Write("Object ID: " + lngRowIDs[i].ToString() + " -- " + acPage.Shapes.ItemFromID[i].Name + "\n");

输出变得更有用:

Object ID: 1 -- ThePage
Object ID: 2 -- Circle
Object ID: 3 -- Rectangle