Remove addin binding mid session

Remove addin binding mid session

kevin_fielding
Advocate Advocate
881 Views
4 Replies
Message 1 of 5

Remove addin binding mid session

kevin_fielding
Advocate
Advocate
Hi,

I'm creating an add-in that restricts the ability to create model in-place elements, however I want the ability to disable or remove the binding mid session is it possible?

The removeaddincommandbinding is only accessible at on start-up and shutdown through the uicontrolledapplication.

I don't fancy holding this in memory for the entire session as I'm sure this isn't healthy or even possible.

Does anyone have any suggestions?

Regards,

Kevin

0 Likes
882 Views
4 Replies
Replies (4)
Message 2 of 5

RPTHOMAS108
Mentor
Mentor

Can understand your feelings towards stale variables but on the other hand when else would you remove a command binding? At OnStartUp when you've only just added it or at OnShutDown when you aren't going to be wanting to press any original commands until next OnStartUp?

 

I understand there is an AddinCommandBinding.CanExecute event that can be handled I guess in the CanExecuteEventArgs you set the CanExecute to false and the command doesn't execute or appears disabled. So probably there is no need to remove command bindings you just decide when your event handler is going to return CanExecute = false vs CanExecute = true.

 

Not sure what overriding a command entails but when you create a binding to only change CanExecute you may have to create an implementation of Execute that posts the original intended command.

 

Never done this, I think command bindings are a can of worms given you don't know what other addins are running and what command bindings they are attempting. Not sure why the feature was ever added to be honest, you can't ever rely on them if your addin is last to load.

0 Likes
Message 3 of 5

jeremytammik
Autodesk
Autodesk

Dear Kevin,

 

Thank you for your query.

 

You can use the IExternalCommandAvailability interface to enable and disable a command any time you like, as often as you like:

 

http://www.revitapidocs.com/2018.1/c05acaf4-4cd9-8fd6-db06-44b22ae4f987.htm

 

Here is a demonstration showing how to use it to enable ribbon items in zero document state:

 

http://thebuildingcoder.typepad.com/blog/2011/02/enable-ribbon-items-in-zero-document-state.html

 

I hope this helps.

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 4 of 5

kevin_fielding
Advocate
Advocate

I ended up using the RemoveAddInCommandBinding method and posting the original command. 

 

I have an additional question, if I have a Command Binding, and I disabled the external command using IExternalCommandAvailability, will the original command run, and how would I reactivate the external command afterwards?

 

0 Likes
Message 5 of 5

jeremytammik
Autodesk
Autodesk

Dear Kevin,

 

Thank you for your update.

 

I agree with your original scepticism when you say, 'I don't fancy holding this in memory for the entire session as I'm sure this isn't healthy or even possible.'

 

I can imagine that you should make sure you are in a valid Revit API context whenever you decide to make the call, e.g., in an external command Execute method.

 

I hope and assume that this is the case when you say that you decided to use the RemoveAddInCommandBinding method after all.

 

Looking at the API documentation, I see no way to revert the action of calling RemoveAddInCommandBinding.

 

I guess you would have to leave some access path via PostCommand in place to retain the possibility to launch the original command.

 

I hope this helps.

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes