AutoCAD .NET API can help us extend context menus in many different situations. We have introduced how to attach context menu extensions to commands and demonstrated how the Local Commands Creator/Wizard can help us do so quickly, reliably and automatically.
We also introduced how to attach a context menu extension to the default application environment previously. In this article, let us see how to attach a context menu extension to the context menu of a particular entity kind, e.g. Circle.
In case we already have a context menu extension, attaching it to the context menu of an entity type is also quite straightforward. Only three steps are necessary. The first step is to create an instance of the context menu extension, the second is to get the RXClass instance from the entity type such as Circle, and the last is to call the AddObjectContextMenuExtension () method of the AutoCAD Application and feed it the two instances we just created or got. Here you go!
[CommandMethod("CmdGroup1", "Command40", null, CommandFlags.Modal, null, "Acad2012NetDemoAddinCS.chm", "Command40")]
public void Command40_Method()
{
Editor ed = AcadApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
//TODO: add your code below.
ed.WriteMessage("Command40 ran.");
ContextMenuExtensioner1 cmExt = new ContextMenuExtensioner1();
RXClass rx = RXClass.GetClass(typeof(Circle));
AcadApplication.AddObjectContextMenuExtension(rx, cmExt);
}
catch (System.Exception ex)
{
Debug.WriteLine(ex.ToString());
ed.WriteMessage(ex.ToString());
}
}
If the assembly is loaded into AutoCAD, the command runs, a circle is selected first, and the right mouse button is clicked, we will see something like this:
As expected, the context menu extension has been attached properly to the circle context menu. By the way, in this environment, the first level of the context menu extension items will be added into the circle context menu as separate menus instead of a group.
The ContextMenuExtensioner1 as referenced in the nice command was created by the ContextMenu Extensioner of AcadNetAddinWizard as we demonstrated before. Here is its content to make the story complete:
#region Namespaces
using System;
using System.Text;
//using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
//using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using AcadDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcadWindows = Autodesk.AutoCAD.Windows;
#endregion
namespace Acad2012NetDemoAddinCS
{
public class ContextMenuExtensioner1 : ContextMenuExtension
{
public ContextMenuExtensioner1()
{
this.Title = "ContextMenuExtensioner1 Context Menu";
Assembly assem = Assembly.GetExecutingAssembly();
AcadWindows.MenuItem mnItem01 = new AcadWindows.MenuItem("item1");
mnItem01.Icon = new Icon(assem.GetManifestResourceStream("Acad2012NetDemoAddinCS.Resources.addin.ico"));
mnItem01.Checked = true;
mnItem01.Click += new EventHandler(ContextMenuExtensioner1.MenuItem_OnClick);
this.MenuItems.Add(mnItem01);
AcadWindows.MenuItem mnItem02 = new AcadWindows.MenuItem("item2");
mnItem02.Icon = new Icon(assem.GetManifestResourceStream("Acad2012NetDemoAddinCS.Resources.addin.ico"));
mnItem02.Checked = false;
mnItem02.Click += new EventHandler(ContextMenuExtensioner1.MenuItem_OnClick);
this.MenuItems.Add(mnItem02);
AcadWindows.MenuItem mnItem02_01 = new AcadWindows.MenuItem("item2_1");
mnItem02_01.Icon = new Icon(assem.GetManifestResourceStream("Acad2012NetDemoAddinCS.Resources.a.ico"));
mnItem02_01.Checked = true;
mnItem02_01.Click += new EventHandler(ContextMenuExtensioner1.MenuItem_OnClick);
mnItem02.MenuItems.Add(mnItem02_01);
AcadWindows.MenuItem mnItem02_02 = new AcadWindows.MenuItem("item2_2");
mnItem02_02.Icon = new Icon(assem.GetManifestResourceStream("Acad2012NetDemoAddinCS.Resources.b.ico"));
mnItem02_02.Checked = false;
mnItem02_02.Click += new EventHandler(ContextMenuExtensioner1.MenuItem_OnClick);
mnItem02.MenuItems.Add(mnItem02_02);
AcadWindows.MenuItem mnItem02_02_01 = new AcadWindows.MenuItem("item2_2_1");
mnItem02_02_01.Icon = new Icon(assem.GetManifestResourceStream("Acad2012NetDemoAddinCS.Resources.c.ico"));
mnItem02_02_01.Checked = true;
mnItem02_02_01.Click += new EventHandler(ContextMenuExtensioner1.MenuItem_OnClick);
mnItem02_02.MenuItems.Add(mnItem02_02_01);
AcadWindows.MenuItem mnItem02_02_02 = new AcadWindows.MenuItem("item2_2_2");
mnItem02_02_02.Icon = new Icon(assem.GetManifestResourceStream("Acad2012NetDemoAddinCS.Resources.d.ico"));
mnItem02_02_02.Checked = false;
mnItem02_02_02.Click += new EventHandler(ContextMenuExtensioner1.MenuItem_OnClick);
mnItem02_02.MenuItems.Add(mnItem02_02_02);
AcadWindows.MenuItem mnItem02_02_02_01 = new AcadWindows.MenuItem("item2_2_2_1");
mnItem02_02_02_01.Icon = new Icon(assem.GetManifestResourceStream("Acad2012NetDemoAddinCS.Resources.e.ico"));
mnItem02_02_02_01.Checked = true;
mnItem02_02_02_01.Click += new EventHandler(ContextMenuExtensioner1.MenuItem_OnClick);
mnItem02_02_02.MenuItems.Add(mnItem02_02_02_01);
AcadWindows.MenuItem mnItem02_02_02_02 = new AcadWindows.MenuItem("item2_2_2_2");
mnItem02_02_02_02.Icon = new Icon(assem.GetManifestResourceStream("Acad2012NetDemoAddinCS.Resources.f.ico"));
mnItem02_02_02_02.Checked = false;
mnItem02_02_02_02.Click += new EventHandler(ContextMenuExtensioner1.MenuItem_OnClick);
mnItem02_02_02.MenuItems.Add(mnItem02_02_02_02);
AcadWindows.MenuItem mnItem03 = new AcadWindows.MenuItem("item3");
mnItem03.Icon = new Icon(assem.GetManifestResourceStream("Acad2012NetDemoAddinCS.Resources.g.ico"));
mnItem03.Checked = true;
mnItem03.Click += new EventHandler(ContextMenuExtensioner1.MenuItem_OnClick);
this.MenuItems.Add(mnItem03);
}
private static void MenuItem_OnClick(object sender, EventArgs e)
{
AcadWindows.MenuItem mnItem = sender as AcadWindows.MenuItem;
if (mnItem != null)
{
MessageBox.Show(string.Format("'{0}' was just clicked.", mnItem.Text));
System.Diagnostics.Debug.WriteLine(string.Format("'{0}' was just clicked.", mnItem.Text));
if (mnItem.Text == "item1")
{
}
if (mnItem.Text == "item2")
{
}
if (mnItem.Text == "item2_1")
{
}
if (mnItem.Text == "item2_2")
{
}
if (mnItem.Text == "item2_2_1")
{
}
if (mnItem.Text == "item2_2_2")
{
}
if (mnItem.Text == "item2_2_2_1")
{
}
if (mnItem.Text == "item2_2_2_2")
{
}
if (mnItem.Text == "item3")
{
}
}
}
}
}
Readers may have thought of that it might be better to attach the circle context menu extension only once during the initialization of an application extension instead of the command as demonstrated here. It would be also better to demonstrate how to remove the context menu extension from the circle context menu. It’s straightforward too. We can call another method RemoveObjectContextMenuExtension () from the same AutoCAD Application to make it done.
AcadApplication.RemoveObjectContextMenuExtension(rx, cmExt);
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides a coder, ContextMenuExtension Attacher, to help us do all the above automatically, quickly and reliably.
Recent Comments