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!");
}
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
Hi Julien,
Have you tried creating a Document macro in your template file? The document macro will be identical to an application macro with one difference being that you get the Document like this:
Document doc = this.Document;
There is also some info about Doucment vs Application macros at https://boostyourbim.wordpress.com/2012/12/05/macros-vs-add-ins-whats-the-difference/
Regards
Harry
Hi Harry,
I managed to create document macro in my template and everytime i created new project from my template, the macro will be included into my new project. Unfortunately, everytime new project are created, revit will prompt a message box asking a user to enable or disable a macro. Are there any other ways to avoid the message box and make it enable everytime i created the project?
Thank you.
Abby
Hi Abby,
Did you try the “Enable document macros” option as described at http://wikihelp.autodesk.com/Revit/enu/2014/Help/0001-Revit_Us1/3274-Customiz3274/3450-Automati3450/3467-Macro_Se3467?
Hi Harry,
Thank you for the reply. But what I meant is to make it enable programatically so that user can not disable it later.
Thank you.
Hi Harry. Sorry. What I meant in the last post is are there any methods to enable it programatically in revit api 2013. I knew that you have mentioned that there are method in 2014 which are :
SetApplicationMacroSecurityOptions Sets the application macro security options.
SetDocumentMacroSecurityOptions Sets the document macro security options.
Thank you.
Abby
Sorry, that is not possible in 2013
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
}
}
Kudos! will try and tell you.
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.
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?
The AddInCommandBinding.BeforeExecuted Event sounds like what you’d like
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.”);
}
}
}
Hi Chris
In the catch block you might put a TaskDialog.Show() statement to let you know that an exception has been thrown.
Also, Module_Startup can be a bit tricky to work with, because in addition to Module_Startup running when Revit starts, it is also called when the macro project is rebuilt. (see https://boostyourbim.wordpress.com/2013/01/06/using-module_startup-to-run-macro-code-when-revit-starts/).
Hope that helps
Harry
Can this be used to override the “URL” button?
Jrn.Grid “Control; Modal , Type Properties , IDD_SYMBOL_ATTRIB; IDC_SYMBOL_GRID” _
, “Button” , “URL” , “Val
This only works for commands. More info at:
http://thebuildingcoder.typepad.com/blog/2012/06/replacing-built-in-commands-and-their-ids.html
http://thebuildingcoder.typepad.com/files/commandids.txt