Sunday, 13 December 2020

How to create a macro in Revit

  In this post, I will create a simple "Hello World" message just to demonstrate how to create and run macros in Revit.

Just follow these steps:

1- Open the Macro manager (Manage > Macro Manager)
2- Create a new Module:

  Modules are just a way of grouping macros. For example, in the image below I have a category with various tools, another one for tests and another one with tools for cleaning views.

There are two kinds of modules: project modules and application modules. The project modules are saved directly into the active document and will be available for all the team working in the model. However, it won't be available for other projects. The application modules will be only available for you but you will be able to use it in any project. My personal preference is to use application modules so I can reuse easily my macros. 

For this example, click on the Application tab and then on Create > Module. Give it a name, select C# and write a description (optional). Click OK and close the macro editor that will appear on the screen.

 3- Create a new macro:

  Now select your module (MyFirstMacro) and click on Create>Macro. Give it a name and a description (optional).

When you click OK, the macro editor will appear again with some code. But we are only interested in the part shown in the image below. Our code goes between the two curly brackets.
4-Write the code:
 Now write the following code between the curly brackets (I recommend that you write it instead of copy/paste):
TaskDialog.Show("My fisrt macro","Hello world!");
 Then just press the key F8 on your keyboard to build the macro.

5- Run the macro:
Select your macro in the macro manager and just click run.
 A message like in the image below should pop up:


Explanation of the code:

 The code in this tutorial is very simple but I will explain each part.


1-  public void HelloWorld(){}

This is how you define a method in C#. In this case, the method is the macro itself. A method in C# is a block of code that performs an action/s. The words "public" and "void" are properties of the method which I will explain in another post. The code of the method must go between the curly brackets.

2- TaskDialog

TaskDialog is a class of the Revit API. For the moment you can think of a class as a group of methods and properties. The TaskDialog class is used to create messages and dialogues for the user.

3- .Show("text 1","text 2")

"Show" is a method. The dot indicates that it belongs to the TaskDialog class. This method makes the dialog visible to the user. The first text in the brackets is the title of the dialog and the second one is the message itself. 

In future posts, I will explain how to create more complex macros.

No comments:

Post a comment