AddDefaultContextMenuExtension 只能添加2级目录的菜单,如果想添加一级目录菜单像AddObjectContextMenuExtension 添加的那样, 就必须通过修改CUI 来实现.
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Customization;
[assembly: CommandClass(typeof(CuiTest.ADSKCommands))]
namespace CuiTest
{
/// <summary>
/// Summary description for ADSKCommands.
/// </summary>
public class ADSKCommands
{
private string strMacroGroup = "mxMenuGroups";
private CustomizationSection cs;
private Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
public ADSKCommands()
{
// retrieve the location of, and open the ACAD Main CUI File
string mainCuiFile = (string)Application.GetSystemVariable("MENUNAME");
mainCuiFile += ".cui";
cs = new CustomizationSection(mainCuiFile);
}
[CommandMethod("Add")]
public void Add() // This method can have any name
{
// First to create a MacroGroup and MenuMacro
MacroGroup oMacroGroup = new MacroGroup(strMacroGroup, cs.MenuGroup);
MenuMacro oMenuMacro = new MenuMacro(oMacroGroup, "CuiTestMacro", "ABC", "CuiTestMacro", MacroType.Any);
// Get the DEFAULT PopMenu
PopMenu defaultMenu = cs.MenuGroup.PopMenus.FindPopWithAlias("POP501");
PopMenuItem pmi = new PopMenuItem(defaultMenu, 2);
pmi.MacroID = oMenuMacro.ElementID;
pmi.Name = "ABC";
// Add a Separator
pmi = new PopMenuItem(defaultMenu, 3); // Separator
SaveCui();
}
[CommandMethod("Remove")]
public void Remove() // This method can have any name
{
MacroGroupCollection macroList = cs.MenuGroup.MacroGroups;
for (int index = 0; index < macroList.Count; index++ )
{
if (macroList[index].Name.Equals(strMacroGroup, StringComparison.CurrentCultureIgnoreCase))
{
macroList.Remove(index);
break;
}
}
PopMenu pm = cs.MenuGroup.PopMenus.FindPopWithAlias("POP501");
if (pm != null)
{
PopMenuItemCollection pmList = pm.PopMenuItems;
int index = 0;
bool bFound = false;
foreach (PopMenuItem item in pmList)
{
if (!item.IsSeparator && item.Name == "ABC")
{
pmList.Remove(item);
bFound = true;
}
// Remove the follow Separator
if (bFound && ((PopMenuItem)pmList[index]).IsSeparator)
{
pmList.RemoveAt(index);
break;
}
index ++;
}
}
SaveCui();
}
[CommandMethod("ABC")]
public void abc() // This method can have any name
{
ed.WriteMessage("\n Hello");
}
[CommandMethod("Savecui")]
public void SaveCui()
{
// Save all Changes made to the CUI file in this session.
// If changes were made to the Main CUI file - save it
// If changes were made to teh Partial CUI files need to save them too
if (cs.IsModified)
cs.Save();
// Here we unload and reload the main CUI file so the changes to the CUI file could take effect immediately.
string flName = cs.CUIFileBaseName;
Application.SetSystemVariable("FILEDIA", 0);
Application.DocumentManager.MdiActiveDocument.SendStringToExecute("cuiunload " + flName + " ", false, false, false);
Application.DocumentManager.MdiActiveDocument.SendStringToExecute("cuiload " + flName + " filedia 1 ", false, false, false);
}
}
}