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()
Super helpful to see this in Python! Thanks a lot for sharing
Thank you for your message. I appreciate :-).
Thank you, very useful 🙂
very very useful,thank you so much…
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
Hi,
Issue is on line :
pickedGrids = Selection.PickElementsByRectangle(filter1)You can see in my code that it is
uidoc.Selection.PickObjectnotSelection.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.