How to Export Revit Linked Model to IFC Programmatically with C# API

How to Export Revit Linked Model to IFC Programmatically with C# API

gia_magnani
Explorer Explorer
1,598 Views
5 Replies
Message 1 of 6

How to Export Revit Linked Model to IFC Programmatically with C# API

gia_magnani
Explorer
Explorer

Hi, I have the following situation: a georeferenced Model A that includes within it a link to a model B (not georeferenced). I need to export both model A and model B in IFC format with the georeferenced elements depending on the position with which the model was connected in A. From Revit, through the user interface, I can obtain the desired result by selecting the “Export linked files as separate IFC” option and launching it from model A, two different files are exported, one of which corresponds to the georeferenced model B.

Using the Revit API with C#, in particular by configuring the IFCExportConfiguration object and setting the relevant parameter for exporting linked models (ExportLinkedFiles) to true, only the file containing model A is exported (without any element contained in model B ); if instead I proceed to manually export model B following the export of model A, acting on the Document object obtained from the GetLinkDocument method of the RevitLinkInstance object, I am able to export B but without the georeferenced elements.

I would like to know, since Revit can perform the operation correctly, what steps should I perform programmatically to obtain the same result.

Thank you.

0 Likes
1,599 Views
5 Replies
Replies (5)
Message 2 of 6

Moustafa_K
Collaborator
Collaborator

I've developed a class to facilitate exporting linked Revit files programmatically. Detailed instructions and explanations are available here.

  1. Download the Class: Add IFCLinkedDocumentExporter.cs to your project.
  2. Initialize and Export: Integrate the class into your code to initialize the exporter and specify the elements you want to export. Here's a sample code snippet to get you started:

 

// if with in project /site
var linkExporter = new IFCLinkedDocumentExporter(Doc, ifcOptions);
linkExporter.SetExportOption(LinkedFileExportAs.ExportSameProject);

doc.Export(@"d:\temp", "IFC_TestSampleFile", ifcOptions);

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

 

 

I have tested it over Revit 2025, but i trust it should also work on the IFC Exporter addin supported Versions. Feel free to rename the class to something shorter if you prefer. This class will help you seamlessly export linked Revit files without relying on the UI.

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 3 of 6

mdb
Contributor
Contributor

Hello Mustafa.

Thanks for your share. I tried implementing your class but I'm stuck at the point where you should use a transaction but use a custom 'IFCLinkDocumentExportScope'.

 

// Normally, IFC export would need a transaction, even if no permanent changes are made. For linked documents, though, that's
// handled by the export itself.
using (IFCLinkDocumentExportScope scope = new IFCLinkDocumentExportScope(linkDocument))
{
    linkDocument.Export(path_, fileName_, exportOptions);
}

 

I cloned the repository and tried searching the GitHub directories but couldn't find the right class.

Any idea what's going wrong?

Thank you.

0 Likes
Message 4 of 6

Moustafa_K
Collaborator
Collaborator

Sure it has to be wrapped within a transaction. See this example, 

 

Which repo and which branch did you clone ?

Document doc = uidoc.Document;
Transaction trans = new Transaction(doc,"export");
trans.Start();
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");

trans.Commit();

 

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 5 of 6

mdb
Contributor
Contributor

Thank you! I made it work 😋

I switched to the latest branch and found that I needed to add a reference to RevitAPPIIFC.dll for the 'IFCLinkDocumentExportScope'

 

I also noticed that you don't provide an extension for the filename of the example

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

 

but you do include a check that aborts the script if none is found.

// get the extension
int index = fileName.LastIndexOf('.');
if (index <= 0)
    return;

 

 

 

0 Likes
Message 6 of 6

gopinath.rajendran
Enthusiast
Enthusiast

Does it export without an open transaction for Revit linked models?

0 Likes