Hi Scott,
Your demo solution works, to add a new button to the Manage tab. However, it does not add the ribbon panel, it adds items from this ribbon panel.
In fact, the code to add a new panel to a system ribbon tab does not work. It seems Revit prevents it. We can only add ribbon items to an existing ribbon panel of a system tab.
I rewrote the code to make it more generic to reuse:
public class Application : IExternalApplication
{
private string _ribbonPanelName = "MyPanel";
public Result OnStartup(UIControlledApplication application)
{
AddRibbonItems(application);
application.ControlledApplication.ApplicationInitialized += ControlledApplication_ApplicationInitialized;
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
private void ControlledApplication_ApplicationInitialized(object sender, Autodesk.Revit.DB.Events.ApplicationInitializedEventArgs e)
{
MoveRibbonItemsBetweenTabPanels("Add-Ins", _ribbonPanelName, "View", "Windows");
}
private void AddRibbonItems(UIControlledApplication application)
{
// Add a new ribbon panel to Revit Add-Ins tab
Autodesk.Revit.UI.RibbonPanel ribbonPanel = application.CreateRibbonPanel(_ribbonPanelName);
// Create ribbon items for this ribbon panel
string location = Assembly.GetExecutingAssembly().Location;
var pushButtonData = new PushButtonData(
"MyApp_Browser_Command",
"MyApp",
location,
"MyApp.ShowWindow");
pushButtonData.AvailabilityClassName = "MyApp.ShowWindow";
PushButton button = ribbonPanel.AddItem(pushButtonData) as PushButton;
}
private void MoveRibbonItemsBetweenTabPanels(
string sourceTabName,
string sourcePanelName,
string targetTabName,
string targetPanelName)
{
try
{
Autodesk.Windows.RibbonPanel sourceRibbonPanel = null;
bool isBreak = false;
RibbonTabCollection tabs = Autodesk.Windows.ComponentManager.Ribbon.Tabs;
// Get source ribbon panel
foreach (Autodesk.Windows.RibbonTab tab in tabs)
{
if (tab.Id.Equals(sourceTabName, StringComparison.OrdinalIgnoreCase))
{
foreach (Autodesk.Windows.RibbonPanel panel in tab.Panels)
{
if (panel.Source.AutomationName.Equals(sourcePanelName, StringComparison.OrdinalIgnoreCase))
{
sourceRibbonPanel = panel;
// Remove this panel of out its ribbon tab
tab.Panels.Remove(panel);
isBreak = true;
break;
}
}
if (isBreak) break;
}
}
// Copy items from this source ribbon panel to another ribbon panel (in different tab)
if (sourceRibbonPanel != null)
{
foreach (Autodesk.Windows.RibbonTab tab in tabs)
{
if (tab.Id.Equals(targetTabName, StringComparison.OrdinalIgnoreCase))
{
//tab.Panels.Add(sourceRibbonPanel); // Not work
foreach (Autodesk.Windows.RibbonPanel panel in tab.Panels)
{
if (panel.Source.AutomationName.Equals(targetPanelName, StringComparison.OrdinalIgnoreCase))
{
// Copy source items to target panel
foreach (Autodesk.Windows.RibbonItem ribbonItem in sourceRibbonPanel.Source.Items)
{
panel.Source.Items.Add(ribbonItem);
}
break;
}
}
break;
}
}
}
}
catch (Exception)
{
}
}
}
This code was tested to work on Revit 2014/2015. It will create a new custom button on the View tab, Windows panel.
Thanks.