Search code examples
c#.netmicrosoft-gleemsagl

Routing only edges with MSAGL


Is there a way to route only edges in existing layout with MSAGL?

I have a GeometryGraph object with layout generated using LayeredLayout and I want to remove/add edges without running the layout algorithm again (this operation makes drastic changes to node positions and is confusing to end user).

Can I somehow simply run layout again with all node positions fixed?


Solution

  • You need to keep InteractiveEdgeRouter in sync with your graph:

    edgeRouter_ = new InteractiveEdgeRouter(Graph.Nodes.Select(n => n.BoundaryCurve), 3, 0.65 * 3, 0);
    

    Every recalc of graph layout should also call edgeRouter_.Run() to keep it in sync with obstacle changes (you should add new nodes as well).

    After you added new edge, instead of calculating layout again, set the edge curve manually using the router:

     private void RouteMissingEdges()
     {
         foreach (var edge in Graph.Edges)
         {
             if (edge.Curve == null)
             {
                 SmoothedPolyline ignore;
    
                 edge.Curve = edgeRouter_.RouteSplineFromPortToPortWhenTheWholeGraphIsReady(
                     new FloatingPort(edge.Source.BoundaryCurve, edge.Source.Center),
                     new FloatingPort(edge.Target.BoundaryCurve, edge.Target.Center),
                     true, out ignore);
    
                 Arrowheads.TrimSplineAndCalculateArrowheads(edge.EdgeGeometry,
                                                 edge.Source.BoundaryCurve,
                                                 edge.Target.BoundaryCurve,
                                                 edge.Curve, true,
                                                 false);
    
             }
         }
    }