以编程方式将形状连接回自身

时间:2016-05-20 12:53:10

标签: c# .net visio

我正在使用AutoConnect方法以编程方式连接形状,以便从我的程序中的数据库导出“actions”。当形状连接回自身时,AutoConnect方法会发生爆炸。我想知道是否有人有其他方法来实现这一点。

   private void connectExportedActions(DataTable dt, Page page, Document currentStencil)
    {
        List<string> Connectors = new List<string>();
        Shape parallelShape;
        Shape successShape;
        Shape unsuccessShape;
        Shape timeoutShape;
        Master connector;
        Shape timeout;
        string timeoutDisplay;


        foreach (DataRow row in dt.Rows)
        {

            Shape shape = page.Shapes.get_ItemFromID((int)row["ShapeID"]);

            if (row["acdParallelActionDefID"].ToString() != "" && row["acdParallelActionDefID"].ToString() != "NULL")
            {
                Connectors.Add("Parallel Connector");
            }
            if (row["acdPositiveActionDefID"].ToString() != "" && row["acdPositiveActionDefID"].ToString() != "NULL")
            {
                Connectors.Add("Successful Connector"); 
            }
            if (row["acdNegativeActionDefID"].ToString() != "" && row["acdNegativeActionDefID"].ToString() != "NULL")
            {
                Connectors.Add("Unsuccessful Connector");
            }
            if (row["acdTimeOutActionDefID"].ToString() != "" && row["acdTimeOutActionDefID"].ToString() != "NULL")
            {
                Connectors.Add("Timeout Connector");

            }

            foreach (string conn in Connectors)
            {

                foreach (Master mst in currentStencil.Masters)
                {
                    if (mst.Name == conn)
                    {
                        Console.WriteLine(String.Format("Action Name: {0}, ActionDefID: {1}", row["acdName"].ToString(), row["ActionDefID"].ToString()));
                        switch (conn)
                        {
                            case "Parallel Connector":
                                connector = mst; //page.Drop(mst, 0, 0);
                                parallelShape = getShape(dt, row, "ActionDefID", "acdParallelActionDefID", page);
                                **shape.AutoConnect(parallelShape, VisAutoConnectDir.visAutoConnectDirRight, connector);**
                                break;
                            case "Successful Connector":
                                connector = mst; //page.Drop(mst, 0, 0);
                                successShape = getShape(dt, row, "ActionDefID", "acdPositiveActionDefID", page);
                                **shape.AutoConnect(successShape, VisAutoConnectDir.visAutoConnectDirDown, connector);**
                                break;
                            case "Unsuccessful Connector":
                                connector = mst; // page.Drop(mst, 0, 0);
                                unsuccessShape = getShape(dt, row, "ActionDefID", "acdNegativeActionDefID", page);
                                //Console.WriteLine(String.Format("Action Name: {0}, ActionDefID: {1}", row["acdName"].ToString(), row["ActionDefID"].ToString()));
                                **shape.AutoConnect(unsuccessShape, VisAutoConnectDir.visAutoConnectDirLeft, connector);**
                                break;
                            case "Timeout Connector":
                                timeout = page.Drop(mst, 0, 0);
                                timeoutShape = getShape(dt, row, "ActionDefID", "acdTimeOutActionDefID", page);
                                timeoutDisplay = "T = " + row["acdDeadlinePeriod"].ToString() + " days";
                                timeout.Cells["Prop.Display"].FormulaU = "\"" + timeoutDisplay + "\"";
                                **shape.AutoConnect(timeoutShape, VisAutoConnectDir.visAutoConnectDirLeft, timeout);**

                                timeout.Delete();

                                break;
                        }


                    }
                }
            }
            Connectors.Clear();
        }
    }

    private Shape getShape(DataTable dt, DataRow row, string fieldname, string rowcolumn, Page page)
    {
        DataRow[] foundRows;

        foundRows = dt.Select(fieldname + " = " + row[rowcolumn].ToString());

        int ShapeID = (int)foundRows[0]["ShapeID"];

        Shape shape = page.Shapes.get_ItemFromID(ShapeID);

        return shape;
    }

2 个答案:

答案 0 :(得分:0)

