Tag Archives: Revit API 2015

[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()