Skip to content

Instantly share code, notes, and snippets.

@johnpierson
Created December 12, 2024 16:29
Show Gist options
  • Save johnpierson/14428e172138baa31bfef0c4b739fc85 to your computer and use it in GitHub Desktop.
Save johnpierson/14428e172138baa31bfef0c4b739fc85 to your computer and use it in GitHub Desktop.
using System.IO;
using System.Reflection;
using System.Windows.Media.Imaging;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using InPlaceWarning.Forms;
using InPlaceWarning.InPlaceWarningAboutButton;
using TaskDialog = Autodesk.Revit.UI.TaskDialog;
namespace InPlaceWarning
{
/// <summary>
/// Application entry point
/// </summary>
public class Application : IExternalApplication
{
/// <summary>
/// The string name of the command to disable. To lookup a command id string, open a session of Revit,
/// invoke the desired command, close Revit, then look to the journal from that session. The command
/// id string will be toward the end of the journal, look for the "Jrn.Command" entry that was recorded
/// when it was selected.
/// </summary>
static readonly String _sCommandToDisable = "ID_INPLACE_COMPONENT";
/// <summary>
/// The command id, stored statically to allow for removal of the command binding.
/// </summary>
static RevitCommandId _sCommandId;
public Result OnStartup(UIControlledApplication application)
{
_sCommandId = RevitCommandId.LookupCommandId(_sCommandToDisable);
// Confirm that the command can be overridden
if (!_sCommandId.CanHaveBinding)
{
ShowDialog("Error", "The target command " + _sCommandToDisable +
" selected for disabling cannot be overridden");
return Result.Failed;
}
// Create a binding to override the command.
// Note that you could also implement .CanExecute to override the accessibiliy of the command.
// Doing so would allow the command to be grayed out permanently or selectively, however,
// no feedback would be available to the user about why the command is grayed out.
try
{
AddInCommandBinding commandBinding = application.CreateAddInCommandBinding(_sCommandId);
commandBinding.Executed += DisableEvent;
}
// Most likely, this is because someone else has bound this command already.
catch (Exception)
{
ShowDialog("Error", "This add-in is unable to disable the target command " + _sCommandToDisable +
"; most likely another add-in has overridden this command.");
}
CreateRibbon(application);
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication application)
{
// Remove the command binding on shutdown
if (_sCommandId.HasBinding)
application.RemoveAddInCommandBinding(_sCommandId);
return Result.Succeeded;
}
// ExternalCommands assembly path
static readonly string m_Path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
private void CreateRibbon(UIControlledApplication application)
{
RibbonPanel panel;
try
{
panel = application.CreateRibbonPanel("PrlxApps - Free Stuff");
}
catch (Exception)
{
panel = application.GetRibbonPanels().First(p => p.Name.Equals("PrlxApps - Free Stuff"));
}
InPlaceWarningAboutCommand.CreateButton(panel);
}
/// <summary>
/// A command execution method which disables any command it is applied to (with a user-visible message).
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="args">Arguments.</param>
private void DisableEvent(object sender, ExecutedEventArgs args)
{
using (InPlaceWarningForm chooser = new InPlaceWarningForm())
{
DialogResult result = chooser.ShowDialog();
if (result == DialogResult.OK)
{
Document doc = args.ActiveDocument;
UIDocument uidoc = new UIDocument(doc);
uidoc.Application.RemoveAddInCommandBinding(_sCommandId);
uidoc.Application.PostCommand(_sCommandId);
}
else if (result == DialogResult.Cancel)
{
}
}
}
/// <summary>
/// Show a task dialog with a message and title.
/// </summary>
/// <param name="title">The title.</param>
/// <param name="message">The message.</param>
private static void ShowDialog(string title, string message)
{
// Show the user a message.
TaskDialog td = new TaskDialog(title)
{
MainInstruction = message,
TitleAutoPrefix = false
};
td.Show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment