Pyrevit中ISelectionFilter无法筛选Revit链接元素问题求助
PyRevit拾取链接模型元素时自定义筛选器失效问题
自定义CustomISelectionFilter可正常筛选当前Revit模型中的元素,但用于筛选链接模型元素时无法选中任何对象。
原始代码
##imports doc = revit.doc uidoc = revit.uidoc ###自定义ISelectionFilter 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: with forms.WarningBar(title="Pick elements in model"): wall_collector = uidoc.Selection.PickObjects(ObjectType.Element, CustomISelectionFilter("Walls")) except: print("No elements") # 拾取链接模型元素 try: with forms.WarningBar(title="Pick elements in linked model"): wall_collector_link = uidoc.Selection.PickObjects(ObjectType.LinkedElement, CustomISelectionFilter("Walls")) except: print("No linked elements") ## Print Ids
问题原因
- 拾取链接元素时,
AllowElement接收的参数是**RevitLinkInstance(链接实例)**,而非链接文件内的实际元素,其类别为"Revit Links",自然匹配不到"Walls"。 AllowReference方法中返回的true是小写,不符合Python语法规范(应改为True)。
修正方案
在AllowReference方法中,通过Reference对象获取链接模型内的元素,再判断其类别:
##imports from pyrevit import revit, forms from Autodesk.Revit.UI.Selection import ISelectionFilter, ObjectType from Autodesk.Revit.DB import ElementId doc = revit.doc uidoc = revit.uidoc 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 # 允许选择链接实例,后续在AllowReference中筛选内部元素 elif e.Category.Name == "Revit Links": return True return False def AllowReference(self, ref, point): # 获取链接模型内的元素ID和链接实例ID linked_element_id = ref.LinkedElementId link_instance_id = ref.ElementId link_instance = doc.GetElement(link_instance_id) # 获取链接模型的文档对象 linked_doc = link_instance.GetLinkDocument() if not linked_doc: return False # 获取链接模型内的实际元素并判断类别 linked_element = linked_doc.GetElement(linked_element_id) if linked_element and linked_element.Category.Name == self.nom_categorie: return True return False # 拾取当前模型元素 try: with forms.WarningBar(title="拾取当前模型中的墙"): wall_collector = uidoc.Selection.PickObjects(ObjectType.Element, CustomISelectionFilter("Walls")) print("选中当前模型元素数量:", len(wall_collector)) except: print("未选中任何当前模型元素") # 拾取链接模型元素 try: with forms.WarningBar(title="拾取链接模型中的墙"): wall_collector_link = uidoc.Selection.PickObjects(ObjectType.LinkedElement, CustomISelectionFilter("Walls")) print("选中链接模型元素数量:", len(wall_collector_link)) except: print("未选中任何链接模型元素")
关键说明
AllowElement中先允许选择链接实例,让用户能点击到链接模型;AllowReference通过ref.LinkedElementId获取链接内元素的ID,再通过链接实例获取链接文档,进而拿到链接内的实际元素进行类别判断;- 修正了
true为True的语法错误。
内容的提问来源于stack exchange,提问作者PawelKinczyk
相关产品推荐
相关产品推荐

