We discussed how to add a new Revit ribbon tab with Revit API 2012 previously. In this post, let us see if it is possible to check whether a ribbon tab is already there and what will happen if we do not do so.
If the following code is run in different Revit external commands or applications:
private RibbonPanel CreateRibbonPanel()
{
cachedUiCtrApp.CreateRibbonTab("RevitAddinWizard");
RibbonPanel panel = _cachedUiCtrApp.CreateRibbonPanel("RevitAddinWizard", Guid.NewGuid().ToString());
//panel.Name = Guid.NewGuid().ToString(); //Set to a friendly and unique name and uncomment the code if necessary.
panel.Title = "RevitAddinCS1";
////Default button:
PushButtonData pbDataExtCmd = new PushButtonData("ExtCmd", "ExtCmd", Assembly.GetExecutingAssembly().Location, "RevitAddinCS1.ExtCmd");
PushButton pbExtCmd = panel.AddItem(pbDataExtCmd) as PushButton;
pbExtCmd.ToolTip = "ExtCmd";
pbExtCmd.LargeImage = BmpImageSource("RevitAddinCS1.Resources.ExtCmd32x32.bmp");
pbExtCmd.Image = BmpImageSource("RevitAddinCS1.Resources.ExtCmd16x16.bmp");
////More buttons:
return panel;
}
An exception saying “Autodesk.Revit.Exceptions.ArgumentException: The tab with the input name exists already. Parameter name: tabName at Autodesk.Revit.UI.UIApplication.CreateRibbonTab(String tabName) at …” like the following will be thrown out:
It is not good. It seems we’d better check whether the ribbon tab has already been there before creating any new tab. But after every corner of the Revit API 2012 both the reference guide and the object model itself was searched nothing avail was got.
Oh?! Then how can we get rid of the problem as stated above?
Using the try and catch blocks. It is not so nice but works.
Here are sample uses in C#, VB.NET and C++/CLI:
C# goes first:
try{_cachedUiCtrApp.CreateRibbonTab("Vendor");}catch{}
RibbonPanel panel = _cachedUiCtrApp.CreateRibbonPanel("Vendor", Guid.NewGuid().ToString());
panel.Name = "Vendor_AddinCS2012_ExtApp";
panel.Title = "AddinCS2012";
VB.NET next:
Try
_cachedUiCtrApp.CreateRibbonTab("Vendor")
Catch
End Try
Dim panel As RibbonPanel = _cachedUiCtrApp.CreateRibbonPanel("Vendor", Guid.NewGuid().ToString())
panel.Name = "Vendor_AddinVB2012_ExtApp"
panel.Title = "AddinVB2012"
Last but not least, Managed C++/CLI:
try{_cachedUiCtrApp->CreateRibbonTab("Vendor");}catch(System::Exception^ ex){}
RibbonPanel^ panel = _cachedUiCtrApp->CreateRibbonPanel("Vendor", Guid::NewGuid().ToString());
panel->Name = "Vendor_AddinVC2012Bit64_ExtApp";
panel->Title = "AddinVC2012Bit64";
They all look similar but have fundamental differences actually. The C# one is the simplest; the VB.NET try/catch have to stand on separate lines; and the C++/CLI one has to be fed an Exception argument even if we don’t use it at all.
Ok, now all the three ribbon panels having either the same titles or different can appear in the same ribbon tab without problems anymore:
Revit Addin Wizard (RevitAddinWizard) provides the ribbon tab creation option when Revit 2012 is targeted at in the wizard page and uses this technique to work around the problem regarding ribbon tab creation and presence.
Recent Comments