How to retrieve rooms filtered by level?

How to retrieve rooms filtered by level?

Anonymous
Not applicable
4,200 Views
7 Replies
Message 1 of 8

How to retrieve rooms filtered by level?

Anonymous
Not applicable

Hi All,

 

Can someone guide me about how to get the rooms programmatically from the model based on the level? I don't need all the rooms rather for each level. 

 

Many thanks in advance

 

Kind Regards

Bilal

0 Likes
Accepted solutions (1)
4,201 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable

Moreover, I have the following code to retrieve the level of the design.

 

public List<Level> GetAllLevels()
        {
            List<Level> levelCollection = new List<Level>();
            FilteredElementCollector collector = new FilteredElementCollector(_document);
            ICollection<Element> collection = collector.OfClass(typeof(Level)).ToElements();
            foreach (var e in collection)
            {
                Level level = e as Level;

                if (null != level)
                {
                    // keep track of number of levels
                    levelCollection.Add(level);
                }
            }
            return levelCollection;
        }

But from this list, I need to query the rooms for each of the level separately.

 

Thanks

0 Likes
Message 3 of 8

Anonymous
Not applicable

You are looking at the problem backwards. Levels are a property of rooms. In other words Rooms have a Level property. Levels do not have a Room property. Think of this. Which exists first, Levels or Rooms? The Level has to exist first. In fact you cannot delete all the Levels in a Revit project. There has to be at least one. You can delete all the rooms. 

 

Sorry, but I cannot do this off the top of my head, but you need to build your collector as a collection of rooms that have the level property you are interested in, not a collector of Levels. There are all sorts of ways to do this and depending on the phases in your project, like existing rooms being demolished during the course of the project phases, you might run into some bumps along the way.

 

Levels being as aspect of Rooms is how you extract the information from the Revit file. It is not necessarily how you organize the information for your application. For example as you extract the information from the collection, as Rooms having Level properties, you could very well turn it around for your purposes into a dictionary of Level, Room list using whatever datatype you want. I'd make it a dictionary of <string><list<string>>  just so I could look at it using simple tools.

0 Likes
Message 4 of 8

Moustafa_K
Collaborator
Collaborator
Accepted solution

Hi

all you have to do is 1st get the level Id that you need to query.

 

in the below example I will assume you are querying the current view.

 

 

ElementId levid = m_doc.ActiveView.GenLevel.Id; // get the desired level ID

  var Roomsbylevel_filcol = new FilteredElementCollector(m_doc, levid ) //search only in this level
.OfClass(typeof(SpatialElement))  //get all rooms
.Cast<SpatialElement>()  //cast results to SpatialElements
.Where(o=>o.LevelId == levid ); //search by the above mentioned Level

//now loop through the results 
foreach(var item in Roomsbylevel_filcol)
{
//do something
}

I hope that helps

 

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
Message 5 of 8

Anonymous
Not applicable

Many Thanks for the response. I tried similar logic and worked for me.

0 Likes
Message 6 of 8

engradil_hotmail_co_uk
Explorer
Explorer

Dear @Moustafa_K 

 

I am also trying to retrieve the Rooms by level and so thanks for the code above. However, I am getting the error that 

 

viewId is not a view.
Parameter name: viewId

 

Any idea how to resolve the issue.

 

Here is the code:

 

//Access Revit session
UIDocument UIDoc = commandData.Application.ActiveUIDocument;
//Access Revit Application
Application Iapp = commandData.Application.Application;
//Revit current project
Document Idoc = UIDoc.Document;

MultiValueDictionary<string, double> Idict = new MultiValueDictionary<string, double>();
StringBuilder Istring = new StringBuilder();

 

 

ElementId levid = Idoc.ActiveView.GenLevel.Id;

//get all rooms
var Roomsbylevel = new FilteredElementCollector(Idoc, levid) //search only in this level
.OfClass(typeof(SpatialElement)) //get all rooms
.Cast<SpatialElement>() //cast results to SpatialElements
.Where(o => o.LevelId == levid); //search by the above mentioned Level
//now loop through the results
foreach (var objRoom in Roomsbylevel)
{
Parameter SP_ID = objRoom.LookupParameter("H_BA_ID");
Parameter SP_GF = objRoom.LookupParameter("H_BA_Gesamtfläche");
if (null != SP_ID.AsString() && 0.00 < objRoom.Area)
{
Idict.Add(SP_ID.AsString(), objRoom.Area);
}
}

0 Likes
Message 7 of 8

Moustafa_K
Collaborator
Collaborator
You need to make sure the active view is not the project browser nor
schedule nor draft... it is preferable to be a plan view
Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 8 of 8

engradil_hotmail_co_uk
Explorer
Explorer

Yes, I am trying it on plan view.

0 Likes