How to collect elements from links visible on a view in the host model?

At the beginning you said the initial code works for you, which means you have 2 models with aligned views Ids, no?
We already have filtered categories in UI in visibility settings for a view. As I see the only method to extract which categories are visible, is to do it one by one by View.GetVisibility method. It’s annoying…

yes, it’s because the link and host file were made from a same file. That’s why the ids of the views were the same.

Cheater :slight_smile:

well I guess that’s the kind of stuff that happens when there are no files to run tests on … You don’t work in the same conditions. @Swapnil_Raut 's files helped clear that mistery

Hi Mostafa,

This code is working perfectly in my Dynamo.
Once again Thanks for your help.

Swapnil

1 Like

this takes visibility settings of each view into account as well :

IN[0] = views , IN[1] = link document

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import*
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
import System
from System.Collections.Generic import *

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import*
doc = DocumentManager.Instance.CurrentDBDocument

#Inputs

linkdoc = IN[1]
views = UnwrapElement(IN[0])

collector = []

#getting project base point elevation
basepointfilter = ElementCategoryFilter(BuiltInCategory.OST_ProjectBasePoint)
basepoint = FilteredElementCollector(doc).WherePasses(basepointfilter).ToElements()
bpelevation = [b.ParametersMap.get_Item('Elev').AsDouble() for b in basepoint]

cats = doc.Settings.Categories
cids = [c.Id for c in cats]

UIunit = Document.GetUnits(doc).GetFormatOptions(UnitType.UT_Length).DisplayUnits
level = []
for v in views:

	filtercatids = filter(lambda x: not v.GetCategoryHidden(x),cids)
	catfilters = []
	for i in filtercatids:
		catfilters.append(ElementCategoryFilter(System.Enum.ToObject(BuiltInCategory, int(str(i)))))
	catfilterlist = List[ElementFilter](catfilters)
	catfilter = LogicalOrFilter(catfilterlist)
	#creating a boundingbox for each view from it's crop box and view range
	bb = v.CropBox
	vr = v.GetViewRange()
	topclip = PlanViewPlane.TopClipPlane
	bottomclip = PlanViewPlane.BottomClipPlane
	cutclip = PlanViewPlane.CutPlane
	toplevel = (doc.GetElement(vr.GetLevelId(topclip)))
	topoffset = vr.GetOffset(topclip)
	cutlevel = (doc.GetElement(vr.GetLevelId(cutclip)))
	cutoffset = vr.GetOffset(cutclip)
	try:
		topZ = toplevel.Elevation + topoffset - bpelevation[0]
	except:
		topZ = cutlevel.Elevation + cutoffset - bpelevation[0]
	bottomlevel = (doc.GetElement(vr.GetLevelId(bottomclip)))
	bottomoffset = vr.GetOffset(bottomclip)
	try:
		bottomZ = bottomlevel.Elevation + bottomoffset - bpelevation[0]
	except :
		bottomZ = cutlevel.Elevation - bpelevation[0]
	min = bb.Min
	max = bb.Max
	newmin = XYZ(UnitUtils.ConvertToInternalUnits(min.X,UIunit),UnitUtils.ConvertToInternalUnits(min.Y,UIunit),bottomZ)
	newmax = XYZ(UnitUtils.ConvertToInternalUnits(max.X,UIunit),UnitUtils.ConvertToInternalUnits(max.Y,UIunit),topZ)
	ol = Outline(newmin,newmax)

	#combining boundingbox and category filters
	bbfilter = BoundingBoxIntersectsFilter(ol)
	andfilter = LogicalAndFilter(catfilter,bbfilter)

	#collecting elements
	collector.append(FilteredElementCollector(linkdoc).WherePasses(andfilter).ToElements())

OUT = collector

Clever… Will try this.

This didn’t work for me.

https://forum.dynamobim.com/t/to-push-element-type-parameters-into-shared-parameters-for-rooms/5846/9

In this link we were arguing about getting the job done in pieces. (room numbers into element numbers). But I have 1000 rooms in linked file and doing a cross product intersect check takes a long time.

@Greg_McDowell suggested to do it chunks, and I thought if I can get the rooms from linked model by view specific properties, then I could do it. But I get the same error like @Swapnil_Raut.

