📜 ⬆️ ⬇️

Build your tab on the ribbon (Ribbon) AutoCad means. Net (C #)

In this publication, we will look at an example of software building your own tab on a tape, as well as several problematic issues related to tape.

Recently, the innovation under the name Ribbon has become very popular. Autodesk did not lag behind life and, starting from the 2009 auto-cascade, also began to use tape.

We will look at an example of creating a tab on a ribbon for the 2010 auto-cad and consider several related "problem" issues. Why for him? Yes, because in 2009 the tape was still “raw” (but you can do it there too), and because for the subsequent auto-cad (2011-2013) code will be similar.
')
There are several options to add your tab to the tape:

Note : I will not delve into every detail and describe everything “beautifully” - the example is not complicated. Yes, and I can not express so beautifully.

The example is considered taking into account that you know how to write (at least the simplest) plug-ins for auto-cad tools using .net. For work, I use Microsoft Visual Studio 2010.

So let's get started:
1. Open VS2010 and create a new project:



2. Next, connect links to our project:



3. Now we will consider what the tape consists of by the example of this image:



Here, in principle, nothing to describe.

4. Add two 16x16 and 32x32 icons to our project in .png format. As I am lazy, I used the same picture as the first available on the Internet.

The main thing for these files is to set the Resource value for the Action at build parameter:



5. Go to the code. Open the Class1.cs file (or you can create your own), delete the Class1 class, and create your own class inherited from IExtensionApplication . Let's call it ExampleRibbon .

Further, I wanted to somehow describe all the actions step by step, but I decided that it would be more convenient and more understandable to bring all the code with explanations right away:
All main project code
using System; using System.Collections.Generic; using System.Linq; using System.Text; // Acad using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.Windows; using acadApp = Autodesk.AutoCAD.ApplicationServices.Application; namespace ACadRibbon { public class ExampleRibbon : IExtensionApplication { //    public void Initialize() { /*      : *    ,    *   , ..  ()  *    */ //Autodesk.Windows.ComponentManager.ItemInitialized += new EventHandler(ComponentManager_ItemInitialized); // ..     NETLOAD,       BuildRibbonTab(); } //     public void Terminate() { //        } /*   *      . *    ,  ""   , * ,      */ void ComponentManager_ItemInitialized(object sender, Autodesk.Windows.RibbonItemEventArgs e) { // ,    if (Autodesk.Windows.ComponentManager.Ribbon != null) { //    BuildRibbonTab(); //    ,     Autodesk.Windows.ComponentManager.ItemInitialized -= new EventHandlerRibbonItemEventArgs>(ComponentManager_ItemInitialized); } } //   void BuildRibbonTab() { //      if (!isLoaded()) { //   CreateRibbonTab(); //       acadApp.SystemVariableChanged += new SystemVariableChangedEventHandler(acadApp_SystemVariableChanged); } } //  ""  bool isLoaded() { bool _loaded = false; RibbonControl ribCntrl = Autodesk.Windows.ComponentManager.Ribbon; //      foreach (RibbonTab tab in ribCntrl.Tabs) { //        ,     if (tab.Id.Equals("RibbonExample_ID") & tab.Title.Equals("RibbonExample")) { _loaded = true; break; } else _loaded = false; } return _loaded; } /*      *      */ void RemoveRibbonTab() { try { RibbonControl ribCntrl = Autodesk.Windows.ComponentManager.Ribbon; //      foreach (RibbonTab tab in ribCntrl.Tabs) { if (tab.Id.Equals("RibbonExample_ID") & tab.Title.Equals("RibbonExample")) { //        ,     ribCntrl.Tabs.Remove(tab); //    acadApp.SystemVariableChanged -= new SystemVariableChangedEventHandler(acadApp_SystemVariableChanged); //   break; } } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { Autodesk.AutoCAD.ApplicationServices.Application. DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message); } } /*      *      WSCURRENT (  ), *     ""     */ void acadApp_SystemVariableChanged(object sender, SystemVariableChangedEventArgs e) { if (e.Name.Equals("WSCURRENT")) BuildRibbonTab(); } //    void CreateRibbonTab() { try { //     RibbonControl ribCntrl = Autodesk.Windows.ComponentManager.Ribbon; //    RibbonTab ribTab = new RibbonTab(); ribTab.Title = "RibbonExample"; //   ribTab.Id = "RibbonExample_ID"; //   ribCntrl.Tabs.Add(ribTab); //     //      ( ) addExampleContent(ribTab); //    ( ,  ) //ribTab.IsActive = true; //   (   ,  ) ribCntrl.UpdateLayout(); } catch (System.Exception ex) { Autodesk.AutoCAD.ApplicationServices.Application. DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message); } } //       void addExampleContent(RibbonTab ribTab) { try { //  panel source RibbonPanelSource ribSourcePanel = new RibbonPanelSource(); ribSourcePanel.Title = "RibbonExample"; //    RibbonPanel ribPanel = new RibbonPanel(); ribPanel.Source = ribSourcePanel; ribTab.Panels.Add(ribPanel); //   tooltip ( ) RibbonToolTip tt; //  split button RibbonSplitButton risSplitBtn = new RibbonSplitButton(); /*  RibbonSplitButton    *  Text,        *   . */ risSplitBtn.Text = "RibbonSplitButton"; //   risSplitBtn.Orientation = System.Windows.Controls.Orientation.Vertical; //   risSplitBtn.Size = RibbonItemSize.Large; //   risSplitBtn.ShowImage = true; //   risSplitBtn.ShowText = true; //   risSplitBtn.ListButtonStyle = Autodesk.Private.Windows.RibbonListButtonStyle.SplitButton; risSplitBtn.ResizeStyle = RibbonItemResizeStyles.NoResize; risSplitBtn.ListStyle = RibbonSplitButtonListStyle.List; /*        *   ,   RibbonSplitButton */ #region - â„–1 //     tt = new RibbonToolTip(); //    (    ) tt.IsHelpEnabled = false; //   RibbonButton ribBtn = new RibbonButton(); /*   CommandParameter ( ) *    Command ( )  *    */ ribBtn.CommandParameter = tt.Command = "_Line"; //   ribBtn.Name = "ExampleButton1"; //     ribBtn.Text = tt.Title = "- â„–1"; //   ()   (.) ribBtn.CommandHandler = new RibbonCommandHandler(); //   ribBtn.Orientation = System.Windows.Controls.Orientation.Horizontal; //   ribBtn.Size = RibbonItemSize.Large; /* ..    Large,   *       (.) */ ribBtn.LargeImage = LoadImage("icon_32"); //   ribBtn.ShowImage = true; //   ribBtn.ShowText = true; //     tt.Content = "  â„–1.      "; //     ribBtn.ToolTip = tt; //    RibbonSplitButton risSplitBtn.Items.Add(ribBtn); #endregion //     risSplitBtn.Current = ribBtn; //         #region - â„–2 tt = new RibbonToolTip(); tt.IsHelpEnabled = false; ribBtn = new RibbonButton(); ribBtn.CommandParameter = tt.Command = "_Pline"; ribBtn.Name = "ExampleButton2"; ribBtn.Text = tt.Title = "- â„–2"; ribBtn.CommandHandler = new RibbonCommandHandler(); ribBtn.Orientation = System.Windows.Controls.Orientation.Horizontal; ribBtn.Size = RibbonItemSize.Large; ribBtn.LargeImage = LoadImage("icon_32"); ribBtn.ShowImage = true; ribBtn.ShowText = true; tt.Content = "  â„–2.      "; ribBtn.ToolTip = tt; risSplitBtn.Items.Add(ribBtn); #endregion //  RibbonSplitButton    ribSourcePanel.Items.Add(risSplitBtn); //    RibbonRowPanel ribRowPanel = new RibbonRowPanel(); //       . //      ( ) #region - â„–3 tt = new RibbonToolTip(); tt.IsHelpEnabled = false; ribBtn = new RibbonButton(); ribBtn.CommandParameter = tt.Command = "_Circle"; ribBtn.Name = "ExampleButton3"; ribBtn.Text = tt.Title = "- â„–3"; ribBtn.CommandHandler = new RibbonCommandHandler(); ribBtn.Orientation = System.Windows.Controls.Orientation.Vertical; ribBtn.Size = RibbonItemSize.Standard; ribBtn.Image = LoadImage("icon_16"); ribBtn.ShowImage = true; ribBtn.ShowText = false; tt.Content = "  â„–3.      "; ribBtn.ToolTip = tt; ribRowPanel.Items.Add(ribBtn); #endregion //      ribSourcePanel.Items.Add(ribRowPanel); } catch (System.Exception ex) { Autodesk.AutoCAD.ApplicationServices.Application. DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message); } } //     //       System.Windows.Media.Imaging.BitmapImage LoadImage(string ImageName) { return new System.Windows.Media.Imaging.BitmapImage( new Uri("pack://application:,,,/ACadRibbon;component/" + ImageName + ".png")); } /*    *          */ class RibbonCommandHandler : System.Windows.Input.ICommand { public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { Document doc = acadApp.DocumentManager.MdiActiveDocument; if (parameter is RibbonButton) { //   ,   CommandParameter  //      SendStringToExecute RibbonButton button = parameter as RibbonButton; acadApp.DocumentManager.MdiActiveDocument.SendStringToExecute( button.CommandParameter + " ", true, false, true); } } } } } 


6. All - the code is ready. Compile it (Build -> Build solution), open AutoCAD 2010, execute the NETLOAD command and select our plugin ... \ ACadRibbon \ ACadRibbon \ bin \ Debug \ ACadRibbon.dll.

And immediately after the download, we will see that we have added a new tab on the ribbon:





This option is actively used in my ModPlus plugin and has not yet caused any complaints.

I hope an example will be useful!

Source: https://habr.com/ru/post/263373/


All Articles