Skip to content

Instantly share code, notes, and snippets.

@alvpickmans
Created November 18, 2019 02:51
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvpickmans/f451c50b1954739d67875f126c18a647 to your computer and use it in GitHub Desktop.
Save alvpickmans/f451c50b1954739d67875f126c18a647 to your computer and use it in GitHub Desktop.
Dynamo Python script to retrieve all elements from a linked model by category
# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
# Importing .NET List colelction, required for ElementMulticategoryFilter
from System.Collections.Generic import List
doc = DocumentManager.Instance.CurrentDBDocument
# The inputs to this node will be stored as a list in the IN variables.
inLink = IN[0]
inCats = IN[1]
# Creating ElementIds from Id integers
catIds = [ElementId(category.Id) for category in inCats]
# Creating a .NET List collection of ElementIds
categoryIds = List[ElementId](catIds)
# Retrieving linked document
mLink = doc.GetElement(ElementId(inLink.Id)).GetLinkDocument()
sysFilter = ElementMulticategoryFilter(categoryIds)
mOut = FilteredElementCollector(mLink) \
.WhereElementIsNotElementType() \
.WherePasses(sysFilter) \
.ToElements()
# Assign your output to the OUT variable.
OUT = mOut
@mary2021-pixel
Copy link

mary2021-pixel commented Feb 6, 2021

Hi, l am new in Dynamo. I would like to ask you how and where do you write this script in Dynamo ?

@alvpickmans
Copy link
Author

You can run it using a Python Script node

@oOBorisOo
Copy link

Could you please clarify what the input are? I'm feeding category id and link id and its not working.
Thanks.

@alvpickmans
Copy link
Author

From memory and looking at the code, inputs are category and link elements, using ootb dynamo nodes tio get them

@mary2021-pixel
Copy link

Thank you

@oOBorisOo
Copy link

Link instance & category did it. Thanks man!

@refrigerator9000
Copy link

refrigerator9000 commented Feb 23, 2021

help guys.. i am a noob.. and trying to get this to work, i feed the link like this:
getting the link

then i feed it into this Python Script node as IN[0] along with a Category in IN[1]

and i get this error.... which i really can't grasp..

error

the link is properly selected, i checked it's ID to make sure i didn't accidentally clicked something else...

help!!!

line 32 is a commented line, starting with #, right?...

@stefanov-srd
Copy link

stefanov-srd commented Dec 13, 2021

When trying to make this code work I get an error message:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File "", line 41, in
TypeError: iteration over non-sequence of type NoneType

image

@JuvenVazquez
Copy link

Las categorias deben darse como lista

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment