.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can I add my context menu for AutoCad objects?

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
pva75
4356 Views, 11 Replies

Can I add my context menu for AutoCad objects?

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.

Tags (2)
11 REPLIES 11
Message 2 of 12
norman.yuan
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

Message 3 of 12
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 12
Keith.Brown
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.

Message 5 of 12
pva75
in reply to: _gile

Thanks a lot! I'll try this solution.

 

Pavel.

Message 6 of 12
norman.yuan
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.

 

Message 7 of 12
Keith.Brown
in reply to: norman.yuan

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

Message 8 of 12
dr5156
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
Message 9 of 12
mchan01
in reply to: dr5156

Can you add a context menu on a paletteset? i
Message 10 of 12
cdinten
in reply to: mchan01

how about adding a context menu to the UserControl that show in the palleteset?
Message 11 of 12
MarkSanchezSPEC
in reply to: _gile

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

 

 

Message 12 of 12
ehsan_bahrani
in reply to: _gile

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost