在上一篇关于AutoCAD的文章中,我将很多关于CAD的博客相关资源进行了说明,这一篇文章我将介绍如何在AutoCAD中的ribbon中添加相应的按钮。就是下面这种按钮:

PS:在开发中我们最好使用中英文两个版本同时进行,因为有些时候那些接口或类中的命名和英文版中的名称一样的,这样方便理解!


-----------------------------------------------------分割线------------------------------------------------


开发工具 :VS2010

开发环境 :.Net Framework4.5

开发语言 :C#

AutoCAD版本:2014中/EN


-----------------------------------------------------分割线-----------------------------------------------

第一种:利用.cuix配置文件


如果你是一个CAD的老用户,你会知道这个配置文件,以前是.cui后缀的,新版本从2013开始是.cuix,不过本质上是一个东西;每当你打开AutoCAD的时候,它根据lisp加载的一大堆配置文件就在.cuix中,一般这些配置文件在C盘的隐藏文件夹里面,C:\Users\当前用户名(Administrator?)\AppData\Roaming\Autodesk\AutoCAD 2014\R19.1\chs\Support,不过最重要的是红色方框那个:

这里我们可以自定义一个自己的.cuix文件,然后再CAD中输入cui命令,打开用户自定义窗体,然后你可以将你的自定义文件cuix添加进去:

这里的mycui文件就是我们自定义的啦,未融入就是之前添加现在路经找不到文件咯!你需要在这个文件中新建自己的按钮:

就是这个东西,多试试你就知道怎么做了,然后给你自定义的按钮添加一个命令宏,也就是你在CAD之中点击这个按钮需要使用的命令,这个命令可以是自己代码写在DLL中加载到CAD中的,也可以是CAD内置的,自定义按钮当然一般是自定义命令啦!

当然你以为这样就行啦,那就错了,现在只是配置文件做好了,下一步我们需要将配置文件刚才弄好的按钮功能添加到ACAD下的功能区—》选项卡—》插件:

接下来你还需要选择工作空间,然后右键选择自定义工作空间(一般我们使用默认工作空间,当然也可以代码设置工作空间),然后将刚才的编辑好的插件添加进去

这样我们就配置好了我们的插件按钮了,最后我们需要输入‘netload’将我们的写好的dll命令添加进来就可以实现按钮互操作了,这里我们每次加载自定义的.cuix之后的操作也可以使用代码实现,我们在VS中安装了AutoCAD的插件开发环境后Autodesk新建项目,在myPlugin.cs这个文件的void IExtensionApplication.Initialize()方法中写入加载.cuix文件的代码,这样每次加载dll就会预先加载cuix文件,那么就不会出现cuix未融入的状态了;然后就是将我们写在cuix中的配置好的按钮添加到主功能区中去!

