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, Ribbon Tabs, Ribbon Buttons (big or small), Stacked Buttons, and Split Buttons from scratch before. In this post, let’s see if it is possible to create some toggle buttons represented by the public CUI Ribbon RibbonToggleButton class, into the AutoCAD Ribbon.
Here is the test code and command.
[CommandMethod("AddRibbonToggleButton")]
public static void AddRibbonToggleButton_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
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[0];
panelSrc.Items.Clear();
RibbonRow row1 = new RibbonRow();
panelSrc.Items.Add(row1);
RibbonToggleButton toggleButton1 = new RibbonToggleButton(row1);
toggleButton1.ButtonStyle = RibbonButtonStyle.LargeWithoutText;
toggleButton1.Text = "Toggle #1";
toggleButton1.KeyTip = "KeyTip 1";
row1.Items.Add(toggleButton1);
RibbonToggleButton toggleButton2 = new RibbonToggleButton(row1);
toggleButton2.ButtonStyle = RibbonButtonStyle.LargeWithoutText;
toggleButton2.Text = "Toggle #2";
toggleButton2.KeyTip = "KeyTip 2";
row1.Items.Add(toggleButton2);
RibbonToggleButton toggleButton3 = new RibbonToggleButton(row1);
toggleButton3.ButtonStyle = RibbonButtonStyle.LargeWithoutText;
toggleButton3.Text = "Toggle #3";
toggleButton3.KeyTip = "KeyTip 3";
row1.Items.Add(toggleButton3);
cs.Save();
}
catch (System.Exception ex)
{
ed.WriteMessage(Environment.NewLine + ex.Message);
}
}
After the assembly is loaded into AutoCAD, the test command run, and the CUI dialog is opened, we will find that the AcadNetAddinWizard ribbon panel has been populated with a RibbonRow however it contains nothing!
So, this time we do not have a cool screenshot for you as it proves that the RibbonToggleButton instances are not possible to be created programmatically. Not sure if the mission is manully possible but I haven’t seen anything looking like toggle buttons in various ribbons provided by AutoCAD or its various flavors.
If anything is missed out here or any problems in the code as provided, please feel free to post comments. We are all ears and welcome any corrections. Let's make things clearer and clearer together. It will benefit multiple sides including you and us and much more.
Otherwise, let’s make a bold educational guess here. The RibbonToggleButton class should be private instead of public, it’s still under construction, it leaves for future expansion, or something like that.
Some highlights may seem good to help understand the code anyway.
• 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.
• The RibbonRow is to layout Ribbon Items 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 RibbonToggleButton accepts a RibbonRow argument in its constructor to find its place too. Each RibbonToggleButton has its own properties to distuiguish itself from others, such as Text, MacroId, ButtonStyle, and Tooltip.
• The existing RibbonPanelSource Items collection has to be emptied first, otherwise the new RibbonRow along with anything inside it will appear in the SLIDEOUT panel instead of the main panel.
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.
Recent Comments