Disabling the AutoCAD ribbon using .NET
I came across this interesting question on the AutoCAD .NET forum, posted by Pete Elliott:
When AutoCAD is loading, I see that my toolbars are disabled (grayed-out) until the loading has finished. Our company does some additional data loading after AutoCAD has become idle. But if the user clicks certain custom toolbar buttons while our additional data is loading, AutoCAD fatal errors. Is there a way we can disable the toolbars (like AutoCAD does) until our loading is finished, and then enable them? I haven't been able to locate an API that provides this capability. Any suggestions greatly appreciated!
It seems this would be a very common requirement. For today's post I'm going to assume that by "toolbars" Pete is actually referring to the ribbon. (It suits my purposes that this be the case, as I've found a way to make it work for the ribbon, but I'll do my best to publish a follow-up post for toolbars, if that turns out to be the actual requirement. :-)
The basic approach is fairly simple: you get access to the current "ribbon palette set" via the RibbonServices class (we call a method to create a new one, but typically this will just return the existing ribbon as we're calling it from a command). We then use it to disable the "ribbon control" as well as its background tab rendering. We also turn off the display of tooltips while the ribbon is disabled.
Here's the C# code that disables and re-enables the ribbon respectively via DR and ER commands. These commands are really just examples of how you might call the EnableRibbon() function from your own code (you'd typically call EnableRibbon(false); as initializaton starts and EnableRibbon(true); when you're done).
using Autodesk.AutoCAD.Runtime;
using Autodesk.Windows;
namespace UserInterfaceManipulation
{
public class Commands
{
private static bool _showTipsOnDisabled = false;
[CommandMethod("DR")]
public static void DisableRibbonCommand()
{
EnableRibbon(false);
}
[CommandMethod("ER")]
public static void EnableRibbonCommand()
{
EnableRibbon(true);
}
public static void EnableRibbon(bool enable)
{
// Start by making sure we have a ribbon
// (if calling from a command this will almost certainly just return
// the ribbin that already exists)
var rps = Autodesk.AutoCAD.Ribbon.RibbonServices.CreateRibbonPaletteSet();
// Enable or disable it
rps.RibbonControl.IsEnabled = enable;
if (!enable)
{
// Store the current setting for "Show tooltips when the ribbon is disabled"
// and then modify the setting
_showTipsOnDisabled = ComponentManager.ToolTipSettings.ShowOnDisabled;
ComponentManager.ToolTipSettings.ShowOnDisabled = enable;
}
else
{
// Restore the setting for "Show tooltips when the ribbon is disabled"
ComponentManager.ToolTipSettings.ShowOnDisabled = _showTipsOnDisabled;
}
// Enable or disable background tab rendering
rps.RibbonControl.IsBackgroundTabRenderingEnabled = enable;
}
}
}
Here are the two commands in action: