C#.net How to connect a shape back to itself

Started by tkanney007, May 19, 2016, 10:39:56 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

tkanney007

Hello!

Some background first:

I am working on a program that will import (new) and export (existing) "Action Flows" to/from a database. In the program, actions link to others (create other actions) based on whether the action performed was successful, unsuccessful or it has timed out. Parallel actions are actions that are created at the same time. I have developed a custom stencil with different shapes for each action type that have shape data behind them - even connectors for each kind of connection between the actions (Successful, Unsuccessful, Timeout, and Parallel). Project mangers create the flow and then the engineers go in and used to create each action manually and link them together. Now with the program I have written it imports the flow directly into the DB and builds everything automatically, saving engineering time.

The problem:

Everything works great with the import side, however, when I try to export a flow I am having issues when an action is unsuccessful a lot of times we want to go back and create the same action again. In the flow it simply looks like a shape connected back to itself. I am using the AutoConnect method to connect my shapes, however, it bombs when you try to connect a shape back to itself. I am guessing a limitation to the AutoConnect method. The questions is how would I achieve this in the most easy effective way? Here is a sample of my code ( I can provide more if needed). Thanks!

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

JohnGoldsmith

Hi,

Have a look at gluing th cells in your connector to the respective cells in your target shape.  Here's an example:

    var shpTarget = vApp.ActivePage.DrawRectangle(1.0, 10.0, 1.5, 9.5);

    shpTarget.CellsU["FillForegnd"].FormulaU = "THEMEGUARD(RGB(122,201,118))";
    shpTarget.CellsU["LineColor"].FormulaU = "THEMEGUARD(RGB(255,255,255))";
   
    shpTarget.AddNamedRow((short)Visio.VisSectionIndices.visSectionConnectionPts,
                          "Top",
                          (short)Visio.VisRowTags.visTagDefault);
    shpTarget.CellsU["Connections.Top.X"].FormulaU = "Width*0.5";
    shpTarget.CellsU["Connections.Top.Y"].FormulaU = "Height";

    shpTarget.AddNamedRow((short)Visio.VisSectionIndices.visSectionConnectionPts,
                          "Right",
                          (short)Visio.VisRowTags.visTagDefault);
    shpTarget.CellsU["Connections.Right.X"].FormulaU = "Width";
    shpTarget.CellsU["Connections.Right.Y"].FormulaU = "Height*0.5";

   
    var shpConnector = vApp.ActivePage.Drop(vApp.ConnectorToolDataObject, 1.5, 10.0);
   
    var beginCell =shpConnector.CellsU["BeginX"];
    var endCell =shpConnector.CellsU["EndX"];

    beginCell.GlueTo(shpTarget.CellsU["Connections.Top.X"]);
    endCell.GlueTo(shpTarget.CellsU["Connections.Right.X"]);

    shpConnector.CellsU["EndArrow"].FormulaU = "5";


(I get hold of vApp as per this post: http://visualsignals.typepad.co.uk/vislog/2015/12/getting-started-with-c-in-linqpad-with-visio.html )

Hope that helps.

Best regards

John
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/