[Revit] ISelectionFilter example using python

Someone asked an exemple of ISelectionFilter on RevitPythonShell group almost a week ago. I don’t know if he still needs it but anyway, it’s interesting  to show this other way to filter. In this exemple we allow user only to select a duct. Let see the code snippet :

from Autodesk.Revit.UI.Selection import *

class CustomISelectionFilter(ISelectionFilter):
	def __init__(self, nom_categorie):
		self.nom_categorie = nom_categorie
	def AllowElement(self, e):
		if e.Category.Name == self.nom_categorie:
			return True
		else:
			return False
	def AllowReference(self, ref, point):
		return true

try:
	ductsel = uidoc.Selection.PickObject(ObjectType.Element,
	CustomISelectionFilter("Ducts"),
	"Select a Duct")			
except Exceptions.OperationCanceledException:
	TaskDialog.Show("Opération annulée","Annulée par l'utilisateur")

__window__.Close()

Here in action :

Here is another exemple to select only objects inherited from MEPCurve (Cable Tray, Wire, InsulationLiningBase, Duct, FlexDuct, FlexPipe, Pipe) :

from Autodesk.Revit.UI.Selection import *
class CustomISelectionFilter(ISelectionFilter):
    def __init__(self, element_class):
        self.element_class = element_class
    def AllowElement(self, e):
        if isinstance(e, self.element_class):
            return True
        else:
            return False
    def AllowReference(self, ref, point):
        return true

try:
    ductsel = uidoc.Selection.PickObject(ObjectType.Element,
    CustomISelectionFilter(MEPCurve),
    "Select a Duct")            
except Exceptions.OperationCanceledException:
    TaskDialog.Show("Opération annulée","Annulée par l'utilisateur")

__window__.Close()

8 comments

  1. Hi, not able to solve this for:

    filter1 = CustomISelectionFilter(“Grids”)
    pickedGrids = Selection.PickElementsByRectangle(filter1)

    Using method:

    class CustomISelectionFilter(ISelectionFilter):
    def __init__(self, nom_categorie):
    self.nom_categorie = nom_categorie
    def AllowElement(self, e):
    if e.Category.Name == self.nom_categorie:
    return True
    else:
    return False
    def AllowReference(self, ref, point):
    return True

    def AllowReference(refer, point):
    return False

    error is “TypeError: expected Selection, got Object#ISelectionFilter_12$12”
    I tried a lot but every time it’s expecting a Selection or other missing arguments

    1. Hi,

      Issue is on line : pickedGrids = Selection.PickElementsByRectangle(filter1)

      You can see in my code that it is uidoc.Selection.PickObject not Selection.PickObject. Selection is a property of UIDocument. It is not the Selection Class.

      To use Selection as you did (and I think you actually do not want to) you need to pass a selection as first argument eg. Selection.PickElementsByRectangle(selection, filter1) where selection needs to be retrieve somehow.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.