Selection Questions

A whole bunch of questions on selection of elements and points came in during the last day or two.
Here are a few of them:

  • How can I force the user to select an element when my external command begins?
    Answer…
  • How can I invoke an existing Revit command when my external command begins?
    Answer…
  • In the discussion of the

    Revit window handle and modeless dialogues

    you list selected elements in a modeless dialogue box.
    Could the reverse operation be implemented, displaying the selected elements in a list box with check marks next to each, and unselecting them in the Revit user interface if the user removes the check mark?
    Answer…
  • I’d like to be able to click at a point on a drawing and get its coordinates.
    Is this possible without actually selecting an existing element?

    Answer…
  • I call PickOne repeatedly, asking the user to pick elements on the screen.
    However, when I iterate the selection set, the order is different.
    How can I retain the selection order when repeatedly calling PickOne?
    Answer…

Selecting an element

This functionality is supported by the Revit API and its use is pretty straightforward.
Two interactive user selection methods can be used while an external command is active, PickOne and WindowSelect.
You can call PickOne in a loop until the user selects the specific element that you require.
The basics of using PickOne are discussed in

this post
.
We also provide an example demonstrating how to

pick a specific element type
.
Finally, here is a more complex example

presenting the current contents of the selection set in a modeless dialogue

and discussing how more complex analysis of the current selection could be added.

Invoking a Revit command

Unfortunately the Revit API does not yet provide access for an external command to invoke any other existing Revit commands.
Even though the official API does not provide this access to the command, you might be able to use the Revit journaling mechanism or the Win32 API to simulate user input to provide this functionality.
Please note that both of these approaches are completely unsupported and must be researched, tested and maintained by you completely at your own risk.

The basics of the API access to the journaling mechanism are demonstrated by the Revit SDK Journaling sample.

Using the Win32 API, you can simply send Windows messages simulating user input to either select the corresponding Revit menu entry.
For example, to invoke the Room command, you could simulate selecting the Revit menu entry Drafting > Room or typing in the keyboard shortcut RM.
This is discussed in the post on

driving Revit from outside
.
An alternative method to using Win32 API calls directly yourself to simulate user input is also available:

AutoHotKey
.
If you do decide to use the Win32 API, you may also be interested in the discussion on the

Revit window handle and modeless dialogues
.

Deselecting elements through a checkmark in a modeless dialogue

Yes, that is perfectly possible. In the

modeless dialogue

we presented, the selected elements are simply listed in a text label.
They could also be displayed in a list box instead, with a check mark for each selected item.
In that case, you could implement code to unselect the element in the Revit user interface by unchecking its corresponding check box in the modeless dialogue.

This is possible because the Document Selection property is a Selection object which has a property Elements, which is a SelElementSet instance, which provides methods named Erase and Remove. This class is derived from ElementSet and Erase is inherited from that class. I am not sure how Erase affects the current selection in the Revit UI; you might like to test that.
Anyway, according to the help file, Remove will “remove the specified from the selected element set, just like de-select it from the UI”.
Although the formulation is rather strange, I would say that calling the method with a given selected element will unselect it in the document selection set and in the Revit user interface.
I am not sure whether Erase will have exactly the same effect as Remove.
I might try it out as soon as I have some time.

Picking a Point

This issue and the meagre workarounds I can think of were already discussed in the post on

picking a point
.

Retaining the element selection order

The reason that there is no inherent order in the Document Selection collection is that its Elements constitute a set and as such are unordered.
If you wish to keep track of the original selection order, you would have to do so yourself. You could achieve this by iterating over the entire selection set after each call to PickOne and determining which new element has been added by the last call. You could then keep track of the elements added in an own ordered list, in parallel with the unordered set maintained by Revit.


Comments

