Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Register a switch between open documents?

Anonymous

Register a switch between open documents?

Anonymous
Not applicable

Hi, 

 

I am hoping to to know if there is any way to tell if the user has switched between two open documents?

I can't see anything in Database Events that would allow me do this, Could the ViewActivated event be used to achieve the same goal?

 

Thank you in advance.

0 Likes
Reply
1,565 Views
8 Replies
Replies (8)

Anonymous
Not applicable

Apologies for premature posting using the ViewActivatedEvent does solve this problem.

 

here is the code so this post is at least a bit helpful.

 

 

        public Result OnStartup(UIControlledApplication a)
{
            a.ViewActivated += new EventHandler<Autodesk.Revit.UI.Events.ViewActivatedEventArgs>(OnViewActivated);
            return Result.Succeeded;
}
public void OnViewActivated(object sender, Autodesk.Revit.UI.Events.ViewActivatedEventArgs args) { Document doc = args.CurrentActiveView.Document; UIApplication uiapp = new UIApplication(doc.Application); Application app = uiapp.Application;

...
...
//handler code
...
...
}

 

rosalesduquej
Autodesk Support
Autodesk Support

Hi Mendo,

 

Great 🙂 thanks for sharing your answer to your own question. I have found that when you post your questions, there is a good chance you answer it yourself since you will be thinking out loud 🙂 so great for this. 

 

I will blog about it to share your answer and make the post more useful than what already is :).



Jaime Rosales D.
Sr. Developer Consultant
Twitter | AEC ADN DevBlog
0 Likes

arnostlobel
Alumni
Alumni
I guess I need to jump in before an incorrect solution is published somewhere else.

The suggested solution is only partially correct. The ViewActivated can indeed be used to capture moments when the end user switches from one visible document to another. The implementation of the event's handler is wrong, however. Programmers are not permitted to instantiate Revit application objects! It is a singleton and can only be instantiated once per session (Revit does it at startup.) In fact, an attempt to do so will throw an exception in 2016 and later releases. To get to the Application, the programmer has two options:


a) The sender of the event is UIApplication - thus the programmer can recast it

b) An instance of Application (the DB kind) can be obtained from the Document instance.

Thank you
Arno?t L?bel
Arnošt Löbel

Revitalizer
Advisor
Advisor

Hi,

 

what about:

 

private void viewActivated(object sender, ViewActivatedEventArgs args)
{
    if ((args.PreviousActiveView != null) && (args.PreviousActiveView.Document != null)) // start screen has neither view nor document
    {
        if (args.PreviousActiveView.Document.PathName != args.CurrentActiveView.Document.PathName)
        {
            // document has been switched
        }
    }
}

 

Note:

As for worksharing environments, Document.PathName is empty, as far as I know.

Either compare ModelPaths or just the Document.Title property:

 

private void viewActivated(object sender, ViewActivatedEventArgs args)
{
    if ((args.PreviousActiveView != null) && (args.PreviousActiveView.Document != null)) // start screen has neither view nor document
    {
        if (args.PreviousActiveView.Document.Title != args.CurrentActiveView.Document.Title)
        {
            // document has been switched
        }
    }
}

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





arnostlobel
Alumni
Alumni
Revitalizer,

There is no need to rely on either the Path or Title property in order to compare two currently open documents. The Document.Equals method should handle the comparison just fine. Thus, if the document of the previously active view does not equal the document of the currently active view, then an active document switch has just happened.

Cheers

Arno?t
Arnošt Löbel

Revitalizer
Advisor
Advisor

Dear Arnost,

 

thank you for pointing this out.

I didn't know that.

 

In fact, I usually don't rely on the Equals method.

Some years ago, for example, this method didn't work when comparing ElementIds, so I used to compare their IntegerValues.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





arnostlobel
Alumni
Alumni
Not relying on the standard Equals method is generally the recommended approach, as there is a lot of inconsistencies in the Revit API. However, we had made sure the comparison works for Documents especially because it is rather difficult to compare documents using other ways.

Arno?t
Arnošt Löbel

Revitalizer
Advisor
Advisor

Hi Arnost and all,

 

to sum it up, this is my candidate for the solution of this thread:

 

private void viewActivated(object sender, ViewActivatedEventArgs args)
{
    if ((args.PreviousActiveView != null) && (args.PreviousActiveView.Document != null)) // start screen has neither view nor document
    {
        if (!args.PreviousActiveView.Document.Equals(args.CurrentActiveView.Document))
        {
            // document has been switched
        }
    }
}

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine