Select Elements by Category (error can not get IselectionFilter)

Hi!
How to select elements by category.
I tried but allway return except:

image

image

image

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from Autodesk.Revit.UI.Selection import ISelectionFilter

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

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

def selects():
    try:
        picked = uidoc.Selection.PickObjects(Selection.ObjectType.Element, Single_Category('Walls'),"Let Select Model")
        element = [doc.GetElement(i) for i in picked]
    except:
        TaskDialog.Show("Operation canceled","Canceled by the user")
    #return element

clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
import System.Drawing
import System.Windows.Forms
from System.Drawing import *
from System.Windows.Forms import *

class MainForm(Form):
	def __init__(self):
		self.InitializeComponent()
	def InitializeComponent(self):
		self.label1 = System.Windows.Forms.Label()
		self._button1 = System.Windows.Forms.Button()
		self._textBox1 = System.Windows.Forms.TextBox()
		self.SuspendLayout()
		# 
		# label1
		#
		self.label1.AutoSize = True
		self.label1.Location = System.Drawing.Point(15, 30)
		self.label1.Name = "label1"
		self.label1.Size = System.Drawing.Size(65, 25)
		self.label1.TabIndex = 1
		self.label1.Text = "1.A"
		# 
		# button1
		# 
		self._button1.Location = System.Drawing.Point(260, 230)
		self._button1.Name = "button2"
		self._button1.Size = System.Drawing.Size(75, 25)
		self._button1.TabIndex = 0
		self._button1.Text = "Select"
		self._button1.UseVisualStyleBackColor = True
		self._button1.Click += self.Button1Click
		# 
		# textBox1
		# 
		self._textBox1.Location = System.Drawing.Point(160, 30)
		self._textBox1.Name = "textBox1"
		self._textBox1.Size = System.Drawing.Size(175, 25)
		self._textBox1.TabIndex = 1
		self._textBox1.Text = "50"
		# 
		# MainForm
		# 
		self.ClientSize = System.Drawing.Size(360, 270)
		self.Controls.Add(self.label1)
		self.Controls.Add(self._textBox1)
		self.Controls.Add(self._button1)
		self.MaximizeBox = False
		self.MinimizeBox = False
		self.ShowIcon = False
		self.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
		self.Name = "MainForm"
		self.Text = "SelectByCatagory"
		self.ResumeLayout(False)
		self.PerformLayout()

	def Button1Click(self, sender, e):
		self.selected = selects()
		pass

form = MainForm()
Application.Run(form)
OUT = form.selected

Hello,
Which version of Revit / Python Engine are you using ?

1 Like

Hi!
I tested for Revit2022 and Revit2023-IronPython, but not work.

Hi,

Why don’t you use the node from the Data-Shapes package? Additionally, Data-Shapes has nodes for creating Window Forms in Revit. Check: Blog – data|shapes

Here the code fixed

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from Autodesk.Revit.UI.Selection import ISelectionFilter

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

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

def selects():
	elements = []
	try:
		pickeds = uidoc.Selection.PickObjects(Selection.ObjectType.Element, Single_Category('Murs'),"Let Select Model")
		elements = [doc.GetElement(i) for i in pickeds]
	except:
		TaskDialog.Show("Operation canceled","Canceled by the user")
	return elements

clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
import System.Drawing
import System.Windows.Forms
from System.Drawing import *
from System.Windows.Forms import *

class MainForm(Form):
	def __init__(self):
		self.InitializeComponent()
	def InitializeComponent(self):
		self.label1 = System.Windows.Forms.Label()
		self._button1 = System.Windows.Forms.Button()
		self._textBox1 = System.Windows.Forms.TextBox()
		self.SuspendLayout()
		# 
		# label1
		#
		self.label1.AutoSize = True
		self.label1.Location = System.Drawing.Point(15, 30)
		self.label1.Name = "label1"
		self.label1.Size = System.Drawing.Size(65, 25)
		self.label1.TabIndex = 1
		self.label1.Text = "1.A"
		# 
		# button1
		# 
		self._button1.Location = System.Drawing.Point(260, 230)
		self._button1.Name = "button2"
		self._button1.Size = System.Drawing.Size(75, 25)
		self._button1.TabIndex = 0
		self._button1.Text = "Select"
		self._button1.UseVisualStyleBackColor = True
		self._button1.Click += self.Button1Click
		# 
		# textBox1
		# 
		self._textBox1.Location = System.Drawing.Point(160, 30)
		self._textBox1.Name = "textBox1"
		self._textBox1.Size = System.Drawing.Size(175, 25)
		self._textBox1.TabIndex = 1
		self._textBox1.Text = "50"
		# 
		# MainForm
		# 
		self.ClientSize = System.Drawing.Size(360, 270)
		self.Controls.Add(self.label1)
		self.Controls.Add(self._textBox1)
		self.Controls.Add(self._button1)
		self.MaximizeBox = False
		self.MinimizeBox = False
		self.ShowIcon = False
		self.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
		self.Name = "MainForm"
		self.Text = "SelectByCatagory"
		self.ResumeLayout(False)
		self.PerformLayout()

	def Button1Click(self, sender, e):
		self.selected = selects()
		self.Close()

form = MainForm()
Application.Run(form)
OUT = form.selected
3 Likes

Yes, I know it. But I want control my form.

Thank you !!!

1 Like