[csharp]  view plain copy print ?
  1. //使用cuix文件添加对应ribbonbutton
  2. //先判断是否添加配置cuix
  3. //将配置cuix中的配置功能项添加到acad.cuix中的功能区
  4. //刷新工作空间的功能区命令
  5. private void AddRibbonButtonByCustomCui()
  6. {
  7. string mainCuiFile = (string)Application.GetSystemVariable("MENUNAME");
  8. mainCuiFile += ".cuix";
  9. CustomizationSection csLoad = new CustomizationSection(mainCuiFile);
  10. PartialCuiFileCollection pPartialCuiFileCollection = csLoad.PartialCuiFiles;
  11. if (pPartialCuiFileCollection.Contains("mycui.cuix"))
  12. {
  13. System.Windows.Forms.MessageBox.Show("已加载插件!");
  14. Application.UnloadPartialMenu(strCuipath);
  15. //return;
  16. }
  17. bool isOK = Application.LoadPartialMenu(strCuipath);
  18. //加载自定义cui
  19. if (!isOK)
  20. {
  21. System.Windows.Forms.MessageBox.Show("加载自定义配置文件失败!");
  22. return;
  23. }
  24. //加载CUI
  25. //Application.QuitWillStart += new EventHandler(Application_BeginQuit);
  26. //Application.BeginQuit += new EventHandler(Application_BeginQuit);
  27. //Autodesk.Windows.ComponentManager.ApplicationMenu.Opening += new EventHandler<EventArgs>(ApplicationMenu_Opening);
  28. CustomizationSection cs = new CustomizationSection(mainCuiFile);
  29. PartialCuiFileCollection cuiFiles = cs.PartialCuiFiles;
  30. //acad.cuix配置文件
  31. if (cuiFiles.Contains("mycui.cuix"))
  32. {//将my.cuix文件中的配置按钮写入acad.cuix文件中去
  33. string strPartialCui = cuiFiles.GetFileNameByIndex(cuiFiles.IndexOf("mycui.cuix"));
  34. CustomizationSection csCustom = new CustomizationSection(strPartialCui);
  35. RibbonPanelSource pRibbonPanelSource = csCustom.MenuGroup.RibbonRoot.FindPanel("RBNU_191_C0DED");//自定义panel中的ElementID
  36. RibbonPanelSource pCloneRibbonPanelSource = pRibbonPanelSource.Clone() as RibbonPanelSource;
  37. cs.MenuGroup.RibbonRoot.RibbonPanelSources.Add(pCloneRibbonPanelSource);
  38. RibbonTabSource pRibbonTableSource2 = cs.MenuGroup.RibbonRoot.FindTab("RBN_00012112");//插件Tab的ElementID
  39. RibbonPanelSourceReference pRibbonPanelSourceRefrence = new RibbonPanelSourceReference(pRibbonTableSource2);
  40. //这一步ID一定要赋值
  41. pRibbonPanelSourceRefrence.PanelId = pCloneRibbonPanelSource.ElementID;
  42. pRibbonTableSource2.Items.Add(pRibbonPanelSourceRefrence);
  43. cs.Save();
  44. Application.ReloadAllMenus();//否则不现实按钮
  45. }
  46. }

最后的按钮效果啦。。。

这一篇主要讲了太多关于配置的流程,大多可以使用手动配置实现,核心是需要自定义的配置文件cuix,然后读取他的命令按钮添加到我们主ribbon中,下两篇将从纯代码开始添加按钮,但是不同的命名空间!



上一篇相关文章主要借助了cuix配置文件来制作插件按钮,但是对于纯码农来说还是喜欢以代码来说话,今天这篇文章就来讲讲纯代码添加按钮。

开发IDE:VS2010

环境:.Net Framework4.0

AutoCAD版本:2014中/EN

今天介绍的代码主要借助的是AcCui.dll这个动态链接库,因为在我的了解中,CAD的开发库中有很多类似的类,又没有相关的介绍API的文档(你不是专业人员真是心累~),都是自己尝试或者在AutoDesk社区中找到的相关内容。

下面先说说思路,再添加核心代码,如果你有需要,欢迎关注我或者联系我,我很愿意与你共享资源与共同学习进步

1.首先还是要找到相关的主cuix文件,就是AutoCAD二次开发三种添加插件按钮的方法之一中介绍的acad.cuix文件(我觉得添加图片排版太不方便了,所以尽量减少图片~);

[csharp]  view plain copy print ?
  1. //获取到ACAD.cuix
  2. CustomizationSection cs = new CustomizationSection((string)Application.GetSystemVariable("MENUNAME"));
  3. string strCurWorkspace = (string)Application.GetSystemVariable("WSCURRENT");
  4. //workspace的操作
  5. Workspace curWorkspace = cs.getWorkspace(strCurWorkspace);
  6. if (IsExistPluginTab(ed, curWorkspace))//如果自定义工作空间存在指定的Tab
  7. {
  8. //初始化功能按钮
  9. InitialRibbonBtn(ed, cs);
  10. }

2.由于我们是需要将按钮添加到‘插件’这个Tab中,而这个Tab是在Ribbon中的(而AutoCAD又是可以自定义是否显示Ribbon的,而且不同的工作空间显示的Ribbon还不同,所以我这里尽量简化了,不讨论那些了),这个Tab的ElementID叫做RBN_00012112,我们可以根据此判断这个Tab是否存在还是被删掉了(默认安装时存在的);这里只讨论存在的咯。

3.如果存在,我们直接获取到这个Tab,在其中在其中添加一个Panel按钮;

