Search code examples
c#ribbonrevit-apirevit

How to add a button to a contextual ribbon in Revit


I am wanting to add buttons to certain contextual ribbons...  Specifically:

Modify | Multi-Select, Modify | Pipes, Modify | Sprinklers, Modify | Pipe Accessories, Modify | Pipe Fittings, Modify | Mechanical Equipment and Modify | Generic Models   and place the buttons within a panel of my creation.  How can I accomplish this? 

I've tried:

if (pTab.Id == "Modify | Pipes")
{
    foreach (var pPanel in pTab.Panels)
    {
        if (pPanel.Source.Id == "Edit") //Also tried edit_shr
        {// Add button.
            pIcon = Properties.Resources.AS_Revit_UI_hydraulicParameters_icon.GetHbitmap();
            var pBtn = new Autodesk.Windows.RibbonButton()
            {
                Name = "Hydraulic Parameters",
                Image = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(pIcon, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(32, 32)),
                Id = "id_hydParam",
                AllowInStatusBar = true,
                AllowInToolBar = true,
                GroupLocation = Autodesk.Private.Windows.RibbonItemGroupLocation.Middle,
                MinHeight = 0,
                MinWidth = 0,
                Height = 32,
                Width = 32,
                IsEnabled = true,
                IsVisible = true,
                IsCheckable = true,
                ShowImage = true,
                ShowText = true,
                Orientation = System.Windows.Controls.Orientation.Vertical,
                Text = "Hydraulic Parameters",
                Size = Autodesk.Windows.RibbonItemSize.Large,
                ResizeStyle = Autodesk.Windows.RibbonItemResizeStyles.HideText
            };

            pPanel.Source.Items.Add(pBtn);
            //Add event handler for button push
        }
    }
}

That, unfortunately, didn't work.  I'm sure this is possible - I just don't know how.  I feel like it's a matter of not know the Revit-issued tab names - like Modify | Pipes is really something like modify_pipes or something like that.      The code above was me trying to put my button in a Revit panel...  Is there a way to add my own panel with my own buttons?  Something like this:  

enter image description here

That's the ideal situation.  I'm more than comfortable with any other solutions like adding the buttons to existing panels.  Any help is good help!  Thanks!!


Solution

  • This can indeed be achieved in the following way:

    • Create a ribbon panel button in the normal way, anywhere you like, in one of the default locations.
    • Move the button to some other location using the .NET Automation API.

    If your target location is a contextual tab, you may have to relocate your button every time the tab is opened.

    This process is documented by The Building Coder:

    http://thebuildingcoder.typepad.com/blog/2014/07/moving-an-external-command-button-within-the-ribbon.html

    This is not recommended for production use, and I have heard reports that this approach may lead to crashes and file corruption, so beware!

    Please note the Disclaimer!