I am trying to enable selecting one single space for use in Revit MEP 2019 by using the GUI and store the selection for further use in scripts. The code is written in pyRevit. The script runs both from the shell and from the addin button, but when entering the selection mode (PickObject method), I am not allowed to select anything at all. I don't get any errors, it's just that nothing is selectable when entering the selection tool in the GUI.
I have commented in the code what I have tried that didn't work.
from Autodesk.Revit import DB,UI
from Autodesk.Revit.DB import BuiltInCategory
from Autodesk.Revit.UI.Selection import ISelectionFilter,ObjectType
# Definitions:
# Define a space selection filter so that only spaces are selectable
class SpaceSelectionFilter(ISelectionFilter):
def AllowElement(element):
#if element.Category.Name == "Spaces":
#if element.ToString() == "Autodesk.Revit.DB.Mechanical.Space":
if element.Category.Id.IntegerValue== int(BuiltInCategory.OST_MEPSpaces):
return True
return False
def AllowReference(reference, point):
return False
# Function that enables using PickObject from the PythonRevitShell
def shell_pickobject():
__window__.Hide()
elementReference = uidoc.Selection.PickObject(UI.Selection.ObjectType.Element,spaceFilter,"Select a space(room)")
__window__.Show()
__window__.TopMost = True
return elementReference
# Procedure:
# Create a selection filter
spaceFilter = SpaceSelectionFilter()
# User picks a space
ref = shell_pickobject()
# The following line works also outside of the shell_pickobject() function when used from the GUI addin-button, but spaces are still not selectable.
# elementReference = uidoc.Selection.PickObject(UI.Selection.ObjectType.Element,spaceFilter,"Select a space(room)")
I don't understand where the problem is, my best guess is inside the filter definition. The help string "Select a space(room)" displays correctly in the bottom left corner, and everything but the viewport turns grey like it should when I am supposed to select something in the view. The mouse turns into some kind of "forbidden" symbol.
I would very much appreciate some help with this. Thank you in advance to anyone who might wish to help!
You can find examples in pyRevitMEP source code. I also did an article explaining how to use ISelectionFilter : [Revit] ISelectionFilter example using python. Here is one example (running with revitpythonshell) :
from Autodesk.Revit.UI.Selection import ISelectionFilter
class CustomISelectionFilter(ISelectionFilter):
def __init__(self, category_name):
self.category_name = category_name
def AllowElement(self, e):
if e.Category.Name == self.category_name:
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("Operation canceled","Canceled by the user")
__window__.Close()
You can find another example running under pyRevit explained here : [pyRevitMEP] ConnectTo : connect MEP elements