To follow on from yesterday’s post, today we’re going to look at an alternative implementation that – rather than modifying the Edit shortcut menu that appears for all object types – adds our custom menu item to the shortcut menu associated with a specific type of object.
In our case we want to associate the menu item we introduced yesterday with Table objects. The way to associate shortcut menus in CUI with a particular object is to add an alias with “OBJECT_{DXFNAME}”: in our case {DXFNAME} will be ACAD_TABLE, making the alias “OBJECT_ACAD_TABLE”. You can also add an alias which starts with OBJECTS rather than OBJECT: this will be displayed when multiple objects of the same type are selected and will be used for one object in the case where no OBJECT menu has been defined.
We’re only going to define an OBJECT shortcut menu in our example. To make sure we create a shortcut menu we also need an additional alias: it needs to be a POP menu between 500 and 599. If you have a clash with the ID then the menu will be created but not displayed: for this reason we’re going to use POP599, as the other IDs in acad.cuix (which we’re still modifying in today’s post) start sequentially from POP500.
Here’s the C# code:
using System.Collections.Specialized;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Customization;
using Autodesk.AutoCAD.Runtime;
namespace ContextMenuWithIcon
{
public class Commands
{
const string cuiSectionName = "TTIF_Menus";
[CommandMethod("MIC2")]
public void MenuWithIcon2()
{
var acadCui =
new CustomizationSection(
Application.GetSystemVariable("MENUNAME") + ".cuix"
);
var acMenuGroup = acadCui.MenuGroup;
// Check if the macro group exists
var grp = default(MacroGroup);
if (acMenuGroup.MacroGroups.Contains(cuiSectionName))
grp = acMenuGroup.FindMacroGroup(cuiSectionName);
else
grp = new MacroGroup(cuiSectionName, acMenuGroup);
var mac =
grp.CreateMenuMacro(
"Table Export 2",
"_.TABLEEXPORT2",
"",
"Exports a table to a Unicode-compatible CSV",
MacroType.Edit,
"tableexp.bmp",
"tableexp.bmp",
""
);
if (grp.MenuMacros.Contains(mac))
grp.MenuMacros.Remove(mac);
grp.AddMacro(mac);
// Cycle through the PopMenus
PopMenu tabmen = null;
foreach (PopMenu menu in acMenuGroup.PopMenus)
{
// Find the Table-specific shortcut menu, if it exists
if (menu.Aliases.Contains("OBJECT_ACAD_TABLE"))
{
tabmen = menu;
break;
}
}
// Create the menu if it doesn't exist
if (tabmen == null)
{
// We need to provide an alias between POP500 and POP599 to
// designate a shortcut menu. Also need an OBJECT_* alias
// to be associated with a particular object type. The menu
// will get created if using an existing alias (POP5xx), but
// won't display if there's a conflict. Choosing 599 as
// standard acad.cuix shortcuts number upwards from 500
var aliases =
new StringCollection() { "POP599", "OBJECT_ACAD_TABLE" };
tabmen =
new PopMenu(
"Table Object Menu",
aliases,
"",
acMenuGroup
);
}
// Cycle through the menu items
bool found = false;
foreach (PopMenuItem item in tabmen.PopMenuItems)
{
// Check the name: we might also use something more
// reliable (two apps might use the same name), such as
// also checking the command associated. Or something.
if (item.Name == "Unicode Export...")
{
found = true;
break;
}
}
// Create and add the item if it doesn't exist
if (!found)
{
var newItem =
new PopMenuItem(
mac,
"Unicode Export...",
tabmen,
tabmen.PopMenuItems.Count
);
tabmen.PopMenuItems.Add(newItem);
// Only save and reload the CUI if we added our menu
// (the reload is particularly expensive)
acadCui.Save(true);
Application.ReloadAllMenus();
}
}
}
}
When we run it and right-click on a Table object, we should see the menu appears with the new menu item in a different location from where we chose to add it to the Edit menu, yesterday.
It’s of course important to note that while this code adds entries to acad.cuix, the ideal would be to package the shortcut menu in a partial CUI, as this is really the preferred way to deploy AutoCAD UI customizations (especially with the advent of the Exchange Apps Store, which allows you to specific a CUIX file that gets loaded automatically on load of your application).
I’ve started to play around with placing the shortcut menus in a partial CUI file, but haven’t yet had any luck with the menu being picked up from there. If I can find a way to make this work then I’ll go ahead and post again, showing one more alternative implementation.