Select an Element

How to select or virtually "pick" an element from Revit Graphical View.

There are numbered of choices that gives a user the variety of options to select an object. I have combined all of them below.
ps. this is only for the picking single object we will talk about Multiple Object in the next Post

List of Methods: [copied from revitapidoc.com]
Method A:
public Reference PickObject(
 ObjectType objectType
)
Method B:
public Reference PickObject(
 ObjectType objectType,
 ISelectionFilter selectionFilter
)
Method C:
public Reference PickObject(
 ObjectType objectType,
 string statusPrompt
)
Method D:
public Reference PickObject(
 ObjectType objectType,
 ISelectionFilter selectionFilter,
 string statusPrompt
)

How does it work?

Upon this statement is triggered, Revit is turned into Selection Mode waiting for a user to select an element. but the question is: what if user did not need to pick an edge, face ...etc. Well, this is the role of `ObjectType` parameter. `ObjectType` directs what exactly you want to pick from Revit. It has seven different directions of selections:

Nothing: Nothing.
Element: Picks a complete element, such as wall, floor, window, group...etc
PointOnElement: Any point on an element (on a face or curve).
Edge: Any model edge.
Face: Any face on any element.
LinkedElement: Elements in linked RVT files.
Subelement: Whole element or sub-element.

After picking an object a ReferenceObject  to the selected element is returned. You can consider ReferenceObject as an address. Think of it like you are staying in your room that is in your house or apartment,  that in your district....and so on. Finally, in order to contact you I need an address.

ISelection Filter

If you noticed on Methods B,C,D there is a parameter named `selectionFilter`. basically you are adding a condition to what categories (for example) are allowed to be select, any other element that is not a part of this list will be ignored. Read more about ISelectionFilter

statusPrompt

It is a hint to user to let him know what exactly he should search for and select. see this GIF 

How to use it?

As mentioned above there are 4 ways to pick an element from Revit. Example of usage like below:
Method A:
```uidoc.Selection.PickObject(ObjectType.Element);```
Method B:
```uidoc.Selection.PickObject(ObjectType.Element,new windowFilter());```
Method C:
```uidoc.Selection.PickObject(ObjectType.Element,"Please select Window Only");```
Method D:
```uidoc.Selection.PickObject(ObjectType.Element,new windowFilter(),"Please select Window Only");```
`uidoc`: is the User Interface Document, basically it is everything related to GUI for the current project

What requisitions i should consider?

We need to be aware that picking an object doesn't require opened Transaction. Since we are not modifying Document. we will talk about Transaction in another post.
It is always better to encapsulate this statement in a try catch method, since user might cancel the selection operation by press escape key, and by then an exception will be thrown.

And finally your method will look similar to this:
```try
{
  var referenceElement = uidoc.Selection.PickObject(ObjectType.Element, new Room_Filter(), "Please select a Room");
}
catch (Exception ex)
{
  //User canceled selection
}```
I hope the above gets you started.

Troubleshooting:

No comments:

Post a Comment

WPF-Revit -01

In this post, we are going to surface explore WPF structure and how to use it within Revit App. Windows Platform Foundation WPF ...