Custom Shapes Example for WinForms

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

Custom Shapes Example for WinForms Description

Demonstrates how to create custom shapes by combining shapes and also how to create custom formula shapes with control points.

The Coffee Cup shape is created by grouping shapes with different fill styles inside a group. By using this approach you can create shapes that mix different fill and stroke styles.

The Trapezoid Shape is a replica of the Visio Trapezoid Smart Shape. It demonstrates that with NOV Diagram you can replicate the Shape Sheet behavior of almost any Visio shape. Select the shape and move its control point to modify the trapezoid strip width. Note that the geometry, left and right ports and XControl point behavior of this shape are driven by formula expressions.


Custom Shapes Example

C# Custom Shapes Example Code for Nevron Diagram for WinForms
Language: C#

using Nevron.Nov;
using Nevron.Nov.Diagram;
using Nevron.Nov.Diagram.Batches;
using Nevron.Nov.Diagram.Expressions;
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 - NCustomShapesExample";

			// 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()
        {
            // Create a simple drawing
            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()
        {
            return null;
        }
        public string GetExampleDescription()
        {
            return @"

Demonstrates how to create custom shapes by combining shapes and also how to create custom formula shapes with control points.

The Coffee Cup shape is created by grouping shapes with different fill styles inside a group. By using this approach you can create shapes that mix different fill and stroke styles.

The Trapezoid Shape is a replica of the Visio Trapezoid Smart Shape. It demonstrates that with NOV Diagram you can replicate the Shape Sheet behavior of almost any Visio shape. Select the shape and move its control point to modify the trapezoid strip width. Note that the geometry, left and right ports and XControl point behavior of this shape are driven by formula expressions.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Shapes", "https://www.nevron.com/docs/openvision/diagram-shapes.html"), ("Geometry", "https://www.nevron.com/docs/openvision/diagram-shape-geometry.html"), ("Controls", "https://www.nevron.com/docs/openvision/diagram-controls.html"), }; } private void InitDiagram(NDrawingDocument drawingDocument) { m_DrawingView.ActivePage.Interaction.GluingShapes += Interaction_GluingShapes; // create the coffee cup NShape coffeeCup = CreateCoffeeCupShape(); coffeeCup.SetBounds(new NRectangle(50, 50, 100, 200)); drawingDocument.Content.ActivePage.Items.Add(coffeeCup); NShape trapedzoid = CreateTrapedzoidShape(); trapedzoid.SetBounds(new NRectangle(200, 150, 100, 100)); drawingDocument.Content.ActivePage.Items.Add(trapedzoid); } /// /// Creates a custom shape that is essentially a group consisting of three other shapes each with different filling. /// You need to use groups to have shapes that mix different fill, or stroke styles. /// /// private NShape CreateCoffeeCupShape() { // create the points and paths from which the shape consits NPoint[] cupPoints = new NPoint[] { new NPoint(45, 268), new NPoint(63, 331), new NPoint(121, 331), new NPoint(140, 268) }; NGraphicsPath handleGraphicsPath = new NGraphicsPath(); handleGraphicsPath.AddClosedCurve(new NPoint[] { new NPoint(175, 295), new NPoint(171, 278), new NPoint(140, 283), new NPoint(170, 290), new NPoint(128, 323) }, 1); NGraphicsPath steamGraphicsPath = new NGraphicsPath(); steamGraphicsPath.AddCubicBeziers(new NPoint[] { new NPoint(92, 270), new NPoint(53, 163), new NPoint(145, 160), new NPoint(86, 50), new NPoint(138, 194), new NPoint(45, 145), new NPoint(92, 270) }); steamGraphicsPath.CloseFigure(); // calculate some bounds NRectangle handleBounds = handleGraphicsPath.ExactBounds; NRectangle cupBounds = NGeometry2D.GetBounds(cupPoints); NRectangle steamBounds = steamGraphicsPath.ExactBounds; NRectangle geometryBounds = NRectangle.Union(cupBounds, handleBounds, steamBounds); // normalize the points and paths by transforming them to relative coordinates NRectangle normalRect = new NRectangle(0, 0, 1, 1); NMatrix transform = NMatrix.CreateBoundsStretchMatrix(geometryBounds, normalRect); transform.TransformPoints(cupPoints); handleGraphicsPath.Transform(transform); steamGraphicsPath.Transform(transform); // create the cup shape NDrawPolygon cupPolygon = new NDrawPolygon(normalRect, cupPoints); cupPolygon.Relative = true; NShape cupShape = new NShape(); cupShape.Init2DShape(); cupShape.Geometry.Fill = new NColorFill(NColor.Brown); cupShape.Geometry.Add(cupPolygon); cupShape.SetBounds(geometryBounds); // create the cup handle NDrawPath handlePath = new NDrawPath(normalRect, handleGraphicsPath); handlePath.Relative = true; NShape handleShape = new NShape(); handleShape.Init2DShape(); handleShape.Geometry.Fill = new NColorFill(NColor.LightSalmon); handleShape.Geometry.Add(handlePath); handleShape.SetBounds(geometryBounds); // create the steam NDrawPath steamPath = new NDrawPath(steamGraphicsPath.Bounds, steamGraphicsPath); steamPath.Relative = true; NShape steamShape = new NShape(); steamShape.Init2DShape(); steamShape.Geometry.Fill = new NColorFill(new NColor(50, 122, 122, 122)); steamShape.Geometry.Add(steamPath); steamShape.SetBounds(geometryBounds); // group the shapes as a single group NGroup group; NBatchGroup batch = new NBatchGroup(m_DrawingView.Document); batch.Build(cupShape, handleShape, steamShape); batch.Group(null, out group); // alter some properties of the group group.SelectionMode = ENGroupSelectionMode.GroupOnly; group.SnapToShapes = false; return group; } /// /// Creates a custom shape that is a replica of the Visio Trapedzoid shape. With NOV diagram you can replicate the smart behavior of any Visio smart shape. /// /// private NShape CreateTrapedzoidShape() { NShape shape = new NShape(); shape.Init2DShape(); // add controls NControl control = new NControl(); control.SetFx(NControl.XProperty, new NShapeWidthFactorFx(0.3)); control.Y = 0.0d; control.SetFx(NControl.XBehaviorProperty, string.Format("IF(X

Download a fully functional C# (.cs) Custom Shapes Example for WinForms Solution

VB.NET Custom Shapes Example Code for Nevron Diagram for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Diagram
Imports Nevron.Nov.Diagram.Batches
Imports Nevron.Nov.Diagram.Expressions
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Forms

Public Class Form1

#Region "Constructors"

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' Set form title
        Text = "NOV WinForms - NCustomShapesExample"

        ' 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)
        host.Dock = DockStyle.Fill
        Controls.Add(host)
    End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            ' Create a simple drawing
            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
            Return Nothing
        End Function
        Public Function GetExampleDescription() As String
            Return "

Demonstrates how to create custom shapes by combining shapes and also how to create custom formula shapes with control points.

The Coffee Cup shape is created by grouping shapes with different fill styles inside a group. By using this approach you can create shapes that mix different fill and stroke styles.

The Trapezoid Shape is a replica of the Visio Trapezoid Smart Shape. It demonstrates that with NOV Diagram you can replicate the Shape Sheet behavior of almost any Visio shape. Select the shape and move its control point to modify the trapezoid strip width. Note that the geometry, left and right ports and XControl point behavior of this shape are driven by formula expressions.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Shapes", "https://www.nevron.com/docs/openvision/diagram-shapes.html"), ("Geometry", "https://www.nevron.com/docs/openvision/diagram-shape-geometry.html"), ("Controls", "https://www.nevron.com/docs/openvision/diagram-controls.html")} End Function Private Sub InitDiagram(drawingDocument As NDrawingDocument) Me.m_DrawingView.ActivePage.Interaction.GluingShapes += AddressOf Interaction_GluingShapes ' create the coffee cup Dim coffeeCup As NShape = CreateCoffeeCupShape() coffeeCup.SetBounds(New NRectangle(50, 50, 100, 200)) drawingDocument.Content.ActivePage.Items.Add(coffeeCup) Dim trapedzoid As NShape = CreateTrapedzoidShape() trapedzoid.SetBounds(New NRectangle(200, 150, 100, 100)) drawingDocument.Content.ActivePage.Items.Add(trapedzoid) End Sub ''' ''' Creates a custom shape that is essentially a group consisting of three other shapes each with different filling. ''' You need to use groups to have shapes that mix different fill, or stroke styles. ''' ''' Private Function CreateCoffeeCupShape() As NShape ' create the points and paths from which the shape consits Dim cupPoints As NPoint() = New NPoint() {New NPoint(45, 268), New NPoint(63, 331), New NPoint(121, 331), New NPoint(140, 268)} Dim handleGraphicsPath As NGraphicsPath = New NGraphicsPath() handleGraphicsPath.AddClosedCurve(New NPoint() {New NPoint(175, 295), New NPoint(171, 278), New NPoint(140, 283), New NPoint(170, 290), New NPoint(128, 323)}, 1) Dim steamGraphicsPath As NGraphicsPath = New NGraphicsPath() steamGraphicsPath.AddCubicBeziers(New NPoint() {New NPoint(92, 270), New NPoint(53, 163), New NPoint(145, 160), New NPoint(86, 50), New NPoint(138, 194), New NPoint(45, 145), New NPoint(92, 270)}) steamGraphicsPath.CloseFigure() ' calculate some bounds Dim handleBounds As NRectangle = handleGraphicsPath.ExactBounds Dim cupBounds = NGeometry2D.GetBounds(cupPoints) Dim steamBounds As NRectangle = steamGraphicsPath.ExactBounds Dim geometryBounds = NRectangle.Union(cupBounds, handleBounds, steamBounds) ' normalize the points and paths by transforming them to relative coordinates Dim normalRect As NRectangle = New NRectangle(0, 0, 1, 1) Dim transform = NMatrix.CreateBoundsStretchMatrix(geometryBounds, normalRect) transform.TransformPoints(cupPoints) handleGraphicsPath.Transform(transform) steamGraphicsPath.Transform(transform) ' create the cup shape Dim cupPolygon As NDrawPolygon = New NDrawPolygon(normalRect, cupPoints) cupPolygon.Relative = True Dim cupShape As NShape = New NShape() cupShape.Init2DShape() cupShape.Geometry.Fill = New NColorFill(NColor.Brown) cupShape.Geometry.Add(cupPolygon) cupShape.SetBounds(geometryBounds) ' create the cup handle Dim handlePath As NDrawPath = New NDrawPath(normalRect, handleGraphicsPath) handlePath.Relative = True Dim handleShape As NShape = New NShape() handleShape.Init2DShape() handleShape.Geometry.Fill = New NColorFill(NColor.LightSalmon) handleShape.Geometry.Add(handlePath) handleShape.SetBounds(geometryBounds) ' create the steam Dim steamPath As NDrawPath = New NDrawPath(steamGraphicsPath.Bounds, steamGraphicsPath) steamPath.Relative = True Dim steamShape As NShape = New NShape() steamShape.Init2DShape() steamShape.Geometry.Fill = New NColorFill(New NColor(50, 122, 122, 122)) steamShape.Geometry.Add(steamPath) steamShape.SetBounds(geometryBounds) ' group the shapes as a single group Dim group As NGroup Dim batch As NBatchGroup(Of NShape) = New NBatchGroup(Of NShape)(m_DrawingView.Document) batch.Build(cupShape, handleShape, steamShape) batch.Group(Nothing, group) ' alter some properties of the group group.SelectionMode = ENGroupSelectionMode.GroupOnly group.SnapToShapes = False Return group End Function ''' ''' Creates a custom shape that is a replica of the Visio Trapedzoid shape. With NOV diagram you can replicate the smart behavior of any Visio smart shape. ''' ''' Private Function CreateTrapedzoidShape() As NShape Dim shape As NShape = New NShape() shape.Init2DShape() ' add controls Dim control As NControl = New NControl() control.SetFx(NControl.XProperty, New NShapeWidthFactorFx(0.3)) control.Y = 0.0R control.SetFx(NControl.XBehaviorProperty, String.Format("IF(X

Download a fully functional VB.NET (.vb) Custom Shapes Example for WinForms Solution

Related Documentation Links