AutoCAD二次开发添加Ribbon菜单
对一款软件进行二次开发,不可避免的要添加菜单或者功能栏,通过一些按钮触发命令。以下是对AutoCAD二次开发添加Ribbon菜单的代码编写:
本文参考的文章如下:
https://www.cnblogs.com/junqilian/archive/2012/06/18/2553662.html
https://blog.csdn.net/hisinwang/article/details/78797068
利用vs添加AutoCAD 的.net Plugin之后,找到解决方案中的myPlugin(在myCommands中写代码需要利用命令进行操作,在myPlugin中编写可以直接自动加载Ribbon),把代码写在Initialize()中。
1、声明需要添加的Ribbon Tab页:
1 Autodesk.Windows.RibbonControl ribbonControl = Autodesk.Windows.ComponentManager.Ribbon; 2 RibbonTab Tab = new RibbonTab(); 3 Tab.Title = "图层权限管理"; 4 Tab.Id = "RibbonSample_TAB_ID"; 5 6 ribbonControl.Tabs.Add(Tab);
2、 创建新的Panel:
1 // create Ribbon panels 2 Autodesk.Windows.RibbonPanelSource panel1Panel = new RibbonPanelSource(); 3 panel1Panel.Title = "图层管理"; 4 RibbonPanel Panel1 = new RibbonPanel(); 5 Panel1.Source = panel1Panel; 6 Tab.Panels.Add(Panel1);
3、 添加按钮到Panel中:(添加按钮时,在函数的参数中,输入cmd参数后加一个空格,可以直接触发命令,不加空格代表只是输入,还需要点击Enter键。)
1 RibbonButton pan1btnAddLayers = NewRibbonButton("添加图层", "ADDLAYER ",Resource1.AddLayer16x16,Resource1.AddLayer32x32); 2 RibbonButton pan1btnEraseLayers = NewRibbonButton("允许导出", "DelteObjects ", Resource1.AddLayer16x16, Resource1.AddLayer32x32); 3 4 RibbonRowPanel pan1row1 = new RibbonRowPanel(); 5 pan1row1.Items.Add(pan1btnEraseLayers); 6 pan1row1.Items.Add(new RibbonRowBreak()); 7 8 panel1Panel.Items.Add(pan1btnAddLayers); 9 //panel1Panel.Items.Add(new RibbonSeparator()); 10 panel1Panel.Items.Add(pan1row1);
4、创建一个按钮触发的自定义类commandHandlers:用来实现System.Windows.Input.ICommand接口,在Execute时把commandParameter发送到AutoCAD命令行窗口执行。
1 using System; 2 using System.Collections.Generic; 3 //using System.Linq; 4 //using System.Text; 5 using Autodesk.Windows; 6 7 8 namespace AutoCAD_Debugger 9 { 10 class RibbonCommandHandler : System.Windows.Input.ICommand 11 { 12 public bool CanExecute(object parameter) 13 { 14 return true; 15 } 16 17 public event EventHandler CanExecuteChanged; 18 19 public void Execute(object parameter) 20 { 21 //is from Ribbon Button 22 RibbonButton ribBtn = parameter as RibbonButton; 23 if (ribBtn != null) 24 { 25 //execute the command 26 Autodesk.AutoCAD.ApplicationServices.Application 27 .DocumentManager.MdiActiveDocument 28 .SendStringToExecute( 29 (string)ribBtn.CommandParameter, true, false, true); 30 } 31 } 32 } 33 }
5、 设置按钮属性的函数:参数分别代表按钮名称、触发命令(myCommands中编写命令)、显示小图片、显示大图片。
1 private static RibbonButton NewRibbonButton(string text, string cmd,Bitmap image, Bitmap largeimage) 2 { 3 RibbonButton button = new RibbonButton(); 4 button.Text = text; 5 button.ShowText = true; 6 button.Image = imagesourceForBitmap(image); 7 button.LargeImage = imagesourceForBitmap(largeimage); 8 button.ShowImage = true; 9 button.Size = RibbonItemSize.Large; 10 button.Orientation = System.Windows.Controls.Orientation.Vertical; 11 button.CommandHandler = new RibbonCommandHandler(); 12 button.CommandParameter = cmd; 13 14 return button; 15 }
6、在添加处理图片的函数之前需要把按钮图标添加到解决方案中:选择项目-添加-资源文件,添加一个资源文件夹之后,把32x32和16x16的png图片拖拽到资源文件夹中。
7、处理图片的函数:
1 [DllImport("gdi32.dll", EntryPoint = "DeleteObject")] 2 [return: MarshalAs(UnmanagedType.Bool)] 3 public static extern bool DeleteObject([In] IntPtr hObject); 4 5 public static ImageSource imagesourceForBitmap(Bitmap bmp) 6 { 7 var handle = bmp.GetHbitmap(); 8 try 9 { 10 return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 11 } 12 finally { DeleteObject(handle); } 13 }
至此,已完成AutoCAD的Ribbon菜单的编写工作。进行测试,打开AutoCAD—命令行输入netload—选择编译生成的.dll文件—确定加载一次或者永久加载或者取消,就可以看到自己设计的Ribbon菜单。
AutoCAD添加Ribbon菜单的基本过程应该就是这样,如果你正在寻找这方面的资料,可以参考。