AutoCAD has provided the Ribbon UI for some time and also provided the CUI .NET API for us to manipulate the modern UI elements including Ribbon Tab, Ribbon Panel and various Ribbon Buttons.
We created Ribbon Panels and Ribbon Tabs from scratch before. We also added Ribbon Panels to some existing Ribbon Tabs. CUI workspaces have also been addressed such as referencing to some Ribbon Tabs and Ribbon Panels into the current workspace and looping through the Ribbon Tab references to find one of interest.
In this post, let’s create some small Ribbon Buttons and stack them into an existing Ribbon Panel to make them align nicely and look good.
Here is the code and command.
public static void AddRibbonSmallButtons2_Method()
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
CustomizationSection cs = new CustomizationSection((string)MgdAcApplication.GetSystemVariable("MENUNAME"));
string curWorkspace = (string)MgdAcApplication.GetSystemVariable("WSCURRENT");
RibbonPanelSource panelSrc = GetRibbonPanel(cs, "AcadNetAddinWizard");
if (panelSrc == null) return;
MacroGroup macGroup = cs.MenuGroup.MacroGroups;
RibbonRow row1 = new RibbonRow();
RibbonCommandButton button1 = new RibbonCommandButton(row1);
button1.Text = "SmallButton 1";
MenuMacro menuMac1 = macGroup.CreateMenuMacro("SmallButton1_Macro", "^C^CSmallButton1_Command ", "SmallButton1_Tag", "SmallButton1_Help",
MacroType.Any, "ANAW_16x16.bmp", "ANAW_32x32.bmp", "SmallButton1_Label_Id");
button1.MacroID = menuMac1.ElementID;
button1.ButtonStyle = RibbonButtonStyle.SmallWithText;
button1.KeyTip = "SmallButton1 Key Tip";
button1.TooltipTitle = "SmallButton1 Tooltip Title!";
RibbonRow row2 = new RibbonRow();
RibbonCommandButton button2 = new RibbonCommandButton(row2);
button2.Text = "SmallButton 2";
MenuMacro menuMac2 = macGroup.CreateMenuMacro("SmallButton2_Macro", "^C^CSmallButton2_Command ", "SmallButton2_Tag", "SmallButton2_Help",
MacroType.Any, "ANAW_16x16.bmp", "ANAW_32x32.bmp", "SmallButton2_Label_Id");
button2.MacroID = menuMac2.ElementID;
button2.ButtonStyle = RibbonButtonStyle.SmallWithText;
button2.KeyTip = "SmallButton2 Key Tip";
button2.TooltipTitle = "SmallButton2 Tooltip Title!";
RibbonRow row3 = new RibbonRow();
RibbonCommandButton button3 = new RibbonCommandButton(row3);
button3.Text = "SmallButton 3";
MenuMacro menuMac3 = macGroup.CreateMenuMacro("SmallButton3_Macro", "^C^CSmallButton3_Command ", "SmallButton3_Tag", "SmallButton3_Help",
MacroType.Any, "ANAW_16x16.bmp", "ANAW_32x32.bmp", "SmallButton3_Label_Id");
button3.MacroID = menuMac3.ElementID;
button3.ButtonStyle = RibbonButtonStyle.SmallWithText;
button3.KeyTip = "SmallButton3 Key Tip";
button3.TooltipTitle = "SmallButton3 Tooltip Title!";
catch (System.Exception ex)
ed.WriteMessage(Environment.NewLine + ex.Message);
After the assembly is loaded into AutoCAD, the test command run, and the current workspace properly updated, the new small Ribbon Buttons will appear in the leftmost existing Ribbon Panel hosted by an existing Ribbon Tab (the looking of other tabs and panels depending on the current workspace and its settings, of course):
Some highlights may seem good to help understand the code.
• The AutoCAD system variable MENUNAME will give us the menu (CUI or CUIx actually as far as Ribbon is concerned) file name.
• The CustomizationSection object is the root object for the AutoCAD CUI .NET API and everything should start from there.
• The CustomizationSection instance had better be maintained as a single one. Otherwise, synchronization issues might occur.
• Another AutoCAD system variable WSCURRENT can tell us what the current workspace name is.
• The RibbonRoot is the root object for all Ribbon related CUI stuffs which can be got from the MenuGroup property of the CustomizationSection that is created earlier.
• The RibbonTabSource is to define some basic information about the Ribbon Tab such as tab name, text, id and similar.
• The RibbonPanelSource is to define some basic information about the Ribbon Panel such as its name, text and id.
• The RibbonPanelSourceReference is to specify where the associated RibbonPanelSource to go, likely in the ribbon panel source reference collection of a RibbonTabSource of interest. So a single ribbon panel could be hosted by different ribbon tabs just as a Block can be inserted as different Block References in different locations or even layouts.
• Each Ribbon Button is represented by a RibbonCommandButton instance. It can be big or small but we choose small here.
• Each Ribbon Button has an AutoCAD Macro assosicated with it so as to trigger a command or a script. MacroGroup is the container for macros of Ribbon Buttons or any other kind ribbon items. It can be retrieved from the CustomizationSection.MenuGroup.MacroGroups property.
• The RibbonRow is to layout Ribbon Items, RibbonCommandButton instances here, in a single ribbon row. It needs to be added into a Ribbon Panel Source through the method RibbonPanelSource.Items.Add((RibbonItem)xyz) so as to find its place in the new Ribbon environment.
• The RibbonCommandButton accepts a RibbonRow argument in its constructor to find its place too. Each RibbonCommandButton has its own properties to distuiguish itself from others, such as Text, MacroId, ButtonStyle, KepTip and TooltipTitle.
• The existing RibbonPanelSource Items collection has to be emptied first, otherwise the new RibbonRow along with its small button items will appear in the SLIDEOUT panel instead of the main panel.
As can be noticed, this time the small buttons are stacked nicely and the limited ribbon spaces can also be saved.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders including a Ribbon Creator, and widgets to help program AutoCAD .NET addins.