Difference if change element selection using External command vs External event

Difference if change element selection using External command vs External event

Raymond_Fan
Contributor Contributor
1,043 Views
3 Replies
Message 1 of 4

Difference if change element selection using External command vs External event

Raymond_Fan
Contributor
Contributor

I am using Revit2014 UR3 and implemented addin to change element selection. When I run following codes in an event handler, element selection is updated in Revit view, but properties palette is not refreshed to show the selected element; If run same codes in external command, properties palette will be updated to show selected element. 

 

 

Autodesk.Revit.UI.Selection.SelElementSet elementSet = uiDoc.Selection.Elements;
elementSet.Clear();
elementSet.Insert(elem);
uiDoc.Selection.Elements = elementSet;
// Places the element in the center of screen by moving the view
uiDoc.ShowElements(elementSet);

 

What is the difference between external command and external event for such use case? I prefer to use event handler becuase if use external command, there is more things need be done, like how to hide the command from user, and if hide the command, PostCommand will not work as expected. 

 

Is this a known problem of Revit2014?

0 Likes
1,044 Views
3 Replies
Replies (3)
Message 2 of 4

arnostlobel
Alumni
Alumni
On one hand, there is a huge difference between external commands and external events. On the other hand, their actual execution is rather the same.

Regarding the former - the biggest differences is in usage. While external commands are executed and invoked directly by the end user (i.e. the user must do something in the UI), external events are execution upon programmatic request from an add-in working on other than the main thread. The end-user can still be involved in the execution and triggering the event, but only indirectly (i.e. he or she may do something in the add-in's UI, but the even invocation is done by the application.) Thus, for these differences, the applications of either a command or external event differ greatly.

Now, as far as actual execution goes, I do not see a huge difference there. Both external events and external command implement an Execute method on an interface, and within the method they can do pretty much anything (commands can do more than external events.) The Execute method gets the Revit Application object via its argument in both cases. Via that object the programmer has access to the API (the application, documents, UI etc.)

That being said, there may be differences between those two when it comes to UI operations (such as refreshing a selection). It is because External Command is modeless and is executed upon a particular UI command message, External Event is executed during an Idling state. Because of that, there are certain differences in how the UI can be kept updated during the execution. We do not actually recommend modifying the UI in any way while executing an external command. It is because Revit is Idling and it cannot process incoming windows messages, at least not predictably. On the other hand. Since External Commands are modals, the programmers may need to explicitly refresh the active UIDocument.RefreshActiveView) view in order to make it reflect changes.

Arno?t
Arnošt Löbel
0 Likes
Message 3 of 4

Raymond_Fan
Contributor
Contributor

Thanks for your detailed reply.

 

My problem is, my addin shows a modeless form, and when click a button in it, it will highlight some elements in view, and it is better to update properties palette as well. So I has to use either external event or command for this purpose. when I use External event to highlight elements in view, it will not update Properties palette; using External command, it will. So I has to select external command and use PostCommand to call it. But to use command, problem is I don't want it be visible in Revit ribbon and let user be aware of it, so I tried to hide it. After hide the command, PostCommand cannot work any more. So I has to toggle the external command before and after highlight elements, it is not nice solution, and because highlight elements in view takes some time, during the processing period, user will see the command in ribbon.

 

Do you have any suggestion on my problem?    

0 Likes
Message 4 of 4

arnostlobel
Alumni
Alumni
Raymond_Fan,

Thanks for the overview; I have now a better understanding of what it is you are trying to achieve.

Unfortunately, I can offer only once advice at this time, and I even expect you not to like it a lot. In my honest opinion, the best thing to do is to report it a bug that the properties palette does not reflect changed dimension if you make the changes from a modeless dialog. In fact, I think I have heard (or seen) a discussion about this already, or something closely related. Reporting a bug (ideally through the AND channel) that can be fixed will benefit everybody. With some likeliness, someone may actually be working on it already (I will look into that and will connect with the team responsible for dock-able palettes).

If you are not using a dock-able panel, but a regular, custom modeless form, I suggest you try a dock-able panel. While it has its own set of problems, it has been designed primarily for the purpose you described.

What I definitely do not recommend is for you to invoke an external dialog via posting it from your modeless form. Like I already mentioned, I am rather surprised it works at all, but regardless of that, it sounds like an obscure workflow (however! I do understand that you are just trying to make things work, thus you naturally try whatever will do.)

Thank you
Arnošt Löbel
0 Likes