Skip to main content

How to Use Custom Graph Layout Algorithms to Arrange Shapes in DiagramControl

  • 13 minutes to read

DiagramControl provides two methods that make it easier to use external graph layout algorithms to arrange diagram shapes. The GraphOperations.GetDiagramGraph method reads the diagram currently loaded into DiagramControl and returns the Graph 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 PositionInfoobject containing the shape reference and its position. To apply the layout to the loaded diagram, call the DiagramControl.RelayoutDiagramItems method that accepts the collection of PositionInfo objects.This example demonstrates how the GetDiagramGraph and RelayoutDiagramItems methods can be used to connect the Microsoft Automatic Graph Layout (MSAGL) library to DiagramControl.

View Example

Imports DevExpress.Diagram.Core
Imports DevExpress.Diagram.Core.Layout
Imports DevExpress.Diagram.Core.Routing
Imports Microsoft.Msagl.Core.Layout
Imports Microsoft.Msagl.Core.Routing
Imports System.Collections.Generic

Namespace MsaglHelpers
    Public Class GraphLayout
        Private Property GeometryGraph() As GeometryGraph
        Private ReadOnly Property RoutingMode() As EdgeRoutingMode
            Get
                Return LayoutCalculator.LayoutAlgorithmSettings.EdgeRoutingSettings.EdgeRoutingMode
            End Get
        End Property
        Protected Property LayoutCalculator() As ILayoutCalculator

        Public Sub New(ByVal layoutCalculator As ILayoutCalculator)
            Me.LayoutCalculator = layoutCalculator
        End Sub
        Public Overridable Function RelayoutGraphNodesPosition(ByVal graph As Graph(Of IDiagramItem)) As IEnumerable(Of PositionInfo(Of IDiagramItem))
            GeometryGraph = MsaglGeometryGraphHelpers.CreateGeometryGraph(graph)
            LayoutCalculator.CalculateLayout(GeometryGraph)
            Return MsaglGeometryGraphHelpers.GetGetNodesPositionInfo(GeometryGraph)
        End Function
        Public Function GetDiagramConnectorType() As ConnectorType
            Return RoutingHelper.GetDiagramConnectorType(RoutingMode)
        End Function
    End Class
End Namespace