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: 

collect all room in leve xx

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
sonicer
3956 Views, 13 Replies

collect all room in leve xx

How can I collect all rooms in level 01...a then do somethin with this rooms?

i used coolectelement filter....no idea how to get....have somebody complet source code....

I can do that in dynamo ...now I want do that in c# ....c# beginner programmer...

 

 

code for revit 2016 api or revit 2017 api.

 

thx.

13 REPLIES 13
Message 2 of 14
MarryTookMyCoffe
in reply to: sonicer

you have problem because you can't collect Rooms, you need collect SpatialElement.

 

        public List<Room> getRoomFromLevel(Document document, Level level)
        {
            
            List<Element> Rooms = new FilteredElementCollector(document).OfClass(typeof(SpatialElement)).WhereElementIsNotElementType().Where(room => room.GetType() == typeof(Room)).ToList();

            return new List<Room>(Rooms.Where(room => document.GetElement(room.LevelId) == level).Select(r => r as Room));
        }
-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug
Message 3 of 14
sonicer
in reply to: MarryTookMyCoffe

sorry don't work..

it does!nt know "Where.()..."and new List<room>

Message 4 of 14
jeremytammik
in reply to: sonicer

Very good solution!

 

Thank you!

 

'Where' and 'List<>' come from the System.Collections.Generic namespace.

 

Here are some further small improvements:

 

      return new FilteredElementCollector( doc )
        .WhereElementIsNotElementType()
        .OfClass( typeof( SpatialElement ) )
        .Where( e => e.GetType() == typeof( Room ) )
        .Where( e => e.LevelId.IntegerValue.Equals( 
          idLevel.IntegerValue ) )
        .Cast<Room>();

 

More efficient because of:

 

  • Elimination of ToList()
  • Elimination of New List<>()
  • Elimination of doc.GetElement()

You can see how to compile it in The Building Coder samples:

 

https://github.com/jeremytammik/the_building_coder_samples/compare/2017.0.132.7...2017.0.132.8

 

Cheers,

 

Jeremy

 



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

Message 5 of 14
sonicer
in reply to: jeremytammik

wou thx Tammik your code work for me....But I want know how to use the first code?I make some change

 

          

List<Element> Rooms = new FilteredElementCollector(doc).OfClass(typeof(SpatialElement)).WhereElementIsNotElementType().Where(room => room.GetType() == typeof(Room)).ToList();

List<Room> Rooms1 = new List<Room>(Rooms.Where(room => doc.GetElement(room.LevelId) == level).Select(r => r as Room));

 I know the level 01 have id =355....why can I use 355 instead of "level"??? Or make some other mistake? Levelid is string, integer ??

 

In your code I inserted value 355 instead of idLevel.IntegerValue ...this work.

Message 6 of 14
MarryTookMyCoffe
in reply to: sonicer

level.levelId is type ElementId and you can use integervalue if you what have (int) or toString and then you gonna have string.

I suggest using Jeremy code, don't focus much on code that I write in one minute.

 

 

by the way thanks jeremy for improving it. It look much better in one lane.

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug
Message 7 of 14
sonicer
in reply to: MarryTookMyCoffe

thx ...much

 

this is some funny 😄 its not element type and then is typ of spacial element and type of room....not so much logical.....

WhereElementIsNotElementType()
        .OfClass( typeof( SpatialElement ) )
        .Where( e => e.GetType() == typeof( Room ) 

 

Message 8 of 14
jeremytammik
in reply to: sonicer
Message 9 of 14
jeremytammik
in reply to: sonicer

I summarised this discussion on The Building Coder for legibility and future reference:

 

http://thebuildingcoder.typepad.com/blog/2017/03/events-uv-coordinates-and-rooms-on-level.html#6

 

Cheers,

 

Jeremy



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

Message 10 of 14
greg7KTAR
in reply to: jeremytammik

Jeremy:

 

Can you please explain what is the variable e in your reply? It was not used in the example you corrected. Does it need to be defined as the selected element?

Message 11 of 14
jeremy_tammik
in reply to: greg7KTAR

I see no variable `e` except within the LINQ query expressions, so I do not understand your question.

  

Jeremy Tammik, Developer Advocacy and Support, The Building Coder, Autodesk Developer Network, ADN Open
Message 12 of 14
rhanzlick
in reply to: greg7KTAR

hi Greg, the 'e' in his code is used for LINQ/Lambda expressions. It is a sort of abbreviated (and in most cases preferred IMO) 'foreach loop notation'. The logic is you are assigning an arbitrary variable name (in this case e) to each item in the enumerable on which you are using the expression, ultimately applying some function to each item. GoogleSearch can provide much nicer additional examples and explanations than I could. Additionally, I believe the BuildingCoder also has a few nice articles that discuss the topic.
Message 13 of 14
rhanzlick
in reply to: sonicer

I realize the API has very likely been updated since this OP, but I find the addition of .OfCategory(BuiltinCategory.OST_Rooms) in lieu of the oftype filters.

Message 14 of 14
jeremy_tammik
in reply to: rhanzlick

Oh yes! Thank you for the pointer. OfCategory is much better, being a fast filter, rather than post-processing with .NET Where, which is not a filter at all, and hence much, much less performant.

 

Jeremy Tammik, Developer Advocacy and Support, The Building Coder, Autodesk Developer Network, ADN Open

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