使用CUIx加载Ribbon和传统菜单

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

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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public 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文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public 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文件

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

评论