AutoCAD .NET Addin Wizard (AcadNetAddinWizard) is far more than just some templates scattered around. It is a real-wizard-sense wizard, i.e. multi paged, configurable, flexible and intelligent. It also includes various project item wizards, coders, and widgets for AutoCAD .NET programming.
In this article, let us see how the ContextMenu Extensioner of AutoCAD .NET Addin Wizard (AcadNetAddinWizard) can help implement ContextMenuExtension interfaces automatically, shortly, and still very flexibly.
The ContextMenu Extensioner can be found from the Add New Item UI of both Visual C# and VB.NET of Visual Studio both full-fledged and Express in version both 2008 and 2010. Let’s take the C# of Visual Studio 2010 for an example. The AutoCAD Addin category will appear under the Visual C# Items node of the Installed Templates and its various item wizards including the ContextMenu Extensioner appear in turn in the middle pane:
Here are wizard pages and some sample fill-outs of the ContextMenu Extensioner:
The auto-generated class and code may look like the following:
#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")
{
}
}
}
}
}
A lot of more magic stuffs are behind the friendly UIs actually. Just name one here: the Icon column of the ContextMenu items accept icon file picking when a cell is clicked and the wizard will turn the picked external icon file into an internal icon resource automatically and use it instead in the ContextMenu item creation. It will be far better than using some hard-coded icon paths in the ContextMenuExtension implementations, the wizard thinks.
Ok, now create a test command to exercise the ContextMenuExtension, something like:
[CommandMethod("LocalCommands1", "Cmd1", "LocalCommands1_Cmd1_ID", CommandFlags.Modal | CommandFlags.Interruptible, typeof(Acad2012NetDemoAddinCS.ContextMenuExtensioner1), "Acad2012NetDemoAddinCS.chm", "LocalCommands1_Cmd1_Index")]
public static void Cmd1_Method()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = AcadApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//TODO: add your code below.
Debug.WriteLine("Cmd1 ran.");
ed.WriteMessage("Cmd1 ran.\n");
ed.GetAngle("right click to see context menu.");
tr.Commit();
}
}
catch (System.Exception ex)
{
Debug.WriteLine(ex.ToString());
ed.WriteMessage(ex.ToString());
}
}
Next is to launch AutoCAD, NetLoad the assembly, and run the test command so as to have a look at the cool context menu. In case the project itself is also created by the AutoCAD .NET Addin Wizard (AcadNetAddinWizard) and so does the test command, a single F5 press will get us all the way there.
Give the ContextMenu Extensioner of the leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) a try and you will not feel any regret.
Recent Comments