[csharp]  view plain copy print ?
  1. /// <summary>
  2. /// 在acad.cuix中的(选项卡)Tab中添加panel
  3. /// </summary>
  4. /// <param name="cs">acad.cuix的引用</param>
  5. /// <param name="tabName">Tab的中文名字</param>
  6. /// <param name="tabEnName">Tab的英文版名称</param>
  7. /// <param name="panelName">新建的Panel名称(自定义)</param>
  8. /// <returns></returns>
  9. private  Autodesk.AutoCAD.Customization.RibbonPanelSource AddRibbonPanelToTab(CustomizationSection cs, string tabName,string tabEnName, string panelName)
  10. {
  11. RibbonRoot root = cs.MenuGroup.RibbonRoot;
  12. Autodesk.AutoCAD.Customization.RibbonPanelSourceCollection panels = root.RibbonPanelSources;
  13. RibbonTabSource rts = root.FindTab("RBN_00012112");//ElementID
  14. if (rts == null)
  15. {
  16. Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("未找到指定的插件Tab");
  17. }
  18. if (rts.Name == tabName || rts.Name == tabEnName)
  19. {
  20. //创建一个panel并添加到panels集合中
  21. Autodesk.AutoCAD.Customization.RibbonPanelSource panelSrc = new Autodesk.AutoCAD.Customization.RibbonPanelSource(root);
  22. panelSrc.Text = panelSrc.Name = panelName;
  23. panelSrc.ElementID = panelSrc.Id = panelName + "_PanelSourceID";
  24. panels.Add(panelSrc);
  25. RibbonPanelSourceReference ribPanelSourceRef = new RibbonPanelSourceReference(rts);
  26. ribPanelSourceRef.PanelId = panelSrc.ElementID;
  27. rts.Items.Add(ribPanelSourceRef);
  28. return panelSrc;
  29. }
  30. return null;
  31. }

4.在这个自定义的Panel直接添加按钮,最后重新加载所有menu。

[csharp]  view plain copy print ?
  1. private void InitialRibbonBtn(Editor ed, CustomizationSection cs)
  2. {
  3. Autodesk.AutoCAD.Customization.RibbonPanelSource panelSrc = AddRibbonPanelToTab(cs, "插件", "Plug-ins","测试Panel");
  4. MacroGroup macGroup = cs.MenuGroup.MacroGroups[0];
  5. //int macroCount = cs.MenuGroup.MacroGroups.Count;
  6. //foreach (MacroGroup macro in cs.MenuGroup.MacroGroups)
  7. //{
  8. //    string name = macro.Name;
  9. //}
  10. RibbonRow row = new RibbonRow();
  11. panelSrc.Items.Add((Autodesk.AutoCAD.Customization.RibbonItem)row);
  12. RibbonCommandButton button1 = new RibbonCommandButton(row);
  13. button1.Text = "测试LargeBtn1";
  14. MenuMacro menuMac1 = macGroup.CreateMenuMacro("Button1_Macro", "^C^CButton1_Command ", "Button1_Tag", "Button1_Help",
  15. MacroType.Any, "RibbonImages//test16.png", "RibbonImages//test32.png", "Button1_Label_Id");
  16. button1.MacroID = menuMac1.ElementID;
  17. button1.ButtonStyle = RibbonButtonStyle.LargeWithText;
  18. button1.KeyTip = "Button1 Key Tip";
  19. button1.TooltipTitle = "Button1 Tooltip Title!";
  20. row.Items.Add((Autodesk.AutoCAD.Customization.RibbonItem)button1);
  21. RibbonCommandButton button2 = new RibbonCommandButton(row);
  22. button2.Text = "测试SmallBtn1";
  23. MenuMacro menuMac2 = macGroup.CreateMenuMacro("Button2_Macro", "^C^CButton2_Command ", "Button2_Tag", "Button2_Help",
  24. MacroType.Any, "RibbonImages//test16.png", "RibbonImages//test32.png", "Button2_Label_Id");
  25. button2.MacroID = menuMac2.ElementID;//ID不能少
  26. button2.ButtonStyle = RibbonButtonStyle.SmallWithText;
  27. button2.KeyTip = "Button2 Key Tip";
  28. button2.TooltipTitle = "Button2 Tooltip Title!";
  29. row.Items.Add((Autodesk.AutoCAD.Customization.RibbonItem)button2);
  30. RibbonCommandButton button3 = new RibbonCommandButton(row);
  31. button3.Text = "测试LargeBtn3";
  32. MenuMacro menuMac3 = macGroup.CreateMenuMacro("Button3_Macro", "^C^CButton3_Command ", "Button3_Tag", "Button3_Help",
  33. MacroType.Any, "RibbonImages//test16.png", "RibbonImages//test32.png", "Button3_Label_Id");
  34. button3.MacroID = menuMac3.ElementID;
  35. button3.ButtonStyle = RibbonButtonStyle.LargeWithText;
  36. button3.KeyTip = "Button3 Key Tip";
  37. button3.TooltipTitle = "Button3 Tooltip Title!";
  38. row.Items.Add((Autodesk.AutoCAD.Customization.RibbonItem)button3);
  39. cs.Save();
  40. Application.ReloadAllMenus();//重加载所有的按钮
  41. ed.WriteMessage("Add buttons successed!");
  42. }

