![77cdf9334285945edfe4983446ac8e7f.png](https://i-blog.csdnimg.cn/blog_migrate/c2a11ef90840ed8e4a1a8a1c42c5b145.jpeg)
ObjectArx-C#下拉菜单命令
C#实现AutoCad下拉菜单命令。
主要思路是:
- 先搭建c#的project,添加相关依赖库。
- 在入口class中添加创建下拉菜单CommandMethod命令。
- 编写CommandMethod代码,实现下拉菜单。
代码示例如下:
- 入口点函数代码
//zhaoanan helloworld命令
[CommandMethod("HelloWorld")]//设计的新命令
public void HelloWorld()//调用HelloWorld 命令要调用的函数
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;//获取当前的活动文档
Editor acEd = acDoc.Editor;//当前的编辑器对象,命令行对象?
acEd.WriteMessage("Hello World 2017 ,LEADSOFT.ORG");//输出字符串
}
- 增加下拉菜单命令BMPM
/************************************************************************/
class Main
{
[CommandMethod("BMPM")]
public void BuildMyPopMenu()
{
try
{
//自定义的组名
string strMyGroupName = "MyGroup";
//保存的CUI文件名(从CAD2010开始,后缀改为了cuix)
//这里可以写文件路径或者仅写文件名
//如果仅写文件名,则最后保存时将默认保存在此程序集DLL所在目录
//如果写文件路径,则会按路径进行保存
string strCuiFileName = "MyMenu.cui";
//创建一个自定义组(这个组中将包含我们自定义的命令、菜单、工具栏、面板等)
CustomizationSection myCSection = new CustomizationSection();
myCSection.MenuGroupName = strMyGroupName;
//创建自定义命令组
MacroGroup mg = new MacroGroup("MyMethod", myCSection.MenuGroup);
MenuMacro mm1 = new MenuMacro(mg, "打开文件", "OF", "");
MenuMacro mm2 = new MenuMacro(mg, "打开模板", "OM", "");
MenuMacro mm3 = new MenuMacro(mg, "保存", "SV", "");
//声明菜单别名
StringCollection scMyMenuAlias = new StringCollection(); //菜单别名(仅查看CUI源文件时能看到)
scMyMenuAlias.Add("MyPop1");
scMyMenuAlias.Add("MyTestPop");
//菜单项(将显示在项部菜单栏中)
PopMenu pmParent = new PopMenu("我的菜单", scMyMenuAlias, "我的菜单", myCSection.MenuGroup);
//子项的菜单(多级)
//***多级菜单时,子项列表放到一个单独的PopMenu中,然后将此PopMenu放到一个PopMenuRef中
//***最后将PopMenuRef添加到菜单中
PopMenu pm1 = new PopMenu("打开", new StringCollection(), "", myCSection.MenuGroup);
PopMenuRef pmr1 = new PopMenuRef(pm1, pmParent, -1);
PopMenuItem pmi1 = new PopMenuItem(mm1, "文件", pm1, -1);
PopMenuItem pmi2 = new PopMenuItem(mm2, "模板", pm1, -1);
//子项的菜单(单级)
PopMenuItem pmi3 = new PopMenuItem(mm3, "保存(&S)", pmParent, -1);
// 最后保存文件
myCSection.SaveAs(strCuiFileName);
}
catch (System.Exception ex)
{
throw new System.Exception("自定义CAD菜单失败:" + ex.Message);
}
}
}
- 增加辅助绘图工具thmenu下拉菜单命令
//zhaoanan 下拉菜单
/// <summary>
/// 创建菜单
/// </summary>
[CommandMethod("thmenu")]
public static void thmenu()
{
//自定义的组名
string myCUI = "thcad.cuix";
string strMyGroupName = "crscu_zaa_thcad";
//保存的CUI文件名(从CAD2010开始,后缀改为了cuix)
//这里可以写文件路径或者仅写文件名
//如果仅写文件名,则最后保存时将默认保存在此程序集DLL所在目录
//如果写文件路径,则会按路径进行保存
//创建一个自定义组(这个组中将包含我们自定义的命令、菜单、工具栏、面板等)
CustomizationSection myCSection = new CustomizationSection();
myCSection.MenuGroupName = strMyGroupName;
//创建自定义命令组
MacroGroup mg = new MacroGroup("MyMethod", myCSection.MenuGroup);
MenuMacro mm1 = new MenuMacro(mg, "用户登录", "thuserlogin", "");
MenuMacro mm2 = new MenuMacro(mg, "用户注销", "thuserlogout", "");
MenuMacro mm31 = new MenuMacro(mg, "新建工程", "thprojectnew", "");
MenuMacro mm32 = new MenuMacro(mg, "浏览工程", "thprojectlist", "");
MenuMacro mm33 = new MenuMacro(mg, "重设当前工程", "thprojectreset", "");
MenuMacro mm4 = new MenuMacro(mg, "自动绘图", "thdevicecreate", "");
MenuMacro mm51 = new MenuMacro(mg, "上传图纸", "thfileupload", "");
MenuMacro mm52 = new MenuMacro(mg, "浏览图纸", "thfilelist", "");
MenuMacro mm61 = new MenuMacro(mg, "上传数据", "thmodelsave", "");
MenuMacro mm62 = new MenuMacro(mg, "保存工程", "thdbsave", "");
MenuMacro mm63 = new MenuMacro(mg, "上传模型及保存数据", "thdrawsave", "");
MenuMacro mm64 = new MenuMacro(mg, "上传模型", "thmodelsave", "");
MenuMacro mm65 = new MenuMacro(mg, "保存数据", "thdbsave", "");
MenuMacro mm71 = new MenuMacro(mg, "生成配线图", "thgendraws", "");
MenuMacro mm72 = new MenuMacro(mg, "上传所有配线图", "thuploaddraws", "");
MenuMacro mm73 = new MenuMacro(mg, "选择配线图上传", "thuploaddraw", "");
MenuMacro mm81 = new MenuMacro(mg, "应答器LEU对照表", "baliseleumap", "");
MenuMacro mm82 = new MenuMacro(mg, "工程数据对照表", "thenginedatagenerat", "");
MenuMacro mm83 = new MenuMacro(mg, "联锁表", "thciroutegenerat", "");
MenuMacro mm9 = new MenuMacro(mg, "自动编号", "thdevicename", "");
MenuMacro mm101 = new MenuMacro(mg, "站台区自动布置", "thdevicecreate", "");
MenuMacro mm1021 = new MenuMacro(mg, "自动绘制逻辑区段选全图", "thAllboundarycreate", "");
MenuMacro mm1022 = new MenuMacro(mg, "自动绘制逻辑区段选车站", "thstaboundarycreate", "");
MenuMacro mm1023 = new MenuMacro(mg, "自动绘制逻辑区段选主区段", "thlineboundarycreate", "");
MenuMacro mm1031 = new MenuMacro(mg, "区间自动布置区间固定应答器", "thstafixedbalise", "");
MenuMacro mm1032 = new MenuMacro(mg, "区间自动布置折返轨", "thlinereentryrail", "");
MenuMacro mm1033 = new MenuMacro(mg, "区间自动布置转换轨", "thlinetransitionrail", "");
//声明菜单别名
StringCollection scMyMenuAlias = new StringCollection(); //菜单别名(仅查看CUI源文件时能看到)
scMyMenuAlias.Add("user_manage");
scMyMenuAlias.Add("project_manage");
scMyMenuAlias.Add("autodraw_manage");
scMyMenuAlias.Add("drawing_manage");
scMyMenuAlias.Add("data_manage");
//菜单项(将显示在项部菜单栏中)
PopMenu pmParent = new PopMenu("绘图工具(&H)", scMyMenuAlias, "绘图工具(&H)", myCSection.MenuGroup);
//子项的菜单(多级)
//***多级菜单时,子项列表放到一个单独的PopMenu中,然后将此PopMenu放到一个PopMenuRef中
//***最后将PopMenuRef添加到菜单中
PopMenu pm1 = new PopMenu("用户管理(&U)", new StringCollection(), "", myCSection.MenuGroup);
PopMenuRef pmr1 = new PopMenuRef(pm1, pmParent, -1);
PopMenuItem pmi1 = new PopMenuItem(mm1, "用户登录(&L)", pm1, -1);
PopMenuItem pmi2 = new PopMenuItem(mm2, "用户注销(&S)", pm1, -1);
//子项的菜单(多级)
PopMenu pmi3 = new PopMenu("工程管理(&W)", new StringCollection(), "", myCSection.MenuGroup);
PopMenuRef pmr3 = new PopMenuRef(pmi3, pmParent, -1);
PopMenuItem pmi31 = new PopMenuItem(mm31, "新建工程(&E)", pmi3, -1);
PopMenuItem pmi32 = new PopMenuItem(mm32, "浏览工程(&V)", pmi3, -1);
PopMenuItem pmi33 = new PopMenuItem(mm33, "重设当前工程(&R)", pmi3, -1);
//子项的菜单(单级)
PopMenuItem pmi4 = new PopMenuItem(mm4, "自动绘图(&T)", pmParent, -1);
//子项的菜单(多级)
PopMenu pmi5 = new PopMenu("图纸管理(&M)", new StringCollection(), "", myCSection.MenuGroup);
PopMenuRef pmr5 = new PopMenuRef(pmi5, pmParent, -1);
PopMenuItem pmi51 = new PopMenuItem(mm51, "上传图纸(&U)", pmi5, -1);
PopMenuItem pmi52 = new PopMenuItem(mm52, "浏览图纸(&V)", pmi5, -1);
//子项的菜单(多级)
PopMenu pmi6 = new PopMenu("数据管理(&D)", new StringCollection(), "", myCSection.MenuGroup);
PopMenuRef pmr6 = new PopMenuRef(pmi6, pmParent, -1);
PopMenuItem pmi61 = new PopMenuItem(mm61, "上传数据(&U)", pmi6, -1);
PopMenuItem pmi62 = new PopMenuItem(mm62, "保存工程(&S)", pmi6, -1);
PopMenuItem pmi63 = new PopMenuItem(mm63, "上传模型及保存数据(&W)", pmi6, -1);
PopMenuItem pmi64 = new PopMenuItem(mm64, "上传模型(&M)", pmi6, -1);
PopMenuItem pmi65 = new PopMenuItem(mm65, "保存数据(&B)", pmi6, -1);
//子项的菜单(多级)
PopMenu pmi7 = new PopMenu("配线图管理(&P)", new StringCollection(), "", myCSection.MenuGroup);
PopMenuRef pmr7 = new PopMenuRef(pmi7, pmParent, -1);
PopMenuItem pmi71 = new PopMenuItem(mm71, "生成配线图(&S)", pmi7, -1);
PopMenuItem pmi72 = new PopMenuItem(mm72, "上传所有配线图(&C)", pmi7, -1);
PopMenuItem pmi73 = new PopMenuItem(mm73, "选择配线图上传(&X)", pmi7, -1);
//子项的菜单(多级)
PopMenu pmi8 = new PopMenu("导出设计数据表(&T)", new StringCollection(), "", myCSection.MenuGroup);
PopMenuRef pmr8 = new PopMenuRef(pmi8, pmParent, -1);
PopMenuItem pmi81 = new PopMenuItem(mm81, "应答器LEU对照表(&Y)", pmi8, -1);
PopMenuItem pmi82 = new PopMenuItem(mm82, "工程数据对照表(&G)", pmi8, -1);
PopMenuItem pmi83 = new PopMenuItem(mm83, "联锁表(&L)", pmi8, -1);
//子项的菜单(单级)
PopMenuItem pmi9 = new PopMenuItem(mm9, "自动编号(&B)", pmParent, -1);
//子项的菜单(多级)
PopMenu pmi10 = new PopMenu("区间自动绘图(&D)", new StringCollection(), "", myCSection.MenuGroup);
PopMenuRef pmr10 = new PopMenuRef(pmi10, pmParent, -1);
PopMenuItem pmi101 = new PopMenuItem(mm101, "站台区自动布置(&Y)", pmi10, -1);
PopMenu pmi1020 = new PopMenu("自动绘制逻辑区段(&L)", new StringCollection(), "", myCSection.MenuGroup);
PopMenuRef pmr1020 = new PopMenuRef(pmi1020, pmi10, -1);
PopMenuItem pmi1021 = new PopMenuItem(mm1021, "选全图(&A)", pmi1020, -1);
PopMenuItem pmi1022 = new PopMenuItem(mm1022, "选车站(&S)", pmi1020, -1);
PopMenuItem pmi1023 = new PopMenuItem(mm1023, "选主区段(&M)", pmi1020, -1);
PopMenu pmi1030 = new PopMenu("区间自动布置(&R)", new StringCollection(), "", myCSection.MenuGroup);
PopMenuRef pmr1030 = new PopMenuRef(pmi1030, pmi10, -1);
PopMenuItem pmi1031 = new PopMenuItem(mm1031, "区间固定应答器(选车站)(&B)", pmi1030, -1);
PopMenuItem pmi1032 = new PopMenuItem(mm1032, "折返轨(选车站)(&Z)", pmi1030, -1);
PopMenuItem pmi1033 = new PopMenuItem(mm1033, "转换轨(选车站)(&H)", pmi1030, -1);
// 最后保存文件
myCSection.SaveAs(myCUI);
}
- 参考意见和网页地址:
- https://blog.csdn.net/bullzerone/article/details/73744540
- https://www.cnblogs.com/bomb12138/p/3600842.html
5.项目下载地址如下:
链接:https://pan.baidu.com/s/1gjSYld7VF9HQmnrp8MwAMQ
提取码:62cm 复制这段内容后打开百度网盘手机App,操作更方便哦
链接:https://pan.baidu.com/s/1WToYTIxBtpLVCbUKRbgvdA
提取码:euuw 复制这段内容后打开百度网盘手机App,操作更方便哦