API for getting if revit if in command ?

API for getting if revit if in command ?

sizy458
Advocate Advocate
599 Views
5 Replies
Message 1 of 6

API for getting if revit if in command ?

sizy458
Advocate
Advocate

Hello,

 

Is there a Revit API function to know if we are in a command?

 

For example the command returns true if we have clicked on the wall command, revit waiting for a point click.

 

Thank.

0 Likes
600 Views
5 Replies
Replies (5)
Message 2 of 6

mhannonQ65N2
Collaborator
Collaborator

You only have access to the Revit API in a valid API context. Revit only gives you access to the API context in these specific ways: custom commands, updaters, external events (which are not actual .NET events), various .NET events (most notably Idling, DocumentOpened, and DocumentChanged), and custom external servers/services.

 

In your example, when Revit is waiting for a point click inside its wall command, it will not provide API context to any add-ins.

 

As far as I know, the only way you can have access to the API context in relation to a Revit command is through an Updater or the DocumentChanged event. However, these only run when a transaction has been or is being committed. There are probably some Revit commands that use (or can use) multiple transactions, in which case the updater or DocumentChanged event will run while the command is still running (well, technically every command is still running while its transaction is being committed but many exit after it has committed).

 

I am not aware of any way to get what command committed the transaction that triggered an updater or is responsible for a DocumentChanged event. It might be possible to check the latest entries in the journal file, but I am not sure it will be up to date with the current/latest command--it may just show the previous command.

Message 3 of 6

jeremy_tammik
Alumni
Alumni

Very tricky, as @mhannonQ65N2  points out. If your subscribe to the Idling event in continuous mode, it will fire as long as Revit is not actively involved in some other process. But, but, but. Here is another related discussion: 

  

https://thebuildingcoder.typepad.com/blog/2015/06/archsample-active-transaction-and-adnrme-for-revit...

  

Again, it hints at various difficulties. In summary, tricky.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 4 of 6

rhanzlick
Advocate
Advocate

you can use the UIApplication property: ActiveAddinId

 

https://www.revitapidocs.com/2015/8e0bea86-9882-c8a5-4562-471989c6b56c.htm

0 Likes
Message 5 of 6

jeremy_tammik
Alumni
Alumni

Very interesting! Do you have to be in a valid Revit API context to call it, or can it be called from outside such a context? Have you used it yourself? How, exactly, please? Thank you!

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 6 of 6

rhanzlick
Advocate
Advocate
I've tried it but never found it particularly useful for anything of value. I'm not sure of the specifics, just aware it exists! Sorry, I couldn't provide more background.
0 Likes