.NET

.NET

Reply
Active Contributor
48 Posts
2 Kudos
Registered: ‎07-22-2012
Post 1 of 10

Can I add my context menu for AutoCad objects?

1112 Views, 9 Replies
07-22-2012 12:19 PM

Can I add context menu from C# module to AutoCAD objects (for example - line). So, I want to create C# class library and load it from AutoCad by netload command. In code I want to add contect menu. How I can do it?

 

Thanks,

Pavel.

*Expert Elite*
1,088 Posts
217 Kudos
Registered: ‎04-27-2009
Post 2 of 10

Re: Can I add my context menu for AutoCad objects?

07-23-2012 06:45 AM in reply to: pva75

If you are talking about context menu when right-clicking AutoCAD editor screen with or without entity being selected, then, yes, you can use ContextMuneExtension via:

 

Application.Add[Remove]Default[Object]ContextMenuExtension()

 

There are quite a few links if you google for "AutoCAD ContextMenuExtension". Here are some:

 

http://through-the-interface.typepad.com/through_the_interface/2007/05/adding_a_contex.html

http://through-the-interface.typepad.com/through_the_interface/2008/11/displaying-a-co.html

http://through-the-interface.typepad.com/through_the_interface/2007/05/its_all_in_the_.html

*Expert Elite*
2,142 Posts
255 Kudos
Registered: ‎04-29-2006
Post 3 of 10

Re : Can I add my context menu for AutoCad objects?

07-23-2012 06:57 AM in reply to: pva75

Hi, 

 

If you want to do it programmatically (this can be done without programmation through the CUI), you can use:

Application.AddObjectContextMenuExtensions() method.

 

Here's a little example:

 

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
 
namespace ContextMenuSample
{
    public class Commands : IExtensionApplication
    {
        // a command called from the click event handler of the context menu item
        // the command needs the UsePickSet command flag
        [CommandMethod("ToRed"CommandFlags.Modal | CommandFlags.UsePickSet)]
        public static void ToRed()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptSelectionResult psr = ed.GetSelection();
            if (psr.Status != PromptStatus.OK)
                return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                foreach (ObjectId id in psr.Value.GetObjectIds())
                {
                    Entity line = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                    line.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
                }
                tr.Commit();
            }
        }
 
        // adds the context menu item
        [CommandMethod("AddContextMenu")]
        public void AddContextMenu()
        {
            LineContextMenu.Attach();
        }
 
        // removes the context menu item
        [CommandMethod("RemoveContextMenu")]
        public void RemoveContextMenu()
        {
            LineContextMenu.Detach();
        }
 
        // adds the context menu item on loading
        public void Initialize()
        {
            LineContextMenu.Attach();
        }
 
        public void Terminate()
        {
            LineContextMenu.Detach();
        }
    }
 
    // defines a context menu item
    class LineContextMenu
    {
        private static ContextMenuExtension menuExtension;
 
        // adds the item to the Line context menu
        internal static void Attach()
        {
            menuExtension = new ContextMenuExtension();
            MenuItem item = new MenuItem("To Red");
            item.Click += new EventHandler(item_Click);
            menuExtension.MenuItems.Add(item);
            RXClass rxClass = Entity.GetClass(typeof(Line));
            Application.AddObjectContextMenuExtension(rxClass, menuExtension);
        }
 
        // removes the item from the Line context menu
        internal static void Detach()
        {
            RXClass rxClass = Entity.GetClass(typeof(Line));
            Application.RemoveObjectContextMenuExtension(rxClass, menuExtension);
        }
 
        // item clicked event handler (calls "ToRed" command)
        static void item_Click(object sender, EventArgs e)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            doc.SendStringToExecute("ToRed "falsefalsetrue);
        }
    }
}
Gilles Chanteau
*Expert Elite*
1,277 Posts
109 Kudos
Registered: ‎03-13-2008
Post 4 of 10

Re : Can I add my context menu for AutoCad objects?

07-23-2012 07:37 AM in reply to: _gile

Nice code Gile.  Quick question for you though.  Can you restrict the context menu to lines that are on say a certain layer.  Or in my case using Autocad MEP, restrict it to the entity Mvparts that have a certain type such as Air Terminal?

 

Also, what if you are not defining a new command to use in the menu but using a command already defined.  Can you just add that to the event handler?  thanks.


Keith Brown AutoCAD MEP BLOG | RSS Feed
AutoCAD MEP 2014 | Revit 2014 | EastCoast CAD/CAM V6.1 | Visual Studio 2013
────────────────────────────────────
⁞|⁞ Please use Mark Solutions!.Accept as Solution and Give Kudos!Give Kudos as appropriate. Thank you!
Active Contributor
48 Posts
2 Kudos
Registered: ‎07-22-2012
Post 5 of 10

Re : Can I add my context menu for AutoCad objects?

07-23-2012 10:31 AM in reply to: _gile

Thanks a lot! I'll try this solution.

 

Pavel.

*Expert Elite*
1,088 Posts
217 Kudos
Registered: ‎04-27-2009
Post 6 of 10

Re : Can I add my context menu for AutoCad objects?

07-23-2012 10:55 AM in reply to: Keith.Brown

The simplest way to make your own context menu item's availability to specifically targeted entity would be load the the ObjectContextMenuExtension menu item as _gile's code shows and then handle its PopUp event. That is, when the context menu pops up, you test if the current selected entity (accessing via Editor.SelectImplied()) is your target, and enable/disable the menu item accordingly.

 

*Expert Elite*
1,277 Posts
109 Kudos
Registered: ‎03-13-2008
Post 7 of 10

Re : Can I add my context menu for AutoCad objects?

07-23-2012 05:25 PM in reply to: norman.yuan

Nice idea Norman.  That should work nicely for me.  Thanks!


Keith Brown AutoCAD MEP BLOG | RSS Feed
AutoCAD MEP 2014 | Revit 2014 | EastCoast CAD/CAM V6.1 | Visual Studio 2013
────────────────────────────────────
⁞|⁞ Please use Mark Solutions!.Accept as Solution and Give Kudos!Give Kudos as appropriate. Thank you!
Active Contributor
43 Posts
0 Kudos
Registered: ‎12-08-2010
Post 8 of 10

Re : Can I add my context menu for AutoCad objects?

01-02-2013 01:04 AM in reply to: Keith.Brown

Hi

 

I would like to implement the menu in FDO layers(like save layer)

 

Could please help me

 

Regards

Dayalan

Regards,
Dayalan
Active Member
7 Posts
0 Kudos
Registered: ‎01-08-2014
Post 9 of 10

Re : Can I add my context menu for AutoCad objects?

02-24-2014 01:39 PM in reply to: dr5156
Can you add a context menu on a paletteset? i
Valued Contributor
61 Posts
0 Kudos
Registered: ‎11-19-2010
Post 10 of 10

Re : Can I add my context menu for AutoCad objects?

02-24-2014 07:37 PM in reply to: mchan01
how about adding a context menu to the UserControl that show in the palleteset?
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!