I love LINQ. LINQ, short for Language Integrated Query, allows us to execute list queries in a short and clean manner, rather than using recursive methods. Other than using the Where( ) expression as a filter, the Grouping functions comes in really handy as well.
The code below groups of all Duct elements by its System Classification parameter, i.e. Supply, Exhaust, Return Air.
The groups are essentially Dictionaries, with the System Classification as Key, and its corresponding List of Duct elements as group Elements. Afterwhich, we can simply access its individual elements and its properties by looping through the List.
Document doc = commandData.Application.ActiveUIDocument.Document; List<Duct> allDucts = new FilteredElementCollector(doc) .OfClass(typeof(Duct)) .WhereElementIsNotElementType() .Cast<Duct>() .ToList(); var ductGroups = from duct in allDucts group duct by duct.get_Parameter(BuiltInParameter.RBS_SYSTEM_CLASSIFICATION_PARAM).AsString(); foreach (var sysGrp in ductGroups) { foreach(Duct ductEle in sysGrp) { //ductEle : Access indivdual duct elements //sysGrp.Key : System Classification, i.e. Supply Air, Return Air, Exhaust Air, etc } }
You can find the source code for the full implementation HERE! Happy coding!
Hi, I thought the code was complete, or is it just the idea?
Hi Reynaldo,
I’d say its an idea, since what you wish to do with the sorted groups in the
foreach
loops is entirely up to you.Perhaps you could share or DM me what you wish to achieve, and I’ll definitely try my best to help 🙂
regards,
Han
Hello, my question has nothing to do with the course. Can I shared a shared parameter to a tag by Revit API? I’m not a programmer, that’s why I ask the question, I wrote code, but I can’t associate the labels with the shared parameter. My english.Thanks for your time.(how to associate a shared parameter with a tag Revit API?)
Hello Reynaldo,
Apologies, but I can’t picture what you wish to achieve.
I hope you don’t mind that I’ve dropped an email to you, perhaps you could send me some screenshots to help me understand your problem better.
regards,
Han
My English is not very good
thank han, just i have a question. why you cast to duct if you allready have a list of ducts?
Hello Carlos,
its because the FilteredElementCollector defaults to “Element” type, which is the parent class of “Duct”.
Hence, we have to cast it to “Duct” type to access its class properties & methods.