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.
Solved! Go to Solution.
Solved by _gile. Go to Solution.
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
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 ", false, false, true); } } }
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.
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
Hi
I would like to implement the menu in FDO layers(like save layer)
Could please help me
Regards
Dayalan
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
Can't find what you're looking for? Ask the community or share your knowledge.