This example demonstrates the built in support for different graph traversal methods.
There are three graph traversal methods:
To explore graph traversal just click on a shape to select it. The example will automatically highlight the visited shapes and assign a visiting order to them.

| Language: C# |
using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Diagram;
using Nevron.Nov.Diagram.Layout;
using Nevron.Nov.Diagram.Shapes;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.Windows.Forms;
namespace NovWinFormsProject
{
public partial class Form1 : Form
{
#region Constructors
public Form1()
{
InitializeComponent();
// Set form title
Text = "NOV WinForms - NGraphTraversalExample";
// Create the NOV example and its controls
NWidget exampleContent = CreateExampleContent();
NWidget exampleControls = CreateExampleControls();
if (exampleControls != null)
{
exampleControls.PreferredWidth = 300;
}
// Place the example and its controls in a pair box
NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
pairBox.FillMode = ENStackFillMode.First;
pairBox.FitMode = ENStackFitMode.First;
pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);
// Host the example in the form
NNovWidgetHost host = new NNovWidgetHost(pairBox);
host.Dock = DockStyle.Fill;
Controls.Add(host);
}
#endregion
#region Example
public NWidget CreateExampleContent()
{
NDrawingViewWithRibbon drawingViewWithRibbon = new NDrawingViewWithRibbon();
m_DrawingView = drawingViewWithRibbon.View;
m_DrawingView.Document.HistoryService.Pause();
try
{
InitDiagram(m_DrawingView.Document);
}
finally
{
m_DrawingView.Document.HistoryService.Resume();
}
return drawingViewWithRibbon;
}
public NWidget CreateExampleControls()
{
NStackPanel stack = new NStackPanel();
m_TraversalMethodRadioGroup = new NRadioButtonGroup();
m_TraversalMethodRadioGroup.FillFromEnum();
m_TraversalMethodRadioGroup.SelectedIndexChanged += OnRadioGroupSelectedIndexChanged;
stack.Add(new NGroupBox("Graph Traversal Method:", m_TraversalMethodRadioGroup));
m_GraphTypeRadioGroup = new NRadioButtonGroup();
m_GraphTypeRadioGroup.FillFromEnum();
m_GraphTypeRadioGroup.SelectedIndexChanged += OnRadioGroupSelectedIndexChanged;
stack.Add(new NGroupBox("Graph Type:", m_GraphTypeRadioGroup));
return stack;
}
public string GetExampleDescription()
{
return @"
This example demonstrates the built in support for different graph traversal methods.
There are three graph traversal methods:
- Depth First Traversal - in this type of graph traversal the algorithm first visits the start vertex and then recursively visits its destination or neighbour vertices (depending on whether you want to treat the graph as directed or undirected).
- Breadth First Traversal - in this type of graph traversal the algorithm first visits the starting vertex, then all the vertices which can be reached in one step, then in two steps and so on. In Breadth First Traversal you can also treat the graph as directed or undirected.
- Topological Order Traversal - in this type of graph traversal the algorithm visits the vertices in their topological order. A topological sort is a list of the vertices of a directed graph in which all the successors of any given vertex appear in the sequence after that vertex. Topological Order Traversal is only applicable for directed graphs.
To explore graph traversal just click on a shape to select it. The example will automatically highlight the visited shapes and assign a visiting order to them.
";
}
///
/// Gets the relevant documentation links for the example
///
///
public (string Name, string Url)[] GetDocumentationUrls()
{
return new[]
{
("Shape Navigation", "https://www.nevron.com/docs/openvision/diagram-shape-navigation.html"),
("Shapes", "https://www.nevron.com/docs/openvision/diagram-shapes.html"),
};
}
private void InitDiagram(NDrawingDocument drawingDocument)
{
NDrawing drawing = drawingDocument.Content;
drawing.ScreenVisibility.ShowPorts = false;
drawing.ActivePage.Selection.Mode = ENSelectionMode.Single;
drawing.ActivePage.Selection.Deselected += OnPageItemDeselected;
drawing.ActivePage.Selection.Selected += OnPageItemSelected;
// Add a stylesheet for highlighted shapes
NStyleSheet styleSheet = new NStyleSheet();
drawingDocument.StyleSheets.Add(styleSheet);
NRule rule = styleSheet.CreateRule(sb =>
{
sb.Type(NGeometry.NGeometrySchema);
sb.ChildOf();
sb.Type(NShape.NShapeSchema);
sb.UserClass(HighlightedUserClassName);
});
rule.Declarations.Add(new NValueDeclaration(NGeometry.FillProperty, new NColorFill(NColor.FromARGB(80, 255, 0, 0))));
rule.Declarations.Add(new NValueDeclaration(NGeometry.StrokeProperty, new NStroke(2, NColor.Red)));
rule = styleSheet.CreateRule(sb =>
{
sb.Type(NGeometry.NGeometrySchema);
sb.ChildOf();
sb.Type(NShape.NShapeSchema);
sb.UserClass(NDR.StyleSheetNameConnectors);
sb.UserClass(HighlightedUserClassName);
});
rule.Declarations.Add(new NValueDeclaration(NGeometry.EndArrowheadProperty, new NArrowhead(ENArrowheadShape.Triangle)));
// Create a rectangular grid template
NGraphTemplate template = new NRectangularGridTemplate();
template.Origin = new NPoint(10, 23);
template.VertexShape = ENBasicShape.Rectangle;
template.EdgeUserClass = NDR.StyleSheetNameConnectors;
template.Create(drawingDocument);
// Create a tree template
template = new NGenericTreeTemplate();
template.Origin = new NPoint(250, 23);
template.VertexShape = ENBasicShape.Triangle;
template.EdgeUserClass = NDR.StyleSheetNameConnectors;
template.Create(drawingDocument);
// Create an elliptical grid template
template = new NEllipticalGridTemplate();
template.Origin = new NPoint(10, 250);
template.VertexShape = ENBasicShape.Ellipse;
template.EdgeUserClass = NDR.StyleSheetNameConnectors;
template.Create(drawingDocument);
}
private void ClearHighlightedShapes()
{
// Remove the highligthed user class of all shapes
NList shapes = m_DrawingView.ActivePage.GetShapes(false);
for (int i = 0; i < shapes.Count; i++)
{
NShape shape = shapes[i];
shape.RemoveUserClass(HighlightedUserClassName);
shape.TextBlock = null;
}
}
private void HighlightShapes()
{
ClearHighlightedShapes();
// Highlight the selected type of shapes
NShape shape = m_DrawingView.ActivePage.Selection.FirstSelected as NShape;
if (shape == null || shape.ShapeType == ENShapeType.Shape1D)
return;
// get the graph in which the shape resides
NGraphBuilder graphBuilder = new NGraphBuilder(new NShapeGraphAdapter(), new NGraphPartFactory());
NObjectGraphPartMap objectMap;
NGraph graph = graphBuilder.BuildGraph(shape, out objectMap);
if (graph == null)
return;
// get the vertex which represents the shape in the graph
NGraphVertex vertex = objectMap.GetPartFromObject(shape) as NGraphVertex;
if (vertex == null)
return;
// create a visitor which will visit and highlight the graph parts
NHighlightingVisitor visitor = new NHighlightingVisitor(objectMap);
ENGraphType graphType = (ENGraphType)m_GraphTypeRadioGroup.SelectedIndex;
switch ((ENGraphTraversalMethod)m_TraversalMethodRadioGroup.SelectedIndex)
{
case ENGraphTraversalMethod.DepthFirstTraversal:
graph.DepthFirstTraversal(graphType, visitor, vertex);
break;
case ENGraphTraversalMethod.BreadthFirstTraversal:
graph.BreadthFirstTraversal(graphType, visitor, vertex);
break;
case ENGraphTraversalMethod.TopologicalOrderTraversal:
graph.TopologicalOrderTraversal(visitor);
break;
}
}
#endregion
#region Implementation
#endregion
#region Event Handlers
private void OnPageItemDeselected(NItemEventArgs arg)
{
ClearHighlightedShapes();
}
private void OnPageItemSelected(NItemEventArgs arg)
{
HighlightShapes();
}
private void OnRadioGroupSelectedIndexChanged(NValueChangeEventArgs arg)
{
HighlightShapes();
}
#endregion
#region Fields
private NDrawingView m_DrawingView;
private NRadioButtonGroup m_TraversalMethodRadioGroup;
private NRadioButtonGroup m_GraphTypeRadioGroup;
#endregion
#region Static Methods
#endregion
#region Constants
private const string HighlightedUserClassName = "Highlighted";
#endregion
#region Nested Types
private enum ENGraphTraversalMethod
{
DepthFirstTraversal,
BreadthFirstTraversal,
TopologicalOrderTraversal
}
private class NHighlightingVisitor : NGraphPartVisitor
{
public NHighlightingVisitor(NObjectGraphPartMap objectMap)
{
m_ObjectMap = objectMap;
}
public override bool Visit(NGraphPart part)
{
if (part is NGraphVertex)
{
NShape shape = (NShape)m_ObjectMap.GetObjectFromPart(part);
shape.Text = m_VertexCount.ToString();
shape.UserClass = HighlightedUserClassName;
m_VertexCount++;
}
return true;
}
private NObjectGraphPartMap m_ObjectMap;
private int m_VertexCount;
}
#endregion
}
} | Language: VB.NET |
Imports Nevron.Nov
Imports Nevron.Nov.DataStructures
Imports Nevron.Nov.Diagram
Imports Nevron.Nov.Diagram.Layout
Imports Nevron.Nov.Diagram.Shapes
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
Class MainWindow
#Region "Constructors"
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' Set form title
Title = "NOV WPF - NGraphTraversalExample"
' Create the NOV example and its controls
Dim exampleContent As NWidget = CreateExampleContent()
Dim exampleControls As NWidget = CreateExampleControls()
If Not exampleControls Is Nothing Then
exampleControls.PreferredWidth = 300
End If
' Place the example and its controls in a pair box
Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
pairBox.FillMode = ENStackFillMode.First
pairBox.FitMode = ENStackFitMode.First
pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)
' Host the example in the form
Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
Content = host
End Sub
#End Region
#Region "Example"
Public Function CreateExampleContent() As NWidget
Dim drawingViewWithRibbon As NDrawingViewWithRibbon = New NDrawingViewWithRibbon()
m_DrawingView = drawingViewWithRibbon.View
m_DrawingView.Document.HistoryService.Pause()
Try
InitDiagram(m_DrawingView.Document)
Finally
m_DrawingView.Document.HistoryService.Resume()
End Try
Return drawingViewWithRibbon
End Function
Public Function CreateExampleControls() As NWidget
Dim stack As NStackPanel = New NStackPanel()
m_TraversalMethodRadioGroup = New NRadioButtonGroup()
m_TraversalMethodRadioGroup.FillFromEnum(Of ENGraphTraversalMethod)()
Me.m_TraversalMethodRadioGroup.SelectedIndexChanged += AddressOf OnRadioGroupSelectedIndexChanged
stack.Add(New NGroupBox("Graph Traversal Method:", m_TraversalMethodRadioGroup))
m_GraphTypeRadioGroup = New NRadioButtonGroup()
m_GraphTypeRadioGroup.FillFromEnum(Of ENGraphType)()
Me.m_GraphTypeRadioGroup.SelectedIndexChanged += AddressOf OnRadioGroupSelectedIndexChanged
stack.Add(New NGroupBox("Graph Type:", m_GraphTypeRadioGroup))
Return stack
End Function
Public Function GetExampleDescription() As String
Return "
This example demonstrates the built in support for different graph traversal methods.
There are three graph traversal methods:
- Depth First Traversal - in this type of graph traversal the algorithm first visits the start vertex and then recursively visits its destination or neighbour vertices (depending on whether you want to treat the graph as directed or undirected).
- Breadth First Traversal - in this type of graph traversal the algorithm first visits the starting vertex, then all the vertices which can be reached in one step, then in two steps and so on. In Breadth First Traversal you can also treat the graph as directed or undirected.
- Topological Order Traversal - in this type of graph traversal the algorithm visits the vertices in their topological order. A topological sort is a list of the vertices of a directed graph in which all the successors of any given vertex appear in the sequence after that vertex. Topological Order Traversal is only applicable for directed graphs.
To explore graph traversal just click on a shape to select it. The example will automatically highlight the visited shapes and assign a visiting order to them.
"
End Function
'''
''' Gets the relevant documentation links for the example
'''
'''
Public Function GetDocumentationUrls() As (String, String)()
Return {("Shape Navigation", "https://www.nevron.com/docs/openvision/diagram-shape-navigation.html"), ("Shapes", "https://www.nevron.com/docs/openvision/diagram-shapes.html")}
End Function
Private Sub InitDiagram(drawingDocument As NDrawingDocument)
Dim drawing = drawingDocument.Content
drawing.ScreenVisibility.ShowPorts = False
drawing.ActivePage.Selection.Mode = ENSelectionMode.Single
drawing.ActivePage.Selection.Deselected += AddressOf OnPageItemDeselected
drawing.ActivePage.Selection.Selected += AddressOf OnPageItemSelected
' Add a stylesheet for highlighted shapes
Dim styleSheet As NStyleSheet = New NStyleSheet()
drawingDocument.StyleSheets.Add(styleSheet)
Dim rule As NRule = styleSheet.CreateRule(Sub(sb)
sb.Type(NGeometry.NGeometrySchema)
sb.ChildOf()
sb.Type(NShape.NShapeSchema)
sb.UserClass(HighlightedUserClassName)
End Sub)
rule.Declarations.Add(New NValueDeclaration(Of NFill)(NGeometry.FillProperty, New NColorFill(NColor.FromARGB(80, 255, 0, 0))))
rule.Declarations.Add(New NValueDeclaration(Of NStroke)(NGeometry.StrokeProperty, New NStroke(2, NColor.Red)))
rule = styleSheet.CreateRule(Sub(sb)
sb.Type(NGeometry.NGeometrySchema)
sb.ChildOf()
sb.Type(NShape.NShapeSchema)
sb.UserClass(NDR.StyleSheetNameConnectors)
sb.UserClass(HighlightedUserClassName)
End Sub)
rule.Declarations.Add(New NValueDeclaration(Of NArrowhead)(NGeometry.EndArrowheadProperty, New NArrowhead(ENArrowheadShape.Triangle)))
' Create a rectangular grid template
Dim template As NGraphTemplate = New NRectangularGridTemplate()
template.Origin = New NPoint(10, 23)
template.VertexShape = ENBasicShape.Rectangle
template.EdgeUserClass = NDR.StyleSheetNameConnectors
template.Create(drawingDocument)
' Create a tree template
template = New NGenericTreeTemplate()
template.Origin = New NPoint(250, 23)
template.VertexShape = ENBasicShape.Triangle
template.EdgeUserClass = NDR.StyleSheetNameConnectors
template.Create(drawingDocument)
' Create an elliptical grid template
template = New NEllipticalGridTemplate()
template.Origin = New NPoint(10, 250)
template.VertexShape = ENBasicShape.Ellipse
template.EdgeUserClass = NDR.StyleSheetNameConnectors
template.Create(drawingDocument)
End Sub
Private Sub ClearHighlightedShapes()
' Remove the highligthed user class of all shapes
Dim shapes = m_DrawingView.ActivePage.GetShapes(False)
For i = 0 To shapes.Count - 1
Dim shape = shapes(i)
shape.RemoveUserClass(HighlightedUserClassName)
shape.TextBlock = Nothing
Next
End Sub
Private Sub HighlightShapes()
ClearHighlightedShapes()
' Highlight the selected type of shapes
Dim shape As NShape = TryCast(m_DrawingView.ActivePage.Selection.FirstSelected, NShape)
If shape Is Nothing OrElse shape.ShapeType Is ENShapeType.Shape1D Then Return
' get the graph in which the shape resides
Dim graphBuilder As NGraphBuilder = New NGraphBuilder(New NShapeGraphAdapter(), New NGraphPartFactory())
Dim objectMap As NObjectGraphPartMap
Dim graph = graphBuilder.BuildGraph(shape, objectMap)
If graph Is Nothing Then Return
' get the vertex which represents the shape in the graph
Dim vertex As NGraphVertex = TryCast(objectMap.GetPartFromObject(shape), NGraphVertex)
If vertex Is Nothing Then Return
' create a visitor which will visit and highlight the graph parts
Dim visitor As NHighlightingVisitor = New NHighlightingVisitor(objectMap)
Dim graphType As ENGraphType = m_GraphTypeRadioGroup.SelectedIndex
Select Case m_TraversalMethodRadioGroup.SelectedIndex
Case ENGraphTraversalMethod.DepthFirstTraversal
graph.DepthFirstTraversal(graphType, visitor, vertex)
Case ENGraphTraversalMethod.BreadthFirstTraversal
graph.BreadthFirstTraversal(graphType, visitor, vertex)
Case ENGraphTraversalMethod.TopologicalOrderTraversal
graph.TopologicalOrderTraversal(visitor)
End Select
End Sub
#End Region
#Region "Implementation"
#End Region
#Region "Event Handlers"
Private Sub OnPageItemDeselected(arg As NItemEventArgs(Of NPageItem))
ClearHighlightedShapes()
End Sub
Private Sub OnPageItemSelected(arg As NItemEventArgs(Of NPageItem))
HighlightShapes()
End Sub
Private Sub OnRadioGroupSelectedIndexChanged(arg As NValueChangeEventArgs)
HighlightShapes()
End Sub
#End Region
#Region "Fields"
Private m_DrawingView As NDrawingView
Private m_TraversalMethodRadioGroup As NRadioButtonGroup
Private m_GraphTypeRadioGroup As NRadioButtonGroup
#End Region
#Region "Static Methods"
#End Region
#Region "Constants"
Private Const HighlightedUserClassName As String = "Highlighted"
#End Region
#Region "Nested Types"
Private Enum ENGraphTraversalMethod
DepthFirstTraversal
BreadthFirstTraversal
TopologicalOrderTraversal
End Enum
Private Class NHighlightingVisitor
Inherits NGraphPartVisitor
Public Sub New(objectMap As NObjectGraphPartMap)
m_ObjectMap = objectMap
End Sub
Public Overrides Function Visit(part As NGraphPart) As Boolean
If TypeOf part Is NGraphVertex Then
Dim shape As NShape = m_ObjectMap.GetObjectFromPart(part)
shape.Text = m_VertexCount.ToString()
shape.UserClass = HighlightedUserClassName
m_VertexCount += 1
End If
Return True
End Function
Private m_ObjectMap As NObjectGraphPartMap
Private m_VertexCount As Integer
End Class
#End Region
End Class