Groups Example Example for WPF

Nevron NOV Diagram for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF 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.

Groups Example Example for WPF Description

Demonstrates how to create and use groups.
Groups Example

C# Groups Example Example Code for Nevron Diagram for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Diagram;
using Nevron.Nov.Diagram.Batches;
using Nevron.Nov.Diagram.Shapes;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.IO;
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 - NGroupsExample";

			// 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 and use groups.";
		}

		/// 
		/// Gets the relevant documentation links for the example
		/// 
		/// 
		public (string Name, string Url)[] GetDocumentationUrls()
		{
			return new[]
			{
				("Groups", "https://www.nevron.com/docs/openvision/diagram-groups.html"),
				("Shapes", "https://www.nevron.com/docs/openvision/diagram-shapes.html"),
			};
		}

		private void InitDiagram(NDrawingDocument drawingDocument)
		{
			// create networks
			NGroup network1 = CreateNetwork(new NPoint(200, 20), "Network 1");
			NGroup network2 = CreateNetwork(new NPoint(400, 250), "Network 2");
			NGroup network3 = CreateNetwork(new NPoint(20, 250), "Network 3");
			NGroup network4 = CreateNetwork(new NPoint(200, 475), "Network 4");

			// connect networks
			ConnectNetworks(network1, network2);
			ConnectNetworks(network1, network3);
			ConnectNetworks(network1, network4);

			// hide some elements
			drawingDocument.Content.ScreenVisibility.ShowPorts = false;
			drawingDocument.Content.ScreenVisibility.ShowGrid = false;
		}

		#endregion

		#region Implementation

		protected NGroup CreateNetwork(NPoint location, string labelText)
		{
			bool rectValid = false;
			NRectangle rect = NRectangle.Zero;
			NPage activePage = m_DrawingView.ActivePage;

			NList shapes = activePage.GetShapes(false);
			for (int i = 0; i < shapes.Count; i++)
			{
				NRectangle bounds = shapes[i].GetAlignBoxInPage();
				if (rectValid)
				{
					rect = NRectangle.Union(rect, bounds);
				}
				else
				{
					rect = bounds;
					rectValid = true;
				}
			}

			if (rectValid)
			{
				// determine how much is out of layout area
			}

			// create computer1
			NShape computer1 = CreateComputer();
			computer1.SetBounds(0, 0, computer1.Width, computer1.Height);

			// create computer2
			NShape computer2 = CreateComputer();
			computer2.SetBounds(150, 0, computer2.Width, computer2.Height);

			// create computer3
			NShape computer3 = CreateComputer();
			computer3.SetBounds(75, 120, computer3.Width, computer3.Height);

			// create the group that contains the comptures
			NGroup group = new NGroup();
			NBatchGroup batchGroup = new NBatchGroup(m_DrawingView.Document);
			batchGroup.Build(computer1, computer2, computer3);
			batchGroup.Group(m_DrawingView.ActivePage, out group);

			// connect the computers in the network
			ConnectComputers(computer1, computer2, group);
			ConnectComputers(computer2, computer3, group);
			ConnectComputers(computer3, computer1, group);

			// insert a frame
			NDrawRectangle drawRect = new NDrawRectangle(0, 0, 1, 1);
			drawRect.Relative = true;
			group.Geometry.Add(drawRect);

			// change group fill style
			group.Geometry.Fill = new NStockGradientFill(ENGradientStyle.FromCenter, ENGradientVariant.Variant2, NColor.Gainsboro, NColor.White);

			// reposition and resize the group
			group.SetBounds(location.X, location.Y, group.Width, group.Height);

			// set label text
			group.TextBlock.Text = labelText;

			return group;
		}

		protected NShape CreateComputer()
		{
			NShape computerShape = m_FlowchartShapes.CreateShape(ENFlowchartShape.Database);
			return computerShape;
		}

		private void ConnectNetworks(NGroup fromNetwork, NGroup toNetwork)
		{
			NShape lineShape = m_ConnectorShapes.CreateShape(ENConnectorShape.Line);
			lineShape.GlueBeginToShape(fromNetwork);
			lineShape.GlueEndToShape(toNetwork);
			m_DrawingView.ActivePage.Items.Add(lineShape);
		}

		private void ConnectComputers(NShape fromComputer, NShape toComputer, NGroup group)
		{
			NShape lineShape = m_ConnectorShapes.CreateShape(ENConnectorShape.Line);
			lineShape.GlueBeginToShapeBoxIntersection(fromComputer);
			lineShape.GlueEndToShapeBoxIntersection(toComputer);
			group.Shapes.Add(lineShape);
		}

		#endregion

		#region Event Handlers
		#endregion

		#region Fields

		private NDrawingView m_DrawingView;
		private NConnectorShapeFactory m_ConnectorShapes = new NConnectorShapeFactory();
		private NFlowchartShapeFactory m_FlowchartShapes = new NFlowchartShapeFactory();

		#endregion

		#region Static Methods
		#endregion

		#region Constants
		#endregion

		#region Nested Types
		#endregion
	}
}

Download a fully functional C# (.cs) Groups Example Example for WPF Solution

