It is necessary for a Revit Addin to manipulate ribbon panels (RibbonPanel) and their items (RibbonItem) created by some other Revit Addins sometimes. In this article, we will focus on this topic.
Let’s prepare two other Addins beforehand, each of which will create a RibbonPanel and some RibbonItem instances. The following method in the first Addin project, RevitAddinCS1, will create a panel with different name and title and then add two PushButton items to it:
private RibbonPanel CreateRibbonPanel()
{
RibbonPanel panel = _cachedUiCtrApp.CreateRibbonPanel(Tab.AddIns, "RevitAddinCS1");
panel.Enabled = true;
panel.Visible = true;
panel.Name = "ABC_APP1_PNL1";
panel.Title = "Panel 1 of APP1 of ABC";
PushButtonData pbDataExtCmd1 = new PushButtonData("ExtCmd1", "Command 1", Assembly.GetExecutingAssembly().Location, "RevitAddinCS1.ExtCmd");
PushButton pbExtCmd1 = panel.AddItem(pbDataExtCmd1) as PushButton;
pbExtCmd1.ToolTip = "External Command 1";
pbExtCmd1.LargeImage = BmpImageSource("RevitAddinCS1.Resources.ExtCmd32x32.bmp");
pbExtCmd1.Image = BmpImageSource("RevitAddinCS1.Resources.ExtCmd16x16.bmp");
PushButtonData pbDataExtCmd2 = new PushButtonData("ExtCmd2", "Command 2", Assembly.GetExecutingAssembly().Location, "RevitAddinCS1.ExtCmd");
PushButton pbExtCmd2 = panel.AddItem(pbDataExtCmd2) as PushButton;
pbExtCmd2.ToolTip = "External Command 2";
pbExtCmd2.LargeImage = BmpImageSource("RevitAddinCS1.Resources.ExtCmd32x32.bmp");
pbExtCmd2.Image = BmpImageSource("RevitAddinCS1.Resources.ExtCmd16x16.bmp");
return panel;
}
As can be noticed, apart from using the two arguments of the CreateRibbonPanel method to specify a Ribbon Tab and a default title for the panel, some properties are explicitly set. Setting the Enabled and Visible explicitly as true does not make much sense in this case since they are by default true, but setting the Name and the Title apparently does. This is the only way to give a different name and title to the RibbonPanel since though the CreateRibbonPanel has two signatures none of them accepts both a name and a title. In terms of the Enabled and Visible properties, we at least get some ideas that panels (and items) can be hidden or disabled. Sometime later on, we will demonstrate how to hide and disable some ribbon items.
Another important matter is the naming convention for the RibbonPanel name. Since RibbonPanel does not carry any information of its host AddIn at all, panel naming conversion becomes important. Then identifying them in other Addins becomes possible. Here a pattern like CompanyName_ApplicationName_PanelName is applied. For reliability and simplicity purpose, the name had better leave any spaces or special characters out. Of course, the Title property should by all means be different from the Name and have a more meaningful and descriptive phrase.
Let’s prepare one more panel in another Addin, RevitAddinCS2:
private RibbonPanel CreateRibbonPanel()
{
RibbonPanel panel = _cachedUiCtrApp.CreateRibbonPanel("RevitAddinCS2");
panel.Enabled = true;
panel.Visible = true;
panel.Name = "ABC_APP2_PNL1";
panel.Title = "Panel 1 of APP2 of ABC";
PushButtonData pbDataExtCmd = new PushButtonData("ExtCmd1", "Command 1", Assembly.GetExecutingAssembly().Location, "RevitAddinCS2.ExtCmd");
PushButton pbExtCmd = panel.AddItem(pbDataExtCmd) as PushButton;
pbExtCmd.ToolTip = "External Command 1";
pbExtCmd.LargeImage = BmpImageSource("RevitAddinCS2.Resources.ExtCmd32x32.bmp");
pbExtCmd.Image = BmpImageSource("RevitAddinCS2.Resources.ExtCmd16x16.bmp");
return panel;
}
It also follows the same naming convention for the Name of the ribbon panel and gives a different meaningful Title.
Ok, now it is time to manipulate the two panels in the main Addin, RevitAddinCSProject. To make things clearer, we will also create a panel in this Addin so as to trigger some commands, represented by three PushButton items in a PulldownButton group, to modify some items of the two other panels.
private RibbonPanel CreateRibbonPanel()
{
RibbonPanel panel = _cachedUiCtrApp.CreateRibbonPanel("RevitAddinCSProject");
panel.Enabled = true;
panel.Visible = true;
panel.Name = "ABC_APP3_PNL1";
panel.Title = "Panel 1 of APP3 of ABC";
PulldownButtonData group1Data = new PulldownButtonData("PulldownGroup1", "Pulldown Group 1");
group1Data.Image = BmpImageSource("RevitAddinCSProject.Resources.Embedded_Item1_16x16.bmp");
group1Data.LargeImage = BmpImageSource("RevitAddinCSProject.Resources.Embedded_Item1_32x32.bmp");
PulldownButton group1 = panel.AddItem(group1Data) as PulldownButton;
PushButtonData itemData11 = new PushButtonData("itemName11", "Add TextBox", AssemblyFullName, "RevitAddinCSProject.ExtCmd1");
PushButton item11 = group1.AddPushButton(itemData11) as PushButton;
item11.ToolTip = "Add TextBox to ABC_APP1_PNL1";
item11.Image = BmpImageSource("RevitAddinCSProject.Resources.Embedded_Item1_16x16.bmp");
item11.LargeImage = BmpImageSource("RevitAddinCSProject.Resources.Embedded_Item1_32x32.bmp");
PushButtonData itemData12 = new PushButtonData("itemName12", "Hide ExtCmd2", AssemblyFullName, "RevitAddinCSProject.ExtCmd2");
PushButton item12 = group1.AddPushButton(itemData12) as PushButton;
item12.ToolTip = "Hide ExtCmd2 in ABC_APP1_PNL1";
item12.Image = BmpImageSource("RevitAddinCSProject.Resources.Embedded_Item2_16x16.bmp");
item12.LargeImage = BmpImageSource("RevitAddinCSProject.Resources.Embedded_Item2_32x32.bmp");
group1.AddSeparator();
PushButtonData itemData13 = new PushButtonData("itemName13", "Disable ExtCmd1", AssemblyFullName, "RevitAddinCSProject.ExtCmd3");
PushButton item13 = group1.AddPushButton(itemData13) as PushButton;
item13.ToolTip = "Disable ExtCmd1 in ABC_APP2_PNL1";
item13.Image = new BitmapImage(new Uri(Path.Combine(AssemblyPath, "Standalone_Item1_16x16.bmp"), UriKind.Absolute));
item13.LargeImage = new BitmapImage(new Uri(Path.Combine(AssemblyPath, "Standalone_Item1_32x32.bmp"), UriKind.Absolute));
return panel;
}
If the three Addins are all loaded into Revit, the AddIn Ribbon Tab may look like:
The first command is defined as:
try
{
List<RibbonPanel> allAddinPanels = CachedUiApp.GetRibbonPanels();
RibbonPanel panel = allAddinPanels.FirstOrDefault(p => p.Name.Equals("ABC_APP1_PNL1"));
if (panel != null)
{
if (panel.GetItems().FirstOrDefault(i => i.Name.Equals("textBox1")) == null)
{
TextBoxData tbData1 = new TextBoxData("textBox1");
Autodesk.Revit.UI.TextBox item3 = panel.AddItem(tbData1) as Autodesk.Revit.UI.TextBox;
item3.Value = "TEXT";
item3.ToolTip = "Text box sample";
item3.Image = ExtApp.BmpImageSource("RevitAddinCSProject.Resources.Embedded_Item2_16x16.bmp");
item3.ShowImageAsButton = true;
}
}
return Result.Succeeded;
}
After it is run, a TextBox item will be added to the RibbonPanel with title ABC_APP1_PNL1:
The code of the second command looks like:
try
{
List<RibbonPanel> allAddinPanels = CachedUiApp.GetRibbonPanels();
RibbonPanel panel = allAddinPanels.FirstOrDefault(p => p.Name.Equals("ABC_APP1_PNL1"));
if (panel != null)
{
RibbonItem item = panel.GetItems().FirstOrDefault(i => i.Name.Equals("ExtCmd2"));
if (item != null)
{
item.Visible = false;
}
}
return Result.Succeeded;
}
After it’s triggered, the second PushButton in the same panel will be hidden:
The third command definition is pretty the same except for the Visible property being replace with the Enabled:
try
{
List<RibbonPanel> allAddinPanels = CachedUiApp.GetRibbonPanels();
RibbonPanel panel = allAddinPanels.FirstOrDefault(p => p.Name.Equals("ABC_APP2_PNL1"));
if (panel != null)
{
RibbonItem item = panel.GetItems().FirstOrDefault(i => i.Name.Equals("ExtCmd1"));
if (item != null)
{
item.Enabled = false;
}
}
return Result.Succeeded;
}
So this time, the first PushButton in the second RibbonPanel will be disabled rather than hidden:
The RevitAddinWizard can help create ribbons automatically in a moment.
Links to some related articles:
Ribbon of Revit API - PushButton And TextBox
Use Ribbon Creator of RevitAddCoder to Create PushButton And Separator
Use Ribbon Creator of RevitAddCoder to Create TextBox
Ribbon of Revit API - ComboBox And ComboBoxMember
Use Ribbon Creator of RevitAddCoder to Create ComboBox And ComboBoxMember
Ribbon of Revit API - PulldownButton And SplitButton
Use Ribbon Creator of RevitAddCoder to Create PulldownButton And PushButton
Use Ribbon Creator of RevitAddCoder to Create SplitButton And PushButton
Ribbon of Revit API - RadioButtonGroup And ToggleButton
Use Ribbon Creator of RevitAddCoder to Create RadioButtonGroup And ToggleButton
Ribbon of Revit API - Stacked Group And AddStackedItems
Ribbon of Revit API - Stacked Group And PulldownButton
Use Ribbon Creator of RevitAddCoder to Create Stacked Group And PushButton Items
Use Ribbon Creator of RevitAddCoder to Create Stacked Group And TextBox Items
Use Ribbon Creator of RevitAddCoder to Create Stacked Group And PulldownButton Items
Use Ribbon Creator of RevitAddCoder to Create Stacked Group And ComboBox Items
Use Ribbon Creator of RevitAddCoder to Create Stacked Group And Various Items
Ribbon of Revit API - Slideout
Use Ribbon Creator of RevitAddCoder to Create Slideout And Various Buttons
Ribbon of Revit API - Manipulate Panels Created by Other Addins
Access Ribbons Created by Ribbon Creator From Another Addin
Update Ribbons Created by Ribbon Creator From Another Addin
Use Ribbon Creator of RevitAddCoder to Create a Comprehensive Ribbon Panel
Ribbon of Revit API - Title And Name of Panels
Ribbon of Revit API - Text And Name of RibbonItem
Ribbon of Revit API - LongDescription And TooltipImage of RibbonItem
Recent Comments