How to override commands in the Revit UI (when you REALLY don’t want people to import CAD files)

The previous post showed how to delete CAD imports that are already in your Revit model. What if you want to prevent those CAD imports from being added in the first place?

The 2013 API introduced functionality that allows commands in the Revit UI to be replaced with your API code. In this simple case, the Import command will be replaced with an error dialog.

You will need to find the ID for the command that you want to replace. The easiest way to do that is to execute the command through the Revit UI and then open the Revit journal file (in C:\Users\HP002\AppData\Local\Autodesk\Revit\Autodesk Revit Architecture 2013\Journals or something like that). For import, the entry will be

 Jrn.Command "Internal" , "Import vector data from other programs , ID_FILE_IMPORT"

The string in capital letters near the end of the line is the command id. Here’s the code showing how to replace the Import command, and in the video you see that while Link still works as usual, Import does not.

private void Module_Startup(object sender, EventArgs e)
{
    UIApplication uiapp = new UIApplication(this.Application);

    // Get the command id for the import command
    RevitCommandId commandId = RevitCommandId.LookupCommandId("ID_FILE_IMPORT");

    // use try/catch because CreateAddInCommandBinding will throw if there is already a binding for this id
    // for more info see https://boostyourbim.wordpress.com/2013/01/06/using-module_startup-to-run-macro-code-when-revit-starts/
    try
    {
        AddInCommandBinding importBinding = uiapp.CreateAddInCommandBinding(commandId);
        importBinding.Executed += new EventHandler<Autodesk.Revit.UI.Events.ExecutedEventArgs>(importReplacement);
    }
    catch
    {}
}

private void importReplacement(object sender, Autodesk.Revit.UI.Events.ExecutedEventArgs arg)
{
    TaskDialog.Show("Stop!","Do not import!");
}
Advertisements

16 thoughts on “How to override commands in the Revit UI (when you REALLY don’t want people to import CAD files)

  1. thanks for a lot for this nice blog. lots of cool stuff here.
    I’m quite n00b with API and macro; I managed to get it work on my laptop. My question is: is it possible to link this macro to a document, ie my template file? so that each new project started from this template has the macro? thanks for the answer. Julien

  2. thanks again for the answer. i will have to train a little to make it work; it doesn’t with just the document doc changed. Document is “ambiguous”, and as I changed uiapp.CreateAddInCommandBinding(commandId) for doc or document or what ever CreateAddInCommandBinding(commandId)… still getting an error. road is long to success for n00b!!!

    • Julien,

      Here is a more complete reply. This is what the contents of the macro file should be for a document macro.

      Harry

      /*
      * Revit Macro created by SharpDevelop
      * User: HP002
      * Date: 1/17/2013
      * Time: 4:47 PM
      *
      * To change this template use Tools | Options | Coding | Edit Standard Headers.
      */
      using System;
      using Autodesk.Revit.DB;
      using Autodesk.Revit.UI;
      using Autodesk.Revit.ApplicationServices;

      namespace docmacro
      {
      [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
      [Autodesk.Revit.UI.Macros.AddInId(“BE31C03B-F505-4214-AA22-7B400B3A2F47”)]
      public partial class ThisDocument
      {

      private void Module_Startup(object sender, EventArgs e)
      {
      UIApplication uiapp = new UIApplication(this.Document.Application);

      RevitCommandId commandId = RevitCommandId.LookupCommandId(“ID_FILE_IMPORT”);

      try
      {
      AddInCommandBinding importBinding = uiapp.CreateAddInCommandBinding(commandId);
      importBinding.Executed += new EventHandler(importReplacement);
      }
      catch
      {}
      }

      private void importReplacement(object sender, Autodesk.Revit.UI.Events.ExecutedEventArgs arg)
      {
      TaskDialog.Show(“Stop!”,”Do not import!”);
      }

      private void Module_Shutdown(object sender, EventArgs e)
      {

      }

      #region Revit Macros generated code
      private void InternalStartup()
      {
      this.Startup += new System.EventHandler(Module_Startup);
      this.Shutdown += new System.EventHandler(Module_Shutdown);
      }
      #endregion
      }
      }

  3. Hi Harry, I’m trying this out (in 2013) after adding it as a Document macro. For some reason, after the document with the macro is opened, I can’t use Import CAD in other documents which don’t have the macro until I restart Revit. Any idea why this might be happening? Thanks.

    • CreateAddInCommandBinding is a member of the UIApplication class, which represents an active session of the Autodesk Revit user interface. So even though you put this in a Document macro, the command binding is being registered for the UIApplication, not the Document.

  4. Can the overriden command be fired during the eventhandler so that you get a warning box telling the user that importing CAD is bad but the command still fires after the user clicks OK? kind of like a health warning on a pack of smokes?

  5. boostyourbim, I’ve been moving slowly into the revit api world and would love to disable the CAD Import button of my users, but to no avail have I been successful in implementing the code you’ve shared in this post. I’m working in visual basic 2012 and Revit 2015 and loading this code through an addin manifest. Below is full exact code I am trying to load, any help would be greatly appreciated. Thanks, Chris.

    using System;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.ApplicationServices;

    namespace CADImport
    {
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class CADImportWarning
    {
    private void Module_Startup(object sender, EventArgs e)
    {
    UIApplication uiapp = sender as UIApplication;
    RevitCommandId commandId = RevitCommandId.LookupCommandId(“ID_FILE_IMPORT”);

    try
    {
    AddInCommandBinding importBinding = uiapp.CreateAddInCommandBinding(commandId);
    importBinding.Executed += new EventHandler(importReplacement);
    }
    catch
    { }
    }

    private void importReplacement(object sender, Autodesk.Revit.UI.Events.ExecutedEventArgs arg)
    {
    TaskDialog.Show(“Warning”, “Be sure to LINK CAD files instead of importing.”);
    }
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s