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: