Skip to content

Multiple Revit Versions

Roman edited this page Apr 10, 2024 · 25 revisions

To write code incompatible with different Revit versions, use the directives #if, #elif, #else, #endif.

To write code for the required Revit version, set the solution configuration in your IDE interface to match that version, for example select Debug R25 for Revit 2025.

Code specific to that API version, wrap in a block:

#if REVIT2025
    //Your code here
#endif

The https://github.com/Nice3point/Revit.Build.Tasks package generates preprocessor directives automatically based on your project configuration, just add this Nuget package to your project and you will get generated constants like REVIT2025, REVIT2025_OR_GREATER, REVIT2024_OR_GREATER. Revit.Build.Tasks are included in templates by default.

Examples

To support the latest APIs in legacy Revit versions:

#if REVIT2021_OR_GREATER
    UnitUtils.ConvertFromInternalUnits(69, UnitTypeId.Millimeters);
#else
    UnitUtils.ConvertFromInternalUnits(69, DisplayUnitType.DUT_MILLIMETERS);
#endif

#if REVIT2021_OR_GREATER сompiles a block of code for Revit versions 21, 22, 23 and greater.

To support removed APIs in newer versions of Revit, you can invert the constant:

#if !REVIT2023_OR_GREATER
    var builtinCategory = (BuiltInCategory) category.Id.IntegerValue;
#endif

#if !REVIT2023_OR_GREATER сompiles a block of code for Revit versions 22, 21, 20 and lower.

Extending/reducing the supported Revit API versions

To add support for Revit API versions not included by default in templates or remove it, follow the steps below.

Solution configurations

A solution configuration defines which projects in the solution are build, and which project configurations are used for specific projects within the solution.

To build a project for multiple versions of Revit, you need to add configurations for each version:

  • Open .sln file
  • Add the Revit version to the Debug\Release configuration name so that it can be identified
GlobalSection(SolutionConfigurationPlatforms) = preSolution
    Debug R19|Any CPU = Debug R19|Any CPU
    Debug R20|Any CPU = Debug R20|Any CPU
    Debug R21|Any CPU = Debug R21|Any CPU
    Debug R22|Any CPU = Debug R22|Any CPU
    Release R19|Any CPU = Release R19|Any CPU
    Release R20|Any CPU = Release R20|Any CPU
    Release R21|Any CPU = Release R21|Any CPU
    Release R22|Any CPU = Release R22|Any CPU
EndGlobalSection

For example Debug R19 is the Debug configuration for Revit 2019 version.

If you want to remove support for some Revit versions from your add-in, do it here as well, delete the unnecessary lines in SolutionConfigurations

Project configurations

Project configurations create conditions for building for a specific version.

To build a project for multiple versions of Revit, similar to SolutionConfiguration, you need to add configurations for each version:

  • Open .csproj file
  • Add the Revit version to the Debug\Release configuration name so that it can be identified
<PropertyGroup>
    <Configurations>Debug R19;Debug R20;Debug R21;etc</Configurations>
</PropertyGroup>

Note: edit the .csproj file only manually. Don't use project properties in your IDE.

Then simply map the solution configuration to the project configuration in your IDE interface:

Solution and project configuration names may differ, but the templates use the same naming style to avoid confusion.

Properties

In the .csproj file, add additional properties specifying the framework and Revit version to automatically add the required Revit API version and compile the project with supported methods and properties specific to that version:

<PropertyGroup Condition="$(Configuration.Contains('R19'))">
    <RevitVersion>2019</RevitVersion>
    <TargetFramework>net48</TargetFramework>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="Nice3point.Revit.RevitAPI" Version="$(RevitVersion).*"/>
    <PackageReference Include="Nice3point.Revit.RevitAPIUI" Version="$(RevitVersion).*"/>
</ItemGroup>

Accordingly, to add new configurations when a new version of Revit is released or deleting old ones, you need to update these blocks. Add new solution and project configurations. Copy properties block and update the values.

If there are a lot of configurations in the project, and you are not comfortable using horizontal scrolling, do something like this:

<Configurations>Debug R19;Debug R20;Debug R21;Debug R22</Configurations>
<Configurations>$(Configurations);Release R19;Release R20;Release R21;Release R22</Configurations>

To add dependencies, use Nuget packages:

The Nuget package version must include wildcards Version="$(RevitVersion).*" to automatically include adding a right package version, depending on the selected solution configuration.

Why Nuget ? To support CI/CD pipelines. To build a project for Revit versions not installed on your computer.