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. We also created Ribbon Big Buttons, Small Buttons, Stacked Buttons and Ribbon Separators succefully into some Ribbon Panels. We tried to create some AutoCAD Ribbon Toggle Buttons but found out it’s not possible.
In this post, let’s create a Ribbon Slidout and host some Ribbon Small Buttons inside it.
Here is the code and command.
[CommandMethod("AddRibbonSlidout")]
public static void AddRibbonSlidout_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);
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!";
row1.Items.Add(button1);
RibbonRow row2 = new RibbonRow();
panelSrc.Items.Add(row2);
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!";
row2.Items.Add(button2);
RibbonRow row3 = new RibbonRow();
panelSrc.Items.Add(row3);
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!";
row3.Items.Add(button3);
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 current workspace properly updated, the Slideout along with the three nicely stacked small buttons appear in the leftmost Ribbon Panel that we created previously into the CUI file:
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.
• 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(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 RibbonSeparator can appear as either a line or a space as indicated by its SeparatorStyle property.
• The Ribbon Slideout will be automatically generated in case a full Ribbon Row is already there and some other rows are going to be appended to the same Ribbon Panel.
• If a short Ribbon Row is already there, it depends on the height of the next Ribbon Row that the Slideout appears or not. If the new Ribbon Row is short enough to be accommodated into the existing Ribbon Panel, the Slideout will not show up. So we need to do the calculations.
• A single Ribbon Panel can contain a tall Ribbon Row but three short ones.
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: |