Profile Picture

Connector tool Customization

Posted By Pramod Sreekanthan 14 Years Ago
Author
Message
Nevron Support
Posted 14 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)

Group: Forum Members
Last Active: 2 days ago @ 1:54 AM
Posts: 3,054, Visits: 4,009
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



Pramod Sreekanthan
Posted 14 Years Ago
View Quick Profile
Junior Member

Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)

Group: Forum Members
Last Active: 13 Years Ago
Posts: 17, Visits: 1
Thanks for the answers, it would much helpful if a sample can be provided.

Nevron Support
Posted 14 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)

Group: Forum Members
Last Active: 2 days ago @ 1:54 AM
Posts: 3,054, Visits: 4,009
Hi Pramod,

The customization would require several changes:

1. Create a new diagram command that enables your custom create connector tool.

That would involve creating a diagram command that is similar to the NEnableCreateConnectorCommand. This command is an option menu command that consists of the individual commands (SetNewConnectorToLine, SetNewConnectorToBezier etc.), so as part of the implementation you will need to create individual commands for your specific connector subtypes.

2. Create a custom create connector tool that derives from the default connector tool.

That would involve creating a connector tool that behaves similarly to the default connector tool, but slightly alters the newly created connectors.

3. Create a few stylesheets for your specific connectors appearance.

Check out this topic in the Open Vision help - it explains styling in details:
Diagram for .NET > User's Guide > Document Object Model > Styles, Style Composition and Style Sheets

We will be posting a sample code for this requirement tomorrow.

Best Regards,
Nevron Support Team



Pramod Sreekanthan
questionmark Posted 14 Years Ago
View Quick Profile
Junior Member

Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)Junior Member (17 reputation)

Group: Forum Members
Last Active: 13 Years Ago
Posts: 17, Visits: 1

Is it possible to customize Connector tool in the toolbar? Requirement is to

1. Add our own custom connectors, as in the attachment

2. "Duct" - should behave as a Dynamic HC with Rerouting enabled, the connector line style should be green in color and thick.

3. "Duct Top to Side" - should behave like Top to side connector with a blue line color and thin.

4."Controller" - a dynamic hv with line style red and dash line. 

5. Default Connector tool will also be shown as a separate tool bar.



Attachments
Custom Connector Toolbar.zip (84 views, 46.00 KB)


Similar Topics


Reading This Topic