Add Filter to your Selection

As pointed out in Select Element post, selection Filter, is adding a filter to avoid picking unnecessary elements. for example, if you are dragging a window selection to select only walls, ISelectionFilter interface comes in handy for this purpose.

More details about ISelectionFilter:

ISelectionFilter is  an interface class that holds a specific methods to be implemented and to match your needs. it contains two methods, AllowElement() and AllowReference().

AllowElement()

it is a boolean function that tells revit back if this element is allowed to pass this filter or not. Meaning the story starts when a user start moving mouse pointer over an element, this function is called sending in the element. the element is then passed to some condition we as API user specify.
see this example if i want to select only Windows from this project:
```public bool AllowElement(Element elem)
{
   return elem.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Windows;
}
```

AllowReference()

this is the second method in this interface. It is an additional filter but for ElementReferences. it is targeting geometry Type such as edge, face, GeometryObject...etc it comes handy at the time you need to specific or generally pick edges. for example see the below snippet:
```public bool AllowReference(Reference reference, XYZ position)
{
  return reference.GeometryObject is Edge;
}```

How to implement this class

  1. first create a new class and give it any meaningful name such as WindowSelectionFilter.cs
  2. beside the class name add :ISelectionFilter
  3. implement the interface
  4. now you would see the above two mentioned methods are generated
  5. type as described above.
  6. that's it, now in your PickObject statement add a new instance of this class
and this is an example on how the implementation would be:
```public class WindowSelectionFilter: ISelectionFilter { public bool AllowElement(Element elem) { return testElement.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Windows; } public bool AllowReference(Reference testReference, XYZ testPosition) { return testReference.GeometryObject is Edge; } } ```
and the usage would be like this
    ```uidoc.Selection.PickObject(ObjectType.Face, new WindowSelectionFilter(), "Select a Window edge");
    ```

    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 ...