Get all open Revit Documents

Get all open Revit Documents

mcaruso
Participant Participant
2,962 Views
9 Replies
Message 1 of 10

Get all open Revit Documents

mcaruso
Participant
Participant

Hi,

 

I'm coming from AutoCAD development background and was looking for a similar method to get all open documents opened in the Revit UI.

 

Is there something like this for Revit?

 

Thanks

 

0 Likes
2,963 Views
9 Replies
Replies (9)
Message 2 of 10

ricaun
Advisor
Advisor

The Application class has a Documents method, doc below.

https://www.revitapidocs.com/2022/8a32ffe0-7e80-e85c-78aa-1af48ace4d74.htm

 

Some like this:

UIApplication uiapp = commandData.Application;
var documents = uiapp.Application.Documents;

 

See yaa

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

Message 3 of 10

perry.swoboda
Advocate
Advocate

You can also use the GetOpenUIViews method from the UIDocument.

IList<UIView> currentOpenViews = uidoc.GetOpenUIViews();
0 Likes
Message 4 of 10

perry.swoboda
Advocate
Advocate

Oh wait, you wanted documents.  🙂

0 Likes
Message 5 of 10

mcaruso
Participant
Participant

Thanks, everyone! I ended up using:

 

Autodesk.Revit.ApplicationServices.Application app = cmdData.Application.Application;
DocumentSet ds = app.Documents;

 

0 Likes
Message 6 of 10

YorickSOHO
Contributor
Contributor

Hi, I am reopening this thread as my researches keep leading me here.

 

I feel the app.Documents solution doesn't really answer the question as it was written because it gets all database opened documents not just the ones opened and visible in the UI.

 

I would like to get only UI opened documents.

 

I would like my code to close all documents (app.Documents) except for the ones that are opened in the UI. Script should not temper with my user interface and work.
I thought I could do this thanks to UIDocument and GetOpenUIViews() but it only returns the opened UIViews for the UIDocument  passed (logic you may say)
I also tryed to use Application.Documents, go through each Document and get their ActiveView, but it returns null even for documents which are opened in the UI but are not the active document.

 

I hope someone can get me out of this Ourobos puzzle 

0 Likes
Message 7 of 10

perry.swoboda
Advocate
Advocate

Maybe try a combination, though I was unaware you could open a document in Revit without opening at least one view.

Go through all the docs, convert them to UIDocuments and then check if they have any open views.

This is untested code but something like this.

var docs = app.Documents;
foreach (var doc in docs)
{
    UIDocument uiDoc = new UIDocument(doc);
    if (uiDoc.GetOpenViews().IsAny())
    {
        // This is a doc with an open view in the UI.
    }
}

Message 8 of 10

TripleM-Dev.net
Advisor
Advisor

Hi @YorickSOHO ,

 

What's happening that you need to close documents that aren't visible in the UI?

 

There can be valid reasons a document is open but not visible in the UI, for instance Linked Revit models opens it as read-only so it can be displayed as link. This is also the reason a document can't be opened in the UI AND linked to a document in the same session. See: Document.Islinked I wouldn't advise closing those (if that's even possible like that)

 

The only one I can think off is that a addin has opened the document in the background and not closed it properly.

0 Likes
Message 9 of 10

YorickSOHO
Contributor
Contributor

Thank you @perry.swoboda, it was what I was after. I too often neglect looking at API constructors.

 

For a bit of context, yes you can open a document without any view opened. For instance, and my specific case, using doc.EditFamily(Family) will open the family document in the background. OpenDocumentFile() will do just that as well,  API documentation says : "This method opens the document into memory but does not make it visible to the user in any way."

 

What my code does is from a family opened in UI, it goes through the tree of nested families, make changes to all of them and reloads them reversing the tree. I then need to Close these documents to release memory.
I just wanted to prevent closing of all documents but only close the ones opened by my script (without needing to log them in any way).

 

As for @TripleM-Dev.net fear (?), Close method returns an InvalidOperationException if you try to close a link, or any document you shouldn't close, like the active one,  so we're safe here 😉

 

Regards,

0 Likes
Message 10 of 10

TripleM-Dev.net
Advisor
Advisor

Hi,

 

"I just wanted to prevent closing of all documents but only close the ones opened by my script (without needing to log them in any way)."

 

I Use the document's hash code as a reference to find the correct document again,

Remarks
The hash code is the same for document instances that represent the same document currently opened in the Revit session. The hash code is generated when a Revit file is opened or created in session. If the same Revit file is opened later (in the same session or a different session) the hash code will not be the same.