0

I have the method here http://teocomi.com/export-revit-warnings-list-from-api/ and am calling it from an application macro method to export warnings for a folder of rvt files:

public async void ExportWarningHTML()
{
    Autodesk.Revit.UI.UIApplication uiapp = this;
    Document doc = uiapp.ActiveUIDocument.Document;

    // Input Directory
    string inputDir = @"C:\input";

    // Output Directory
    string outputDir = @"C:\output";

    //Get files from inputDir
    string[] files = Directory.GetFiles(inputDir, "*.rvt");

    // Set open options to detach from central and preserve ws
    OpenOptions openOptions = new OpenOptions();
    openOptions.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets;

    // Process each *.rvt file in folder
    // Naive approach. DOES NOT WORK.           
    foreach(string file in files)
    {           
        // Get current doc
        var docLast = uiapp.ActiveUIDocument.Document;

        // Open new document
        var docNext = ActiveUIDocument.Application.OpenAndActivateDocument(file);

        // Close last document          
        docLast.Close(false);

        // Export Warnings
        var html = await Win32Api.ExportWarinings(uiapp, outputDir);    
    }   
}

}

However this only works for the first file then crashes. How can I modify this code or the linked "ExportWarnings" code I linked to to have this process a folder of .rvt files.

arch tech
  • 3
  • 3
  • It looks like it's crashing because of the way you're opening and closing documents. I may be wrong, this is just a hunch. Try adding try catch and break points to see where it crashes. Just guessing it would probably read best to read ```ActiveUIDocument.Application.OpenAndActivateDocument(file); await Win32Api.ExportWarinings(uiapp, outputDir); ActiveUIDocument.Application.Close(false);``` Possibly wrong but I don't see, from this code, any reason to do it that way. – Michael Puckett II Jan 13 '18 at 19:10

1 Answers1

0

Congratulations on your very nice solution to Export Revit Warnings List From Api!

As you know, the Revit API can only be used within a valid Revit API context. Such a context is provided only within callback functions provided by the Revit API, such as external command Execute. Furthermore, the Revit API is not multi-threading. Making calls to the API outside such a context can lead to a crash. That may well be exactly what you are experiencing.

Therefore, I wonder whether async can be used at all in this context. One possibility to handle these restrictions is by making use of external events:

http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

Is this code running in an external command Execute method? If so, how about just removing the async stuff, and simply calling Sleep repeatedly until Revit has finished processing the first file?

No, that will probably not work, and is probably not right at all.

Next suggestion: remove async; make the call to process the next file; when it is done, raise an external event; within the external event, repeat the algorithm to process the next file; etc.

I am very much looking forward to hearing how you resolve this!

Jeremy Tammik
  • 7,333
  • 2
  • 12
  • 17
  • Thanks Jeremy! An honor to get a response from you. I was thinking along the same lines and I now have a solution using external events that works. I will post a more detailed follow up comment later. Your links to the event documentation helped set me straight. For the record the code I linked to in my question is not mine. Thank you. – arch tech Jan 15 '18 at 21:44
  • my pleasure! thx for your appreciation! congratulations on solving! looking forward to a more detailed sample anon. if it is complete and generic enough to be of general interest, i would love to add it to the blog. – Jeremy Tammik Jan 17 '18 at 06:07