This example demonstrates how to create custom drawing tools. Click on a shape and the custom click select tool of this example will select it together with its ancestor and descendant shapes.

| Language: C# |
using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Diagram;
using Nevron.Nov.Diagram.DrawingTools;
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 - NCustomDrawingToolsExample";
// 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();
NDrawingView drawingView = drawingViewWithRibbon.View;
// Configure the selection
NDrawingSettings drawingSettings = drawingView.Settings;
drawingSettings.ShowSelectionBoxTrackers = false;
drawingSettings.ShowSelectionPinRotationTrackers = false;
// Change selection outline colors
drawingSettings.SelectedOutlineStroke = new NStroke(3, NColor.Firebrick);
drawingSettings.FormatAnchorOutlineStroke = new NStroke(4, NColor.Orange);
drawingSettings.AlignmentAnchorOutlineStroke = (NStroke)drawingSettings.SelectedOutlineStroke.DeepClone();
// Replace the default pointer tool with the ancestor and descendants pointer tool
drawingView.Interactor.ReplaceTool();
drawingView.Document.HistoryService.Pause();
try
{
InitDiagram(drawingView.Document);
// Set selection anchor modes
drawingView.ActivePage.Selection.FormatAnchorMode = ENSelectionAnchorMode.First;
drawingView.ActivePage.Selection.AlignmentAnchorMode = ENSelectionAnchorMode.First;
}
finally
{
drawingView.Document.HistoryService.Resume();
}
return drawingViewWithRibbon;
}
public NWidget CreateExampleControls()
{
return null;
}
public string GetExampleDescription()
{
return @"
This example demonstrates how to create custom drawing tools. Click on a shape and the custom click select tool of
this example will select it together with its ancestor and descendant shapes.
";
}
///
/// Gets the relevant documentation links for the example
///
///
public (string Name, string Url)[] GetDocumentationUrls()
{
return new[]
{
("Drawing View Tools", "https://www.nevron.com/docs/openvision/diagram-drawing-view-tools.html"),
("Interactors", "https://www.nevron.com/docs/openvision/ui-interactors.html"),
};
}
private void InitDiagram(NDrawingDocument drawingDocument)
{
const double width = 40;
const double height = 40;
const double distance = 80;
// Hide ports
drawingDocument.Content.ScreenVisibility.ShowPorts = false;
NBasicShapeFactory basicShapes = new NBasicShapeFactory();
NPage activePage = drawingDocument.Content.ActivePage;
int[] from = new int[] { 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 6 };
int[] to = new int[] { 2, 3, 4, 4, 5, 6, 7, 5, 9, 8, 9 };
NShape[] shapes = new NShape[9];
int vertexCount = shapes.Length, edgeCount = from.Length;
int i, j, count = vertexCount + edgeCount;
for (i = 0; i < count; i++)
{
if (i < vertexCount)
{
j = vertexCount % 2 == 0 ? i : i + 1;
shapes[i] = basicShapes.CreateShape(ENBasicShape.Rectangle);
if (vertexCount % 2 != 0 && i == 0)
{
shapes[i].SetBounds(new NRectangle(
(width + (distance * 1.5)) / 2,
distance + (j / 2) * (distance * 1.5),
width,
height));
}
else
{
shapes[i].SetBounds(new NRectangle(
width / 2 + (j % 2) * (distance * 1.5),
height + (j / 2) * (distance * 1.5),
width,
height));
}
activePage.Items.Add(shapes[i]);
}
else
{
NRoutableConnector edge = new NRoutableConnector();
edge.UserClass = NDR.StyleSheetNameConnectors;
activePage.Items.Add(edge);
edge.GlueBeginToShape(shapes[from[i - vertexCount] - 1]);
edge.GlueEndToShape(shapes[to[i - vertexCount] - 1]);
}
}
// arrange diagram
ArrangeDiagram(drawingDocument);
// fit active page
drawingDocument.Content.ActivePage.ZoomMode = ENZoomMode.Fit;
}
///
/// Arranges the shapes in the active page.
///
///
private void ArrangeDiagram(NDrawingDocument drawingDocument)
{
// get all top-level shapes that reside in the active page
NPage activePage = drawingDocument.Content.ActivePage;
NList shapes = activePage.GetShapes(false);
// create a layout context and use it to arrange the shapes using the current layout
NDrawingLayoutContext layoutContext = new NDrawingLayoutContext(drawingDocument, activePage);
NLayeredGraphLayout layout = new NLayeredGraphLayout();
layout.Arrange(shapes.CastAll