Search code examples
c#office-interopvisio

Connection a Visio connector to another connector in C# (Snap to)?


I have been using c# to create Visio flowcharts, adding shapes, and connecting them, both using AutoConnect(), and GlueTo(), which all works fine.

I have been trying to figure out how to connect to a connector instead of a "normal shape". E.g.: I have Shapes A & B have a connector between them, and Shape C needs to connect to the connector between Shapes A & B. If I do this manually in Visio, the connector "Snaps to" the middle of the A-B connector, and it looks just the way I want it, but I haven't been able to do that programmatically.

I borrowed the below AutoConnect method to be able to connect shapes, and save the connector in a variable:

static Shape AutoConnect(Shape fromShape, Shape toShape)
    {
        fromShape.AutoConnect(toShape, VisAutoConnectDir.visAutoConnectDirNone);

        return (
            from Connect c1 in fromShape.FromConnects
            from Connect c2 in c1.FromSheet.Connects
            where c2.ToSheet.ID == toShape.ID
            select c2.FromSheet)
            .FirstOrDefault();
    }

if I use the above, I get a connector that is not connected, and if I use GlueTo(), I get and Exception: "Inappropriate target object for this action". The reason I need this is that some of the flowcharts have a lot of parallel operations that makes an ugly clutter of connections when they merge into one shape, as the flowchart nears the end. If there is another method to "merge" the connectors, I would be interested to know, as well.


Solution

  • AFAIK it is not possible to connect a connector to another connector in Visio. Snapping is done when you move a shape, it does not save anything in the diagram (so if you move either of the shapes being connected, it may "break").

    What you could try is changing routing style for your page: enter image description here

    You could try changing the routing style programmatically AFTER creating the diagram, this may clean up the "mess" with connectors.