revit - export IFC unattended

revit - export IFC unattended

javier.mujica
Participant Participant
1,027 Views
11 Replies
Message 1 of 12

revit - export IFC unattended

javier.mujica
Participant
Participant

I have little or no experience with this...

 

but I need to write a macro in a document (*.rvt), where when opening the document the execution of the macro is launched and said macro is exported to IFC using a configuration already created in the document.

 

I have created a macro in C++ language and I understand that all code that I put in the "private void Module_Startup(object sender, EventArgs e)" is executed automatically when opening the document, what I need is the syntax of the code that performs the export.

 

thanks for any help

0 Likes
1,028 Views
11 Replies
Replies (11)
Message 2 of 12

jeremy_tammik
Alumni
Alumni

Well, yes and no.

 

When you say, said macro is exported to IFC, I assume you actually mean, said BIM is exported to IFC.

 

You say, Module_Startup(object sender, EventArgs e)" is executed automatically when opening the document.

 

Nope, sorry to say, it is not. That method executes when your macro module is initialised.

 

However, you can easily execute code when the document is opened. To do so, subscribe to the DocumentOpened event:

 

https://www.revitapidocs.com/2022/b9415c2d-442e-f61d-aafa-de31cce7959b.htm

 

The code to export an IFC file is more or less a one-liner using Document.Export:

 

https://www.revitapidocs.com/2022/7efa4eb3-8d94-b8e7-f608-3dbae751331d.htm

  

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

javier.mujica
Participant
Participant

Thanks for the clarification of concepts, also for giving me a starting point.

 

From what I understand, this is the code I have written.

 

As I mentioned, what I need is to open the *.RVT and run the export to IFC unattended using the "IFC4 Design Transfer View" configuration already existing in the document

 

 

*** but I get the following error when I run the macro ***

javiermujica_0-1649334627291.png

 

 

I appreciate all the help you could give me to correct the code, thanks in advance

 

 

 

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace ExportIFCmodulo
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("E8D464C5-72AC-483C-BF8A-EB45BEA17DB8")]
    public partial class ThisDocument
    {
        private void Module_Startup(object sender, EventArgs e)
        {
        }
        private void Module_Shutdown(object sender, EventArgs e)
        {
        }
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }
        public void ExportIFCmacro()
        {
            string folder = "C:\\Temp\\";
            string name = this.Application.ActiveUIDocument.Document.Title;

            IFCExportOptions options = new IFCExportOptions();
            options.AddOption("ConfigName""IFC4 Design Transfer View");
            this.Application.ActiveUIDocument.Document.Export(folder, name, options);
        }
    }
}

0 Likes
Message 4 of 12

jeremy_tammik
Alumni
Alumni

One would expect the Export method to be a read-only operation.

 

However, apparently, it requires an open transaction to run. 

 

That is what the error message tells you.

 

Here is some advice on handling transactions:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.53

  

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

javier.mujica
Participant
Participant

Perfect!, it's already solved, I already export IFC, BUT I have a problem.

 

I have links in my Revit project. If I manually export with the "Export linked files as separate IFCs" checkbox enabled, I get multiple IFC files in the output.

 

In the code I have placed the following instruction "options.AddOption("ExportLinkedFiles", "true");" to export several IFCs, but it doesn't work! I keep exporting a single IFC without the links.

 

Thank you very much in advance for all the help

0 Likes
Message 6 of 12

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @javier.mujica ,

 

Could you please try using the IFCExportConfiguration class?

Please take a look at the below link

https://forums.autodesk.com/t5/revit-api-forum/ifc-export-options/td-p/9686404 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 7 of 12

javier.mujica
Participant
Participant

Thanks a lot

 

I can understand the logic that it sends me, but the creation of the instance for the IFCExportConfiguration does not work

"//Create an instance of the IFC Export Configuration Class

BIM.IFC.Export.UI.IFCExportConfiguration myIFCExportConfiguration = BIM.IFC.Export.UI.IFCExportConfiguration.CreateDefaultConfiguration();"

 

I'm pretty sure I need to load a gallery, dll or similar, but I can't identify what it is and especially how I do it

 

Thanks in advance

0 Likes
Message 8 of 12

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @javier.mujica .

 

Yes, you need to import IFCExportUI.dll.

This dll is in "C: \ Program Files \ Autodesk \ Revit 202x \ AddIns \ IFCEexporterUI \ IFCExportUI.dll".

You have to import this dll in the same way as you imported RevitAPI.dll and RevitAPIUI.dll.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 9 of 12

javier.mujica
Participant
Participant

Thank you very much for the help.

 

BUT, now I run into another stumbling block...

 

I have to set the VisibleElementsOfCurrentView to true.

configuration.VisibleElementsOfCurrentView = true

 

But for this above, it is necessary to define the ElementId and I do not give how to do it, since it cannot be defined as null.

ElementId ExportViewId = null

 

Remember my previous problem was "I have links in my Revit project. If I manually export with the "Export linked files as separate IFCs" checkbox enabled, I get multiple IFC files in the output."

 

Thanks in advance for everything you can help me.

 

0 Likes
Message 10 of 12

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @javier.mujica ,

 

Use FilteredElementCollector and get your required 3D view and pass the 3D view id to ExportViewId.

There are a lot of posts on the Revit API forum on how to get the 3D view from the document.

Could you please try this suggestion and let me know whether it helped you or not?


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 11 of 12

javier.mujica
Participant
Participant

Hi everyone,

 

I already have the macro/api exporting the IFCs as I wanted, thanks for all your help.

 

I only need one thing, I have the macro/api at the application level (public partial class ThisApplication).

 

I need that when I open a document, any RVT document, the macro/api is executed by itself, how can I do the latter?

 

I can't use the "private void Module_Startup", because it only works if the macro/api is at the document level and as I said it is not the case.

 

 

thanks in advance for your help

 

 

 

0 Likes
Message 12 of 12

jeremy_tammik
Alumni
Alumni

In `OnStartup`, you can subscribe to `DocumentOpened`. In that event handler, you can do the dirty deed:

  

https://www.revitapidocs.com/2022/7e5bc7a1-0475-b2ec-0aec-c410015737fe.htm

  

  

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