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
containing two mechanisms:

  • Subscribing to events such as ribbon bar clicks and Revit main window boundary changes.
  • Invoking a command twice + pressing the DWG explode button afterwards.

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.


Comments

5 responses to “Explode a DWG”

  1. Hello Jeremy, I am a student in University and doing a project for a software company.
    The topic is redeveloping an application to do the same thing as click MANAGE>SETTINGS>LINE PATTERNS>DELETE in Revit. Please tell me if the API is available for that. Thank you.

  2. Dear Jian,
    So that command simply deletes all line patterns that are stored in the model and not currently used?
    Funnily enough, this was also requested recently by Owen in his comment
    http://thebuildingcoder.typepad.com/blog/2011/02/set-elbow-fitting-type.html?cid=6a00e553e168978833014e8614cab4970d#comment-6a00e553e168978833014e8614cab4970d
    As I told Owen, this request sounds very similar to the one described by Chuck in
    http://thebuildingcoder.typepad.com/blog/2010/11/purge-unused-text-note-types.html
    I implemented a Building Coder sample command CmdPurgeTextNoteTypes to determine all unused text note types and delete them.
    I would assume that that can easily be adapted to handle unused line patterns as well. No guarantee, though, there may be hidden stumbling blocks along the way.
    Anyway, that should give you a good starting point.
    Cheers, Jeremy.

  3. I have tried the RevitUIAutoSamples with Revit 2012 Beta 2 in an XP SP3 Virtual Machine, but have been unable to get OnRibbonbarClickedEvent called (while OnMainWindowResizedEvent does.) A different approach was also not successful, so there seems to be something funny about this environment.

  4. Dear Larry,
    Why am I not surprised? :-)
    Cheers, Jeremy.

  5. Dear Larry,
    I passed your question on to Rudolf, and he replied:
    I tried to use UIAutomation with 2012. It seems that the event is fired less frequently than in 2011.
    I just made the following experience: Two minutes ago, I clicked different tab headers; and now, writing these lines, the events are fired. There is a major delay of several minutes!
    The event listener needs (far too) much time to get initialized. The click events are “collected”, beginning on Revit starting, and after some time, they are fired in the correct order. It behaves like a UI recorder, replaying which items you have clicked in the past.
    I suggest to skip this kind of UIAutomation event listener.
    Instead, I would use the approach described in
    http://thebuildingcoder.typepad.com/blog/2011/02/pimp-my-autocad-or-revit-ribbon.html
    There is a piece of code there that may be used as an replacement:
    ComponentManager.UIElementActivated
    += new EventHandlerAutodesk.Windows
    .UIElementActivatedEventArgs
    ;
    void ComponentManager_UIElementActivated(
    object sender,
    Autodesk.Windows.UIElementActivatedEventArgs e )
    {
    // e.UiElement.PersistId says which item has been pressed
    }
    I haven’t tried this in 2012, but it should be much faster than using UIAutomation.
    Cheers from Rudolf and Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading