Select element in activeview or document

I want to make one Node to select element in ActiveView or Document.
With a boolean i can active my choice.

What i want is to:
True = only select elements in the Active View

False = select elements in document

But with Boolean=True it won’t work. It should find the elements in my Active View! (Yes I see my elements in the active view)
What am I doing wrong in the Python-script?

import clr

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

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

doc = DocumentManager.Instance.CurrentDBDocument
#doc.ActiveView.ToDSType(True)

active_view_only = IN[0]


# Vind elementen in document of in actieve View die voldoen aan een filter.
# Meer info over FilteredElementCollector
# https://dynamopythonprimer.gitbook.io/dynamo-python-primer/4-revit-specific-topics/fetching-revit-elements

# if True: Collect all instances of current View, False: entire document
if active_view_only is True:
    alleElementen = FilteredElementCollector(doc, doc.ActiveView.Id)
else:
    alleElementen = FilteredElementCollector(doc)

alleElementen.OfCategory(BuiltInCategory.OST_StructuralFoundation) # Refine filter: only BuiltInCategory Structural Foundations
alleElementen.WhereElementIsElementType() # Refine filter: only Types
alleElementen.ToElementIds() # Return Revit ElementIds


# Maak een lijst van alle gevonden paaltypen (= Family Types) waarvan de Family Name begint met '17' (volgens NL-SfB code)
alleFamilyNames = []
for ele in alleElementen:
    familynaam = ele.FamilyName
    if familynaam.startswith('17'):
        alleFamilyNames.append(ele)

# Returnvalue: lijst met Family Types 
OUT = alleFamilyNames

R2022 2.12 palenplan - selecteer alle palen v2.dyn (12.8 KB)

Hi
2 remarks:

  • Methods of FilteredElementCollector doesn’t modify the collector ‘inPlace’ you need reassign return values

  • to compare values ‘is’ is not appropriate (== vs is, is is for reference equality) but in your case you can directly write if active_view_only :

@mrl Try this:


I think defining 2 filters, then using a function to get the desired result is easier to understand, hope it helps…

active_view_only = IN[0]

allElems =  FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralFoundation).WhereElementIsNotElementType().ToElements() 
activeElems = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_StructuralFoundation).WhereElementIsNotElementType().ToElements()

def fetch(elems, x):
	allFams = []
	for ele in elems:
		familynaam = ele.Name
		if familynaam.startswith(x):
			allFams.append(ele)
	return allFams

if active_view_only:
	OUT = fetch(activeElems, IN[1])  
else:
	OUT = fetch(allElems, IN[1])
1 Like

Using if active_view_only : does not work.
It returns an empty list when if active_view_only is True

Sorry, but this does not work at all in Revit 2022 with CPython3
No errors, but it returns an empty list if Boolean is True
No errors, but it returns an empty list if Boolean is False

Am I missing something?

because it’s not possible to get an ElementType from a view

try this python script, it replaces all your nodes, (get all instances in project or in current view)

import clr

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

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

doc = DocumentManager.Instance.CurrentDBDocument
#doc.ActiveView.ToDSType(True)

active_view_only = IN[0]

if active_view_only:
    allInstances = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_StructuralFoundation).WhereElementIsNotElementType().ToElements()
else:
    allInstances = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralFoundation).WhereElementIsNotElementType().ToElements()


# filter  allInstances
filter_Instances = []
for ele in allInstances:
    familynaam = ele.Symbol.FamilyName
    if familynaam.startswith('17'):
        alleFamilyNames.append(ele)

OUT = filter_Instances
```**strong text**
1 Like

Thanks !!! This script works!
Is this because the difference of filtering instances “Symbol.FamilyName” instead of filtering Instance “Name”?
(Sorry for by bad English, I’m Dutch)

@mrl It is working perfectly from my side, with CPython and IronPython:


But I think you are good now with @c.poupin solution

1 Like