最后,效果和第一篇最后的效果是一样的哦!~

再之前两篇的介绍中,无论是第一篇还是第二篇都需要依赖于AutoCAD中的acad.cuix文件,这是写插件所不能容忍的啊!所以这一篇我们不依赖任何的配置,直接加载dll并输入相应的命令即可实现添加命令按钮的操作!这次我们需要使用的dll是AcWindows.dll!~

逻辑如下:

1.如果没有打开Ribbon,控制命令行打开

2.判断当前的Tabs中是否存在同title的Tab,初次加载不会存在的

3.添加Panel并在panel中添加button

[csharp]  view plain copy print ?
  1. private const string New_Tab_ID = "New_Tab_ID";
  2. private bool PluginAdded = false;//是否已经加载了插件--读取配置文件判断
  3. [CommandMethod("addplugin")]
  4. public void AddPlugIn()
  5. {
  6. if (Autodesk.Windows.ComponentManager.Ribbon == null)
  7. {//如果Ribbon没有打开,直接操作命令行打开
  8. Application.DocumentManager.MdiActiveDocument.SendStringToExecute("_.ribbon\r", true, false, false);
  9. }
  10. if (PluginAdded)
  11. {
  12. System.Windows.Forms.MessageBox.Show("自定义插件已加载!");
  13. return;
  14. }
  15. bool TabExist = false;//名称为自定义插件的tab是否存在
  16. Autodesk.Windows.RibbonControl pRibbonControl = Autodesk.Windows.ComponentManager.Ribbon;
  17. foreach (RibbonTab itemTab in pRibbonControl.Tabs)
  18. {
  19. if (itemTab.Title == "自定义插件Tab")
  20. {
  21. AddNewPanel(itemTab);
  22. TabExist = true;
  23. }
  24. }
  25. if (!TabExist)
  26. {
  27. RibbonTab newTab = new RibbonTab();
  28. newTab.Title = "自定义插件Tab";
  29. newTab.Id = New_Tab_ID;
  30. pRibbonControl.Tabs.Add(newTab);
  31. AddNewPanel(newTab);
  32. newTab.IsActive = true;
  33. }
  34. }
  35. //添加新的panel
  36. private void AddNewPanel(RibbonTab newTab)
  37. {
  38. try
  39. {
  40. Autodesk.Windows.RibbonPanelSource panelSource = new Autodesk.Windows.RibbonPanelSource();
  41. panelSource.Title = "自定义插件Panel";
  42. RibbonPanel newRibbonPanel = new RibbonPanel();
  43. newRibbonPanel.Source = panelSource;
  44. newTab.Panels.Add(newRibbonPanel);
  45. Autodesk.Windows.RibbonButton pButton = new Autodesk.Windows.RibbonButton();
  46. pButton.Text = "自定义插件button";
  47. pButton.ToolTip = "自定义的button1测试";
  48. pButton.ShowToolTipOnDisabled = true;
  49. pButton.IsToolTipEnabled = true;
  50. pButton.Size = RibbonItemSize.Large;
  51. pButton.LargeImage = LoadImage(AutoCAD_CSharp_plug_in1_2010.Resource1.test32);
  52. pButton.Image = LoadImage(AutoCAD_CSharp_plug_in1_2010.Resource1.test16);
  53. pButton.Orientation = System.Windows.Controls.Orientation.Vertical;
  54. pButton.ShowText = true;
  55. pButton.ShowImage = true;
  56. pButton.CommandParameter = "plugincommand ";
  57. pButton.CommandHandler = new TestCommandHandler();
  58. Autodesk.Windows.RibbonButton pRemovePluginButton = new Autodesk.Windows.RibbonButton();
  59. pRemovePluginButton.Text = "删除插件按钮";
  60. pRemovePluginButton.ToolTip = "自定义的button2测试";
  61. pRemovePluginButton.ShowToolTipOnDisabled = true;
  62. pRemovePluginButton.IsToolTipEnabled = true;
  63. pRemovePluginButton.Size = RibbonItemSize.Standard;
  64. pRemovePluginButton.LargeImage = LoadImage(AutoCAD_CSharp_plug_in1_2010.Resource1.test32);
  65. pRemovePluginButton.Image = LoadImage(AutoCAD_CSharp_plug_in1_2010.Resource1.test16);
  66. pButton.Orientation = System.Windows.Controls.Orientation.Vertical;//小按钮都是横向的,即使设置纵向
  67. pRemovePluginButton.ShowImage = true;
  68. pRemovePluginButton.ShowText = true;
  69. pRemovePluginButton.CommandParameter = "removeplugin ";
  70. pRemovePluginButton.CommandHandler = new RemovePluginCommandHandler();
  71. panelSource.Items.Add(pButton);
  72. panelSource.Items.Add(pRemovePluginButton);
  73. }
  74. catch (System.Exception ex)
  75. {
  76. Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage( Environment.NewLine,ex.Message);
  77. }
  78. }
  79. //--配置图片
  80. private System.Windows.Media.Imaging.BitmapImage LoadImage(System.Drawing.Bitmap bitmap)
  81. {
  82. System.IO.MemoryStream ms = new System.IO.MemoryStream ();
  83. bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  84. System.Windows.Media.Imaging.BitmapImage bitImage = new BitmapImage();
  85. bitImage.BeginInit();
  86. bitImage.StreamSource = ms;
  87. bitImage.EndInit();
  88. return bitImage;
  89. }

