Get Custom Ribbon Tab name safely

TomB.ADV
Advocate
Advocate

Get Custom Ribbon Tab name safely

TomB.ADV
Advocate
Advocate

Hi, I'm in the development process of a modular plugin solution where all the buttons are under the same
Ribbon tab but the construction of the Ribbon Panels under this common tab is being done for each module.

Meaning a user may choose to have different combinations of function which I can't predict so naturally

the code assumes it has to create this common tab because the modules aren't "aware" of each other.

The problem is, I can't find a way to check if the tab name is already exists, I can check for panels using:

GetRibbonPanels(string tabName)

I was hoping that calling for panels in a tab which doesn't exists, will return nothing but it throws an exception...

TomBADV_0-1688983405418.png

I tried using same name to check if the .CreateRebbonTab() will just ignore it, but is also throws an exception... 

TomBADV_1-1688983715194.png
I could use try-catch statement but this is typically last resort and overall wrong approach in my opinion

 

            try
            {
                application.CreateRibbonTab("AdvTools");
            }
            catch(Exception)
            {
               //do nothing
            }

 


Are there any decent work-arounds for this situation ?

0 Likes
Reply
Accepted solutions (1)
327 Views
4 Replies
Replies (4)

jeremy_tammik
Autodesk
Autodesk
Accepted solution

This article shares code to read all ribbon panel titles safely:

  

https://thebuildingcoder.typepad.com/blog/2011/03/ribbon-panel-title-conflict.html

  

I hope this helps. 

  

Best regards,

  

Jeremy

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes

TomB.ADV
Advocate
Advocate

I see, so currently the only way to do this is using a private method which will iterate over all the tab names

and return an indicator if it is safe or not to call .CareateRibbonTab()

0 Likes

jeremy_tammik
Autodesk
Autodesk

Well, maybe not the only way, just one possibility. There may be others, and some may be better. Who knows?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes

ricaun
Advisor
Advisor

To create the RibbonTab that's the way to do it, using a try, to guarantee that the RibbonTab gonna be created.

 

To create the RibbonPanel you gonna need to search and maybe return the existent panel so you could add other buttons in the existent panel.

 

Using some extensions like:

 

public static RibbonPanel CreateOrSelectRibbonPanel(this UIControlledApplication application, string tabName, string panelName)
{
    if (application.GetRibbonPanels(tabName).FirstOrDefault(p => p.Name == panelName) is RibbonPanel ribbonPanel)
        return ribbonPanel;

    return application.CreateRibbonPanel(tabName, panelName);
}
Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes