Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sort Levels by Elevation

Message 1 of 5
3010 Views, 4 Replies

Sort Levels by Elevation

How would I go about getting a collection of levels sorted by elevation?  From reading the info I've found so far I think that filtered element collector returns an Ienumerable and that can be sorted by using "OrderBy", then ToElementIds could be used to create the collection of element ids in the correct order.  Is that correct?  Below is some code I tried and didn't work.  Can someone please point me in the right direction?


Also, is an Icollection sortable?


UIDocument uiDoc = this.ActiveUIDocument;
Document doc = uiDoc.Document;

FilteredElementCollector levCollector = new FilteredElementCollector(doc);
ICollection<Element> levelsCollection = levCollector.OfClass(typeof(Level)).OrderBy(lev => lev.Elevation).ToElementIds();


Message 2 of 5
in reply to: Anonymous

I didn't look at all the other stuff, but if you define an Element collection, you should fill it with Elements, not ElementIds.

Message 3 of 5
in reply to: Anonymous

public static IOrderedEnumerable<Level> FindAndSortLevels(Document doc)
return new FilteredElementCollector(doc)
.WherePasses(new ElementClassFilter(typeof(Level), false))
.OrderBy(e => e.Elevation);

Message 4 of 5
in reply to: Anonymous


You can change


    ICollection<Element> levelsCollection = levCollector.OfClass(typeof(Level)).OrderBy(lev => lev.Elevation).ToElementIds();


to this:


    List<Level> levelsCollection = levCollector.OfClass(typeof(Level)).OfType<Level>().OrderBy(lev => lev.Elevation).ToList();


1. Return value can't be ICollection<Element> when you call .ToElementIds

2. OfClass() method will return a collection of Element, you can only order them by elevation after casting Element to Level, that's why I suggest using OfType<Level>()

3. OrderBy() method returns IOrderedEnumerable<T>, if you want to have index operator, i.e. list[i], you can call ToList() to let it return a List<T>.

Aaron Lu
Developer Technical Services
Autodesk Developer Network
Message 5 of 5
in reply to: Anonymous

Hey guys, thank you for all your cool suggestions!


I summarised the discussion and added my contribution to it on The Building Coder:


I also added my code snippet suggestion to The Building Coder samples:





Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Customer Advisory Groups

Rail Community