我正在研究类似于你正在做的事情。我已经阻止了形状连接到它们自己以避免无限循环,但是如果你需要这样做,Visio SDK代码示例中的ConnectWithDynamicGlueAndConnector方法应该能够做到这一点。

    public void ConnectWithDynamicGlueAndConnector(
        Microsoft.Office.Interop.Visio.Shape shapeFrom, 
        Microsoft.Office.Interop.Visio.Shape shapeTo) {


        if (shapeFrom == null || shapeTo == null) {
            return;
        }


        const string BASIC_FLOWCHART_STENCIL = 
            "Basic Flowchart Shapes (US units).vss";
        const string DYNAMIC_CONNECTOR_MASTER = "Dynamic Connector";
        const string MESSAGE_NOT_SAME_PAGE = 
            "Both the shapes are not on the same page.";

        Microsoft.Office.Interop.Visio.Application visioApplication;
        Microsoft.Office.Interop.Visio.Document stencil;
        Microsoft.Office.Interop.Visio.Master masterInStencil;
        Microsoft.Office.Interop.Visio.Shape connector;
        Microsoft.Office.Interop.Visio.Cell beginX;
        Microsoft.Office.Interop.Visio.Cell endX;

        // Get the Application object from the shape.
        visioApplication = (Microsoft.Office.Interop.Visio.Application)
            shapeFrom.Application;

        try {

            // Verify that the shapes are on the same page.
            if (shapeFrom.ContainingPage != null && shapeTo.ContainingPage != null && 
                shapeFrom.ContainingPage.Equals(shapeTo.ContainingPage)) {

                // Access the Basic Flowchart Shapes stencil from the
                // Documents collection of the application.
                stencil = visioApplication.Documents.OpenEx(
                    BASIC_FLOWCHART_STENCIL,
                    (short)Microsoft.Office.Interop.Visio.
                        VisOpenSaveArgs.visOpenDocked);

                // Get the dynamic connector master on the stencil by its
                // universal name.
                masterInStencil = stencil.Masters.get_ItemU(
                    DYNAMIC_CONNECTOR_MASTER);

                // Drop the dynamic connector on the active page.
                connector = visioApplication.ActivePage.Drop(
                    masterInStencil, 0, 0);

                // Connect the begin point of the dynamic connector to the
                // PinX cell of the first 2-D shape.
                beginX = connector.get_CellsSRC(
                    (short)Microsoft.Office.Interop.Visio.
                        VisSectionIndices.visSectionObject,
                    (short)Microsoft.Office.Interop.Visio.
                        VisRowIndices.visRowXForm1D,
                    (short)Microsoft.Office.Interop.Visio.
                        VisCellIndices.vis1DBeginX);

                beginX.GlueTo(shapeFrom.get_CellsSRC(
                    (short)Microsoft.Office.Interop.Visio.
                        VisSectionIndices.visSectionObject,
                    (short)Microsoft.Office.Interop.Visio.
                        VisRowIndices.visRowXFormOut,
                    (short)Microsoft.Office.Interop.Visio.
                        VisCellIndices.visXFormPinX));

                // Connect the end point of the dynamic connector to the
                // PinX cell of the second 2-D shape.
                endX = connector.get_CellsSRC(
                    (short)Microsoft.Office.Interop.Visio.
                        VisSectionIndices.visSectionObject,
                    (short)Microsoft.Office.Interop.Visio.
                        VisRowIndices.visRowXForm1D,
                    (short)Microsoft.Office.Interop.Visio.
                        VisCellIndices.vis1DEndX);

                endX.GlueTo(shapeTo.get_CellsSRC(
                    (short)Microsoft.Office.Interop.Visio.
                        VisSectionIndices.visSectionObject,
                    (short)Microsoft.Office.Interop.Visio.
                        VisRowIndices.visRowXFormOut,
                    (short)Microsoft.Office.Interop.Visio.
                        VisCellIndices.visXFormPinX));
            }
            else {

                // Processing cannot continue because the shapes are not on 
                // the same page.
                System.Diagnostics.Debug.WriteLine(MESSAGE_NOT_SAME_PAGE);
            }
        }
        catch (Exception err) {
            System.Diagnostics.Debug.WriteLine(err.Message);
            throw;
        }
    }

答案 1 :(得分:0)

AutoConnect创建动态胶水,您可以将其视为胶合到整个形状。但在这种情况下,您需要创建静态或点对点粘合剂。正如@bcwhims指出的那样,你使用了单元格对象的GlueTo方法。

我将添加一些我已经编写的代码,它通过替代方式使用单元名称语法:

qno

这会生成以下形状,连接器的开始/结束单元格显示生成的胶水公式:

enter image description here