Check if an Element is of Room type?

Check if an Element is of Room type?

Anonymous
Not applicable
1,303 Views
3 Replies
Message 1 of 4

Check if an Element is of Room type?

Anonymous
Not applicable

How to check programmatically if a given Element is of type Room?

0 Likes
1,304 Views
3 Replies
Replies (3)
Message 2 of 4

matthew_taylor
Advisor
Advisor
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.


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes
Message 3 of 4

jeremytammik
Autodesk
Autodesk

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



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 4 of 4

Mustafa.Salaheldin
Collaborator
Collaborator

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
    }
}

¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes