CLIifc export does not output ifc file

Hello pyrevit community,

I need a simple script that takes rvt file and exports it in ifc format. First I tested this example code. Then I used the code from this thread as a basis for the export, calling HOST_APP.uiapp.OpenAndActivateDocument(models[0]) to open the file in CLI.

Here is the current iteration of my code:

from pyrevit import HOST_APP, DB, coreutils, revit
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
import os

def export_ifc():
    logger = coreutils.logger.get_logger("test")
    logger.dev_log("Exporting IFC")
    folder_path = os.path.dirname(__file__)
    
    uidoc = HOST_APP.uiapp.OpenAndActivateDocument(__models__[0])
    doc = DB.Document(uidoc.Document)
    t = Transaction(doc,"IfcExport")
    t.Start()
    options = IFCExportOptions()
    options.FileVersion = IFCVersion.IFC2x3
    options.WallAndColumnSplitting = False
    options.AddOption("Export2DElements", "false")
    options.AddOption("ExportRoomsInView", "false")
    options.AddOption("VisibleElementsOfCurrentView ", "true")
    options.AddOption("ExportLinkedFiles", "false")
    options.ExportBaseQuantities = True
    options.AddOption("ExportInternalRevitPropertySets", "true")
    options.AddOption("ExportIFCCommonPropertySets", "true")
    options.AddOption("ExportSchedulesAsPsets", "false")
    options.AddOption("ExportSpecificSchedules", "false")
    options.AddOption("ExportUserDefinedPsets", "false")
    options.AddOption("Use2DRoomBoundaryForVolume ", "false")
    options.AddOption("UseFamilyAndTypeNameForReference ", "false")
    options.AddOption("ExportPartsAsBuildingElements", "false")
    options.AddOption("ExportBoundingBox", "false")
    options.AddOption("ExportSolidModelRep", "true")
    options.AddOption("StoreIFCGUID", "true")
    options.AddOption("UseActiveViewGeometry", "true")
    options.AddOption("IncludeSiteElevation", "true")
    options.AddOption("ExportAnnotations ", "true")

    options.AddOption("TessellationLevelOfDetail", "0,5")
    options.AddOption("IFCFileType", "0")
    doc.Export(folder_path, "output.ifc", options)

    t.Commit()
    t.Dispose()
        

# Run the export function
export_ifc()

This code did not produce errors or the result ifc file to the script folder when ran with the following command:

pyrevit run “<script_path>” “<file_path>” --revit=24.3.10.22

I tried to search the documentation and the forums for a solution, but was unable to diagnose the problem. The file was created with revit 2021.

1 Like

Ho @Kyosti_Nyrhila,welcome to the pyRevit forum!
I’m sorry you’re having trouble with the pyrevit run command.
Can you please provide the output of that command, and more importantly the contents of the journal and log files listed there?
That way we can understand better what is going on.

I can also offer some advice:

  • I don’t think you need to explicitly use the dev_log logger method, as it should be routed to the log file automatically
  • you can decouple the retrieval of the document from your logic by introducing a doc argument in the function; this has also the benefit to make it easier to switch the run command to a batch processing utility without touching the main logic function
  • the ifc options can be created outside of the transaction, and since they don’t depend on the open document, they can be created before opening it (and potentially created once for all the documents in a batch processing, but the performance gain shouldn’t be too big); it can also be made into a separate function to call in the main one
  • use the pyrevit.revit.Transaction object instead of the Revit API to get automatic commit and dispose
  • since you are importing pyrevit.DB, you don’t need to also import Autodesk.Revit.DB

This is an untested refactor of your code following the above:

import os

from pyrevit import HOST_APP, DB, revit, script

# here I added an optional version parameter 
# to quickly change the output format from the caller
def create_export_options(version=DB.IFCVersion.IFC2x3):
    """Initialize the IFC export options with opinionated defaults."""
    options = DB.IFCExportOptions()
    options.FileVersion = version
    options.WallAndColumnSplitting = False
    options.AddOption("Export2DElements", "false")
    options.AddOption("ExportRoomsInView", "false")
    options.AddOption("VisibleElementsOfCurrentView ", "true")
    options.AddOption("ExportLinkedFiles", "false")
    options.ExportBaseQuantities = True
    options.AddOption("ExportInternalRevitPropertySets", "true")
    options.AddOption("ExportIFCCommonPropertySets", "true")
    options.AddOption("ExportSchedulesAsPsets", "false")
    options.AddOption("ExportSpecificSchedules", "false")
    options.AddOption("ExportUserDefinedPsets", "false")
    options.AddOption("Use2DRoomBoundaryForVolume ", "false")
    options.AddOption("UseFamilyAndTypeNameForReference ", "false")
    options.AddOption("ExportPartsAsBuildingElements", "false")
    options.AddOption("ExportBoundingBox", "false")
    options.AddOption("ExportSolidModelRep", "true")
    options.AddOption("StoreIFCGUID", "true")
    options.AddOption("UseActiveViewGeometry", "true")
    options.AddOption("IncludeSiteElevation", "true")
    options.AddOption("ExportAnnotations ", "true")
    options.AddOption("TessellationLevelOfDetail", "0,5")
    options.AddOption("IFCFileType", "0")
    return options

def export_ifc(doc):
    """Export the given model as IFC file."""
    logger = script.get_logger("test")
    logger.info("Exporting IFC")
    folder_path = os.path.dirname(__file__)
    options = create_export_options()
    with revit.Transaction(doc,"IfcExport"):
        doc.Export(folder_path, "output.ifc", options)

# Run the export function
for model in __models__:
    uidoc = HOST_APP.uiapp.OpenAndActivateDocument(model)
    doc = DB.Document(uidoc.Document)
    export_ifc(doc)

Reference

this is the reference page for the pyrevit run command