The following code is a simple form that demonstrates the customization of the command bars and the custom create connector tool.
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Nevron.GraphicsCore;
using Nevron.Diagram;
using Nevron.Diagram.WinForm;
using Nevron.Diagram.WinForm.Commands;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// create the commands bar manager
NDiagramCommandBarsManager commandBarsManager = new NDiagramCommandBarsManager();
commandBarsManager.ParentControl = this;
// create the view
NDrawingView view = new NDrawingView();
view.Dock = DockStyle.Fill;
Controls.Add(view);
commandBarsManager.View = view;
// create the document
NDrawingDocument document = new NDrawingDocument();
view.Document = document;
// add the NMyConnectorTool to the view
NToolCollection tools = view.Controller.Tools;
int index = tools.IndexOf(tools.GetToolByName(NDWFR.ToolCreateConnector));
view.Controller.Tools.Insert(index, new NMyConnectorTool());
// add the commands to the commander
commandBarsManager.Commander.Commands.Add(new NMySetNewConnectorToDuctCommand());
commandBarsManager.Commander.Commands.Add(new NMySetNewConnectorToDuctTopToSideCommand());
commandBarsManager.Commander.Commands.Add(new NMySetNewConnectorToControllerCommand());
commandBarsManager.Commander.Commands.Add(new NMyEnableConnectorToolCommand());
// add the connector command to the tools toolbar
List
toolsCommandIds = new List(commandBarsManager.ToolbarsBuilder.ToolsCommandIds);
toolsCommandIds.Add(MyEnableConnectorToolCommandId);
commandBarsManager.ToolbarsBuilder.ToolsCommandIds = toolsCommandIds.ToArray();
commandBarsManager.Recreate();
}
public class NMyEnableConnectorToolCommand : NDiagramOptionMenuCommand, INDiagramCheckButtonCommand
{
public NMyEnableConnectorToolCommand()
: base((int)DiagramCommandRange.Tools, MyEnableConnectorToolCommandId, "##My Connector tool", "##My Connector tool")
{
Items = new int[] {
MySetNewConnectorToDuctCommandId,
MySetNewConnectorToDuctTopToSideCommandId,
MySetNewConnectorToControllerCommandId
};
}
public override bool GetSelectedItemImageInfo(out ImageList imageList, out int imageIndex)
{
imageList = null;
imageIndex = -1;
if (Commander == null)
return false;
NDrawingView view = Commander.View;
if (view == null)
return false;
NMyConnectorTool tool = (view.Controller.Tools.GetToolByName(MyConnectorToolName) as NMyConnectorTool);
if (tool == null)
return false;
bool res = false;
switch (tool.MyConnectorType)
{
case MyConnectorType.Duct:
case MyConnectorType.Controller:
res = NDWFR.GetCommandImageInfo(DiagramCommand.SetNewConnectorToDynamicHV, out imageList, out imageIndex);
break;
case MyConnectorType.DuctTopToSide:
res = NDWFR.GetCommandImageInfo(DiagramCommand.SetNewConnectorToStep2FirstVertical, out imageList, out imageIndex);
break;
default:
Debug.Assert(false, "New line edge style?");
break;
}
return res;
}
public override bool Enabled
{
get
{
if (Commander == null || Commander.View == null)
return false;
return true;
}
}
public virtual void Execute()
{
if (Commander == null)
return;
NDrawingView view = Commander.View;
if (view == null)
return;
view.Controller.Tools.SingleEnableTool(MyConnectorToolName);
}
public virtual bool Checked
{
get
{
if (Commander == null)
return false;
NDrawingView view = Commander.View;
if (view == null)
return false;
return view.Controller.Tools.IsToolEnabled(MyConnectorToolName);
}
}
}
public class NMySetNewConnectorToDuctCommand : NDiagramCheckButtonCommand
{
public NMySetNewConnectorToDuctCommand()
: base((int)DiagramCommandRange.Tools, MySetNewConnectorToDuctCommandId, "##Duct", "##Duct")
{
}
public override bool Enabled
{
get
{
if (Commander == null || Commander.View == null)
return false;
return true;
}
}
public override bool Checked
{
get
{
if (Commander == null)
return false;
NDrawingView view = Commander.View;
if (view == null)
return false;
NMyConnectorTool tool = (view.Controller.Tools.GetToolByName(MyConnectorToolName) as NMyConnectorTool);
if (tool == null)
return false;
return (tool.MyConnectorType == MyConnectorType.Duct);
}
}
public override void Execute()
{
if (Commander == null)
return;
NDrawingView view = Commander.View;
if (view == null)
return;
NMyConnectorTool tool = (view.Controller.Tools.GetToolByName(MyConnectorToolName) as NMyConnectorTool);
if (tool == null)
return;
tool.MyConnectorType = MyConnectorType.Duct;
view.Controller.Tools.SingleEnableTool(MyConnectorToolName);
}
public override bool GetImageInfo(out ImageList imageList, out int imageIndex)
{
return NDWFR.GetCommandImageInfo(DiagramCommand.SetNewConnectorToDynamicHV, out imageList, out imageIndex);
}
}
public class NMySetNewConnectorToDuctTopToSideCommand : NDiagramCheckButtonCommand
{
public NMySetNewConnectorToDuctTopToSideCommand()
: base((int)DiagramCommandRange.Tools, MySetNewConnectorToDuctTopToSideCommandId, "##Duct Top To Side", "##Duct Top To Side")
{
}
public override bool Enabled
{
get
{
if (Commander == null || Commander.View == null)
return false;
return true;
}
}
public override bool Checked
{
get
{
if (Commander == null)
return false;
NDrawingView view = Commander.View;
if (view == null)
return false;
NMyConnectorTool tool = (view.Controller.Tools.GetToolByName(MyConnectorToolName) as NMyConnectorTool);
if (tool == null)
return false;
return (tool.MyConnectorType == MyConnectorType.DuctTopToSide);
}
}
public override void Execute()
{
if (Commander == null)
return;
NDrawingView view = Commander.View;
if (view == null)
return;
NMyConnectorTool tool = (view.Controller.Tools.GetToolByName(MyConnectorToolName) as NMyConnectorTool);
if (tool == null)
return;
tool.MyConnectorType = MyConnectorType.DuctTopToSide;
view.Controller.Tools.SingleEnableTool(MyConnectorToolName);
}
public override bool GetImageInfo(out ImageList imageList, out int imageIndex)
{
return NDWFR.GetCommandImageInfo(DiagramCommand.SetNewConnectorToStep2FirstVertical, out imageList, out imageIndex);
}
}
public class NMySetNewConnectorToControllerCommand : NDiagramCheckButtonCommand
{
public NMySetNewConnectorToControllerCommand()
: base((int)DiagramCommandRange.Tools, MySetNewConnectorToControllerCommandId, "##Controller", "##Controller")
{
}
public override bool Enabled
{
get
{
if (Commander == null || Commander.View == null)
return false;
return true;
}
}
public override bool Checked
{
get
{
if (Commander == null)
return false;
NDrawingView view = Commander.View;
if (view == null)
return false;
NMyConnectorTool tool = (view.Controller.Tools.GetToolByName(MyConnectorToolName) as NMyConnectorTool);
if (tool == null)
return false;
return (tool.MyConnectorType == MyConnectorType.Controller);
}
}
public override void Execute()
{
if (Commander == null)
return;
NDrawingView view = Commander.View;
if (view == null)
return;
NMyConnectorTool tool = (view.Controller.Tools.GetToolByName(MyConnectorToolName) as NMyConnectorTool);
if (tool == null)
return;
tool.MyConnectorType = MyConnectorType.Controller;
view.Controller.Tools.SingleEnableTool(MyConnectorToolName);
}
public override bool GetImageInfo(out ImageList imageList, out int imageIndex)
{
return NDWFR.GetCommandImageInfo(DiagramCommand.SetNewConnectorToDynamicHV, out imageList, out imageIndex);
}
}
[Serializable]
public class NMyConnectorTool : NCreateConnectorTool
{
public NMyConnectorTool()
{
base.Name = MyConnectorToolName;
}
public MyConnectorType MyConnectorType
{
get
{
return m_MyConnectorType;
}
set
{
m_MyConnectorType = value;
}
}
protected override INDiagramElement CreateElement(bool preview)
{
NDiagramElementFactory factory = View.ElementFactory;
NShape connector;
switch (m_MyConnectorType)
{
case MyConnectorType.Duct:
connector = factory.CreateRoutableConnector(preview, RoutableConnectorType.DynamicPolyline);
break;
case MyConnectorType.DuctTopToSide:
connector = factory.CreateStep2Connector(preview, true);
break;
case MyConnectorType.Controller:
connector = factory.CreateRoutableConnector(preview, RoutableConnectorType.DynamicCurve);
break;
default:
throw new Exception("New MyConnectorType?");
}
if (preview == false)
{
m_NewConnector = connector;
}
return connector;
}
public override void Deactivate()
{
base.Deactivate();
if (m_NewConnector != null && m_NewConnector.Drawing != null)
{
INRoutableShape routable = m_NewConnector as INRoutableShape;
if (routable != null)
{
routable.Reroute();
}
}
m_NewConnector = null;
}
MyConnectorType m_MyConnectorType = MyConnectorType.Duct;
NDiagramElement m_NewConnector;
}
public enum MyConnectorType
{
Duct,
DuctTopToSide,
Controller
}
// custom connector tool name
public static readonly string MyConnectorToolName = "MyConnectorTool";
// custom command ids
public static int MyEnableConnectorToolCommandId = (int)DiagramCommand.LastCommandId + 1;
public static int MySetNewConnectorToDuctCommandId = MyEnableConnectorToolCommandId + 2;
public static int MySetNewConnectorToDuctTopToSideCommandId = MyEnableConnectorToolCommandId + 3;
public static int MySetNewConnectorToControllerCommandId = MyEnableConnectorToolCommandId + 4;
}
}
Best Regards,
Nevron Support Team