If TypeOf elem Is DB.Architecture.Room Then dim rm as DB.Architecture.Room =trycast (elem, DB.Architecture.Room ) end if
I figure that'll get you where you need to go.
If you're handling a user selection, you could implement an ISelectionFilter filter to limit the selection to just rooms.
In addition to Matt's very helpful suggestion (thank you very much, Matt!), please note that the room element cannot be directly used for element filtering.
Use an intermediate spatial element instead:
http://thebuildingcoder.typepad.com/blog/2011/11/accessing-room-data.html
Cheers,
Jeremy
You can filter by category or you can check the category name of the element if it equals Rooms. Please have a look on the following code and if it satisfies your needs mark this reply as an answer.
#region Namespaces using System; using System.Text; using System.Linq; using System.Xml; using System.Reflection; using System.ComponentModel; using System.Collections; using System.Collections.Generic; using System.Windows; using System.IO; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Events; using Autodesk.Revit.DB.Architecture; using Autodesk.Revit.DB.Structure; using Autodesk.Revit.DB.Mechanical; using Autodesk.Revit.DB.Electrical; using Autodesk.Revit.DB.Plumbing; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; using Autodesk.Revit.UI.Events; //using Autodesk.Revit.Collections; using Autodesk.Revit.Exceptions; using Autodesk.Revit.Utility; using RvtApplication = Autodesk.Revit.ApplicationServices.Application; using RvtDocument = Autodesk.Revit.DB.Document; #endregion namespace RvtTest { [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class ExtCmd : IExternalCommand { #region Cached Variables private static ExternalCommandData _cachedCmdData; public static UIApplication CachedUiApp { get { return _cachedCmdData.Application; } } public static RvtApplication CachedApp { get { return CachedUiApp.Application; } } public static RvtDocument CachedDoc { get { return CachedUiApp.ActiveUIDocument.Document; } } #endregion #region IExternalCommand Members public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet) { _cachedCmdData = cmdData; try { //TODO: add your code below. FilteredElementCollector collector = new FilteredElementCollector(CachedDoc); ICollection<Element> rooms = collector.OfCategory(BuiltInCategory.OST_Rooms).ToElements(); collector.WhereElementIsNotElementType(); StringBuilder sb = new StringBuilder(); foreach (Element el in rooms) { if (el.Category.Name == "Rooms") { //This is a room } } TaskDialog.Show("reavit", sb.ToString()); return Result.Succeeded; } catch (Exception ex) { msg = ex.ToString(); return Result.Failed; } } #endregion } }