Sort Levels by Elevation

Sort Levels by Elevation

Anonymous
Not applicable
3,364 Views
4 Replies
Message 1 of 5

Sort Levels by Elevation

Anonymous
Not applicable

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();

 

0 Likes
Accepted solutions (1)
3,365 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
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();
0 Likes
Message 3 of 5

Anonymous
Not applicable

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

0 Likes
Message 4 of 5

Aaron.Lu
Autodesk
Autodesk
Accepted solution

 

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
0 Likes
Message 5 of 5

jeremytammik
Autodesk
Autodesk

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

0 Likes