Exporting Linked To IFC

Exporting Linked To IFC

As an avid user of Autodesk Revit, I've often encountered limitations, particularly with exporting linked Revit instances via the API. This inspired me to contribute to the community by addressing this shortfall. In this blog post, I'll walk you through the enhancements I made to the IFC Exporter, an open-source tool by Autodesk, enabling the export of linked Revit files programmatically.

Understanding the Limitation

Previously, exporting linked Revit instances was heavily tied to the Revit UI, making it impossible to achieve the same through the API. This restriction posed a significant challenge for developers looking to automate the export process.

The Solution: Decoupling UI and API

To resolve this, I forked the IFC Exporter repository and created a pull request with my modifications. The primary goal was to decouple the export functionality from the UI, making it accessible through the API. I achieved this by creating the export logic into a new class, IFCLinkDocumentExporter.

Key Changes

  1. Separation of Concerns: The export code, previously embedded within the UI logic, was extracted and placed into a standalone class. This separation allows the same functionality to be called programmatically without UI dependencies.

  2. New Export Class: Introducing IFCLinkDocumentExporter, a dedicated class responsible for handling the export of linked Revit instances. This class can be renamed to better suit your project's naming conventions, as long as it remains intuitive and descriptive.

Using the New Export Functionality

Integrating the new export functionality into your Revit API workflow is straightforward. Below is a sample code snippet demonstrating its usage:

usage via API is straight forward, and has to be wrapped within Transaction:

Document doc = uidoc.Document;
View view = uidoc.ActiveGraphicalView;
IFCExportOptions ifcOptions = new IFCExportOptions();

IFCExportConfiguration configExporter = IFCExportConfiguration.CreateDefaultConfiguration();
configExporter.IFCVersion = IFCVersion.IFC2x3;
configExporter.UseActiveViewGeometry = true;
configExporter.UpdateOptions(ifcOptions, view.Id);

// if with in project /site
var linkExporter = new IFCLinkedDocumentExporter(Doc, ifcOptions);
linkExporter.SetExportOption(LinkedFileExportAs.ExportSameProject);
if(!string.IsNullOrEmpty(linkExporter.GetErrors()))
{
    // something went wrong
}
else
{
    doc.Export(@"d:\", "IFC_TestSampleFile", ifcOptions);
}

// if separated
linkExporter.SetExportOption(LinkedFileExportAs.ExportAsSeparate);
linkExporter.ExportSeparateDocuments(@"d:\IFC_TestSampleFile");

I have tested it over Revit 2025, but i trust it should also work on the IFC Exporter addin supported Versions.

Conclusion

This enhancement opens up new possibilities for Revit API developers, allowing for greater automation and flexibility in exporting linked Revit models to IFC. By decoupling the export functionality from the UI, we can now leverage the full potential of the Revit API for streamlined workflows.

I encourage you to try out this new feature and contribute to its improvement. You can find my fork and pull request on the IFC Exporter GitHub repository. Together, we can continue to push the boundaries of what’s possible with Revit and the Revit API.

Till the pull request is approved and merged you can download the IFCLinkDocumentExporter to your project and use it like the sample above.

Did you find this article valuable?

Support Moustafa Khalil by becoming a sponsor. Any amount is appreciated!