How to get the elements inside a boundingbox?

python

#12

You can do something like this:

With FilteredElementCollector you get all elements in the (active) view. And with BoundingBoxIsInsideFilter or BoundingBoxIntersectsFilter you can filter out the elements inside the bounding box:

##Python code:

import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitNodes')
clr.AddReference('RevitServices')
import Revit
import RevitServices
from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
doc = DocumentManager.Instance.CurrentDBDocument

#Preparing input from dynamo to revit
bb = IN[0].ToRevitType()
outline = Outline(bb.Min, bb.Max)
#filter = BoundingBoxIntersectsFilter(outline)
filter = BoundingBoxIsInsideFilter(outline)

collector = FilteredElementCollector(doc, doc.ActiveView.Id).WherePasses(filter)

OUT = [e.ToDSType(True) for e in collector]

##File:
getElementsInBB.dyn (7.8 KB)


Getting all Elements of type within a bounding box
#13

Dear Einar_Raknes,

Thank you very much. I am gonna try it out. It’s fascinating!!! THX!


#14

Dear Sir Einar_Raknes,

Sorry for being stupid, can me further explain to me what is the output of the python script note, because I use a watch node to check what it gives out, it shows me a null.


#15

Oh Sorry, I quit and restart, it returns to normal. Sorry Sorry.


#16

Hi,

Interesting topic. I have a similar question.
I have used this python script with a scope box and all works fine as long as i use the “Select model element” with one scope box. But i want to take this a step further by using categories to select all scope boxes. Then the issues starts. The script won´t run through.

So to my question… What am i doing wrong? Is it possible to repeat the script for each scope box?
I have tried to get around this but i do not manage to full fill my wish :slight_smile:

Is there anyone who has any ides what i´m doing wrong?

>


#17

I wrote this in another topic: How to make a pyton script work with a list as input


#18

Thanks for the fast reply.
Ok, so i have no experience with python or programming… i tried to read trough and understand witch part i should implement and i got this.
I did not work. Maybe you could point me in the right direction?

import clr
clr.AddReference('RevitAPI')
clr.AddReference('System')
clr.AddReference('RevitNodes')
clr.AddReference('RevitServices')
import Revit
import RevitServices
from Autodesk.Revit.DB import *
from System.Collections.Generic import List
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
doc = DocumentManager.Instance.CurrentDBDocument

#Functions for list handling
def ProcessList(_func, _list):
   return map( lambda x: ProcessList(_func, x) if type(x)==list else _func(x), _list )

def ProcessParallelLists(_func, *lists):
return map( lambda *xs: ProcessParallelLists(_func, *xs) if all(type(x) is list for x in xs) else _func(*xs), *lists )

#Preparing input from dynamo to revit
def Unwrap(item):
return UnwrapElement(item)

def ToRevit(item):
return item.ToRevitType(True)

if isinstance(IN[0], list):
bb = ProcessList(Unwrap, IN[0])
else:
bb = list(Unwrap(IN[0]))

bb = IN[0].ToRevitType()
outline = Outline(bb.Min, bb.Max)
filter = BoundingBoxIntersectsFilter(outline)
#filter = BoundingBoxIsInsideFilter(outline)

collector = FilteredElementCollector(doc, doc.ActiveView.Id).WherePasses(filter)

OUT = [e.ToDSType(True) for e in collector]

Collect elements from multiple Boundingboxes in same view (HELP needed)
#19
import clr
clr.AddReference('RevitAPI')
clr.AddReference('System')
clr.AddReference('RevitNodes')
clr.AddReference('RevitServices')
import Revit
import RevitServices
from Autodesk.Revit.DB import *
from System.Collections.Generic import List
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
doc = DocumentManager.Instance.CurrentDBDocument

#Functions for list handling
def ProcessList(_func, _list):
   return map( lambda x: ProcessList(_func, x) if type(x)==list else _func(x), _list )

#Preparing input from dynamo to revit

def ToRevit(item):
	return item.ToRevitType(True)

if isinstance(IN[0], list):
	bblist = ProcessList(ToRevit, IN[0])
else:
	bblist = [ToRevit(IN[0])]

def collectElementsInBB(bb):
	outline = Outline(bb.Min, bb.Max)
	filter = BoundingBoxIntersectsFilter(outline)
	collector = FilteredElementCollector(doc, doc.ActiveView.Id).WherePasses(filter)
	return [e.ToDSType(True) for e in collector]

OUT = ProcessList(collectElementsInBB, bblist)

How to see what elements are in which scope box
Get all elements by View
Set Multiple Worksets to elements based on Scope Box Location
Multistory stair components
#20

Thank you Einar!
It works perfect!
You made my day!
:grin:


#21

Hi @Einar_Raknes ,
that’s really awesome, but can we modify this python node to let it give the elements that intersects with the bounding box not only the elements that are completely inside it?

thank you


#22

Sure, change

filter = BoundingBoxIsInsideFilter(outline)

to:

filter = BoundingBoxIntersectsFilter(outline)


#23

ohh thank you that’s what I want,

any links to get familiar with such functions and python?


How to get text-notes / boundingbox or PointAtSpace
#24

Yes, the complete Revit API documentation is available here:

http://www.revitapidocs.com/2017/eb8735d7-28fc-379d-9de9-1e02326851f5.htm#exampleToggle


#25

Thank you :slight_smile:


#26

Great post Einar_Raknes. This is defiantly a nugget of gold I have found, thank you. I am a new Dynamo user and not that familiar with code writing. I have been trying to modify your code to collect the elements within the active model and linked models as well. Unfortunately I am not succeeding. I would greatly appreciate any help you could offer in helping me achieve my goal.


#27

Hello, im new user dynamo
Now i got 1 case. I have 1 element get bounding box and 1 element list. I want to find the elements intersection with bounding box.
Example: Bounding box
List Element ( A, B,C)…
===> Find Who element in List Element if it intersection Bounding box.
Thanks
Sr English so bad


Create Annotation Tag - Issue with the amount of tags
#28

Hello to all,

is there a reason, why “text notes” are not shown up with the python script ?


#29

Start a new topic we can’t have 2 solutions in one topic.


#30

Legend!


#31

hi thanks to your methord,awesome job

and i wonder if we can “Access Elements Through all Linked File” by Python script

or can you update your code and made it can access the linked elements

many thanks