A reader was having difficulty compiling source code from a previous post, so I thought I’d post on some basics to explain a few terms and requirements about Revit macros:
A macro is an individual command that can be run in Revit. Here is the source and output from the simplest macro
A module is a collection of macros. To create a macro you must first create a module that will contain the macro. The macros in a module do not need to have any functional relationship. They can do completely different things and do not need to interact with each other in any way. You can put 1 macro in each module or 100 macros in each module. If you want to create utility functions (maybe to convert feet to meters or radians to degrees) then those functions need to be in the same module as the macros that use them. Here are a few restrictions:
- You cannot have two macros in the same module with the same name
- All macros in the same module must compile without errors for you to run any of the macros in that module
- Each module opens in its own session of SharpDevelop
Module_Startup and Module_Shutdown are special pieces of code that are automatically created by Revit when you create a new module. Their purpose is to give you a way to automatically run code when Revit starts and just before Revit exits. Module_Startup code is also executed every time you compile the module. By default they are empty and have no code that they run, and in most cases this is fine.
Each module must have one and only one instance of Module_Startup and Module_Shutdown. If you delete them you will get errors like:
The name 'Module_Startup' does not exist in the current context
If you have them more than once (or any other macro more than once) you will get errors like:
Type 'BoostYourBIM1.ThisApplication' already defines a member called 'Module_Startup' with the same parameter types
When a sample on this blog requires Module_Startup code (like https://boostyourbim.wordpress.com/2013/02/14/set-adaptive-component-parameter-values-with-placement-point-coordinates/where it is used to register Dynamic Model Update triggers) I will include the startup code in the blog post. You can copy that startup code into your existing startup method, or replace your existing startup method with this new one. But if you paste the new Module_Startup code and leave your existing Module_Startup code that will cause the error that there is already a Module_Startup in that module.