collect all room in leve xx

collect all room in leve xx

sonicer
Collaborator Collaborator
7,114 Views
13 Replies
Message 1 of 14

collect all room in leve xx

sonicer
Collaborator
Collaborator

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.

0 Likes
Accepted solutions (1)
7,115 Views
13 Replies
Replies (13)
Message 2 of 14

MarryTookMyCoffe
Collaborator
Collaborator

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
Collaborator
Collaborator

sorry don't work..

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

0 Likes
Message 4 of 14

jeremytammik
Autodesk
Autodesk
Accepted solution

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
Collaborator
Collaborator

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.

0 Likes
Message 6 of 14

MarryTookMyCoffe
Collaborator
Collaborator

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

sonicer
Collaborator
Collaborator

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 ) 

 

0 Likes
Message 8 of 14

jeremytammik
Autodesk
Autodesk
0 Likes
Message 9 of 14

jeremytammik
Autodesk
Autodesk

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
Contributor
Contributor

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?

0 Likes
Message 11 of 14

jeremy_tammik
Alumni
Alumni

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

rhanzlick
Advocate
Advocate
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
Advocate
Advocate

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.

0 Likes
Message 14 of 14

jeremy_tammik
Alumni
Alumni

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
0 Likes