Can I add my context menu for AutoCad objects?

Can I add my context menu for AutoCad objects?

Anonymous
Not applicable
6,286 Views
11 Replies
Message 1 of 12

Can I add my context menu for AutoCad objects?

Anonymous
Not applicable

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.

0 Likes
Accepted solutions (1)
6,287 Views
11 Replies
Replies (11)
Message 2 of 12

norman.yuan
Mentor
Mentor

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

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 12

_gile
Consultant
Consultant
Accepted solution

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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 12

Keith.Brown
Advisor
Advisor

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.

0 Likes
Message 5 of 12

Anonymous
Not applicable

Thanks a lot! I'll try this solution.

 

Pavel.

0 Likes
Message 6 of 12

norman.yuan
Mentor
Mentor

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.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 7 of 12

Keith.Brown
Advisor
Advisor

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

0 Likes
Message 8 of 12

Anonymous
Not applicable

Hi

 

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

 

Could please help me

 

Regards

Dayalan

0 Likes
Message 9 of 12

mchan01
Contributor
Contributor
Can you add a context menu on a paletteset? i
0 Likes
Message 10 of 12

Anonymous
Not applicable
how about adding a context menu to the UserControl that show in the palleteset?
0 Likes
Message 11 of 12

MarkSanchezSPEC
Advocate
Advocate

Very helpful Gilles, but for my VB brethren out there, here is the Attach code I had trouble porting (2 lines were quite different)

 

    Public Sub Attach()
        If cme Is Nothing Then
            cme = New ContextMenuExtension
            Dim mi As MenuItem = New MenuItem("To Red")
            AddHandler mi.Click, AddressOf item_Click      'this was different
            cme.MenuItems.Add(mi)
            Dim rxc As RXClass = Entity.GetClass(GetType(Entity))     'this was different
            Application.AddObjectContextMenuExtension(rxc, cme)
        End If
    End Sub

 

 

0 Likes
Message 12 of 12

ehsan_bahrani
Enthusiast
Enthusiast
Hi
Is there any way to show context menu only when one entity selected?
(or other conditions)
0 Likes