VB.NET Groups Example Example Code for Nevron Diagram for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Diagram
Imports Nevron.Nov.Diagram.Batches
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 - NGroupsExample"

		' 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
            ' 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 and use groups."
        End Function

        ''' 
        ''' Gets the relevant documentation links for the example
        ''' 
        ''' 
        Public Function GetDocumentationUrls() As (String, String)()
            Return {("Groups", "https://www.nevron.com/docs/openvision/diagram-groups.html"), ("Shapes", "https://www.nevron.com/docs/openvision/diagram-shapes.html")}
        End Function

        Private Sub InitDiagram(drawingDocument As NDrawingDocument)
            ' create networks
            Dim network1 As NGroup = CreateNetwork(New NPoint(200, 20), "Network 1")
            Dim network2 As NGroup = CreateNetwork(New NPoint(400, 250), "Network 2")
            Dim network3 As NGroup = CreateNetwork(New NPoint(20, 250), "Network 3")
            Dim network4 As NGroup = CreateNetwork(New NPoint(200, 475), "Network 4")

            ' connect networks
            ConnectNetworks(network1, network2)
            ConnectNetworks(network1, network3)
            ConnectNetworks(network1, network4)

            ' hide some elements
            drawingDocument.Content.ScreenVisibility.ShowPorts = False
            drawingDocument.Content.ScreenVisibility.ShowGrid = False
        End Sub

#End Region

#Region "Implementation"

        Protected Function CreateNetwork(location As NPoint, labelText As String) As NGroup
            Dim rectValid = False
            Dim rect = NRectangle.Zero
            Dim activePage = m_DrawingView.ActivePage

            Dim shapes = activePage.GetShapes(False)
            For i = 0 To shapes.Count - 1
                Dim bounds As NRectangle = shapes(CInt(i)).GetAlignBoxInPage()
                If rectValid Then
                    rect = NRectangle.Union(rect, bounds)
                Else
                    rect = bounds
                    rectValid = True
                End If
            Next

            If rectValid Then
                ' determine how much is out of layout area
            End If

            ' create computer1
            Dim computer1 As NShape = CreateComputer()
            computer1.SetBounds(0, 0, computer1.Width, computer1.Height)

            ' create computer2
            Dim computer2 As NShape = CreateComputer()
            computer2.SetBounds(150, 0, computer2.Width, computer2.Height)

            ' create computer3
            Dim computer3 As NShape = CreateComputer()
            computer3.SetBounds(75, 120, computer3.Width, computer3.Height)

            ' create the group that contains the comptures
            Dim group As NGroup = New NGroup()
            Dim batchGroup As NBatchGroup(Of NShape) = New NBatchGroup(Of NShape)(m_DrawingView.Document)
            batchGroup.Build(computer1, computer2, computer3)
            batchGroup.Group(m_DrawingView.ActivePage, group)

            ' connect the computers in the network
            ConnectComputers(computer1, computer2, group)
            ConnectComputers(computer2, computer3, group)
            ConnectComputers(computer3, computer1, group)

            ' insert a frame
            Dim drawRect As NDrawRectangle = New NDrawRectangle(0, 0, 1, 1)
            drawRect.Relative = True
            group.Geometry.Add(drawRect)

            ' change group fill style
            group.Geometry.Fill = New NStockGradientFill(ENGradientStyle.FromCenter, ENGradientVariant.Variant2, NColor.Gainsboro, NColor.White)

            ' reposition and resize the group
            group.SetBounds(location.X, location.Y, group.Width, group.Height)

            ' set label text
            group.TextBlock.Text = labelText

            Return group
        End Function

        Protected Function CreateComputer() As NShape
            Dim computerShape = m_FlowchartShapes.CreateShape(ENFlowchartShape.Database)
            Return computerShape
        End Function

        Private Sub ConnectNetworks(fromNetwork As NGroup, toNetwork As NGroup)
            Dim lineShape = m_ConnectorShapes.CreateShape(ENConnectorShape.Line)
            lineShape.GlueBeginToShape(fromNetwork)
            lineShape.GlueEndToShape(toNetwork)
            m_DrawingView.ActivePage.Items.Add(lineShape)
        End Sub

        Private Sub ConnectComputers(fromComputer As NShape, toComputer As NShape, group As NGroup)
            Dim lineShape = m_ConnectorShapes.CreateShape(ENConnectorShape.Line)
            lineShape.GlueBeginToShapeBoxIntersection(fromComputer)
            lineShape.GlueEndToShapeBoxIntersection(toComputer)
            group.Shapes.Add(lineShape)
        End Sub

#End Region

#Region "Event Handlers"
#End Region

#Region "Fields"

        Private m_DrawingView As NDrawingView
        Private m_ConnectorShapes As NConnectorShapeFactory = New NConnectorShapeFactory()
        Private m_FlowchartShapes As NFlowchartShapeFactory = New NFlowchartShapeFactory()

#End Region

#Region "Static Methods"
#End Region

#Region "Constants"
#End Region

#Region "Nested Types"
#End Region

End Class

Download a fully functional VB.NET (.vb) Groups Example Example for WPF Solution

Related Documentation Links