翻译:才鸟(http://www.objectarx.net/)
Code
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Customization;
using System;
using System.Collections.Specialized;
namespace PartialCUI
{
public class Commands : IExtensionApplication
{
public void Initialize()
{
BuildMenuCUI();
}
public void Terminate()
{
}
[CommandMethod("bm")]
public void BuildMenuCUI()
{
const string myCuiFile = "c:\\kean.cui";
const string myCuiFileToSend = "c:/kean.cui";
const string myCuiSectionName = "自定义菜单";
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
string mainCui = Application.GetSystemVariable("MENUNAME") + ".cui";
CustomizationSection cs = new CustomizationSection(mainCui);
PartialCuiFileCollection pcfc = cs.PartialCuiFiles;
pcfc.Remove(myCuiFile);
if (pcfc.Contains(myCuiFile))
{
ed.WriteMessage("\nCustomization file \"" + myCuiFile + "\" already loaded.");
}
else
{
if (System.IO.File.Exists(myCuiFile))
{
ed.WriteMessage("\nCustomization file \"" + myCuiFile+ "\" exists - loading it.");
LoadMyCui(myCuiFileToSend);
}
else
{
ed.WriteMessage("\nCustomization file \"" + myCuiFile + "\" does not exist - building it.");
// 为我们的局部菜单创建一个自定义区域(customization section)
CustomizationSection pcs = new CustomizationSection();
pcs.MenuGroupName = myCuiSectionName;
// 添加一个带两个命令的菜单组
MacroGroup mg = new MacroGroup(myCuiSectionName, pcs.MenuGroup);
MenuMacro mm1 = new MenuMacro(mg, "Cmd 1", "^C^CCmd1", "ID_MyCmd1");
MenuMacro mm2 = new MenuMacro(mg, "Cmd 2", "^C^CCmd2", "ID_MyCmd2");
// 现在添加一个带两个项的下拉菜单
StringCollection sc = new StringCollection();
sc.Add("POP15");
PopMenu pm = new PopMenu(myCuiSectionName,sc,"ID_MyPop1",pcs.MenuGroup);
PopMenuItem pmi1 = new PopMenuItem(mm1, "Pop Cmd 1", pm, -1);
PopMenuItem pmi2 = new PopMenuItem(mm2, "Pop Cmd 2", pm, -1);
// 最后保存文件并重新载入它
pcs.SaveAs(myCuiFile);
LoadMyCui(myCuiFileToSend);
}
}
}
private void LoadMyCui(string cuiFile)
{
// 这个载入的技术是向命令行发送一个LISP字符串
// (这可以避免设置FILEDIA为0)
// 设置CMDECHO为0后,尽量不显示回显信息。
// 我们要确保在最后重置LISP字符串的值
// (由于字符串的执行是非同步的,所以我们没有机会在调用的函数中来实现)。
Document doc = Application.DocumentManager.MdiActiveDocument;
object oldCmdEcho = Application.GetSystemVariable("CMDECHO");
Application.SetSystemVariable("CMDECHO", 0);
doc.SendStringToExecute("(command \"_.CUILOAD\" \"" + cuiFile + "\")(setvar \"CMDECHO\" " + oldCmdEcho + ")(princ) ",false,false,false);
}
}
}
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Customization;
using System;
using System.Collections.Specialized;
namespace PartialCUI
{
public class Commands : IExtensionApplication
{
public void Initialize()
{
BuildMenuCUI();
}
public void Terminate()
{
}
[CommandMethod("bm")]
public void BuildMenuCUI()
{
const string myCuiFile = "c:\\kean.cui";
const string myCuiFileToSend = "c:/kean.cui";
const string myCuiSectionName = "自定义菜单";
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
string mainCui = Application.GetSystemVariable("MENUNAME") + ".cui";
CustomizationSection cs = new CustomizationSection(mainCui);
PartialCuiFileCollection pcfc = cs.PartialCuiFiles;
pcfc.Remove(myCuiFile);
if (pcfc.Contains(myCuiFile))
{
ed.WriteMessage("\nCustomization file \"" + myCuiFile + "\" already loaded.");
}
else
{
if (System.IO.File.Exists(myCuiFile))
{
ed.WriteMessage("\nCustomization file \"" + myCuiFile+ "\" exists - loading it.");
LoadMyCui(myCuiFileToSend);
}
else
{
ed.WriteMessage("\nCustomization file \"" + myCuiFile + "\" does not exist - building it.");
// 为我们的局部菜单创建一个自定义区域(customization section)
CustomizationSection pcs = new CustomizationSection();
pcs.MenuGroupName = myCuiSectionName;
// 添加一个带两个命令的菜单组
MacroGroup mg = new MacroGroup(myCuiSectionName, pcs.MenuGroup);
MenuMacro mm1 = new MenuMacro(mg, "Cmd 1", "^C^CCmd1", "ID_MyCmd1");
MenuMacro mm2 = new MenuMacro(mg, "Cmd 2", "^C^CCmd2", "ID_MyCmd2");
// 现在添加一个带两个项的下拉菜单
StringCollection sc = new StringCollection();
sc.Add("POP15");
PopMenu pm = new PopMenu(myCuiSectionName,sc,"ID_MyPop1",pcs.MenuGroup);
PopMenuItem pmi1 = new PopMenuItem(mm1, "Pop Cmd 1", pm, -1);
PopMenuItem pmi2 = new PopMenuItem(mm2, "Pop Cmd 2", pm, -1);
// 最后保存文件并重新载入它
pcs.SaveAs(myCuiFile);
LoadMyCui(myCuiFileToSend);
}
}
}
private void LoadMyCui(string cuiFile)
{
// 这个载入的技术是向命令行发送一个LISP字符串
// (这可以避免设置FILEDIA为0)
// 设置CMDECHO为0后,尽量不显示回显信息。
// 我们要确保在最后重置LISP字符串的值
// (由于字符串的执行是非同步的,所以我们没有机会在调用的函数中来实现)。
Document doc = Application.DocumentManager.MdiActiveDocument;
object oldCmdEcho = Application.GetSystemVariable("CMDECHO");
Application.SetSystemVariable("CMDECHO", 0);
doc.SendStringToExecute("(command \"_.CUILOAD\" \"" + cuiFile + "\")(setvar \"CMDECHO\" " + oldCmdEcho + ")(princ) ",false,false,false);
}
}
}