Tracking the active document with a modeless form

Tracking the active document with a modeless form

Kevin.Bell
Advisor Advisor
1,444 Views
6 Replies
Message 1 of 7

Tracking the active document with a modeless form

Kevin.Bell
Advisor
Advisor

Hi,

 

I've come across an expected issue with a browser addon that I'm writing, the added essentially allows the user to open some views. This all works via a modeless form.

 

The addon keeps track of the active view by tracking the ViewActivated event

 

uiapp.ViewActivated += new EventHandler<ViewActivatedEventArgs>(my-routine);

This also works well, the issue comes when the user clicks into a different open project, different to the one that was open when the addon was started.

 

When the user tries to open a view, the addon uses the stored View Id to open the view, the Id does not apply in the other project and an error is generated.

 

I thought I could simply keep track of the project name: doc.Title; and 'refresh' my addon (by using a FilteredElementCollector to get the projects views) whenever the user clicks in my addon and the document title does not match.

 

This does not work as I thought, and I've noticed that the ViewActivated event only seems to apply to the project that was open and not any project, consequently, the other open project does not trigger the event the addon does not refresh and an error is generated when the user tries to open a view.

 

I think I need a 'user-has-clicked-into-another-open-document' event, but I can't seem to find one.

 

Is there any way I can detect that the current document has changed, or get the ViewActivated event to apply to all open projects?

 

Thanks.

0 Likes
Accepted solutions (1)
1,445 Views
6 Replies
Replies (6)
Message 2 of 7

BobbyC.Jones
Advocate
Advocate
Accepted solution

The ViewActivatedEventArgs provides the new view's document.

 

You'll need to refresh your UI with info from this document when this change is detected.

 

application.ViewActivated += (sender, args) =>
{
    var document = args.Document;
    //Refresh your UI with updated document info
};
--
Bobby C. Jones
0 Likes
Message 3 of 7

Kevin.Bell
Advisor
Advisor

Exactly what I was looking for - thanks for your help!

Message 4 of 7

Anonymous
Not applicable

Hey,

 

i have pretty much the same problem. I have an WPF UI, with a function that gets me all Spaces. When i change the project, i still get the spaces of the old project. So on project change i need to refresh my ActiveUIDocument or something. I saw that the answers, but i don't get it. Can someone please help me.

 

Thx

0 Likes
Message 5 of 7

RPTHOMAS108
Mentor
Mentor

You subscribe to  this event e.g. UIApplication.ViewActivated, your hander of the event gets an object of ViewActivatedEventArgs, this has a member on it named  '.Document' representing the Document associated with the event and from that you refresh your information.

 

Pay attention to ViewActivatedEventArgs.Status as the values within this enum imply the event may have been cancelled or otherwise failed (check for Succeeded I guess).

 

"This event is raised immediately after Revit has finished activating a view of the document. It is raised even when view activating failed or was cancelled (during ViewActivating event).

Check the 'Status' field in event's argument to see whether the action itself was successful or not."

 

You can't get UIDocument from ViewActivatedEventArgs. However if the sender is UIApplication then you can get it from Sender.ActiveUIDocument. This may be a better approach because it tells no lies i.e. UIApplication is always the same UIApplication and UIApplication only has one ActiveUIDocument regardless of action sucess / failure leading to a raised event you take an object from...I wonder?

0 Likes
Message 6 of 7

SONA-ARCHITECTURE
Advocate
Advocate

Hi

where to you subscribe the viewactivated event ? In your externalApplication? And if the modeless form is closed (event catching in the form) where and how do you unsubscribe the viewactivated event?

thx

Pierre NAVARRA
SONA-Architecture.
http://www.sona-architecture.com
https://fr.linkedin.com/in/pierre-navarra-62032a107
0 Likes
Message 7 of 7

matveevri
Explorer
Explorer

Did you find a solution how to unsubscribe from ViewActivated event inside modeless form?

I get revit crushed every time i try to unsubscribe from this event, tryed different ways, no result(