Translation Slaves 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.

Translation Slaves Example for WinForms Description

This example demonstrates the shape move slaves.

It is in many cases necessary to implement rigid connected shapes structures. In NOV Diagram this is achieved with the help of shape move slaves.

The move slaves of a shape are such shapes, which are moved together with the shape. The accumulation of the shape move slaves is recursive. For example: if a shape has to be moved, because it is a move slave, then its move slaves will also be moved.

To explore move slaves behavior just select a shape in the diagram and check the shapes, which must be moved when it is move. Then move the shape.

Note that the example will automatically highlight the move slaves of the currently selected shape.


Translation Slaves Example

C# Translation Slaves Example Code for Nevron Diagram for WinForms
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.Editors;
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 - NTranslationSlavesExample";

			// 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()
        {
            m_PropertyEditorHolder = new NContentHolder();
            return m_PropertyEditorHolder;
        }
        public string GetExampleDescription()
        {
            return @"

This example demonstrates the shape move slaves.

It is in many cases necessary to implement rigid connected shapes structures. In NOV Diagram this is achieved with the help of shape move slaves.

The move slaves of a shape are such shapes, which are moved together with the shape. The accumulation of the shape move slaves is recursive. For example: if a shape has to be moved, because it is a move slave, then its move slaves will also be moved.

To explore move slaves behavior just select a shape in the diagram and check the shapes, which must be moved when it is move. Then move the shape.

Note that the example will automatically highlight the move slaves of the currently selected shape.

"; } /// /// 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"), ("Snap and Glue", "https://www.nevron.com/docs/openvision/diagram-snap-and-glue.html"), }; } private void InitDiagram(NDrawingDocument drawingDocument) { NGraphTemplate template; // create rectangular grid template template = new NRectangularGridTemplate(); template.Origin = new NPoint(10, 23); template.VertexShape = ENBasicShape.Rectangle; template.EdgeUserClass = NDR.StyleSheetNameConnectors; template.Create(drawingDocument); // create tree template template = new NGenericTreeTemplate(); template.Origin = new NPoint(250, 23); template.VertexShape = ENBasicShape.Triangle; template.EdgeUserClass = NDR.StyleSheetNameConnectors; template.Create(drawingDocument); // create elliptical grid template template = new NEllipticalGridTemplate(); template.Origin = new NPoint(10, 250); template.VertexShape = ENBasicShape.Ellipse; template.EdgeUserClass = NDR.StyleSheetNameConnectors; template.Create(drawingDocument); // hook selection events NPageSelection selection = drawingDocument.Content.ActivePage.Selection; selection.Mode = Nov.UI.ENSelectionMode.Single; selection.Selected += OnSelectionSelected; selection.Deselected += OnSelectionDeselected; } #endregion #region Implementation /// /// Called when a diagram item has been selected. /// /// void OnSelectionSelected(NItemEventArgs arg) { NShape shape = arg.Item as NShape; if (shape == null) return; // create the shape move slaves property editor m_PropertyEditorHolder.Content = NDesigner.GetDesigner(shape).CreateMemberEditor(shape, NShape.MoveSlavesProperty); // subscribe for move slaves property changes. shape.AddEventHandler(NShape.MoveSlavesProperty.ValueChangedEvent, new NEventHandler(OnMoveSlavesPropertyChanged)); // highlight the shape current slaves HighlightSlaves(shape); } /// /// Called when a diagram item has been deselected. /// /// void OnSelectionDeselected(NItemEventArgs arg) { NShape shape = arg.Item as NShape; if (shape == null) return; // unhook move slaves property changed event shape.RemoveEventHandler(NShape.MoveSlavesProperty.ValueChangedEvent, new NEventHandler(OnMoveSlavesPropertyChanged)); // clear hightlights ClearHighlights(); // destroy property editor m_PropertyEditorHolder.Content = null; } /// /// Called when the MoveSlaves property of the currently selected shape has changed. /// /// void OnMoveSlavesPropertyChanged(NValueChangeEventArgs arg) { NShape shape = arg.TargetNode as NShape; if (shape == null) return; // highligh the shape move slaves HighlightSlaves(shape); } /// /// Clears the highlighting on all shapes in the active page. /// void ClearHighlights() { NList shapes = m_DrawingView.ActivePage.GetShapes(true); for (int i = 0; i < shapes.Count; i++) { NShape shape = shapes[i]; shape.Geometry.ClearValue(NGeometry.FillProperty); shape.Geometry.ClearValue(NGeometry.StrokeProperty); } } /// /// Highlights the move slaves of the specified shape /// /// void HighlightSlaves(NShape shape) { ClearHighlights(); NList shapes = shape.GetMoveSlaves(); for (int i = 0; i < shapes.Count; i++) { NShape cur = shapes[i]; cur.Geometry.Fill = new NColorFill(NColor.LightCoral); cur.Geometry.Stroke = new NStroke(2, NColor.DarkRed); } } #endregion #region Event Handlers #endregion #region Fields private NDrawingView m_DrawingView; private NContentHolder m_PropertyEditorHolder; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Translation Slaves Example for WinForms Solution

VB.NET Translation Slaves Example Code for Nevron Diagram for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.DataStructures
Imports Nevron.Nov.Diagram
Imports Nevron.Nov.Diagram.Shapes
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Editors
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 - NTranslationSlavesExample"

        ' 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
            m_PropertyEditorHolder = New NContentHolder()
            Return m_PropertyEditorHolder
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates the shape move slaves.

It is in many cases necessary to implement rigid connected shapes structures. In NOV Diagram this is achieved with the help of shape move slaves.

The move slaves of a shape are such shapes, which are moved together with the shape. The accumulation of the shape move slaves is recursive. For example: if a shape has to be moved, because it is a move slave, then its move slaves will also be moved.

To explore move slaves behavior just select a shape in the diagram and check the shapes, which must be moved when it is move. Then move the shape.

Note that the example will automatically highlight the move slaves of the currently selected shape.

" 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"), ("Snap and Glue", "https://www.nevron.com/docs/openvision/diagram-snap-and-glue.html")} End Function Private Sub InitDiagram(drawingDocument As NDrawingDocument) Dim template As NGraphTemplate ' create rectangular grid template template = New NRectangularGridTemplate() template.Origin = New NPoint(10, 23) template.VertexShape = ENBasicShape.Rectangle template.EdgeUserClass = NDR.StyleSheetNameConnectors template.Create(drawingDocument) ' create tree template template = New NGenericTreeTemplate() template.Origin = New NPoint(250, 23) template.VertexShape = ENBasicShape.Triangle template.EdgeUserClass = NDR.StyleSheetNameConnectors template.Create(drawingDocument) ' create elliptical grid template template = New NEllipticalGridTemplate() template.Origin = New NPoint(10, 250) template.VertexShape = ENBasicShape.Ellipse template.EdgeUserClass = NDR.StyleSheetNameConnectors template.Create(drawingDocument) ' hook selection events Dim selection = drawingDocument.Content.ActivePage.Selection selection.Mode = ENSelectionMode.[Single] selection.Selected += AddressOf OnSelectionSelected selection.Deselected += AddressOf OnSelectionDeselected End Sub #End Region #Region "Implementation" ''' ''' Called when a diagram item has been selected. ''' ''' Private Sub OnSelectionSelected(arg As NItemEventArgs(Of NPageItem)) Dim shape As NShape = TryCast(arg.Item, NShape) If shape Is Nothing Then Return ' create the shape move slaves property editor m_PropertyEditorHolder.Content = NDesigner.GetDesigner(shape).CreateMemberEditor(shape, NShape.MoveSlavesProperty) ' subscribe for move slaves property changes. shape.AddEventHandler(NShape.MoveSlavesProperty.ValueChangedEvent, New NEventHandler(Of NValueChangeEventArgs)(AddressOf OnMoveSlavesPropertyChanged)) ' highlight the shape current slaves HighlightSlaves(shape) End Sub ''' ''' Called when a diagram item has been deselected. ''' ''' Private Sub OnSelectionDeselected(arg As NItemEventArgs(Of NPageItem)) Dim shape As NShape = TryCast(arg.Item, NShape) If shape Is Nothing Then Return ' unhook move slaves property changed event shape.RemoveEventHandler(NShape.MoveSlavesProperty.ValueChangedEvent, New NEventHandler(Of NValueChangeEventArgs)(AddressOf OnMoveSlavesPropertyChanged)) ' clear hightlights ClearHighlights() ' destroy property editor m_PropertyEditorHolder.Content = Nothing End Sub ''' ''' Called when the MoveSlaves property of the currently selected shape has changed. ''' ''' Private Sub OnMoveSlavesPropertyChanged(arg As NValueChangeEventArgs) Dim shape As NShape = TryCast(arg.TargetNode, NShape) If shape Is Nothing Then Return ' highligh the shape move slaves HighlightSlaves(shape) End Sub ''' ''' Clears the highlighting on all shapes in the active page. ''' Private Sub ClearHighlights() Dim shapes = m_DrawingView.ActivePage.GetShapes(True) For i = 0 To shapes.Count - 1 Dim shape = shapes(i) shape.Geometry.ClearValue(NGeometry.FillProperty) shape.Geometry.ClearValue(NGeometry.StrokeProperty) Next End Sub ''' ''' Highlights the move slaves of the specified shape ''' ''' Private Sub HighlightSlaves(shape As NShape) ClearHighlights() Dim shapes As NList(Of NShape) = shape.GetMoveSlaves() For i = 0 To shapes.Count - 1 Dim cur = shapes(i) cur.Geometry.Fill = New NColorFill(NColor.LightCoral) cur.Geometry.Stroke = New NStroke(2, NColor.DarkRed) Next End Sub #End Region #Region "Event Handlers" #End Region #Region "Fields" Private m_DrawingView As NDrawingView Private m_PropertyEditorHolder As NContentHolder #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) Translation Slaves Example for WinForms Solution

Related Documentation Links