|
1 | 1 | # Revit.Extensions |
2 | | -The unofficial Extensions for Revit API |
| 2 | + |
| 3 | +[](https://github.com/apibim/Revit.Extensions/actions/workflows/ci.yml) |
| 4 | +[](https://www.nuget.org/packages/Revit.Extensions) |
| 5 | +[](https://www.nuget.org/packages/Revit.Extensions) |
| 6 | +[](LICENSE) |
| 7 | + |
| 8 | +A collection of extension methods for the **Autodesk Revit API**, designed to reduce boilerplate and make Revit add-in development more expressive. Supports **Revit 2020 – 2027**. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +``` |
| 15 | +dotnet add package Revit.Extensions |
| 16 | +``` |
| 17 | + |
| 18 | +Or search for **`Revit.Extensions`** in the NuGet Package Manager inside Visual Studio. |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Revit version support |
| 23 | + |
| 24 | +| Revit | Target framework | NuGet version | |
| 25 | +| ----- | ---------------- | ------------- | |
| 26 | +| 2020 | net47 | 2020.0.\* | |
| 27 | +| 2021 | net48 | 2021.0.\* | |
| 28 | +| 2022 | net48 | 2022.0.\* | |
| 29 | +| 2023 | net48 | 2023.0.\* | |
| 30 | +| 2024 | net48 | 2024.0.\* | |
| 31 | +| 2025 | net8.0-windows | 2025.0.\* | |
| 32 | +| 2026 | net8.0-windows | 2026.0.\* | |
| 33 | +| 2027 | net8.0-windows | 2027.0.\* | |
| 34 | + |
| 35 | +The package automatically targets the correct framework — no manual configuration needed. |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## API reference |
| 40 | + |
| 41 | +### `GeometryExtensions` |
| 42 | + |
| 43 | +Helpers for working with Revit geometry types (`XYZ`, `Outline`, `BoundingBoxXYZ`). |
| 44 | + |
| 45 | +```csharp |
| 46 | +using Revit.Extensions; |
| 47 | + |
| 48 | +// Convert XYZ ↔ value tuple |
| 49 | +XYZ point = new XYZ(1.0, 2.0, 3.0); |
| 50 | +(double X, double Y, double Z) vec = point.ToVector(); // (1.0, 2.0, 3.0) |
| 51 | +XYZ restored = vec.ToXYZ(); // XYZ(1.0, 2.0, 3.0) |
| 52 | +
|
| 53 | +// Expand an Outline uniformly (default: 10 ft) |
| 54 | +Outline outline = new Outline(new XYZ(0, 0, 0), new XYZ(5, 5, 5)); |
| 55 | +Outline expanded = outline.Extend(size: 2); // min −2, max +2 in every axis |
| 56 | +
|
| 57 | +// Transform a BoundingBoxXYZ from local to world space |
| 58 | +BoundingBoxXYZ bbox = element.get_BoundingBox(view); |
| 59 | +BoundingBoxXYZ? worldBbox = bbox.TransformBoundingBox(); |
| 60 | +``` |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +### `PointExtensions` |
| 65 | + |
| 66 | +Unit conversion for `XYZ` coordinates using Revit's `UnitUtils`. |
| 67 | + |
| 68 | +```csharp |
| 69 | +using Autodesk.Revit.DB; |
| 70 | +using Revit.Extensions; |
| 71 | + |
| 72 | +// Revit stores coordinates in feet internally. |
| 73 | +// Convert a point to meters (Revit 2021+): |
| 74 | +XYZ pointInFeet = new XYZ(3.28084, 0, 0); |
| 75 | +XYZ pointInMeters = pointInFeet.Recalculate(UnitTypeId.Meters); |
| 76 | +// → XYZ(1.0, 0, 0) |
| 77 | +``` |
| 78 | + |
| 79 | +> **Revit 2020** uses the legacy `DisplayUnitType` enum — the overload is selected automatically via conditional compilation. |
| 80 | +
|
| 81 | +--- |
| 82 | + |
| 83 | +### `ElementExtensions` |
| 84 | + |
| 85 | +Helpers for collecting elements and grids from the active document. |
| 86 | + |
| 87 | +```csharp |
| 88 | +using Revit.Extensions; |
| 89 | + |
| 90 | +// Collect selected elements, or fall back to all FamilyInstances + HostObjects in the active view |
| 91 | +IList<Element> elements = ElementExtensions.GetAllElementOrSelected(uiDocument); |
| 92 | + |
| 93 | +// Get all grids from the level closest to elevation 0 |
| 94 | +IList<Grid> grids = ElementExtensions.GetAllGridFromFirstLevel(document, out string? levelName); |
| 95 | +if (levelName is not null) |
| 96 | + TaskDialog.Show("Grids", $"Found {grids.Count} grids on level '{levelName}'."); |
| 97 | +``` |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +### `AsyncTasksExecutor` |
| 102 | + |
| 103 | +Run async code synchronously inside the Revit event loop, where `async/await` on the main thread is unsupported. |
| 104 | + |
| 105 | +```csharp |
| 106 | +using Revit.Extensions; |
| 107 | + |
| 108 | +// Execute a Task<T> synchronously |
| 109 | +string result = AsyncTasksExecutor.Execute(async () => |
| 110 | +{ |
| 111 | + await Task.Delay(100); |
| 112 | + return "done"; |
| 113 | +}); |
| 114 | + |
| 115 | +// Execute a ValueTask<T> synchronously |
| 116 | +int value = AsyncTasksExecutor.Execute(async () => |
| 117 | +{ |
| 118 | + await Task.Yield(); |
| 119 | + return 42; |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## Contributing |
| 126 | + |
| 127 | +Contributions are welcome! Please open an issue or submit a pull request. |
| 128 | + |
| 129 | +1. Fork the repository |
| 130 | +2. Create a feature branch: `git checkout -b feature/my-extension` |
| 131 | +3. Add your extension in `Revit.Extensions/Extensions/` with a corresponding test in `tests/Extensions/` |
| 132 | +4. Open a pull request against `main` |
| 133 | + |
| 134 | +See [CLAUDE.md](CLAUDE.md) for build commands and project conventions. |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## License |
| 139 | + |
| 140 | +MIT © [Apibim SpA](https://apibim.com) |
0 commit comments