.NET

Reply
Active Contributor
pva75
Posts: 48
Registered: ‎07-22-2012
Message 1 of 10 (951 Views)

Can I add my context menu for AutoCad objects?

951 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*
norman.yuan
Posts: 959
Registered: ‎04-27-2009
Message 2 of 10 (931 Views)

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*
_gile
Posts: 2,075
Registered: ‎04-29-2006
Message 3 of 10 (931 Views)

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*
Keith.Brown
Posts: 1,085
Registered: ‎03-13-2008
Message 4 of 10 (927 Views)

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
pva75
Posts: 48
Registered: ‎07-22-2012
Message 5 of 10 (912 Views)

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*
norman.yuan
Posts: 959
Registered: ‎04-27-2009
Message 6 of 10 (909 Views)

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*
Keith.Brown
Posts: 1,085
Registered: ‎03-13-2008
Message 7 of 10 (899 Views)

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
dr5156
Posts: 32
Registered: ‎12-08-2010
Message 8 of 10 (752 Views)

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
Member
mchan01
Posts: 4
Registered: ‎01-08-2014
Message 9 of 10 (200 Views)

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
cdinten
Posts: 61
Registered: ‎11-19-2010
Message 10 of 10 (193 Views)

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?

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community