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 big Ribbon Buttons and add them to a new Ribbon Panel which is hosted by an existing Ribbon Tab and in turn referenced into the current workspace.
Here is the code and command.
[CommandMethod("AddRibbonBigButtons")]
public static void AddRibbonBigButtons_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
CustomizationSection cs = new CustomizationSection((string)MgdAcApplication.GetSystemVariable("MENUNAME"));
string curWorkspace = (string)MgdAcApplication.GetSystemVariable("WSCURRENT");
RibbonPanelSource panelSrc = AddRibbonPanelToTab(cs, "ANAW", "AcadNetAddinWizard2");
MacroGroup macGroup = cs.MenuGroup.MacroGroups[0];
RibbonRow row = new RibbonRow();
panelSrc.Items.Add((RibbonItem)row);
RibbonCommandButton button1 = new RibbonCommandButton(row);
button1.Text = "Button 1";
MenuMacro menuMac1 = macGroup.CreateMenuMacro("Button1_Macro", "^C^CButton1_Command ", "Button1_Tag", "Button1_Help",
MacroType.Any, "ANAW_16x16.bmp", "ANAW_32x32.bmp", "Button1_Label_Id");
button1.MacroID = menuMac1.ElementID;
button1.ButtonStyle = RibbonButtonStyle.LargeWithText;
button1.KeyTip = "Button1 Key Tip";
button1.TooltipTitle = "Button1 Tooltip Title!";
row.Items.Add((RibbonItem)button1);
RibbonCommandButton button2 = new RibbonCommandButton(row);
button2.Text = "Button 2";
MenuMacro menuMac2 = macGroup.CreateMenuMacro("Button2_Macro", "^C^CButton2_Command ", "Button2_Tag", "Button2_Help",
MacroType.Any, "ANAW_16x16.bmp", "ANAW_32x32.bmp", "Button2_Label_Id");
button2.MacroID = menuMac2.ElementID;
button2.ButtonStyle = RibbonButtonStyle.LargeWithText;
button2.KeyTip = "Button2 Key Tip";
button2.TooltipTitle = "Button2 Tooltip Title!";
row.Items.Add((RibbonItem)button2);
RibbonCommandButton button3 = new RibbonCommandButton(row);
button3.Text = "Button 3";
MenuMacro menuMac3 = macGroup.CreateMenuMacro("Button3_Macro", "^C^CButton3_Command ", "Button3_Tag", "Button3_Help",
MacroType.Any, "ANAW_16x16.bmp", "ANAW_32x32.bmp", "Button3_Label_Id");
button3.MacroID = menuMac3.ElementID;
button3.ButtonStyle = RibbonButtonStyle.LargeWithText;
button3.KeyTip = "Button3 Key Tip";
button3.TooltipTitle = "Button3 Tooltip Title!";
row.Items.Add((RibbonItem)button3);
cs.Save();
}
catch (System.Exception ex)
{
ed.WriteMessage(Environment.NewLine + ex.Message);
}
}
public static RibbonPanelSource AddRibbonPanelToTab(CustomizationSection cs, string tabName, string panelName)
{
RibbonRoot root = cs.MenuGroup.RibbonRoot;
RibbonPanelSourceCollection panels = root.RibbonPanelSources;
foreach (RibbonTabSource rts in root.RibbonTabSources)
if (rts.Name == tabName)
{
//Create the ribbon panel source and add it to the ribbon panel source collection
RibbonPanelSource panelSrc = new RibbonPanelSource(root);
panelSrc.Text = panelSrc.Name = panelName;
panelSrc.ElementID = panelSrc.Id = panelName + "_PanelSourceID";
panels.Add(panelSrc);
//Create the ribbon panel source reference and add it to the ribbon panel source reference collection
RibbonPanelSourceReference ribPanelSourceRef = new RibbonPanelSourceReference(rts);
ribPanelSourceRef.PanelId = panelSrc.ElementID;
rts.Items.Add(ribPanelSourceRef);
return panelSrc;
}
return null;
}
After the assembly is loaded into AutoCAD, the test command run, and the current workspace properly updated, the new Ribbon Panel will appear side by side with the existing Ribbon Panels in the existing Ribbon Tab (other tabs and panels depending on the current workspace and its settings, of course) and the three big Ribbon Buttons appear there nicely:
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 WSRibbonTabSourceReference is to specify where the RibbonTabSource to go, likely in a workspace tab source collection, to make it visible to users.
• Otherwise, the RibbonTabSource will just stay there to be found and put somewhere from the CUI dialog, for example.
• Similar situations exist for the Ribbon Panel. 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 WSRibbonRoot is the root object for a particular workspace as far as ribbon is concerned.
• It has a WorkspaceTabs collection that welcomes new WSRibbonTabSourceReference instances to join in.
• Each Ribbon Button is represented by a RibbonCommandButton instance. It can be big or small but we choose big 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 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.
Posted by: |