Stuck with List[ElementId](ids)

hi guys

i did a python code to get only specific type of dimensions in active view, but the ListElementId gives me some trouble.

my ids is not empty.
any ideas?

1 Like

try to get Active view and it’s Id from separate nodes, and show to us what is the active view

Hi Yein

I hope you are well. You don’t need the ids list. You’ve declared the strongbox correctly (i.e. col1) which is a .net collection, so you need to use .net methods to insert items into it (so use Add())
Line 32: col1 = List[ElementId]()

Line 36: col1.Add(i.Id)

2 Likes

!! Thomas!
Long time! hope your a well too :slight_smile:
thank you for the input. now it works perfectly. why the append instead of add?

All good thanks! Append is a python method for python collections. However, the method you are calling from the API requires a strongly-type collection from Windows System.Collections.Generic assembly (that’s why you have to declare the list with square brackets and a type declaration), and there is no method called ‘append’ in this library - it uses the Add() method instead.

3 Likes

good explanation! as usual

for those that may interest, here the final code.

import clr

Import DocumentManager and TransactionManager

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference("System")
from System.Collections.Generic import List

Import RevitAPI

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

doc = DocumentManager.Instance.CurrentDBDocument
view = doc.ActiveView.Id

collector = FilteredElementCollector(doc, view)
Filter = ElementCategoryFilter(BuiltInCategory.OST_Dimensions)

dimensions = collector.WherePasses(Filter).WhereElementIsNotElementType().ToElements()

"Start" the transaction

TransactionManager.Instance.EnsureInTransaction(doc)

ids=[]
col1 = ListElementId

for i in dimensions :
if i.Name == "RED" :
col1.Add(i.Id)
doc.ActiveView.HideElements(col1)

"End" the transaction

TransactionManager.Instance.TransactionTaskDone()

#Uncomment the line below to output an object
OUT = "done!"

1 Like

Hi Thomas,

Sorry to bother you, I’m trying to understand how I could work that out for myself…

Say I’ve found my way to here…
http://www.revitapidocs.com/2018/4c4c2704-48d2-8add-8309-427ff77a8955.htm

I’m then a bit stuck with how I would know to need to reference Windows System Collections Generic assembly?

Apologies if this is obvious,

Cheers,

Mark

Hi Mark,

You can import the assembly like this:

# Import
DocumentManager and TransactionManager
clr.AddReference("RevitServices")`

import
RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

Thanks Thomas,

I am curious as to your process…

If you were to amend a Dimension, where would you look to know that the System Collections are needed?

I couldn’t see a reference in the API docs, I’ve done the Lynda Python tutorials which touch briefly on this, but I can’t quite get my head around it :slight_smile:

Thank you for your time,

Mark

Any method in the API with a collection as an input requires a strong boxed list (in Python obviously). Revit API docs does show the input types, so look out for List< some type > (example shown below) or System.Collections.Generic.List< some type > if you prefer using Visual Studio:

1 Like

Hi Thomas,

I had mistakenly thought it was the dimension that required it, rather than the collector…

If I click on the ICollection link in the API docs it takes me to the Microsoft page which shows the assembly as System.Collections.Generic, which narrows it down a lot.

Thanks for showing me that it is from the RevitServices module and the advice.

Cheers,

Mark

ICollection is the return type of the method, not the input type, and unrelated to the subject. You only need to pay attention to the input type(s) of a method as per my previous comment (to determine if a strong boxed list is needed).

1 Like

can you please help me out … same error