0

Sup' I would like to link an IFC file with Revit Design-Automation, not to directly open it because opening causes troubles: Working with IFC files? Elements missing, limited functionality?, so I'm OMW 2 link it using code from that article:

ModelPath mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(@"C:\input.ifc");
RevitLinkOptions rlo = new RevitLinkOptions(false);
var linkType = RevitLinkType.Create(RevitDoc, mp, rlo);
var instance = RevitLinkInstance.Create(RevitDoc, linkType.ElementId);

The first step is to be able to create a new empty document, however what I'm seeing so far is that NewProjectDocument is available on Application but not on ControlledApplication which is the object available when running in DA.

My question then, is there a way to create a new document from DA? Alternatively is there a way to link the input ifc instead of directly opening it in DA?

Thanks for the insights.

Felipe
  • 4,325
  • 1
  • 14
  • 19

2 Answers2

1

You are able to do both.

There is a way to create a new document in DA. See the documentation (and copied code) here:

  private static void SketchItFunc(DesignAutomationData data)
  {
     if (data == null)
        throw new InvalidDataException(nameof(data));

     Application rvtApp = data.RevitApp;
     if (rvtApp == null)
        throw new InvalidDataException(nameof(rvtApp));

     Document newDoc = rvtApp.NewProjectDocument(UnitSystem.Imperial);
...

If you would like to link an input IFC, you will want to use the CreateFromIFC function that can be found in the RevitAPI docs here.

In addition, if you'd like to use your own template, you can find some examples here. This example is for Inventor but this would work for Revit as well.

If you're using the revit-ifc library found here, make sure to include using Revit.IFC.Import;, along with the following snippet that imports an IFC and saves the Revit model:

         IDictionary<string, string> options = new Dictionary<string, string>();
         options["Action"] = "Link";   // default is Open.
         options["Intent"] = "Reference"; // This is the default.
         string fullIFCFileName = "YourIFC.ifc";
         Importer importer = Importer.CreateImporter(rvtDoc, fullIFCFileName, options);
  
         try
         {
            importer.ReferenceIFC(rvtDoc, fullIFCFileName, options);
            ModelPath path = ModelPathUtils.ConvertUserVisiblePathToModelPath("LinkIFC_Result.rvt");
            rvtDoc.SaveAs(path, new SaveAsOptions());
         }
         catch (Exception ex)
         {
            Console.WriteLine("Exception in linking IFC document. " + ex.Message);
            if (Importer.TheLog != null)
               Importer.TheLog.LogError(-1, ex.Message, false);

            return false;
         }
  • Thanks! Concerning linking an IFC, unfortunately I'm not able to achieve that based on the description given in the API doc, the 3rd parameter "revitLinkedFilePath" is quite cryptic to me. I'm also not able to find any concrete sample using that API method, so I'm wondering if any 3rd party dev ever achieved that. All I found is that https://github.com/Autodesk/revit-ifc/issues/380, which so far has also been left unaddressed. If you have an example please feel free to share. Thanks. – Felipe Nov 08 '22 at 08:36
  • Hi, I've copied an example that uses that library in the above answer. – Ashwin ShivaShankar Nov 08 '22 at 15:39
  • See also: https://github.com/yiskang/DA4R-revit-ifc-linker – Eason Kang Aug 15 '23 at 09:01
  • BTW, while linking IFC with `RevitLinkInstance.Create`, Revit takes an elready created .ifc.RVT file (a cahche of the .ifc file) and links it into the host document, according to our engineering team. So, a concrete .ifc.RVT file is required. – Eason Kang Aug 15 '23 at 09:06
1

Update 2023-Aug-15

The Revit.IFC.Import.Importer also works on Revit DA env with my test, so here is the sample addin: https://github.com/yiskang/DA4R-revit-ifc-linker

==============================

The revitLinkedFilePath is the ifc filename + ".RVT", which is the name of the intermediate Revit file to create for IFC links. This file must be created before linking with the host.

For example, if rac_simple_project.ifc is the IFC filename, then revitLinkedFilePath will be rac_simple_project.ifc.RVT.

Here are the references I found in revit-ifc:

/// <summary>
/// Generates the name of the intermediate Revit file to create for IFC links.
/// </summary>
/// <param name="baseFileName">The full path of the base IFC file.</param>
/// <returns>The full path of the intermediate Revit file.</returns>
public static string GenerateRevitFileName(string baseFileName)
{
   return baseFileName + ".RVT";
}

/// <summary>
/// Get the name of the intermediate Revit file to create for IFC links.
/// </summary>
/// <param name="baseFileName">The full path of the base IFC file.</param>
/// <returns>The full path of the intermediate Revit file.</returns>
public static string GetRevitFileName(string baseFileName)
{
   if (Importer.TheOptions.RevitLinkFileName != null)
      return Importer.TheOptions.RevitLinkFileName;
   return GenerateRevitFileName(baseFileName);
}
Eason Kang
  • 6,155
  • 1
  • 7
  • 24