每个 button都有一个CommandHandler属性,这个就是我们写我们自定义的触发按钮事件的类,继承于System.Windows.Input.ICommand!

至此,三种方式都写完了,这里小结一下。

总体来说这三种方式前两种依赖性较强,依赖于acad.cuix文件,第三种非要说依赖性的话,依赖于Ribbon一定要是打开状态的;第一种的配置文件做好了的话,甚至可以不写代码添加按钮代码就可以实现按钮(但是自定义CommandMethod还是要写的);第二种代码添加按钮写起来较为简便,条理也比较清晰;第三种完全基于代码实现,比较适合纯码农们!也比较适合插件开发的真正实施!亲们喜欢哪种就自己选吧!~

AutoCAD二次开发三种添加插件按钮的方法相关推荐

  1. AutoCAD二次开发三种添加插件按钮的方法之二

    上一篇相关文章主要借助了cuix配置文件来制作插件按钮,但是对于纯码农来说还是喜欢以代码来说话,今天这篇文章就来讲讲纯代码添加按钮. 开发IDE:VS2010 环境:.Net Framework4.0 ...

  2. AutoCAD二次开发三种添加插件按钮的方法之一

    在上一篇关于AutoCAD的文章中,我将很多关于CAD的博客相关资源进行了说明,这一篇文章我将介绍如何在AutoCAD中的ribbon中添加相应的按钮.就是下面这种按钮: PS:在开发中我们最好使用中 ...

  3. Android Studio 三种添加插件的方式,androidstudio

    前几篇blog我们介绍了如何安装和配置Android Studio,这篇Blog我们来聊聊如何给Android Studio添加插件,添加插件的路径有三种,我把他们分类如下: 点击设置小按钮 点击[P ...

  4. ubuntu三种添加环境变量的方法

    第一种临时设置,用 export 指令,如在$PATH中增加JAVA文件夹: $export PATH=$PATH:/usr/local/lib/jdk1.6.0_25 二:用于当前用户: 在用户主目 ...

  5. AutoCAD二次开发基础(三):船体型线绘制

    AutoCAD二次开发系列 文章目录 前言 一.绘制船体横剖线 二.绘制船体纵剖线 三.绘制船体肋骨型线图 前言 用程序生成船体型线图,无疑会提高开发效率. 一.绘制船体横剖线 任务:编制程序,根据水 ...

  6. python能二次开发cad么_2,手动创建CAD二次开发项目--AutoCAD二次开发(2020版)

    本项目使用手动创建,意为不使用SDK模板. 从Visual Studio的"文件"下拉菜单中,选择"新建"->"项目...". 在出现 ...

  7. [转载][AutoCAD二次开发][2017]Autocad2017 ObjectARX 开发 环境配置和踩到的坑

    转自 https://www.cnblogs.com/aweffr/p/7510544.html 其中 链接器(Linker)中添加附加依赖目录和附加依赖项 版本号记得改成自己的版本就好 碎碎念 不得 ...

  8. [AutoCAD二次开发][2017]Autocad2017 ObjectARX 开发 环境配置和踩到的坑

    碎碎念 不得不说autocad二次开发的相关资料真的少,大多数还很旧.图书馆里VBA的一本最近出版时间是2006,AutoLisp的2013(还是个十二五规划教材),ObjectARX的书是2014年 ...

  9. autocad型源代码_VB与AUTOCAD二次开发源代码包

    在发个VB与AUTOCAD二次开发源代码包,包括以下内容: 在学校的话,叫计算机辅助设计 第一章 VB开发AotoCAD基础知识 第一节 欢迎进入VB开发AUTOCAD的精彩世界 第二节 VB 开发 ...

最新文章

  1. wxWidgets:国际化
  2. mysql获取一年中的所有周六周日_数据库查询显示一年中所有的周一到周五的数据...
  3. CF932E-Team Work【斯特林数,组合数学】
  4. python 抓取微博评论破亿_如果利用Python分析14亿条数据!资深程序员手把手教你!过亿级!...
  5. markdown简明使用语法
  6. iec104点号_IEC104规约详细讲解全解.ppt
  7. RNA 22. SCI 文章中基于表达估计恶性肿瘤组织的基质细胞和免疫细胞(ESTIMATE)
  8. EJB到底是什么,真的那么神秘吗?
  9. 网络安全实验室-脚本关1-15
  10. 数码管SR420561K SR410561K的引脚图
  11. java怎么使用sni,启用SNI扩展的SSL握手 - 服务器上的证书选择
  12. 用天翎低代码做办公协同软件,简单高效
  13. Activiti6:解决定时器不执行的问题(定时catching事件的使用)
  14. java pdf 签名_java – PDFBox 1.8.10:填充和签名PDF生成无效签名
  15. 有一种程序员的浪漫叫做,有程序为你宕过机
  16. 2021年广东工业大学第十五届文远知行杯程序设计竞赛(同步赛)部分题解
  17. [Linux学习]语系查询及设置
  18. 测试必须要知道的四个主要阶段
  19. matlab 语音信号采集,MATLAB语音信号采集与处理.doc
  20. Java新手小白入门篇 JDK安装及环境变量配置(超详细)

热门文章

  1. idea 2017 jsp开发环境的搭建
  2. 【全文】狼叔:如何正确的学习Node.js 1
  3. linux内网穿梭无法使用,教你在 Linux 下时光穿梭
  4. 查缺补漏系统学习 EF Core 6 - 数据查询
  5. 给出一百分制成绩,输出成绩等级ABCDE(基础作业
  6. 第五十课第十二章Managing Indexes
  7. Hermite矩阵与正交矩阵-定义及应用
  8. 基于SSM的公益捐赠管理系统设计与实现 Spring+SpringMVC+MyBatis
  9. 假装用某米赛尔号的角度看Python面向对象编程
  10. 鸿蒙系统输入法,讯飞输入法鸿蒙版下载-讯飞输入法 鸿蒙版v10.0.16-PC6鸿蒙网