Here is a small note on how to make use of the PickOne Revit element selection method.

For any source code example of how to use any Revit API method, one obvious to start looking is always the global solution SDKSamples2009.sln in the SDK Samples folder.

PickOne is used in the Revit SDK samples VisibilityControl and PowerCircuit. You can see this by opening SDKSamples2009.sln and searching globally for PickOne(). This returns a list of hits, e.g.:


C:SDKSamplesVisibilityControlCSVisibilityCtrl.cs(138):
m_document.Selection.PickOne();
C:SDKSamplesPowerCircuitCSCircuitOperationData.cs(518):
if (!m_revitDoc.Selection.PickOne())

Here is an excerpt from the VisibilityControl sample, slightly cleaned up and reformatted for readability, and with some numbered comments added in bold:


public void Isolate()
{
  // 1. clear selection:
  m_document.Selection.Elements.Clear();
  switch (m_isolateMode)
  {
    case IsolateMode.PickOne:
      // 2. pick one element:
      m_document.Selection.PickOne();
      break;
 
    case IsolateMode.WindowSelect:
      m_document.Selection.WindowSelect();
      break;
  }
 
  // 3. retrieve selected elements:
  Autodesk.Revit.ElementSet elements
    = m_document.Selection.Elements;
 
  // hide all categories elements
  foreach( Category cat in
    m_document.Settings.Categories )
  {
    SetVisibility(false, cat.Name);
  }
 
  // 4. make use of selected elements:
  // set the selection elements visibility
  foreach( Element element in elements )
  {
    Category cat = element.Category;
    if( null != cat
      && !string.IsNullOrEmpty( cat.Name ) )
    {
      SetVisibility( true, cat.Name );
    }
  }
}

The PickOne method manipulates the document’s current selection set, accessible through the document Selection property.
The bold numbered comments correspond to the critical steps:

  1. Clear the current selection.
  2. Execute the PickOne method.
  3. Access the updated selection set.
  4. Do something with the selected elements.

Hopefully this clarifies the use of the PickOne method.


Comments

9 responses to “PickOne”

  1. Eric Anastas Avatar
    Eric Anastas

    If you use PickOne rather then window select then you know that there is only one element selected. Is there a way to efficiently get at the single selected element, rather then using for each? Here what I’ve been doing:
    ElementSetIterator elemIter;
    Element pickedElement
    //clears the current selection
    doc.Selection.Elements.Clear();
    //prompts the user to select an element
    doc.Selection.StatusbarTip = “Please select an object”;
    //checks if the user hit escape
    if (doc.Selection.PickOne() == false)
    {
    return IExternalCommand.Result.Cancelled;
    }
    //Creates an element iterator
    elemIter = doc.Selection.Elements.ForwardIterator();
    //move to the first item
    elemIter.MoveNext();
    //Assigns pickedElement
    pickedElement = elemIter.Current as Autodesk.Revit.Element;
    What I really want to do is reference the single selected object in one line. Something like.
    Element pickedElement = doc.Selection.Elements[0];

  2. Hi Eric,
    Your query makes perfect sense, but I am not aware of any other method to get at the member elements in the document selection set. The only access to member elements provided by the SelElementSet class and its base class ElementSet are through the ForwardIterator and GetEnumerator members, which both return a forward moving iterator to the set. There is no indexed access.
    Cheers, Jeremy.

  3. Hi Jeremy,
    I am a beginner at Revit API (C#). I am trying to isolate/hide selected elements but I couldn’t find out the function to do it. Could you show me the way to solve this problem? Thank you in advance.
    Trang.

  4. Dear Trang,
    The Revit SDK VisibilityControl sample shows how to control visibility by category.
    To control the visibility of individual selected elements, you can use the View.Hide Method and pass in selected elements to be hidden in the view.
    Cheers, Jeremy.

  5. Thank Jeremy. It works (^_^)
    Trang.

  6. Brilliant! Thank you for letting us know!
    Cheers, Jeremy.

  7. Hi Jeremy,
    I still have a problem with the ActiveView. I can hide selected elements directly in Commands (inherited from IExternalCommand), but I can’t if I use a userform to interact with Revit.
    It is so, I have a form and a button on it. When I click the button, I can read attributes of selected elements but I can’t hide them.
    I don’t know if I must activate the ActiveView again before I want to hide elements. And if that is necessary, I also don’t know how to do that. I tried but I couldn’t find it out.
    Thanks.
    Trang.

  8. Trang Avatar
    Trang

    Hey,
    everything now is OK, Tim in AUGI forum showed me. I didn’t use Transaction and maybe, that was the reason; when I add Transaction, it works :D
    Trang.

  9. Dear Trang,
    Congratulations on solving the issue, I am glad for you!
    Cheers, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading