This example demonstrates how to take advantage of inward/outward ports in order to build a pipeline editor application. Drag and drop pipes from the pipes panel into the drawing area. When a pipe is near other pipe it sticks to its end if possible or automatically rotates if needed and then sticks to the other pipe. Use the end pipe symbols (triangles) to mark pipe ends.

| Language: C# |
using Nevron.Nov;
using Nevron.Nov.Diagram;
using Nevron.Nov.Diagram.Themes;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;
namespace NovWinFormsProject
{
public partial class Form1 : Form
{
#region Constructors
public Form1()
{
InitializeComponent();
// Set form title
Text = "NOV WinForms - NPipelineEditorExample";
// 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()
{
m_DrawingView = new NDrawingView();
NPage page = m_DrawingView.ActivePage;
page.Items.Add(CreateTitleShape("Pipeline Editor"));
return m_DrawingView;
}
public NWidget CreateExampleControls()
{
NLibraryView libraryView = new NLibraryView();
libraryView.Content = CreateLibrary();
return libraryView;
}
public string GetExampleDescription()
{
return @"
This example demonstrates how to take advantage of inward/outward ports in order to build a pipeline editor application.
Drag and drop pipes from the pipes panel into the drawing area. When a pipe is near other pipe it sticks to its end if possible
or automatically rotates if needed and then sticks to the other pipe. Use the end pipe symbols (triangles) to mark pipe ends.
";
}
///
/// Gets the relevant documentation links for the example
///
///
public (string Name, string Url)[] GetDocumentationUrls()
{
return new[]
{
("Controls", "https://www.nevron.com/docs/openvision/diagram-controls.html"),
("Routable Connectors", "https://www.nevron.com/docs/openvision/diagram-routable-connectors.html"),
("Shapes", "https://www.nevron.com/docs/openvision/diagram-shapes.html"),
};
}
#endregion
#region Implementation
///
/// Creates the Title shape.
///
///
///
private NShape CreateTitleShape(string title)
{
NDrawingTheme theme = NDrawingTheme.MyDrawNature;
NShape titleShape = new NShape();
titleShape.SetBounds(0, 0, 500, 50);
titleShape.Text = title;
titleShape.SetProtectionMask(ENDiagramItemOperationMask.All);
NTextBlock titleTextBlock = (NTextBlock)titleShape.TextBlock;
titleTextBlock.ResizeMode = ENTextBlockResizeMode.ShapeSize;
titleTextBlock.FontSize = 28;
titleTextBlock.FontStyleBold = true;
// Set theme-based colors to the title text, so that it changes when the user changes the theme
NColor strokeColor = theme.ColorPalette.Variants[0][0];
strokeColor.Tag = new NThemeVariantColorInfo(0);
titleTextBlock.Stroke = new NStroke(strokeColor);
NColor fillColor = theme.ColorPalette.Variants[0][4];
fillColor.Tag = new NThemeVariantColorInfo(4);
titleTextBlock.Fill = new NColorFill(fillColor);
// Alternatively, you can also use fixed colors (uncomment the 2 lines below)
//titleTextBlock.Stroke = new NStroke(NColor.DarkBlue);
//titleTextBlock.Fill = new NColorFill(NColor.LightBlue);
// Set an expression to center the title horizontally in the page
titleShape.SetFx(NShape.PinXProperty, new NFormulaFx("$ParentSheet.X + $ParentSheet.Width / 2", true));
return titleShape;
}
private NLibrary CreateLibrary()
{
NLibrary library = new NLibrary();
library.Items.Add(new NLibraryItem(CreateHorizontalPipe(), "Horizontal Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateVerticalPipe(), "Vertical Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateCrossPipe(), "Cross Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateElbowPipe("NW"), "North-West Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateElbowPipe("NE"), "North-East Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateElbowPipe("SW"), "South-West Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateElbowPipe("SE"), "South-East Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateTPipe("NEW"), "North-East-West Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateTPipe("NES"), "North-East-South Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateTPipe("NWS"), "North-West-South Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateTPipe("SEW"), "South-East-West Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateEndPipe("W"), "West-End Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateEndPipe("N"), "North-End Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateEndPipe("E"), "East-End Pipe", "Drag me on the drawing"));
library.Items.Add(new NLibraryItem(CreateEndPipe("S"), "South-End Pipe", "Drag me on the drawing"));
return library;
}
private NShape CreateHorizontalPipe()
{
NShape shape = new NShape();
shape.SetBounds(0, BoxSize, 3 * BoxSize, BoxSize);
NDrawRectangle drawRectangle = new NDrawRectangle(0, 0, 1, 1);
drawRectangle.ShowStroke = false;
shape.Geometry.AddRelative(drawRectangle);
shape.Geometry.AddRelative(new NMoveTo(0, 0));
shape.Geometry.AddRelative(new NLineTo(1, 0));
shape.Geometry.AddRelative(new NMoveTo(0, 1));
shape.Geometry.AddRelative(new NLineTo(1, 1));
shape.Ports.Add(CreatePort(Side.Left));
shape.Ports.Add(CreatePort(Side.Right));
SetProtections(shape);
return shape;
}
private NShape CreateVerticalPipe()
{
NShape shape = new NShape();
shape.SetBounds(BoxSize, 0, BoxSize, 3 * BoxSize);
NDrawRectangle drawRectangle = new NDrawRectangle(0, 0, 1, 1);
drawRectangle.ShowStroke = false;
shape.Geometry.AddRelative(drawRectangle);
shape.Geometry.AddRelative(new NMoveTo(0, 0));
shape.Geometry.AddRelative(new NLineTo(0, 1));
shape.Geometry.AddRelative(new NMoveTo(1, 0));
shape.Geometry.AddRelative(new NLineTo(1, 1));
shape.Ports.Add(CreatePort(Side.Top));
shape.Ports.Add(CreatePort(Side.Bottom));
SetProtections(shape);
return shape;
}
private NShape CreateCrossPipe()
{
NShape shape = new NShape();
shape.SetBounds(0, 0, 3 * BoxSize, 3 * BoxSize);
NDrawPolygon drawPolygon = new NDrawPolygon(0, 0, 1, 1,
new NPoint[] {
new NPoint(0, OneThird),
new NPoint(OneThird, OneThird),
new NPoint(OneThird, 0),
new NPoint(TwoThirds, 0),
new NPoint(TwoThirds, OneThird),
new NPoint(1, OneThird),
new NPoint(1, TwoThirds),
new NPoint(TwoThirds, TwoThirds),
new NPoint(TwoThirds, 1),
new NPoint(OneThird, 1),
new NPoint(OneThird, TwoThirds),
new NPoint(0, TwoThirds)
});
drawPolygon.ShowStroke = false;
shape.Geometry.AddRelative(drawPolygon);
shape.Geometry.AddRelative(new NMoveTo(0, OneThird));
shape.Geometry.AddRelative(new NLineTo(OneThird, OneThird));
shape.Geometry.AddRelative(new NMoveTo(OneThird, OneThird));
shape.Geometry.AddRelative(new NLineTo(OneThird, 0));
shape.Geometry.AddRelative(new NMoveTo(TwoThirds, 0));
shape.Geometry.AddRelative(new NLineTo(TwoThirds, OneThird));
shape.Geometry.AddRelative(new NMoveTo(TwoThirds, OneThird));
shape.Geometry.AddRelative(new NLineTo(1, OneThird));
shape.Geometry.AddRelative(new NMoveTo(1, TwoThirds));
shape.Geometry.AddRelative(new NLineTo(TwoThirds, TwoThirds));
shape.Geometry.AddRelative(new NMoveTo(TwoThirds, TwoThirds));
shape.Geometry.AddRelative(new NLineTo(TwoThirds, 1));
shape.Geometry.AddRelative(new NMoveTo(OneThird, 1));
shape.Geometry.AddRelative(new NLineTo(OneThird, TwoThirds));
shape.Geometry.AddRelative(new NMoveTo(OneThird, TwoThirds));
shape.Geometry.AddRelative(new NLineTo(0, TwoThirds));
shape.Ports.Add(CreatePort(Side.Left));
shape.Ports.Add(CreatePort(Side.Top));
shape.Ports.Add(CreatePort(Side.Right));
shape.Ports.Add(CreatePort(Side.Bottom));
SetProtections(shape);
return shape;
}
private NShape CreateElbowPipe(string type)
{
NDrawPolygon drawPolygon;
Side ca1, ca2;
switch (type)
{
case "NW":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[] {
new NPoint(0, OneThird),
new NPoint(OneThird, OneThird),
new NPoint(OneThird, 0),
new NPoint(TwoThirds, 0),
new NPoint(TwoThirds, TwoThirds),
new NPoint(0, TwoThirds)
});
ca1 = Side.Top;
ca2 = Side.Left;
break;
case "NE":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[] {
new NPoint(1, OneThird),
new NPoint(TwoThirds, OneThird),
new NPoint(TwoThirds, 0),
new NPoint(OneThird, 0),
new NPoint(OneThird, TwoThirds),
new NPoint(1, TwoThirds)
});
ca1 = Side.Top;
ca2 = Side.Right;
break;
case "SW":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[] {
new NPoint(0, TwoThirds),
new NPoint(OneThird, TwoThirds),
new NPoint(OneThird, 1),
new NPoint(TwoThirds, 1),
new NPoint(TwoThirds, OneThird),
new NPoint(0, OneThird)
});
ca1 = Side.Bottom;
ca2 = Side.Left;
break;
case "SE":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[] {
new NPoint(1, TwoThirds),
new NPoint(TwoThirds, TwoThirds),
new NPoint(TwoThirds, 1),
new NPoint(OneThird, 1),
new NPoint(OneThird, OneThird),
new NPoint(1, OneThird)
});
ca1 = Side.Bottom;
ca2 = Side.Right;
break;
default:
throw new ArgumentException("Unsupported elbow pipe type");
}
NShape shape = new NShape();
shape.SetBounds(0, 0, 3 * BoxSize, 3 * BoxSize);
drawPolygon.ShowStroke = false;
shape.Geometry.AddRelative(drawPolygon);
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[0]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[1]));
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[1]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[2]));
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[3]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[4]));
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[4]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[5]));
shape.Ports.Add(CreatePort(ca1));
shape.Ports.Add(CreatePort(ca2));
SetProtections(shape);
return shape;
}
private NShape CreateTPipe(string type)
{
NDrawPolygon drawPolygon;
Side ca1, ca2, ca3;
switch (type)
{
case "NEW":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[]{
new NPoint(0, OneThird),
new NPoint(OneThird, OneThird),
new NPoint(OneThird, 0),
new NPoint(TwoThirds, 0),
new NPoint(TwoThirds, OneThird),
new NPoint(1, OneThird),
new NPoint(1, TwoThirds),
new NPoint(0, TwoThirds)
});
ca1 = Side.Top;
ca2 = Side.Left;
ca3 = Side.Right;
break;
case "NES":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[]{
new NPoint(1, OneThird),
new NPoint(TwoThirds, OneThird),
new NPoint(TwoThirds, 0),
new NPoint(OneThird, 0),
new NPoint(OneThird, 1),
new NPoint(TwoThirds, 1),
new NPoint(TwoThirds, TwoThirds),
new NPoint(1, TwoThirds)
});
ca1 = Side.Top;
ca2 = Side.Right;
ca3 = Side.Bottom;
break;
case "NWS":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[]{
new NPoint(0, OneThird),
new NPoint(OneThird, OneThird),
new NPoint(OneThird, 0),
new NPoint(TwoThirds, 0),
new NPoint(TwoThirds, 1),
new NPoint(OneThird, 1),
new NPoint(OneThird, TwoThirds),
new NPoint(0, TwoThirds)
});
ca1 = Side.Top;
ca2 = Side.Left;
ca3 = Side.Bottom;
break;
case "SEW":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[]{
new NPoint(1, TwoThirds),
new NPoint(TwoThirds, TwoThirds),
new NPoint(TwoThirds, 1),
new NPoint(OneThird, 1),
new NPoint(OneThird, TwoThirds),
new NPoint(0, TwoThirds),
new NPoint(0, OneThird),
new NPoint(1, OneThird)
});
ca1 = Side.Bottom;
ca2 = Side.Right;
ca3 = Side.Left;
break;
default:
throw new ArgumentException("Unsupported elbow pipe type");
}
NShape shape = new NShape();
shape.SetBounds(0, 0, 3 * BoxSize, 3 * BoxSize);
drawPolygon.ShowStroke = false;
shape.Geometry.AddRelative(drawPolygon);
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[0]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[1]));
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[1]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[2]));
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[3]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[4]));
if (type.Contains("S") && type.Contains("N"))
{
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[5]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[6]));
}
else
{
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[4]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[5]));
}
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[6]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[7]));
shape.Ports.Add(CreatePort(ca1));
shape.Ports.Add(CreatePort(ca2));
shape.Ports.Add(CreatePort(ca3));
SetProtections(shape);
return shape;
}
private NShape CreateEndPipe(string type)
{
NDrawPolygon drawPolygon;
Side ca;
switch (type)
{
case "W":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[]{
new NPoint(1, OneThird),
new NPoint(TwoThirds, Half),
new NPoint(1, TwoThirds)
});
ca = Side.Right;
break;
case "N":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[]{
new NPoint(OneThird, 1),
new NPoint(Half, TwoThirds),
new NPoint(TwoThirds, 1)
});
ca = Side.Bottom;
break;
case "E":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[]{
new NPoint(0, OneThird),
new NPoint(OneThird, Half),
new NPoint(0, TwoThirds)
});
ca = Side.Left;
break;
case "S":
drawPolygon = new NDrawPolygon(0, 0, 1, 1, new NPoint[]{
new NPoint(OneThird, 0),
new NPoint(Half, OneThird),
new NPoint(TwoThirds, 0)
});
ca = Side.Top;
break;
default:
throw new ArgumentException("Unsupported elbow pipe type");
}
NShape shape = new NShape();
shape.SetBounds(0, 0, 3 * BoxSize, 3 * BoxSize);
drawPolygon.ShowStroke = false;
shape.Geometry.AddRelative(drawPolygon);
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[0]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[1]));
shape.Geometry.AddRelative(new NMoveTo(drawPolygon.Points[1]));
shape.Geometry.AddRelative(new NLineTo(drawPolygon.Points[2]));
shape.Ports.Add(CreatePort(ca));
SetProtections(shape);
return shape;
}
#endregion
#region Event Handlers
#endregion
#region Fields
private NDrawingView m_DrawingView;
#endregion
#region Static Methods
private static void SetProtections(NShape shape)
{
shape.AllowResizeX = false;
shape.AllowResizeY = false;
shape.AllowRotate = false;
shape.AllowInplaceEdit = false;
}
private static NPort CreatePort(Side side)
{
NPort port;
switch (side)
{
case Side.Left:
port = new NPort(0, 0.5, true);
port.SetDirection(ENBoxDirection.Left);
break;
case Side.Top:
port = new NPort(0.5, 0, true);
port.SetDirection(ENBoxDirection.Up);
break;
case Side.Right:
port = new NPort(1, 0.5, true);
port.SetDirection(ENBoxDirection.Right);
break;
case Side.Bottom:
port = new NPort(0.5, 1, true);
port.SetDirection(ENBoxDirection.Down);
break;
default:
return null;
}
port.GlueMode = ENPortGlueMode.InwardAndOutward;
return port;
}
#endregion
#region Constants
private const int BoxSize = 25;
private const double OneThird = 1.0 / 3.0;
private const double TwoThirds = 2.0 / 3.0;
private const double Half = 0.5;
#endregion
#region Nested Types
private enum Side
{
Left,
Top,
Right,
Bottom
}
#endregion
}
} | Language: VB.NET |
Imports Nevron.Nov
Imports Nevron.Nov.Diagram
Imports Nevron.Nov.Diagram.Themes
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Forms
Imports System
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 - NPipelineEditorExample"
' 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
m_DrawingView = New NDrawingView()
Dim page = m_DrawingView.ActivePage
page.Items.Add(CreateTitleShape("Pipeline Editor"))
Return m_DrawingView
End Function
Public Function CreateExampleControls() As NWidget
Dim libraryView As NLibraryView = New NLibraryView()
libraryView.Content = CreateLibrary()
Return libraryView
End Function
Public Function GetExampleDescription() As String
Return "
This example demonstrates how to take advantage of inward/outward ports in order to build a pipeline editor application.
Drag and drop pipes from the pipes panel into the drawing area. When a pipe is near other pipe it sticks to its end if possible
or automatically rotates if needed and then sticks to the other pipe. Use the end pipe symbols (triangles) to mark pipe ends.
"
End Function
'''
''' Gets the relevant documentation links for the example
'''
'''
Public Function GetDocumentationUrls() As (String, String)()
Return {("Controls", "https://www.nevron.com/docs/openvision/diagram-controls.html"), ("Routable Connectors", "https://www.nevron.com/docs/openvision/diagram-routable-connectors.html"), ("Shapes", "https://www.nevron.com/docs/openvision/diagram-shapes.html")}
End Function
#End Region
#Region "Implementation"
'''
''' Creates the Title shape.
'''
'''
'''
Private Function CreateTitleShape(title As String) As NShape
Dim theme = NDrawingTheme.MyDrawNature
Dim titleShape As NShape = New NShape()
titleShape.SetBounds(0, 0, 500, 50)
titleShape.Text = title
titleShape.SetProtectionMask(ENDiagramItemOperationMask.All)
Dim titleTextBlock = CType(titleShape.TextBlock, NTextBlock)
titleTextBlock.ResizeMode = ENTextBlockResizeMode.ShapeSize
titleTextBlock.FontSize = 28
titleTextBlock.FontStyleBold = True
' Set theme-based colors to the title text, so that it changes when the user changes the theme
Dim strokeColor = theme.ColorPalette.Variants(0)(0)
strokeColor.Tag = New NThemeVariantColorInfo(0)
titleTextBlock.Stroke = New NStroke(strokeColor)
Dim fillColor = theme.ColorPalette.Variants(0)(4)
fillColor.Tag = New NThemeVariantColorInfo(4)
titleTextBlock.Fill = New NColorFill(fillColor)
' Alternatively, you can also use fixed colors (uncomment the 2 lines below)
'titleTextBlock.Stroke = new NStroke(NColor.DarkBlue);
'titleTextBlock.Fill = new NColorFill(NColor.LightBlue);
' Set an expression to center the title horizontally in the page
titleShape.SetFx(NShape.PinXProperty, New NFormulaFx("$ParentSheet.X + $ParentSheet.Width / 2", True))
Return titleShape
End Function
Private Function CreateLibrary() As NLibrary
Dim library As NLibrary = New NLibrary()
library.Items.Add(New NLibraryItem(CreateHorizontalPipe(), "Horizontal Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateVerticalPipe(), "Vertical Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateCrossPipe(), "Cross Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateElbowPipe("NW"), "North-West Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateElbowPipe("NE"), "North-East Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateElbowPipe("SW"), "South-West Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateElbowPipe("SE"), "South-East Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateTPipe("NEW"), "North-East-West Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateTPipe("NES"), "North-East-South Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateTPipe("NWS"), "North-West-South Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateTPipe("SEW"), "South-East-West Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateEndPipe("W"), "West-End Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateEndPipe("N"), "North-End Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateEndPipe("E"), "East-End Pipe", "Drag me on the drawing"))
library.Items.Add(New NLibraryItem(CreateEndPipe("S"), "South-End Pipe", "Drag me on the drawing"))
Return library
End Function
Private Function CreateHorizontalPipe() As NShape
Dim shape As NShape = New NShape()
shape.SetBounds(0, BoxSize, 3 * BoxSize, BoxSize)
Dim drawRectangle As NDrawRectangle = New NDrawRectangle(0, 0, 1, 1)
drawRectangle.ShowStroke = False
shape.Geometry.AddRelative(drawRectangle)
shape.Geometry.AddRelative(New NMoveTo(0, 0))
shape.Geometry.AddRelative(New NLineTo(1, 0))
shape.Geometry.AddRelative(New NMoveTo(0, 1))
shape.Geometry.AddRelative(New NLineTo(1, 1))
shape.Ports.Add(CreatePort(Side.Left))
shape.Ports.Add(CreatePort(Side.Right))
SetProtections(shape)
Return shape
End Function
Private Function CreateVerticalPipe() As NShape
Dim shape As NShape = New NShape()
shape.SetBounds(BoxSize, 0, BoxSize, 3 * BoxSize)
Dim drawRectangle As NDrawRectangle = New NDrawRectangle(0, 0, 1, 1)
drawRectangle.ShowStroke = False
shape.Geometry.AddRelative(drawRectangle)
shape.Geometry.AddRelative(New NMoveTo(0, 0))
shape.Geometry.AddRelative(New NLineTo(0, 1))
shape.Geometry.AddRelative(New NMoveTo(1, 0))
shape.Geometry.AddRelative(New NLineTo(1, 1))
shape.Ports.Add(CreatePort(Side.Top))
shape.Ports.Add(CreatePort(Side.Bottom))
SetProtections(shape)
Return shape
End Function
Private Function CreateCrossPipe() As NShape
Dim shape As NShape = New NShape()
shape.SetBounds(0, 0, 3 * BoxSize, 3 * BoxSize)
Dim drawPolygon As NDrawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(0, OneThird), New NPoint(OneThird, OneThird), New NPoint(OneThird, 0), New NPoint(TwoThirds, 0), New NPoint(TwoThirds, OneThird), New NPoint(1, OneThird), New NPoint(1, TwoThirds), New NPoint(TwoThirds, TwoThirds), New NPoint(TwoThirds, 1), New NPoint(OneThird, 1), New NPoint(OneThird, TwoThirds), New NPoint(0, TwoThirds)})
drawPolygon.ShowStroke = False
shape.Geometry.AddRelative(drawPolygon)
shape.Geometry.AddRelative(New NMoveTo(0, OneThird))
shape.Geometry.AddRelative(New NLineTo(OneThird, OneThird))
shape.Geometry.AddRelative(New NMoveTo(OneThird, OneThird))
shape.Geometry.AddRelative(New NLineTo(OneThird, 0))
shape.Geometry.AddRelative(New NMoveTo(TwoThirds, 0))
shape.Geometry.AddRelative(New NLineTo(TwoThirds, OneThird))
shape.Geometry.AddRelative(New NMoveTo(TwoThirds, OneThird))
shape.Geometry.AddRelative(New NLineTo(1, OneThird))
shape.Geometry.AddRelative(New NMoveTo(1, TwoThirds))
shape.Geometry.AddRelative(New NLineTo(TwoThirds, TwoThirds))
shape.Geometry.AddRelative(New NMoveTo(TwoThirds, TwoThirds))
shape.Geometry.AddRelative(New NLineTo(TwoThirds, 1))
shape.Geometry.AddRelative(New NMoveTo(OneThird, 1))
shape.Geometry.AddRelative(New NLineTo(OneThird, TwoThirds))
shape.Geometry.AddRelative(New NMoveTo(OneThird, TwoThirds))
shape.Geometry.AddRelative(New NLineTo(0, TwoThirds))
shape.Ports.Add(CreatePort(Side.Left))
shape.Ports.Add(CreatePort(Side.Top))
shape.Ports.Add(CreatePort(Side.Right))
shape.Ports.Add(CreatePort(Side.Bottom))
SetProtections(shape)
Return shape
End Function
Private Function CreateElbowPipe(type As String) As NShape
Dim drawPolygon As NDrawPolygon
Dim ca1, ca2 As Side
Select Case type
Case "NW"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(0, OneThird), New NPoint(OneThird, OneThird), New NPoint(OneThird, 0), New NPoint(TwoThirds, 0), New NPoint(TwoThirds, TwoThirds), New NPoint(0, TwoThirds)})
ca1 = Side.Top
ca2 = Side.Left
Case "NE"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(1, OneThird), New NPoint(TwoThirds, OneThird), New NPoint(TwoThirds, 0), New NPoint(OneThird, 0), New NPoint(OneThird, TwoThirds), New NPoint(1, TwoThirds)})
ca1 = Side.Top
ca2 = Side.Right
Case "SW"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(0, TwoThirds), New NPoint(OneThird, TwoThirds), New NPoint(OneThird, 1), New NPoint(TwoThirds, 1), New NPoint(TwoThirds, OneThird), New NPoint(0, OneThird)})
ca1 = Side.Bottom
ca2 = Side.Left
Case "SE"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(1, TwoThirds), New NPoint(TwoThirds, TwoThirds), New NPoint(TwoThirds, 1), New NPoint(OneThird, 1), New NPoint(OneThird, OneThird), New NPoint(1, OneThird)})
ca1 = Side.Bottom
ca2 = Side.Right
Case Else
Throw New ArgumentException("Unsupported elbow pipe type")
End Select
Dim shape As NShape = New NShape()
shape.SetBounds(0, 0, 3 * BoxSize, 3 * BoxSize)
drawPolygon.ShowStroke = False
shape.Geometry.AddRelative(drawPolygon)
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(0)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(1)))
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(1)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(2)))
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(3)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(4)))
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(4)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(5)))
shape.Ports.Add(CreatePort(ca1))
shape.Ports.Add(CreatePort(ca2))
SetProtections(shape)
Return shape
End Function
Private Function CreateTPipe(type As String) As NShape
Dim drawPolygon As NDrawPolygon
Dim ca1, ca2, ca3 As Side
Select Case type
Case "NEW"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(0, OneThird), New NPoint(OneThird, OneThird), New NPoint(OneThird, 0), New NPoint(TwoThirds, 0), New NPoint(TwoThirds, OneThird), New NPoint(1, OneThird), New NPoint(1, TwoThirds), New NPoint(0, TwoThirds)})
ca1 = Side.Top
ca2 = Side.Left
ca3 = Side.Right
Case "NES"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(1, OneThird), New NPoint(TwoThirds, OneThird), New NPoint(TwoThirds, 0), New NPoint(OneThird, 0), New NPoint(OneThird, 1), New NPoint(TwoThirds, 1), New NPoint(TwoThirds, TwoThirds), New NPoint(1, TwoThirds)})
ca1 = Side.Top
ca2 = Side.Right
ca3 = Side.Bottom
Case "NWS"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(0, OneThird), New NPoint(OneThird, OneThird), New NPoint(OneThird, 0), New NPoint(TwoThirds, 0), New NPoint(TwoThirds, 1), New NPoint(OneThird, 1), New NPoint(OneThird, TwoThirds), New NPoint(0, TwoThirds)})
ca1 = Side.Top
ca2 = Side.Left
ca3 = Side.Bottom
Case "SEW"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(1, TwoThirds), New NPoint(TwoThirds, TwoThirds), New NPoint(TwoThirds, 1), New NPoint(OneThird, 1), New NPoint(OneThird, TwoThirds), New NPoint(0, TwoThirds), New NPoint(0, OneThird), New NPoint(1, OneThird)})
ca1 = Side.Bottom
ca2 = Side.Right
ca3 = Side.Left
Case Else
Throw New ArgumentException("Unsupported elbow pipe type")
End Select
Dim shape As NShape = New NShape()
shape.SetBounds(0, 0, 3 * BoxSize, 3 * BoxSize)
drawPolygon.ShowStroke = False
shape.Geometry.AddRelative(drawPolygon)
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(0)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(1)))
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(1)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(2)))
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(3)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(4)))
If type.Contains("S") AndAlso type.Contains("N") Then
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(5)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(6)))
Else
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(4)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(5)))
End If
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(6)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(7)))
shape.Ports.Add(CreatePort(ca1))
shape.Ports.Add(CreatePort(ca2))
shape.Ports.Add(CreatePort(ca3))
SetProtections(shape)
Return shape
End Function
Private Function CreateEndPipe(type As String) As NShape
Dim drawPolygon As NDrawPolygon
Dim ca As Side
Select Case type
Case "W"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(1, OneThird), New NPoint(TwoThirds, Half), New NPoint(1, TwoThirds)})
ca = Side.Right
Case "N"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(OneThird, 1), New NPoint(Half, TwoThirds), New NPoint(TwoThirds, 1)})
ca = Side.Bottom
Case "E"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(0, OneThird), New NPoint(OneThird, Half), New NPoint(0, TwoThirds)})
ca = Side.Left
Case "S"
drawPolygon = New NDrawPolygon(0, 0, 1, 1, New NPoint() {New NPoint(OneThird, 0), New NPoint(Half, OneThird), New NPoint(TwoThirds, 0)})
ca = Side.Top
Case Else
Throw New ArgumentException("Unsupported elbow pipe type")
End Select
Dim shape As NShape = New NShape()
shape.SetBounds(0, 0, 3 * BoxSize, 3 * BoxSize)
drawPolygon.ShowStroke = False
shape.Geometry.AddRelative(drawPolygon)
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(0)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(1)))
shape.Geometry.AddRelative(New NMoveTo(drawPolygon.Points(1)))
shape.Geometry.AddRelative(New NLineTo(drawPolygon.Points(2)))
shape.Ports.Add(CreatePort(ca))
SetProtections(shape)
Return shape
End Function
#End Region
#Region "Event Handlers"
#End Region
#Region "Fields"
Private m_DrawingView As NDrawingView
#End Region
#Region "Static Methods"
Private Shared Sub SetProtections(shape As NShape)
shape.AllowResizeX = False
shape.AllowResizeY = False
shape.AllowRotate = False
shape.AllowInplaceEdit = False
End Sub
Private Shared Function CreatePort(side As Side) As NPort
Dim port As NPort
Select Case side
Case Side.Left
port = New NPort(0, 0.5, True)
port.SetDirection(ENBoxDirection.Left)
Case Side.Top
port = New NPort(0.5, 0, True)
port.SetDirection(ENBoxDirection.Up)
Case Side.Right
port = New NPort(1, 0.5, True)
port.SetDirection(ENBoxDirection.Right)
Case Side.Bottom
port = New NPort(0.5, 1, True)
port.SetDirection(ENBoxDirection.Down)
Case Else
Return Nothing
End Select
port.GlueMode = ENPortGlueMode.InwardAndOutward
Return port
End Function
#End Region
#Region "Constants"
Private Const BoxSize As Integer = 25
Private Const OneThird As Double = 1.0 / 3.0
Private Const TwoThirds As Double = 2.0 / 3.0
Private Const Half As Double = 0.5
#End Region
#Region "Nested Types"
Private Enum Side
Left
Top
Right
Bottom
End Enum
#End Region
End Class