Do you have any other suggestions? All I want to do is to make a cross check between rooms in a linked file view and the host file floors.

Hi @Cem_Altineller ,
have you tried this code ?

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import*
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
import System
from System.Collections.Generic import *

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import*
doc = DocumentManager.Instance.CurrentDBDocument

#Inputs
category = IN[2]
linkdoc = IN[1]
views = UnwrapElement(IN[0])

collector = []

#getting project base point elevation
basepointfilter = ElementCategoryFilter(BuiltInCategory.OST_ProjectBasePoint)
basepoint = FilteredElementCollector(doc).WherePasses(basepointfilter).ToElements()
bpelevation = [b.ParametersMap.get_Item('Elev').AsDouble() for b in basepoint]

#Category filter
catfilter = ElementCategoryFilter(System.Enum.ToObject(BuiltInCategory,category.Id))

UIunit = Document.GetUnits(doc).GetFormatOptions(UnitType.UT_Length).DisplayUnits
level = []
for v in views:

	#creating a boundingbox for each view from it's crop box and view range
	bb = v.CropBox
	vr = v.GetViewRange()
	topclip = PlanViewPlane.TopClipPlane
	bottomclip = PlanViewPlane.BottomClipPlane
	cutclip = PlanViewPlane.CutPlane
	toplevel = (doc.GetElement(vr.GetLevelId(topclip)))
	topoffset = vr.GetOffset(topclip)
	cutlevel = (doc.GetElement(vr.GetLevelId(cutclip)))
	cutoffset = vr.GetOffset(cutclip)
	try:
		topZ = toplevel.Elevation + topoffset - bpelevation[0]
	except:
		topZ = cutlevel.Elevation + cutoffset - bpelevation[0]
	bottomlevel = (doc.GetElement(vr.GetLevelId(bottomclip)))
	bottomoffset = vr.GetOffset(bottomclip)
	try:
		bottomZ = bottomlevel.Elevation + bottomoffset - bpelevation[0]
	except :
		bottomZ = cutlevel.Elevation - bpelevation[0]
	min = bb.Min
	max = bb.Max
	newmin = XYZ(UnitUtils.ConvertToInternalUnits(min.X,UIunit),UnitUtils.ConvertToInternalUnits(min.Y,UIunit),bottomZ)
	newmax = XYZ(UnitUtils.ConvertToInternalUnits(max.X,UIunit),UnitUtils.ConvertToInternalUnits(max.Y,UIunit),topZ)
	ol = Outline(newmin,newmax)

	#combining boundingbox and category filters
	bbfilter = BoundingBoxIntersectsFilter(ol)
	andfilter = LogicalAndFilter(catfilter,bbfilter)

	#collecting elements
	collector.append(FilteredElementCollector(linkdoc).WherePasses(andfilter).ToElements())

OUT = collector

Yes sir, I did.

It gave me this error:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 65, in
TypeError: expected Document, got UnknownElement

And this is the screenshot for the visual code. By select model element I was selecting the linked file with rooms in it.

This code expects an Autodesk.Revit.DB.Document input.Try getting the linked document like this :

How does x[0] codeblock work? How do I choose a particular link amongst many? Is it x[1], x[2] etc or y[0], z[0] etc?

x[0] just means first item of list x. It’s the same as :

look at your List of linked documents and get the one you’re interested in.

Ok, thanks getting that out of the way. Learned a very important thing thanks to you sir.

After finding the right link in my file the code worked, but it returns empty list. Uploaded what I’ve done in the screenshot.

Hi @Cem_Altineller ,

there are some unnecessary units conversions on that code that cause trouble. I fixed it and wrapped it in a node in the Data-Shapes packages :


Inputs are fine, but output is still empty. It is so close:)

Hi @Mostafa_El_Ayoubi

I used the same nodes as you showED, but it showed to me only host elements and nothing for linked elements.

Thank You

@Mohammad_Nawar , @Cem_Altineller ,
could you share a dummy version of your files so I can take a look at them?

I was testing on dummy projects and these the Revit files.

Project 1: is the linked model
project 2: is the host model

@Mohammad_Nawar thanks! this will help narrow down the issue faster.

I just tested your file and all seems to actually be working fine :


the elements the node outputs are from the linked file.

1 Like