I think sending keys is not a good solution, not sure mine is better but it works:
1) Attach handler for dialogue showing event
2) Post the purge command
3) Handler then responds with OK to the purge dialogue and increments a shared integer count. If integer count is less than desired number of iterations it repeats step 2. If integer count is equal to desired number of iterations then it resets integer count, removes handler and exits loop.
I’ve written a command for processing models for export on this basis however even five purges in such a sequence often fails to remove all unused. Would be nice if the API had a direct purge call that would return information regarding what is left to purge (or even a Boolean return value indicating if 0 items remain). Processing models to remove clutter before creating IFC’s etc. is a ripe task for automation.
Similarly, you can leverage the built-in spell checker to check spelling of Revit parameters:
- Within the original external command context copy all text parameter values of a selected element or elements into new text elements in a drafting view. Create object in memory to store a link between the ElementID of each text element, the ElementID of the element containing the parameter and the thing that identifies the parameter. You have to check if it is BIP or Shared etc. to know what key you are storing for this (shared parameter GUID, BIP integer value etc). Would not advise using parameter name for a key but you may have to fall back on this if it is neither shared nor built in. If you do have to fall back on name then use the current text value of the parameter as an additional key.
- Make view with text elements current and add dialogue showing event handler
- Post spell check command
- Respond to dialogue event to remove handler and post new external event (to obtain valid context after spell check completion).
- Get active context from external event hander. You can then open a transaction to transfer values from the corrected text elements in the view back into the parameters (by mapping according to the stored relationship). You don’t even need to know if a spelling was accepted or not since that will be evident in the updated text elements, you can just transfer it as is.
- In same context delete the drafting view containing the text elements.
I’ve got this working and I am sure it will prevent the existence of many titled Stellwork G.A.s. Although I wonder why Revit does not have built-in spell-checking for parameters with text storage type?