RevitAPI: Close active document

中文链接

By Aaron Lu

In the forum, customer said he wants to close the active document and reopen it, he took the suggestion from Jeremy’s blog, and had some events related questions, though I did not totally understand yet :(

 

As we know, to close document, we can use UIDocument.SaveAndClose() or Document.Close(), however, if the document is active, we got an InvalidOperationException: The active document may not be closed from the API.

So we need to use something else, Jeremy mentioned one way by sending “Ctrl+F4” message to Revit, i.e.:

 SendKeys.SendWait( "^{F4}" );

Another way is, open and activate a placeholder document, and then close the original one, that’s what the customer tried to do, and it works. To summary what the article is about, I used a shorter code snippet:

var placeholderFile = @"C:placeholder.rvt";
var doc = commandData.Application.ActiveUIDocument.Document;
var file = doc.PathName;
var docPlaceholder = commandData.Application.OpenAndActivateDocument(placeholderFile);
doc.Close(false);
var uidoc = commandData.Application.OpenAndActivateDocument(file);
docPlaceholder.Document.Close(false);

Steps:

  • Open and active the placeholder document
  • Close the doc
  • Open and activate the doc
  • Close the placeholder document

So, we don’t need any events for now :)

 


Comments

4 responses to “RevitAPI: Close active document”

  1. Matt Taylor Avatar
    Matt Taylor

    You may also use postcommand to close the last active document.

  2. Good suggestion, thanks very much!

  3. Dear Matt,
    Thank you for the hint.
    Can you just elaborate a little?
    For instance, what is the postable command id?
    Do you happen to have a sample code snippet at hand?
    Maybe I could summarise Aarons findings and your comment to replace the existing discussions on The Building Coder.
    Thank you!
    Cheers, Jeremy

  4. I’d like to thank the author for writing such an insightful and informative blog post about revit api documents that is not just useful to the readers but also revealing.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading