The Revit SDK contains two samples folders:
\Revit SDK <release>\Software Development Kit\Samples\...
\Revit SDK <release>\Software Development Kit\Macro Samples\...
The programs in the SDK's \Samples\ folder use the standard Revit API. We refer to these samples as the SDK sample code, as distinct from the Revit macro samples.
If you want to use the SDK sample code for macros, modifications are needed. Follow the steps in this section. For a programming language, we will show C# examples. However these instructions also apply to the VB.NET versions of the SDK samples.
Initial Steps
The initial steps to migrate standard API samples from the SDK into your Revit macro IDE project are similar to the section Integrating Macros into the Revit IDE. Except, instead of copying files from the SDK's \Macro Samples\<sample-name>\... folders, you will copy files from the SDK's \Samples\<sample-name>\... folders.
To review, the steps are:
- In the IDE, add required references.
- In the IDE, create folders for the SDK samples you want to migrate.
- In Windows Explorer, copy the SDK standard API samples’ files to the corresponding file system folders.
- In the IDE, add existing files to the macro project.
Update the SDK Samples’ Code
In the Revit macro IDE, the IExternalCommand interface is not available or used. In the SDK standard API sample code, you must update the class that inherits from this interface:
- Remove the method parameters and the return of the Execute method.
- Update other code related to ExternalCommandData.
Code Example Before Edits
The following code snippet is from a program that uses the standard Revit API:
Code Example After Edits
In the Revit macro IDE, we need to update the code as follows. This example shows an application-level macro. The method RunSampleRooms() is the entry for this macro sample. Notice that we used the this pointer to replace commandData.Application.
Additional Migration Notes for SDK Standard API Samples
- By default, the SDK sample namespace is: Revit.SDK.Samples.<SampleName>.CS. As you edit the sample code that came from the SDK standard API samples, be sure to change the namespace for the Revit IDE. For example, in ThisApplication.cs:
namespace AppCSharp.csproj
- The default project references in Revit IDE projects only consist of basic references, such as Revit.Proxy. Remember to add other required references. For example, you have to add System.Windows.Forms and System.Drawing references when running samples that present a user interface.
- Some samples have resources that must be defined in the resources.resx file. GridCreation is one such sample. If you migrate the SDK standard API sample for GridCreation to the Revit IDE, be sure to set the resx property.
- Project build dependence is not supported within the Revit macro IDE. If you want to use other DLLs, you must compile that dependent sample project outside the Revit macro IDE. For example, in the SDK standard API samples, there are several samples related to Viewers, such as AnalyticalViewer and RoomViewer, which depend on references to RevitViewer.
However, we cannot set the project dependency due to a limitation in the Revit IDE and we cannot migrate the RevitViewer sample. Therefore, you need to compile RevitViewer as an independent SDK sample, and then add its DLL as a reference in the Revit IDE project.
- When you migrate SDK standard API samples to the Revit IDE, do not copy any Solution files (*.sln*) or existing project files (*.csproj or *.vbproj).
- Toolbar-related samples are unavailable in the Revit IDE.