This example demonstrates how to use the NOV Diagram API to navigate between shapes. Use the controls on the right to specify which shape should be highlighted and then select a shape from the drawing view. The shapes you have selected will be highlighted based on the selected shape.

| Language: C# |
using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Diagram;
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 - NShapeNavigationExample";
// 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()
{
NMargins innerPadding = new NMargins(30, 0, 0, 0);
NStackPanel vertexStack = new NStackPanel();
{
// Edges
m_AllEdgesCheckBox = new NCheckBox("All edges");
m_AllEdgesCheckBox.CheckedChanged += AllEdgesCheckBox_CheckedChanged;
vertexStack.Add(m_AllEdgesCheckBox);
m_IncomingEdgesCheckBox = new NCheckBox("Incoming edges");
m_IncomingEdgesCheckBox.Padding = innerPadding;
m_IncomingEdgesCheckBox.CheckedChanged += CheckBox_CheckedChanged;
vertexStack.Add(m_IncomingEdgesCheckBox);
m_OutgoingEdgesCheckBox = new NCheckBox("Outgoing edges");
m_OutgoingEdgesCheckBox.Padding = innerPadding;
m_OutgoingEdgesCheckBox.CheckedChanged += CheckBox_CheckedChanged;
vertexStack.Add(m_OutgoingEdgesCheckBox);
// Neighbour vertices
m_NeighbourVerticesCheckBox = new NCheckBox("Neighbour vertices");
m_NeighbourVerticesCheckBox.CheckedChanged += NeighbourVerticesCheckBox_CheckedChanged;
vertexStack.Add(m_NeighbourVerticesCheckBox);
m_SourceVerticesCheckBox = new NCheckBox("Source vertices");
m_SourceVerticesCheckBox.Padding = innerPadding;
m_SourceVerticesCheckBox.CheckedChanged += CheckBox_CheckedChanged;
vertexStack.Add(m_SourceVerticesCheckBox);
m_DestinationVerticesCheckBox = new NCheckBox("Destination vertices");
m_DestinationVerticesCheckBox.Padding = innerPadding;
m_DestinationVerticesCheckBox.CheckedChanged += CheckBox_CheckedChanged;
vertexStack.Add(m_DestinationVerticesCheckBox);
// Accessible vertices
m_AccessibleVerticesCheckBox = new NCheckBox("Accessible vertices");
m_AccessibleVerticesCheckBox.CheckedChanged += AccessibleVerticesCheckBox_CheckedChanged;
vertexStack.Add(m_AccessibleVerticesCheckBox);
m_PredecessorVerticesCheckBox = new NCheckBox("Predecessor vertices");
m_PredecessorVerticesCheckBox.Padding = innerPadding;
m_PredecessorVerticesCheckBox.CheckedChanged += CheckBox_CheckedChanged;
vertexStack.Add(m_PredecessorVerticesCheckBox);
m_SuccessorVerticesCheckBox = new NCheckBox("Successor vertices");
m_SuccessorVerticesCheckBox.Padding = innerPadding;
m_SuccessorVerticesCheckBox.CheckedChanged += CheckBox_CheckedChanged;
vertexStack.Add(m_SuccessorVerticesCheckBox);
}
NStackPanel edgesStack = new NStackPanel();
{
m_FromVertexCheckBox = new NCheckBox("From vertex");
m_FromVertexCheckBox.CheckedChanged += CheckBox_CheckedChanged;
edgesStack.Add(m_FromVertexCheckBox);
m_ToVertexCheckBox = new NCheckBox("To vertex");
m_ToVertexCheckBox.CheckedChanged += CheckBox_CheckedChanged;
edgesStack.Add(m_ToVertexCheckBox);
}
NPairBox pairBox = new NPairBox(
new NGroupBox("Selected vertex highlights:", vertexStack),
new NGroupBox("Selected edge highlights:", edgesStack),
ENPairBoxRelation.Box1AboveBox2);
pairBox.VerticalPlacement = ENVerticalPlacement.Top;
return pairBox;
}
public string GetExampleDescription()
{
return @"
This example demonstrates how to use the NOV Diagram API to navigate between shapes. Use the controls on the right to specify which shape should be highlighted
and then select a shape from the drawing view. The shapes you have selected will be highlighted based on the selected shape.
";
}
///
/// 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++)
{
shapes[i].RemoveUserClass(HighlightedUserClassName);
}
}
private void HighlightShapes()
{
ClearHighlightedShapes();
// Highlight the selected type of shapes
NShape shape = m_DrawingView.ActivePage.Selection.FirstSelected as NShape;
if (shape == null)
return;
if (shape.ShapeType == ENShapeType.Shape2D)
{
// The selected shape is a 2D shape
if (m_IncomingEdgesCheckBox.Checked)
{
HighlightShapes(shape.GetIncomingShapes());
}
if (m_OutgoingEdgesCheckBox.Checked)
{
HighlightShapes(shape.GetOutgoingShapes());
}
if (m_SourceVerticesCheckBox.Checked)
{
HighlightShapes(shape.GetSourceShapes());
}
if (m_DestinationVerticesCheckBox.Checked)
{
HighlightShapes(shape.GetDestinationShapes());
}
if (m_PredecessorVerticesCheckBox.Checked)
{
HighlightShapes(shape.GetPredecessorShapes());
}
if (m_SuccessorVerticesCheckBox.Checked)
{
HighlightShapes(shape.GetSuccessorShapes());
}
}
else
{
// The selected shape is a 1D shape
if (m_FromVertexCheckBox.Checked)
{
shape.FromShape.AddUserClass(HighlightedUserClassName);
}
if (m_ToVertexCheckBox.Checked)
{
shape.ToShape.AddUserClass(HighlightedUserClassName);
}
}
}
private void HighlightShapes(NList shapes)
{
for (int i = 0; i < shapes.Count; i++)
{
shapes[i].AddUserClass(HighlightedUserClassName);
}
}
#endregion
#region Implementation
#endregion
#region Event Handlers
private void CheckBox_CheckedChanged(NValueChangeEventArgs arg)
{
m_bUpdatingCheckBoxes = true;
m_AllEdgesCheckBox.Checked = m_IncomingEdgesCheckBox.Checked && m_OutgoingEdgesCheckBox.Checked;
m_NeighbourVerticesCheckBox.Checked = m_SourceVerticesCheckBox.Checked && m_DestinationVerticesCheckBox.Checked;
m_AccessibleVerticesCheckBox.Checked = m_PredecessorVerticesCheckBox.Checked && m_SuccessorVerticesCheckBox.Checked;
m_bUpdatingCheckBoxes = false;
HighlightShapes();
}
private void AllEdgesCheckBox_CheckedChanged(NValueChangeEventArgs arg)
{
if (m_bUpdatingCheckBoxes)
return;
m_IncomingEdgesCheckBox.Checked = (bool)arg.NewValue;
m_OutgoingEdgesCheckBox.Checked = (bool)arg.NewValue;
}
private void NeighbourVerticesCheckBox_CheckedChanged(NValueChangeEventArgs arg)
{
if (m_bUpdatingCheckBoxes)
return;
m_SourceVerticesCheckBox.Checked = (bool)arg.NewValue;
m_DestinationVerticesCheckBox.Checked = (bool)arg.NewValue;
}
private void AccessibleVerticesCheckBox_CheckedChanged(NValueChangeEventArgs arg)
{
if (m_bUpdatingCheckBoxes)
return;
m_PredecessorVerticesCheckBox.Checked = (bool)arg.NewValue;
m_SuccessorVerticesCheckBox.Checked = (bool)arg.NewValue;
}
private void OnPageItemDeselected(NItemEventArgs arg)
{
ClearHighlightedShapes();
}
private void OnPageItemSelected(NItemEventArgs arg)
{
HighlightShapes();
}
#endregion
#region Fields
private NDrawingView m_DrawingView;
private NCheckBox m_AllEdgesCheckBox;
private NCheckBox m_IncomingEdgesCheckBox;
private NCheckBox m_OutgoingEdgesCheckBox;
private NCheckBox m_NeighbourVerticesCheckBox;
private NCheckBox m_SourceVerticesCheckBox;
private NCheckBox m_DestinationVerticesCheckBox;
private NCheckBox m_AccessibleVerticesCheckBox;
private NCheckBox m_PredecessorVerticesCheckBox;
private NCheckBox m_SuccessorVerticesCheckBox;
private NCheckBox m_FromVertexCheckBox;
private NCheckBox m_ToVertexCheckBox;
private bool m_bUpdatingCheckBoxes;
#endregion
#region Static Methods
#endregion
#region Constants
private const string HighlightedUserClassName = "Highlighted";
#endregion
#region Nested Types
#endregion
}
}