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

Sort Levels by Elevation

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
jmcclanathan
2974 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();

 

4 REPLIES 4
Message 2 of 5
peterjegan
in reply to: jmcclanathan

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

ICollection<Element>.......ToElementIds();
Message 3 of 5
sanjaymann
in reply to: jmcclanathan

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

Message 4 of 5
Aaron.Lu
in reply to: jmcclanathan

 

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
jeremytammik
in reply to: jmcclanathan

Hey guys, thank you for all your cool suggestions!

 

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

 

http://thebuildingcoder.typepad.com/blog/2014/11/webgl-goes-mobile-and-sorted-level-retrieval.html#3

 

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

 

https://github.com/jeremytammik/the_building_coder_samples

 

Cheers,

 

Jeremy



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 DevCon in Munich May 28-29th


Rail Community