Explode a DWG

After yesterday's very practical note on creating a pipe cap, here is another slightly less practical note from Rudolf Honke of acadGraph CADstudio GmbH, who picked up on Sami's comment on subscribing to UI Automation events. Sami asks:

Question: Is it possible to explode an imported DWG file using the API?

Answer: After showing the mechanism of invoking commands twice, I do think this can be achieved.

Just like all other ribbon elements, also the 'Explode' buttons are accessible via UI Automation.

By the way, I have noticed that sometimes an ImportInstance does not contain all the geometry that is visible to the user, even using tiny DWGs consisting just of a few lines or curves. If we explode it after import, we can access all the lines by subscribing to the DocumentChanged event and reading the event argument GetAddedElementIds data, because the explosion results are added to the database as individual new Revit elements. This way, we can work around the issue of the missing elements and retrieve all the DWG data we need.

After collecting the geometrical data, we could roll back the transaction to undo the explosion. If we are using UI Automation, though, we have already left our own command context, so we cannot manipulate the transaction programmatically. Even in this case, though, we can still undo the operation by again using UI Automation to simulate a click on the Undo button.

UI Event Subscription Sample

Slightly less practical?

Who said anything about slightly less practical?

Here is a complete sample application demonstrating some aspects of event handling using UI Automation:

I am not the kind of person who says how easy it is to do something and then laughs maliciously in the background watching the others struggle.

In contrary, as an autodidact, I depend on easy-to-read information myself, and have no use for vague hints lacking concrete information, too.

To make my former UIAutomation hints more substantial, here is a Visual Studio project RevitUIAutoSamples.zip containing two mechanisms:

Please note that this is all very experimental. For instance, the ribbon bar click subscription might hang if your ribbon item is a child of a SplitButton or a ListItem.

I am very glad if you find this useful!

Funnily enough, Kean is also talking about exploding AutoCAD objects using .NET at this very moment.