17 responses to “Selection Questions”

  1. Jeremy,
    The following selection method works fine with Revit 2009 but not Revit 2010. Any idea why?
    SelElementSet selElements = doc.Selection.Elements;
    /*
    – code here for adding elements by whatever filter to the selElements, for example columns in a specific level or a view
    – debuging shows that selElements set has about 30 columns
    – Yet it does not actually select anything even if you do the following
    */
    doc.Selection.Elements = selElements;
    Pleeeeeeeeeeeeeeeeease help.
    Regards,
    Nadim

  2. Dear Nadim,
    I know for a fact that the following code works:
    SelElementSet selElements
    = Document.Selection.Elements;
    IEnumerable walls
    = GetTheWalls();
    foreach( Wall wall in walls )
    {
    bool weWantIt = Check( wall );
    if( weWantIt )
    {
    selElements.Add( wall );
    }
    }
    Document.Selection.Elements = selElements;
    Please stop using the silly “Pleeeeeeeeeeeeeeeeease”.
    Cheers, Jeremy.

  3. Jeremy,
    Sorry for using that silly please, it is just that I spend all this time in the past few weeks writing code for Selection and Printing tools, suffering was with me all along until with your help and experimenting I got the plotting and selection to work fine. The changes they made to the API took me back to where I started. Nothing is working on API 2010. you can understand my frustration.
    back to this problem:
    I have a code that is the same as yours. I also have the last statement
    doc.Selection.Elements = selElements;
    if you degug it, the Selection.Elements is not empty. However, on the screen, nothing is selected.
    Here is what I found today: if I use the pan command and move the building, it colors the elements to be selected with Red (my selection color) but still I can get either of the following:
    1. No Contextual menu appears, i.e. I can’t do anything to the selected elements (makes it useless)
    2. I don’t get the Element Properties command, as if nothing is selected.
    Is there any extra code we have to write for Revit 2010 to give us the complete selection expectations
    Thanks again,
    Nadim

  4. Dear Nadim,
    As I have probably said before, if you are doing this professionally, I strongly suggest you join ADN:
    http://www.autodesk.com/joinadn
    If you do not want to do so, there are other forums where you can discuss with your peers:
    http://thebuildingcoder.typepad.com/blog/2009/10/revit-api-forums.html
    I am happy to answer any question when I can.
    Cheers, Jeremy.

  5. Thanks anyway, you’re doing a good job.
    To answer your question, no I am not doing this professionaly, because I am a Structural Engineer and not a programmer. I just have some overhead time and I spend on the API.
    I wish we can join ADN; however, we do not meet the requirments to joint.
    The reason I posted back to you is that It was obvious the issues I am having are because of bugs they introduced to Revit 2010. Just by installing Web Update 2 fixed half of my problems in all the commands I created. It would be great to know if I have to wait for another update for my code to work again or try to figure it out my self?
    Thanks anyway.
    Regards,
    Nadim

  6. Dear Nadim,
    I am glad that half your problems are solved!
    I am not aware of any requirements to join ADN, it is open to anybody interested in using Autodesk APIs.
    Cheers, Jeremy.

  7. Jeremy,
    Here are the requirments to join ADN:
    http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=723350
    we don’t fullfil the following requirment for sure because we are a structural engineering company
    “You have at least one full-time professional software developer on staff”
    Even if we do, we might use it for a week and then get busy with engineering work and never use it again.
    I hope you bear with us even if we bug you with the same issue again, because sometimes I will find something new that your input would be valuable.
    Examples:
    After installign Web Update 2,
    – my selection commands start to give me the contextual menu but not the Element Properties (half is solved).
    – in printing, the tmpViewSet is not populated back to the ptManager…CurrentViewSet.Views but the printSetup and printParameters are not
    it is abvious that this problem was caused by changed on Autodesk side (it works well with 2009), yet they provide zero documenations on these changes and its effect on our existing code.
    Regards,
    Nadim

  8. Jeremy,
    After enough research and testing, I found that the selection example in the manual is actually having the same problem my code has. Which is: it selects the elements and it provides the contextual menu, but it would not give access to element properties like it does with the UI.
    If the code in the Revit Manual is not working, then I gave up.
    Regards,
    Nadim

  9. Dear Nadim,
    I am sorry to hear that you had to give up on this, and that I am unable to provide any help … at least you know more about the issue now than I do!
    Congratulations on solving the view set problem, at least, as you mentioned in your separate comment on
    http://thebuildingcoder.typepad.com/blog/2009/06/revit-mep-api.html
    Cheers, Jeremy.

  10. Thank Jeremy,
    I am very humbled by your statement. I will never know more than you do. Your blog was a savior for me.
    I am just a structural engineer, and I am just trying to use my time in something meaningful as we don’t have much engineering work to do :) (The current economy story)
    Please don’t treat me as a professional programmer because I am not. Until recently, I only knew VB6. I am new to the .NET, and thanks to you, I shifted to using C# because your examples are in C#, and I love it.
    Please keep the good work as amateurs like me need your help.
    Regards,
    Nadim

  11. Dear Nadim,
    I am glad to hear that you like C#, and congratulations on getting into programming and .NET. I am very glad to be able to help.
    Cheers, Jeremy.

  12. Feedback on RevitAPI 2010:
    After I finished fixing my external application, I have the following feedback on two issues with RevitAPI 2010.
    1. The selection commands do not seem to work properly.
    Sometimes the elements are not highlighted, I worked around that by adding elementSet.Hide() and then elementSet.Unhide() methods to get elements highlighted always.
    Contextual menu is showing now after installing the Revit Web Updates.
    Access to the Element Properties of selected element is NOT possible (unline Revit 2009), no work around in the code. At the user interface, I have to move the selected elements a zero distance and I would get the access to the properties.
    2. Print Project Sheets
    no matter what you do, ptManager.SubmitPrint() will always print the sheets in the order they are created. I guess that Revit sorts ViewSets internally by the element id. Don’t waste your time trying to implement a custom IComparer to compare ViewSheets by their names, it will not do it.
    The work around is to save the names of the views in a Generic List and then sort it. Then send sheets one at the time in the the sequence of the sorted List.
    Regards,
    Nadim

  13. Dear Nadim,
    Congratulations on finishing your application and thank you for the feedback and hints!
    Cheers, Jeremy.

  14. Thanks Jeremy,
    Just like I benefit from this unique blog, I wanted to contribute to it too.
    Best regards,
    Nadim

  15. Vitor Gomes Avatar
    Vitor Gomes

    Hello Jeremy,
    I’m brazilian, 22 years old and need some help with Revit API.
    My question is: how can I retrieve the elements on the same order that I’ve selected?
    I’m doing a foreach, and the order of sellection (sellecion.Elements) is by ElementId.
    Is there a way to retrieve the Elements in order I selected?
    Thank you.
    PS: Please, forgive my language mistakes. I’m not fluent in english.

  16. Dear Vitor,
    Your English is perfectly fine, no need whatsoever to apologise or worry about it.
    If you select elements in bulk there is no control over the selection order.
    If you need control over the selection order, you can implement a loop using the Revit API picking them one at a time, putting them into a sorted collection that you have total control over.
    Cheers, Jeremy.

Leave a Reply to NAycheCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading