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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| using Autodesk.AutoCAD.Runtime; using System; using System.Linq; using Autodesk.Windows; using System.Windows.Media.Imaging; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using System.Windows.Input;
namespace CADButtonDemo { public class Class1 { [CommandMethod("InitMyRibbon")] public void Init() { string ribbonTabTitle = "DSS-定制插件"; string ribbonPanleTitle = "自定义面板"; string ribbonButtonTitle = "自定义按钮"; RibbonControl ribbonControl = ComponentManager.Ribbon; RibbonTab ribbonTab = ribbonControl.FindTab(ribbonTabTitle); if (ribbonTab == null) { ribbonTab = new RibbonTab { Title = ribbonTabTitle, Id = ribbonTabTitle }; ribbonControl.Tabs.Add(ribbonTab); } RibbonPanel ribbonPanel; if (ribbonTab.Panels == null || ribbonTab.Panels.Where(x => x.Source.Title == ribbonPanleTitle).Count() == 0) { RibbonPanelSource ribbonPanelSource = new RibbonPanelSource { Title = ribbonPanleTitle }; ribbonPanel = new RibbonPanel { Source = ribbonPanelSource }; ribbonTab.Panels.Add(ribbonPanel); } else { ribbonPanel = ribbonTab.Panels.First(x => x.Source.Title == ribbonPanleTitle); } if (ribbonPanel.Source == null || ribbonPanel.Source.Items == null || ribbonPanel.Source.Items.Where(x => x.Name == ribbonButtonTitle).Count()== 0) { RibbonButton ribbonButton = new RibbonButton { Orientation = System.Windows.Controls.Orientation.Vertical, AllowInStatusBar = true, Size = RibbonItemSize.Large, Name = ribbonButtonTitle, ShowText = true, Text = ribbonButtonTitle, Description = ribbonButtonTitle, LargeImage = new BitmapImage(new Uri("pack://application:,,,/CADButtonDemo;component/image/按钮图标.png", UriKind.Absolute)), CommandHandler = new ButtonCommandHandler(), CommandParameter ="test\n" }; ribbonPanel.Source.Items.Add(ribbonButton); } } [CommandMethod("test")] public void TestCommand() { Line line = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0)); Line line2 = new Line(new Point3d(0, 0, 0), new Point3d(-1, 1, 0)); using (IDisposable db = HostApplicationServices.WorkingDatabase, trans = (db as Database).TransactionManager.StartTransaction(), blockTable = (trans as Transaction).GetObject((db as Database).BlockTableId, OpenMode.ForRead), blockTableRecord = (trans as Transaction).GetObject((blockTable as BlockTable)[BlockTableRecord.ModelSpace], OpenMode.ForWrite)) { (blockTableRecord as BlockTableRecord).AppendEntity(line); (blockTableRecord as BlockTableRecord).AppendEntity(line2); (trans as Transaction).AddNewlyCreatedDBObject(line, true); (trans as Transaction).AddNewlyCreatedDBObject(line2, true); (trans as Transaction).Commit(); } } }
public class ButtonCommandHandler : ICommand { public bool CanExecute(object parameter) { return true; }
public event EventHandler CanExecuteChanged;
public void Execute(object parameter) { RibbonButton ribBtn = parameter as RibbonButton; if (ribBtn != null) { Autodesk.AutoCAD.ApplicationServices.Application .DocumentManager.MdiActiveDocument .SendStringToExecute( (string)ribBtn.CommandParameter, true, false, true); } } } }
|