Custom Drawing Tools Example for Blazor

Nevron NOV Diagram for Blazor ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All Blazor Diagram examples are also available as a stand-alone Visual Studio solution in C#, which you can download from the links below.

Custom Drawing Tools Example for Blazor Description

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.


Custom Drawing Tools Example

C# Custom Drawing Tools Example Code for Nevron Diagram for Blazor
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(), layoutContext); // size the page to the content size activePage.SizeToContent(); } #endregion #region Implementation /// /// Selects the ancestors and descendants of the given shape. /// /// private void SelectShapeAncestorsAndDescendants(NShape shape) { if (shape.ShapeType == ENShapeType.Shape2D) { SelectShape(shape, true, true); } else { SelectConnector(shape, true, true); } } private void SelectShape(NShape shape, bool selectIncomingConnectors, bool selectOutgoingConnectors) { if (shape == null) return; NPage page = shape.OwnerPage; NPageSelection selection = page.Selection; if (!selection.IsSelected(shape)) { selection.MultiSelect(shape); } if (selectIncomingConnectors) { // Select incoming connectors NList incomingConnectors = shape.GetIncomingShapes(); for (int i = 0; i < incomingConnectors.Count; i++) { SelectConnector(incomingConnectors[i], true, false); } } if (selectOutgoingConnectors) { // Select outgonig connectors NList outgoingConnectors = shape.GetOutgoingShapes(); for (int i = 0; i < outgoingConnectors.Count; i++) { SelectConnector(outgoingConnectors[i], false, true); } } } private void SelectConnector(NShape connector, bool selectFromShape, bool selectToShape) { NPage page = connector.OwnerPage; NPageSelection selection = page.Selection; if (!selection.IsSelected(connector)) { selection.MultiSelect(connector); } if (selectFromShape) { SelectShape(connector.FromShape, true, false); } if (selectToShape) { SelectShape(connector.ToShape, false, true); } } #endregion #region Event Handlers #endregion #region Fields #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types internal class AncestorsAndDescendantsPointerTool : NCompositeTool { #region Constructors /// /// Default constructor. /// public AncestorsAndDescendantsPointerTool() { AddEnabledTool(new AncestorsAndDescendantsClickSelectTool()); } /// /// Static constructor. /// static AncestorsAndDescendantsPointerTool() { AncestorsAndDescendantsPointerToolSchema = NSchema.Create(typeof(AncestorsAndDescendantsPointerTool), NCompositeToolSchema); } #endregion #region Schema /// /// Schema associated with AncestorsAndDescendantsPointerTool. /// public static readonly NSchema AncestorsAndDescendantsPointerToolSchema; #endregion } internal class AncestorsAndDescendantsClickSelectTool : NClickSelectTool { #region Constructors /// /// Default constructor. /// public AncestorsAndDescendantsClickSelectTool() { } /// /// Static constructor. /// static AncestorsAndDescendantsClickSelectTool() { AncestorsAndDescendantsClickSelectToolSchema = NSchema.Create(typeof(AncestorsAndDescendantsClickSelectTool), NClickSelectToolSchema); } #endregion #region Protected Overrides protected override void ClickSelect(INPointerEventArgs args) { NPageSelection selection = OwnerDrawingView?.ActivePage?.Selection; if (selection != null) { selection.DeselectAll(); } base.ClickSelect(args); if (args.TargetNode == null) return; // Make sure only 1 item is selected NShape clickedShape = args.TargetNode.GetFirstAncestor(); if (clickedShape != null) { SelectShapeAncestorsAndDescendants(clickedShape); } } protected override void DrillDownSelect(INPointerEventArgs args) { base.DrillDownSelect(args); // Get the page NPage page = OwnerDrawingView.ActivePage; if (page == null) return; // Get the page selection NPageSelection selection = page.Selection; // Make sure only 1 shape is selected if (selection.SelectedCount == 1 && selection.FirstSelected is NShape shape) { SelectShapeAncestorsAndDescendants(shape); } } #endregion #region Implementation /// /// Selects the ancestors and descendants of the given shape. /// /// private void SelectShapeAncestorsAndDescendants(NShape shape) { if (shape.ShapeType == ENShapeType.Shape2D) { SelectShape(shape, true, true); } else { SelectConnector(shape, true, true); } } private void SelectShape(NShape shape, bool selectIncomingConnectors, bool selectOutgoingConnectors) { if (shape == null) return; NPage page = shape.OwnerPage; NPageSelection selection = page.Selection; if (!selection.IsSelected(shape)) { selection.MultiSelect(shape); } if (selectIncomingConnectors) { // Select incoming connectors NList incomingConnectors = shape.GetIncomingShapes(); for (int i = 0; i < incomingConnectors.Count; i++) { SelectConnector(incomingConnectors[i], true, false); } } if (selectOutgoingConnectors) { // Select outgonig connectors NList outgoingConnectors = shape.GetOutgoingShapes(); for (int i = 0; i < outgoingConnectors.Count; i++) { SelectConnector(outgoingConnectors[i], false, true); } } } private void SelectConnector(NShape connector, bool selectFromShape, bool selectToShape) { NPage page = connector.OwnerPage; NPageSelection selection = page.Selection; if (!selection.IsSelected(connector)) { selection.MultiSelect(connector); } if (selectFromShape) { SelectShape(connector.FromShape, true, false); } if (selectToShape) { SelectShape(connector.ToShape, false, true); } } #endregion #region Schema /// /// Schema associated with AncestorsAndDescendantsClickSelectTool. /// public static readonly NSchema AncestorsAndDescendantsClickSelectToolSchema; #endregion } #endregion } }

Download a fully functional C# (.cs) Custom Drawing Tools Example for Blazor Solution

Related Documentation Links