Group & Sort Revit Elements by Parameter

Group & Sort Revit Elements by Parameter

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!

This Post Has 5 Comments

  1. Hi, I thought the code was complete, or is it just the idea?

    1. 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

  2. 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?)

    1. 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

  3. My English is not very good

Leave a Reply

Close Menu