October 2014

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  










« Adding a context menu to AutoCAD objects using .NET | Main | Automating batch printing of DWF files using Design Review 2008 »

May 07, 2007

It's all in the context: adding a default menu to your AutoCAD application using .NET

To follow on from the last post, we're now going to take a look at adding custom menu items to the default context menu in AutoCAD. The default menu appears when the user right-clicks on the drawing but has no objects selected. This is a good place to put application commands, for instance.

The approach is very similar to the previous one, although I've added some additional commands to control adding and removing the various menus in addition to relying on the module's initialization callback.

Some other notes:

  • We're not just adding a single menu item, but are using a cascading menu - three menu items underneath a main application menu item.
  • Once again we're using the Autodesk.AutoCAD.Internal namespace (which is unsupported and liable to change, and needs the AcMgdInternal.dll assembly referenced), to use the PostCommandPrompt() function. We would not need this if we called our commands via SendStringToExecute(), of course.

Here's the C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Windows;

using Autodesk.AutoCAD.Internal;

using System;


namespace ContextMenuApplication

{

  public class Commands : IExtensionApplication

  {

    public void Initialize()

    {

      CountMenu.Attach();

      ApplicationMenu.Attach();

    }

    public void Terminate()

    {

      CountMenu.Detach();

      ApplicationMenu.Detach();

    }


    [CommandMethod("ADDCONTEXT")]

    static public void AttachContextMenus()

    {

      CountMenu.Attach();

      ApplicationMenu.Attach();

    }

    [CommandMethod("NOCONTEXT")]

    static public void DetachContextMenus()

    {

      CountMenu.Detach();

      ApplicationMenu.Detach();

    }

    [CommandMethod("COUNT", CommandFlags.UsePickSet)]

    static public void CountSelection()

    {

      Editor ed =

        Application.DocumentManager.MdiActiveDocument.Editor;


      PromptSelectionResult psr = ed.GetSelection();

      if (psr.Status == PromptStatus.OK)

      {

        ed.WriteMessage(

          "\nSelected {0} entities.",

          psr.Value.Count

        );

      }

    }

  }


  public class CountMenu

  {

    private static ContextMenuExtension cme;


    public static void Attach()

    {

      if (cme == null)

      {

        cme = new ContextMenuExtension();

        MenuItem mi = new MenuItem("Count");

        mi.Click += new EventHandler(OnCount);

        cme.MenuItems.Add(mi);

      }

      RXClass rxc = Entity.GetClass(typeof(Entity));

      Application.AddObjectContextMenuExtension(rxc, cme);

    }

    public static void Detach()

    {

      RXClass rxc = Entity.GetClass(typeof(Entity));

      Application.RemoveObjectContextMenuExtension(rxc, cme);

    }

    private static void OnCount(Object o, EventArgs e)

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      doc.SendStringToExecute("_.COUNT ", true, false, false);

    }

  }


  public class ApplicationMenu

  {

    private static ContextMenuExtension cme;


    public static void Attach()

    {

      if (cme == null)

      {

        cme = new ContextMenuExtension();

        cme.Title = "Kean's commands";

        MenuItem mi1 = new MenuItem("1st");

        mi1.Click += new EventHandler(On1st);

        cme.MenuItems.Add(mi1);

        MenuItem mi2 = new MenuItem("2nd");

        mi2.Click += new EventHandler(On2nd);

        cme.MenuItems.Add(mi2);

        MenuItem mi3 = new MenuItem("3rd");

        mi3.Click += new EventHandler(On3rd);

        cme.MenuItems.Add(mi3);

      }

      Application.AddDefaultContextMenuExtension(cme);

    }

    public static void Detach()

    {

      Application.RemoveDefaultContextMenuExtension(cme);

    }

    private static void On1st(Object o, EventArgs e)

    {

      Editor ed =

        Application.DocumentManager.MdiActiveDocument.Editor;

      ed.WriteMessage("\nFirst item selected.");

      Utils.PostCommandPrompt();

    }

    private static void On2nd(Object o, EventArgs e)

    {

      Editor ed =

        Application.DocumentManager.MdiActiveDocument.Editor;

      ed.WriteMessage("\nSecond item selected.");

      Utils.PostCommandPrompt();

    }

    private static void On3rd(Object o, EventArgs e)

    {

      Editor ed =

        Application.DocumentManager.MdiActiveDocument.Editor;

      ed.WriteMessage("\nThird item selected.");

      Utils.PostCommandPrompt();

    }

  }

}

And here are our custom menu items on AutoCAD's default context menu:

Default_context_menu

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e200d835005fcc53ef

Listed below are links to weblogs that reference It's all in the context: adding a default menu to your AutoCAD application using .NET:

» [AutoCAD2008新功能介绍3]在AutoCAD中使用.NET来添加一个新的缩放比例 from C#才鸟
在本贴中,我们来看一下只有AutoCAD2008才有的一个功能——给图形添加一个新的缩放比例。下一篇将会讨论如何用编程的方式添加一个比例来让一个实体可以被缩放。 [Read More]

» [AutoCAD2008新功能介绍4]利用.NET使AutoCAD对象可缩放 from C#才鸟
在上一篇帖子中,我们看到了如何为AutoCAD图形添加一个新的缩放比例。 在这一篇帖子中,我们来看一下如何使一个对象可缩放——当然前提条件是它必须是一个支持缩放比例的类型。重复一下,这一篇帖子也是基于AutoCAD 2008中引入的功能。... [Read More]

blog comments powered by Disqus

Feed/Share

10 Random Posts