Importing IFC files to Revit

Importing IFC files to Revit

alberto84AZ9
Observer Observer
386 Views
5 Replies
Message 1 of 6

Importing IFC files to Revit

alberto84AZ9
Observer
Observer

Hello,

 

I am developing an addin to Revit to load ifc files into Revit but I cant get it works as I expected. The Linf IFC interface button works more or less properly but the classes in c# doesnt load the file the same way. My first questions, is there any example code to link properly a ifc file into revit 2025? Besides my ifc file contains multiple properties that I cant see into Revit, how can I load these custom properties into Revit?

 

Greetings

0 Likes
387 Views
5 Replies
Replies (5)
Message 2 of 6

jeremy_tammik
Autodesk
Autodesk

What is the Linf button? Do you want to load or link the file? What problems are you encountering? Yes, there is sample code demonstrating how to link IFC into Revit 2025. For instance, I asked Gemini for you, and it replies:

 

The Revit API approach for linking files is to use the RevitLinkInstance class along with the RevitLinkOptions class. Here is code that works for Revit 2024 and later (including 2025):

C#
 
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.IO;

namespace RevitIFCLink
{
    public class IFCImporter
    {
        public static void LinkIFCFile(Document doc, string ifcFilePath)
        {
            if (!File.Exists(ifcFilePath))
            {
                TaskDialog.Show("Error", "IFC file not found.");
                return;
            }

            // 1. Create Revit Link Options
            RevitLinkOptions revitLinkOptions = new RevitLinkOptions(false); //Workset option. Important for better control.

            // 2. Create IFC Link Options
            IFCLinkOptions ifcLinkOptions = new IFCLinkOptions();

            //Set Options:
            //Import Shared Coordinates:
            ifcLinkOptions.ImportSharedCoordinates = true;
            //Import as Base Point:
            ifcLinkOptions.Placement = IFCLinkPlacement.BasePoint;
            //Openings:
            ifcLinkOptions.Openings = IFCOpenings.Create;
            //Join Elements:
            ifcLinkOptions.JoinElements = true;

            // 3. Link the IFC file
            try
            {
                using (Transaction trans = new Transaction(doc, "Link IFC File"))
                {
                    trans.Start();

                    // Create the RevitLinkType first
                    RevitLinkType rvtLinkType = RevitLinkType.Create(doc, ifcFilePath, revitLinkOptions);

                    if (rvtLinkType != null)
                    {
                        // Now create the RevitLinkInstance using the created type
                        RevitLinkInstance rvtLinkInstance = RevitLinkInstance.Create(doc, rvtLinkType.Id);

                        if (rvtLinkInstance != null)
                        {
                            //Set Name
                            rvtLinkInstance.Name = Path.GetFileNameWithoutExtension(ifcFilePath);
                        }
                        else
                        {
                            TaskDialog.Show("Warning", "IFC link instance creation returned null. Check the IFC file for errors.");
                        }

                    }
                    else
                    {
                        TaskDialog.Show("Warning", "IFC link type creation returned null. Check the IFC file for errors.");
                    }

                    trans.Commit();
                }

                TaskDialog.Show("Success", "IFC file linked successfully.");
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Error", "Error linking IFC file: " + ex.Message);
            }
        }
    }
}

Key Changes and Explanations:

  1. RevitLinkInstance and RevitLinkType: The code now uses RevitLinkInstance and RevitLinkType. This is the correct way to link files (including IFC) in Revit 2024 and later. You first create a RevitLinkType using the file path and link options, and then you create a RevitLinkInstance based on that type.

  2. RevitLinkOptions: The code now uses RevitLinkOptions, which is required for creating a RevitLinkType. The constructor parameter is a boolean that controls whether a new workset will be created for the linked model. It is very important to use this, as it gives you more control over the linked model.

  3. Creation Order: The order of operations is crucial:

    • Create RevitLinkOptions.
    • Create IFCLinkOptions.
    • Create RevitLinkType.
    • Create RevitLinkInstance.
  4. Error Handling: Improved error handling to provide more specific messages.

  5. IFCLinkOptions Still Used: The IFCLinkOptions are still used and passed to the RevitLinkType.Createmethod.

Example Usage in a Revit Command:

C#
 
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

[Transaction(TransactionMode.Manual)]
public class LinkIFCCommand : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        UIApplication uiapp = commandData.Application;
        UIDocument uidoc = uiapp.ActiveUIDocument;
        Document doc = uidoc.Document;

        // Prompt the user to select an IFC file
        Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
        openFileDialog.Filter = "IFC Files (*.ifc)|*.ifc";
        if (openFileDialog.ShowDialog() == true)
        {
            string ifcFilePath = openFileDialog.FileName;
            IFCImporter.LinkIFCFile(doc, ifcFilePath);
        }

        return Result.Succeeded;
    }

     

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 6

alberto84AZ9
Observer
Observer

It doesnt work, I dont find the class IFCLinkOptions 

0 Likes
Message 4 of 6

jeremy_tammik
Autodesk
Autodesk

Yes indeed. These LLMs like to hallucinate. Meanwhile, please answer the questions I asked: Do you want to load or link the file? What exact problems are you encountering?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 5 of 6

jeremy_tammik
Autodesk
Autodesk

You say that the Link IFC interface button works more or less properly. Maybe the best way to start would be to research how to make that work properly. Once that is clear, we should be able to reproduce the same behaviour programmatically as well. In general, it helps a lot to develop an optimal workflow and follow best BIM practices manually in the user interface before trying to address an issue programmatically through the API. What are the problems you see there?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 6 of 6

alberto84AZ9
Observer
Observer

I try to link an ifc because the create button isnt effective according to the Revit itself. I used a lot of examples of code but it doesnt work at Revit 2025. I guess the sdk is constantly updating. So I take a new view about how to achieve my goal. I think I am going to create a family with the model geometry from ifc and I will add the properties I need. Thank you for your time.

0 Likes