|
3 | 3 | [](https://supportcenter.devexpress.com/ticket/details/T394199) |
4 | 4 | [](https://docs.devexpress.com/GeneralInformation/403183) |
5 | 5 | [](#does-this-example-address-your-development-requirementsobjectives) |
6 | | -<!-- default badges end --> |
7 | | -<!-- default file list --> |
8 | | -*Files to look at*: |
| 6 | + |
| 7 | +# WPF Diagram Control - Microsoft Automatic Graph Layout (MSAGL) Algorithms |
| 8 | + |
| 9 | +This example integrates **Microsoft Automatic Graph Layout (MSAGL)** algorithms with the WPF [`DiagramControl`](https://docs.devexpress.com/WPF/DevExpress.Xpf.Diagram.DiagramControl). The workflow extracts a graph from the diagram, processes it with an MSAGL algorithm, and applies calculated node positions to diagram items. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## Implementation Details |
| 14 | + |
| 15 | +### Extract the Graph |
| 16 | + |
| 17 | +Use the `GraphOperations.GetDiagramGraph` method to extract the current diagram. The method returns a `Graph` object that contains the collections of nodes and edges represented by diagram items: |
| 18 | + |
| 19 | +```csharp |
| 20 | +GraphOperations.GetDiagramGraph(diagramControl); |
| 21 | +``` |
| 22 | + |
| 23 | +### Calculate Position Information |
| 24 | + |
| 25 | +For each shape, calculate diagram shape coordinates and store them in `PositionInfo` objects: |
| 26 | + |
| 27 | +```csharp |
| 28 | +layout.RelayoutGraphNodesPosition(GraphOperations.GetDiagramGraph(diagramControl)); |
| 29 | + |
| 30 | +public virtual IEnumerable<PositionInfo<IDiagramItem>> RelayoutGraphNodesPosition(Graph<IDiagramItem> graph) { |
| 31 | + GeometryGraph = MsaglGeometryGraphHelpers.CreateGeometryGraph(graph); |
| 32 | + LayoutCalculator.CalculateLayout(GeometryGraph); |
| 33 | + return MsaglGeometryGraphHelpers.GetGetNodesPositionInfo(GeometryGraph); |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Apply Layout |
| 38 | + |
| 39 | +Update the diagram with calculated node positions: |
| 40 | + |
| 41 | +```csharp |
| 42 | +diagramControl.RelayoutDiagramItems( |
| 43 | + layout.RelayoutGraphNodesPosition(GraphOperations.GetDiagramGraph(diagramControl)) |
| 44 | +); |
| 45 | +``` |
| 46 | + |
| 47 | +### Update Shape Connectors |
| 48 | + |
| 49 | +After shapes are repositioned, reroute shape connectors and register a routing strategy: |
| 50 | + |
| 51 | +```csharp |
| 52 | +diagramControl.Items.OfType<IDiagramConnector>().ForEach(connector => { |
| 53 | + connector.Type = layout.GetDiagramConnectorType(); |
| 54 | + connector.UpdateRoute(); |
| 55 | +}); |
| 56 | + |
| 57 | +diagramControl.Controller.RegisterRoutingStrategy( |
| 58 | + layout.GetDiagramConnectorType(), |
| 59 | + layout.GetDiagramRoutingStrategy() |
| 60 | +); |
| 61 | +``` |
| 62 | + |
| 63 | +### Display Entire Diagram |
| 64 | + |
| 65 | +Fit the view so that all items are visible in the viewport: |
| 66 | + |
| 67 | +```csharp |
| 68 | +diagramControl.FitToDrawing(); |
| 69 | +``` |
| 70 | + |
| 71 | +### Ribbon commands |
| 72 | + |
| 73 | +Create ribbon items and handle their `ItemClick` events to apply different MSAGL algorithms: |
| 74 | + |
| 75 | +```csharp |
| 76 | +void ApplySugiyama(object s, ItemClickEventArgs e) { |
| 77 | + ApplyLayout(new GraphLayout(new SugiyamaLayoutCalculator())); |
| 78 | +} |
| 79 | +void ApplyRanking(object s, ItemClickEventArgs e) { |
| 80 | + ApplyLayout(new GraphLayout(new RankingLayoutCalculator())); |
| 81 | +} |
| 82 | +void ApplyPhyloTree(object s, ItemClickEventArgs e) { |
| 83 | + ApplyLayout(new PhyloTreeLayout(new PhyloTreeLayoutCalculator())); |
| 84 | +} |
| 85 | +void ApplyMDS(object s, ItemClickEventArgs e) { |
| 86 | + ApplyLayout(new GraphLayout(new MDSLayoutCalculator())); |
| 87 | +} |
| 88 | +void ApplyDisconnectedGraphs(object s, ItemClickEventArgs e) { |
| 89 | + ApplyLayout(new GraphLayout(new DisconnectedGraphsLayoutCalculator())); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Files to Review |
9 | 94 |
|
10 | 95 | * [LayoutExampleWindow.xaml](./CS/DXDiagram.CustomLayoutAlgorithms/LayoutExampleWindow.xaml) (VB: [LayoutExampleWindow.xaml](./VB/DXDiagram.CustomLayoutAlgorithms/LayoutExampleWindow.xaml)) |
11 | 96 | * [LayoutExampleWindow.xaml.cs](./CS/DXDiagram.CustomLayoutAlgorithms/LayoutExampleWindow.xaml.cs) (VB: [LayoutExampleWindow.xaml.vb](./VB/DXDiagram.CustomLayoutAlgorithms/LayoutExampleWindow.xaml.vb)) |
|
14 | 99 | * [Converter.cs](./CS/MsaglHelpers/Converter.cs) (VB: [Converter.vb](./VB/MsaglHelpers/Converter.vb)) |
15 | 100 | * [GraphLayout.cs](./CS/MsaglHelpers/Layout/GraphLayout.cs) (VB: [GraphLayout.vb](./VB/MsaglHelpers/Layout/GraphLayout.vb)) |
16 | 101 | * [PhyloTreeLayout.cs](./CS/MsaglHelpers/Layout/PhyloTreeLayout.cs) (VB: [PhyloTreeLayout.vb](./VB/MsaglHelpers/Layout/PhyloTreeLayout.vb)) |
17 | | -* [DisconnectedGraphsLayoutCaculator.cs](./CS/MsaglHelpers/LayoutCalculators/DisconnectedGraphsLayoutCaculator.cs) (VB: [DisconnectedGraphsLayoutCaculator.vb](./VB/MsaglHelpers/LayoutCalculators/DisconnectedGraphsLayoutCaculator.vb)) |
| 102 | +* [DisconnectedGraphsLayoutCalculator.cs](./CS/MsaglHelpers/LayoutCalculators/DisconnectedGraphsLayoutCalculator.cs) (VB: [DisconnectedGraphsLayoutCalculator.vb](./VB/MsaglHelpers/LayoutCalculators/DisconnectedGraphsLayoutCalculator.vb)) |
18 | 103 | * [ILayoutCalculator.cs](./CS/MsaglHelpers/LayoutCalculators/ILayoutCalculator.cs) (VB: [ILayoutCalculator.vb](./VB/MsaglHelpers/LayoutCalculators/ILayoutCalculator.vb)) |
19 | 104 | * [MDSLayoutCalculator.cs](./CS/MsaglHelpers/LayoutCalculators/MDSLayoutCalculator.cs) (VB: [MDSLayoutCalculator.vb](./VB/MsaglHelpers/LayoutCalculators/MDSLayoutCalculator.vb)) |
20 | 105 | * [PhyloTreeLayoutCalculator.cs](./CS/MsaglHelpers/LayoutCalculators/PhyloTreeLayoutCalculator.cs) (VB: [PhyloTreeLayoutCalculator.vb](./VB/MsaglHelpers/LayoutCalculators/PhyloTreeLayoutCalculator.vb)) |
21 | 106 | * [RankingLayoutCalculator.cs](./CS/MsaglHelpers/LayoutCalculators/RankingLayoutCalculator.cs) (VB: [RankingLayoutCalculator.vb](./VB/MsaglHelpers/LayoutCalculators/RankingLayoutCalculator.vb)) |
22 | 107 | * [SugiyamaLayoutCalculator.cs](./CS/MsaglHelpers/LayoutCalculators/SugiyamaLayoutCalculator.cs) (VB: [SugiyamaLayoutCalculator.vb](./VB/MsaglHelpers/LayoutCalculators/SugiyamaLayoutCalculator.vb)) |
23 | 108 | * [MsaglGeometryGraphHelpers.cs](./CS/MsaglHelpers/MsaglGeometryGraphHelpers.cs) (VB: [MsaglGeometryGraphHelpers.vb](./VB/MsaglHelpers/MsaglGeometryGraphHelpers.vb)) |
24 | 109 | * [RoutingHelper.cs](./CS/MsaglHelpers/RoutingHelper.cs) (VB: [RoutingHelper.vb](./VB/MsaglHelpers/RoutingHelper.vb)) |
25 | | -<!-- default file list end --> |
26 | | -# WPF Diagram Control - Microsoft Automatic Graph Layout (MSAGL) Algorithms |
27 | 110 |
|
| 111 | +## Documentation |
28 | 112 |
|
29 | | -DiagramControl provides two methods that make it easier to use external graph layout algorithms to arrange diagram shapes. The <strong><em>GraphOperations.GetDiagramGraph</em></strong> method reads the diagram currently loaded into DiagramControl and returns the <strong><em>Graph</em></strong> object that contains collections of edges and nodes represented by diagram items. You can use this information to calculate positions for diagram shapes. Then, for every shape, create the <strong><em>PositionInfo</em> </strong>object containing the shape reference and its position. To apply the layout to the loaded diagram, call the <em><strong>DiagramControl.RelayoutDiagramItems</strong></em> method that accepts the collection of PositionInfo objects.<br><br>This example demonstrates how the GetDiagramGraph and RelayoutDiagramItems methods can be used to connect the <a href="https://github.com/Microsoft/automatic-graph-layout">Microsoft Automatic Graph Layout (MSAGL)</a> library to DiagramControl. |
| 113 | +* [DiagramControl](https://docs.devexpress.com/WPF/DevExpress.Xpf.Diagram.DiagramControl) |
| 114 | +* [Diagram Items](https://docs.devexpress.com/WPF/116675/controls-and-libraries/diagram-control/diagram-items) |
| 115 | +* [DiagramDesignerControl](https://docs.devexpress.com/WPF/DevExpress.Xpf.Diagram.DiagramDesignerControl) |
| 116 | +* [RibbonControl](https://docs.devexpress.com/WPF/DevExpress.Xpf.Ribbon.RibbonControl) |
| 117 | +* [RibbonPageCategory](https://docs.devexpress.com/WPF/DevExpress.Xpf.Ribbon.RibbonPageCategory) |
| 118 | +* [RibbonPage](https://docs.devexpress.com/WPF/DevExpress.Xpf.Ribbon.RibbonPage) |
30 | 119 |
|
31 | | -<br/> |
| 120 | +## More Examples |
32 | 121 |
|
| 122 | +* [WPF DiagramControl - Create Custom Shapes with Connection Points](https://github.com/DevExpress-Examples/wpf-diagramdesigner-create-custom-shapes-with-connection-points) |
| 123 | +* [WPF DiagramControl - Create Custom Context Menus](https://github.com/DevExpress-Examples/wpf-diagram-custom-context-menu) |
| 124 | +* [WPF Diagram Control - Track and Restrict Drag Actions](https://github.com/DevExpress-Examples/wpf-diagram-track-and-restrict-drag-actions) |
33 | 125 |
|
34 | 126 | <!-- feedback --> |
35 | | -## Does this example address your development requirements/objectives? |
36 | | - |
37 | | -[<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=wpf-diagram-ms-automatic-graph-layout-algorithms&~~~was_helpful=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=wpf-diagram-ms-automatic-graph-layout-algorithms&~~~was_helpful=no) |
38 | | - |
| 127 | +## Does this example address your development requirements/objectives? |
| 128 | + |
| 129 | +[<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=wpf-diagram-ms-automatic-graph-layout-algorithms&~~~was_helpful=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=wpf-diagram-ms-automatic-graph-layout-algorithms&~~~was_helpful=no) |
| 130 | + |
39 | 131 | (you will be redirected to DevExpress.com to submit your response) |
40 | 132 | <!-- feedback end --> |
0 commit comments