Calculating total area for multiple rooms

When a single room is selected in Revit, we can easile see its area in the properties window. However it is not so straightforward to immediately check the area of the multiple selected rooms. Below I am going to demonstrate a simple macro script that adds such a functionality.

We are going to work with Room class of the Revit API, which is a part od Revit.DB.Architecture module. Its definition is not automatically included within a standard Revit macro. Thus we have to add it manually at the top of the script code like so:

using System;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;

Now we can start with the function code. I assume that the script will be started after uses selects some rooms in Revit. Therefore at the beginning of the macro we access the UIDocument selection:

Document doc = this.ActiveUIDocument.Document;
UIDocument uiDoc = new UIDocument(doc);
Selection sel = uiDoc.Selection;

Next we collect list of the selected elements’ ids

ICollection<ElementId> ids = sel.GetElementIds();

We create a variable for the total area, with a value of zero.

double totalArea = 0f;

In the next lines we check whether the id list is not empty. If not then for each of the ids we collect an element from the document. If it is a room we add it’s area to the total.

if(0<ids.Count){
    foreach(ElementId id in ids) {
        Element e = doc.GetElement(id);
        if (e is Room) {
            Room r = e as Room;
            totalArea += r.Area;
        }
    }
}

The totalArea variable now contains a sum for all the selected rooms. Now we can convert the units into square meters if we want so, and then display the sum:

double METERS_IN_FEET = 0.3048;
totalArea = totalArea * Math.Pow(METERS_IN_FEET,2);
totalArea = Math.Round(totalArea,2);
TaskDialog.Show("Total area", "Total area : " + totalArea + "m²"); 

The entire function code may look like so:

public void roomArea() {
           
    Document doc = this.ActiveUIDocument.Document;
    UIDocument uiDoc = new UIDocument(doc);
           
    Selection sel = uiDoc.Selection;
           
    ICollection<ElementId> ids = sel.GetElementIds();
           
    double totalArea = 0f;
           
    if(0<ids.Count){
        foreach(ElementId id in ids) {
            Element e = doc.GetElement(id);
            if (e is Room) {
                Room r = e as Room;
                totalArea += r.Area;
            }
        }
    }
           
    double METERS_IN_FEET = 0.3048;
           
    totalArea = totalArea * Math.Pow(METERS_IN_FEET,2);
    totalArea = Math.Round(totalArea,2);
           
    TaskDialog.Show("Total area", "Total area : " + totalArea + "m²");
} 
Posts created 32

2 thoughts on “Calculating total area for multiple rooms

  1. I’ll immediately clutch your rss as I can not in finding your email subscription hyperlink or e-newsletter service.
    Do you’ve any? Kindly allow me recognise so that I could subscribe.
    Thanks.

Leave a Reply

Your email address will not be published.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top