Boost Your BIM – making Revit even better

January 14, 2013

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

Filed under: Import/Link, UI — harrymattison @ 10:18 am

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 http://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!");
}
About these ads

5 Comments »

  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

    Comment by julien benoit — January 17, 2013 @ 11:12 am

  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!!!

    Comment by julien benoit — January 17, 2013 @ 3:35 pm

    • 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
      }
      }

      Comment by harrymattison — January 17, 2013 @ 4:53 pm

  3. Kudos! will try and tell you.

    Comment by julien benoit — January 18, 2013 @ 11:08 am


RSS feed for comments on this post. TrackBack URI

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 )

Connecting to %s

Theme: WordPress Classic. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 369 other followers