使用CUIx加载Ribbon和传统菜单

使用CUIx加载Ribbon和传统菜单

CUIx是AutoCAD加载UI元素(包括Ribbon、传统菜单、工具条等)的最佳方式,它可以将所有UI元素打包至CUIx文件,在程序中直接加载即可。

采用代码动态创建UI元素,会存在一些问题:①如果其它插件采用CUIx后加载UI,将覆盖先前程序加载的UI;②传统菜单只能依靠COM编程方式加载。

创建CUIx文件和UI元素的通用函数

通过以下函数可以生成CUIx文件,并制作相应的命令宏和传统菜单。

可以不用代码制作Ribbon菜单,而是后期加载CUIx文件后在AutoCAD中以可视化的方式制作Ribbon菜单。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263public static class CUITools { // 创建CUIx文件 public static CustomizationSection AddCui(this Document doc, string cuiFile, string menuGroupName) { CustomizationSection cs; if (!File.Exists(cuiFile)) { cs = new CustomizationSection { MenuGroupName = menuGroupName }; cs.SaveAs(cuiFile); } else cs = new CustomizationSection(cuiFile); return cs; } // 为CUIx文件添加命令宏 public static MenuMacro AddMacro(this CustomizationSection cs, string name, string command, string tag, string helpString, string imagePath) { MenuGroup menuGroup = cs.MenuGroup; MacroGroup mg = menuGroup.FindMacroGroup(menuGroup.Name); if (mg == null) mg = new MacroGroup(menuGroup.Name, menuGroup); foreach (MenuMacro macro in mg.MenuMacros) { if (macro.ElementID == tag) return null; } MenuMacro menuMacro = new MenuMacro(mg, name, command, tag); menuMacro.macro.HelpString = helpString; if (!string.IsNullOrWhiteSpace(imagePath) && File.Exists(imagePath)) menuMacro.macro.LargeImage = menuMacro.macro.SmallImage = imagePath; return menuMacro; } // 为CUIx文件添加传统菜单 public static PopMenu AddPopMenu(this MenuGroup menuGroup, string name, StringCollection aliasList, string tag) { PopMenu pm = null; if (menuGroup.PopMenus.IsNameFree(name)) pm = new PopMenu(name, aliasList, tag, menuGroup); return pm; } // 为传统菜单添加菜单项 public static PopMenuItem AddMenuItem(this PopMenu parentMenu, int index, string name, string macroId) { PopMenuItem newPmi = null; foreach (PopMenuItem pmi in parentMenu.PopMenuItems) if (pmi.Name == name) return newPmi; newPmi = new PopMenuItem(parentMenu, index); if (name != null) newPmi.Name = name; newPmi.MacroID = macroId; return newPmi; } // 为传统菜单添加子菜单 public static PopMenu AddSubMenu(this PopMenu parentMenu, int index, string name, string tag) { PopMenu pm = null; if (parentMenu.CustomizationSection.MenuGroup.PopMenus.IsNameFree(name)) { pm = new PopMenu(name, null, tag, parentMenu.CustomizationSection.MenuGroup); PopMenuRef menuRef = new PopMenuRef(pm, parentMenu, index); } return pm; } // 为传统菜单添加分隔符 public static PopMenuItem AddSeparator(this PopMenu parentMenu, int index) { return new PopMenuItem(parentMenu, index); }}

通用函数创建CUIx文件和UI元素实例

调用下面的函数,可以生成mycuix.cuix文件:

123456789101112131415161718public static void CreateCuix(){ var doc=AcadApp.DocumentManeger.MdiActiveDocument; // 创建新的CUIx文件 string cuixPath=@"C:\mycuix.cuix"; var cs=doc.AddCui(cuixPath,"mycuix"); // 定义命令宏 cs.AddMacro("命令1","Cmd1",imagePath:@"res\Images\icon1.png"); cs.AddMacro("命令2","Cmd2",imagePath:@"res\Images\icon2.png"); cs.AddMacro("命令3","Cmd3",imagePath:@"res\Images\icon3.png"); // 添加传统菜单 var popmenu1=cs.MenuGroup.AddPopMenu("测试",new StringCollection(){"myPopMenu1"},"ID_MYPOPMENU1"); if(popmenu1!=null){ popmenu1.AddMenuItem(-1,"命令1","Cmd1"); popmenu1.AddMenuItem(-1,"命令2","Cmd2"); popmenu1.AddMenuItem(-1,"命令3","Cmd3"); } cs.Save();}

在AutoCAD中调用cuiload命令可以选择加载该文件,进而可视化制作Ribbon菜单:

当然,也可以封装自动创建Ribbon的函数,这是与代码动态创建Ribbon别无二致。

在AutoCAD中加载CUIx文件

有两种方法:①使用API函数;②模拟发送cuiload和cuiunload命令。方法①对传统菜单的支持不算太好,会存在莫名奇妙的问题;方法②比较兼容。

最好的方式是:加载用方法②,卸载用方法①,测试可行。

使用API函数

123string cuixPath=@"C:\mycuix.cuix";AcadApp.LoadPartialMenu(cuixPath);AcadApp.LoadPartialMenu("mycuix.cuix");

模拟命令发送

模拟命令发送时,需要在激活文档的编辑器中执行命令,所以当没有激活文档时应该创建一个:

因此,切换菜单时可以采用以下方式:

相关创意

忘记vivo手机密码?六种简单解锁方法全解析
体育投注365下载

忘记vivo手机密码?六种简单解锁方法全解析

📅 10-05 👁️ 110
怎么设置淘金币
365bet网上足球

怎么设置淘金币

📅 08-24 👁️ 1490
EVE星战前夜有哪些阵营 各阵营特点详解
bt365注册

EVE星战前夜有哪些阵营 各阵营特点详解

📅 08-31 👁️ 5262