So far I have written C# code to allow the user to select multiple parts of a model within revit, and it will post the id of the selected elements. I now want to adapted this in two ways:
1, To check whether the element selected is a room. (has a room tag) so then I only work with rooms.
2, post the area of said room instead of just the ID of the element.
I am fairly new to C# and the Revit API so would appreiate any pushes in the right direction, thanks.
My current code:
using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Linq;
using System.Text;
namespace HelloWorld
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
IList<Reference> pickedObjs = uidoc.Selection.PickObjects(ObjectType.Element, "Select elements");
List<ElementId> ids = (from Reference r in pickedObjs select r.ElementId).ToList();
using (Transaction tx = new Transaction(doc))
{
StringBuilder sb = new StringBuilder();
tx.Start("transaction");
if (pickedObjs != null && pickedObjs.Count > 0)
{
foreach (ElementId eid in ids)
{
Element e = doc.GetElement(eid);
sb.Append("/n" +e.Name);
}
TaskDialog.Show("Area Calculator", sb.ToString());
}
tx.Commit();
}
return Result.Succeeded;
}
}
}
If you are new to Revit API then I recommend to get the latest version of RevitLookup from GitHub, deploy it on your Revit and start using it. It will help you to figure out which Revit API objects you can use to get your tools working.
As per your current problem. To find out if given element is a Room:
Room room = e as Room;
if (room!=null) ... ; //then you know it's a Room
alternatively:
if (e is Room) ... ; //then you know it's a Room
Second part: to query element's parameters you write:
Parameter par = e.get_Parameter(BuiltInParameter.ROOM_AREA);
string valSting = par.AsValueString();
double valDouble = par.AsDouble(); //mind the value is in native Revit units, not project units. So square feet in this case
You could as well use par = e.LookupParameter("Area");
but if you work with system parameters, it's better to use built-in enums to refer to them (e.g. because they are language proof)
Normally I develop tools and macros for MEP, all this I figured out in 10 seconds using the RevitLookup add-on. :)