public static string disclaimer = " AutoCAD. – .";
using System; using Autodesk.AutoCAD.Runtime; using Autodesk.Windows; namespace MyAutoCADDll { public class Commands : IExtensionApplication { // AutoCAD "TestCommand" [CommandMethod("TestCommand")] public void MyCommand() { } // Initialize() Terminate() , IExtensionApplication public void Initialize() { } public void Terminate() { } } }
CopyLocal
!). In addition, you need to add links to the three assemblies of .NET itself: Presentation Core , Presentation Framework, and WindowsBase .Autodesk.Windows
namespace (it contains the AdWindows.dll container). // AutoCAD «TestCommand» [CommandMethod("TestCommand")] public void MyCommand() { // Autodesk.Windows.RibbonCombo comboBox1 = new RibbonCombo(); comboBox1.Id = "_combobox1"; // Autodesk.Windows.RibbonButton button1 = new Autodesk.Windows.RibbonButton(); button1.Id = "_button1"; // Autodesk.Windows.RibbonPanelSource rbPanelSource = new Autodesk.Windows.RibbonPanelSource(); rbPanelSource.Title = " "; // rbPanelSource.Items.Add(comboBox1); rbPanelSource.Items.Add(new RibbonSeparator()); rbPanelSource.Items.Add(button1); // RibbonPanel rbPanel = new RibbonPanel(); // rbPanel.Source = rbPanelSource; // RibbonTab rbTab = new RibbonTab(); rbTab.Title = " "; rbTab.Id = "HabrRibbon"; // rbTab.Panels.Add(rbPanel); // AutoCAD Autodesk.Windows.RibbonControl rbCtrl = ComponentManager.Ribbon; // rbCtrl.Tabs.Add(rbTab); // ("") rbTab.IsActive = true; }
ComponentManager.Ribbon.FindTab(string id)
method to search for a tab on a ribbon. As an argument, you must specify the tab Id
specified when it was created.ComponentManager.Ribbon.FindPanel(string id, bool SearchActiveTabOnly)
) and other controls ( ComponentManager.Ribbon.FindItem(string id, bool SearchActiveTabOnly)
).null
will be returned.CommandHandler
class's RibbonButton
. In this property, you must specify a method that implements the System.Windows.Input.ICommand
interface.ICommand
interface, the class must implement the CanExecuteChanged
event, as well as the CanExecute
and Execute
functions. using System; using Autodesk.AutoCAD.Runtime; using Autodesk.Windows; namespace MyAutoCADDll { public class Commands : IExtensionApplication { // AutoCAD "TestCommand" [CommandMethod("TestCommand")] public void MyCommand() { // Autodesk.Windows.RibbonCombo comboBox1 = new RibbonCombo(); comboBox1.Id = "_combobox1"; // Autodesk.Windows.RibbonButton button1 = new Autodesk.Windows.RibbonButton(); button1.Id = "_button1"; // button1.CommandHandler = new CommandHandler_Button1(); // Autodesk.Windows.RibbonPanelSource rbPanelSource = new Autodesk.Windows.RibbonPanelSource(); rbPanelSource.Title = " "; // rbPanelSource.Items.Add(comboBox1); rbPanelSource.Items.Add(new RibbonSeparator()); rbPanelSource.Items.Add(button1); // RibbonPanel rbPanel = new RibbonPanel(); // rbPanel.Source = rbPanelSource; // RibbonTab rbTab = new RibbonTab(); rbTab.Title = " "; rbTab.Id = "HabrRibbon"; // rbTab.Panels.Add(rbPanel); // AutoCAD Autodesk.Windows.RibbonControl rbCtrl = ComponentManager.Ribbon; // rbCtrl.Tabs.Add(rbTab); // ("") rbTab.IsActive = true; } // Initialize() Terminate() , IExtensionApplication public void Initialize() { } public void Terminate() { } } // public class CommandHandler_Button1 : System.Windows.Input.ICommand { public event EventHandler CanExecuteChanged; public bool CanExecute(object param) { return true; } public void Execute(object parameter) { System.Windows.MessageBox.Show("Habr!"); } } }
CanExecuteChanged
event notifies users of a team about a possible change in its availability for execution (in short, it works or does not work). The CanExecute
function allows CanExecute
to find out if a command is available for execution at a given point in time. And the Execute
function is actually those actions that the command should perform when it was called.RibbonCombo
)RibbonCombo
drop-down list RibbonCombo
contained in its Items
property. It has the type System.Collections.ObjectModel.ObservableCollection
, with System.Object
acting as the content type. Thus, a collection item can be an object of any class. Unfortunately, if we simply add several text lines to this array, we will not get the desired effect: comboBox1.Items.Add(""); comboBox1.Items.Add(""); comboBox1.Items.Add("");
RibbonButton
class discussed above as its elements: Autodesk.Windows.RibbonButton tempRibBut1 = new Autodesk.Windows.RibbonButton(); tempRibBut1.Id = "_temp_button_1"; tempRibBut1.Text = " 1"; tempRibBut1.ShowText = true; Autodesk.Windows.RibbonButton tempRibBut2 = new Autodesk.Windows.RibbonButton(); tempRibBut2.Id = "_temp_button_2"; tempRibBut2.Text = " 2"; tempRibBut2.ShowText = true; comboBox1.Items.Add(tempRibBut1); comboBox1.Items.Add(tempRibBut2);
ObservableCollection
, in particular:Remove(object item)
- to delete the specified item;RemoveAt(int index)
method RemoveAt(int index)
- to delete an element at a specified position;Clear()
method Clear()
- to remove all items from the collection;Count
property - to get the number of items in the collection.RibbonButton
is stored in its Current
property.Tag
property of the RibbonButton
class: Autodesk.Windows.RibbonButton tempRibBut1 = new Autodesk.Windows.RibbonButton(); tempRibBut1.Id = "_temp_button_1"; tempRibBut1.Text = " 1"; tempRibBut1.ShowText = true; tempRibBut1.Tag = "elementTag"; // comboBox1.Items.Add(tempRibBut1);
object obj = comboBox1.Items[0]; string itemTag = (obj as RibbonButton).Tag; // "elementTag"
Tag
property is of type System.Object
, an object of any class, including one created by the programmer itself, can act as a tag: tempRibBut1.Tag = new MyClass("objectDecription");
object obj = comboBox1.Items[0]; MyClass itemTag = (obj as RibbonButton).Tag as MyClass; string myClassDecription = itemTag.Description;
string myClassDecription = ((comboBox1.Items[0] as RibbonButton).Tag as MyClass).Description
null
.RibbonCombo
list RibbonCombo
, a CurrentChanged
event is generated. Here is a simple example of the event handler: // public static void comboBox1_CurrentChanged(object o, RibbonPropertyChangedEventArgs args) { if (args.NewValue != null) { System.Windows.MessageBox.Show((args.NewValue as RibbonButton).Text); } }
using System; using Autodesk.AutoCAD.Runtime; using Autodesk.Windows; namespace MyAutoCADDll { public class Commands : IExtensionApplication { // AutoCAD "TestCommand" [CommandMethod("TestCommand")] public void MyCommand() { // Autodesk.Windows.RibbonCombo comboBox1 = new RibbonCombo(); comboBox1.Id = "_combobox1"; // Autodesk.Windows.RibbonButton tempRibBut1 = new Autodesk.Windows.RibbonButton(); tempRibBut1.Id = "_temp_button_1"; tempRibBut1.Text = " 1"; tempRibBut1.ShowText = true; tempRibBut1.Tag = "btn1"; Autodesk.Windows.RibbonButton tempRibBut2 = new Autodesk.Windows.RibbonButton(); tempRibBut2.Id = "_temp_button_2"; tempRibBut2.Text = " 2"; tempRibBut2.ShowText = true; tempRibBut2.Tag = "btn2"; comboBox1.Items.Add(tempRibBut1); comboBox1.Items.Add(tempRibBut2); // comboBox1.CurrentChanged += comboBox1_CurrentChanged; // Autodesk.Windows.RibbonButton button1 = new Autodesk.Windows.RibbonButton(); button1.Id = "_button1"; // button1.CommandHandler = new CommandHandler_Button1(); // Autodesk.Windows.RibbonPanelSource rbPanelSource = new Autodesk.Windows.RibbonPanelSource(); rbPanelSource.Title = " "; // rbPanelSource.Items.Add(comboBox1); rbPanelSource.Items.Add(new RibbonSeparator()); rbPanelSource.Items.Add(button1); // RibbonPanel rbPanel = new RibbonPanel(); // rbPanel.Source = rbPanelSource; // RibbonTab rbTab = new RibbonTab(); rbTab.Title = " "; rbTab.Id = "HabrRibbon"; // rbTab.Panels.Add(rbPanel); // AutoCAD Autodesk.Windows.RibbonControl rbCtrl = ComponentManager.Ribbon; // rbCtrl.Tabs.Add(rbTab); // ("") rbTab.IsActive = true; } // public static void comboBox1_CurrentChanged(object o, RibbonPropertyChangedEventArgs args) { if (args.NewValue != null) { System.Windows.MessageBox.Show((args.NewValue as RibbonButton).Text); } } // Initialize() Terminate() , IExtensionApplication public void Initialize() { } public void Terminate() { } } // public class CommandHandler_Button1 : System.Windows.Input.ICommand { public event EventHandler CanExecuteChanged; public bool CanExecute(object param) { return true; } public void Execute(object parameter) { System.Windows.MessageBox.Show("Habr!"); } } }
using System; using System.Drawing; using Autodesk.AutoCAD.Runtime; using Autodesk.Windows; namespace MyAutoCADDll { public class Commands : IExtensionApplication { // AutoCAD "TestCommand" [CommandMethod("TestCommand")] public void MyCommand() { // ( ) Bitmap bmp = new Bitmap(1, 1); bmp.SetPixel(0, 0, Color.Aquamarine); bmp = new Bitmap(bmp, 1024, 1024); IntPtr hBitmap = bmp.GetHbitmap(); System.Windows.Media.Imaging.BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); // Autodesk.Windows.RibbonCombo comboBox1 = new RibbonCombo(); comboBox1.Id = "_combobox1"; comboBox1.Width = 200; comboBox1.Text = " 1"; comboBox1.ShowText = true; Autodesk.Windows.RibbonCombo comboBox2 = new RibbonCombo(); comboBox2.Id = "_combobox2"; comboBox2.Width = 200; comboBox2.Image = bs; comboBox2.ShowImage = true; // Autodesk.Windows.RibbonButton button1 = new Autodesk.Windows.RibbonButton(); button1.Id = "_button1"; Autodesk.Windows.RibbonButton button2 = new Autodesk.Windows.RibbonButton(); button2.Id = "_button2"; // , Autodesk.Windows.RibbonRowPanel RowPanel1 = new Autodesk.Windows.RibbonRowPanel(); Autodesk.Windows.RibbonRowPanel RowPanel2 = new Autodesk.Windows.RibbonRowPanel(); // RowPanel1.Items.Add(comboBox1); RowPanel1.Items.Add(new RibbonRowBreak()); RowPanel1.Items.Add(comboBox2); RowPanel2.Items.Add(button1); RowPanel2.Items.Add(new RibbonRowBreak()); RowPanel2.Items.Add(button2); // Autodesk.Windows.RibbonButton button3 = new Autodesk.Windows.RibbonButton(); button3.Id = "_button3"; button3.IsToolTipEnabled = true; button3.ToolTip = " "; button3.Size = Autodesk.Windows.RibbonItemSize.Large; button3.LargeImage = bs; Autodesk.Windows.RibbonButton button4 = new Autodesk.Windows.RibbonButton(); button4.Id = "_button4"; button4.Text = "^___^"; button4.ShowText = true; button4.Size = Autodesk.Windows.RibbonItemSize.Large; button4.LargeImage = bs; // Autodesk.Windows.RibbonPanelSource rbPanelSource1 = new Autodesk.Windows.RibbonPanelSource(); rbPanelSource1.Title = " "; Autodesk.Windows.RibbonPanelSource rbPanelSource2 = new Autodesk.Windows.RibbonPanelSource(); rbPanelSource2.Title = " "; // rbPanelSource1.Items.Add(RowPanel1); rbPanelSource1.Items.Add(RowPanel2); rbPanelSource1.Items.Add(new RibbonSeparator()); rbPanelSource1.Items.Add(button3); rbPanelSource2.Items.Add(button4); // RibbonPanel rbPanel1 = new RibbonPanel(); RibbonPanel rbPanel2 = new RibbonPanel(); // rbPanel1.Source = rbPanelSource1; rbPanel2.Source = rbPanelSource2; // RibbonTab rbTab = new RibbonTab(); rbTab.Title = " "; rbTab.Id = "HabrRibbon"; // rbTab.Panels.Add(rbPanel1); rbTab.Panels.Add(rbPanel2); // AutoCAD Autodesk.Windows.RibbonControl rbCtrl = ComponentManager.Ribbon; // rbCtrl.Tabs.Add(rbTab); // ("") rbTab.IsActive = true; } // Initialize() Terminate() , IExtensionApplication public void Initialize() { } public void Terminate() { } } }
Source: https://habr.com/ru/post/243305/
All Articles