check if element is in a group

check if element is in a group

Anonymous
Not applicable
4,723 Views
4 Replies
Message 1 of 5

check if element is in a group

Anonymous
Not applicable

I have seen the posts on finding the elements in a model group.

I have also noticed that the filtered element collectors will find elements that are inside groups.  (e.g. filter for all walls, and I get walls that are inside groups).

 

So my question is:

Is there an efficient way to find out if an element is in a model group?

 

In a model with lots of groups, I am guessing that scanning through all the elements of all groups to find a match would be inefficient.

 

The related question to this is - can I find groups 'geographically'?  I.e. within a rectangular extent?  I am hoping there is a way short of finding all the elements in the group and checking where they are...

 

For clarity, here is what I want to be able to do:

 

Look within an area outline (that's a whole other issue, I know).

Within this area, find a particular type of element (e.g. by family name, etc), which I expect to be in a group.

Make sure the element is in a group and check the name of the group.

 

Thanks for your help in advance.

Abba

0 Likes
4,724 Views
4 Replies
Replies (4)
Message 2 of 5

Mustafa.Salaheldin
Collaborator
Collaborator

Please try this code and if it satisfies your needs please 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.Windows.Media.Imaging;
using System.Windows.Forms;
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 RevitAddinCS2
{
    [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
            {
                UIDocument uidoc = new UIDocument(CachedDoc);
                ISelectionFilter selFilter = new GroupSelectionFilter();
                List<Reference> groupsReference = uidoc.Selection.PickObjects(ObjectType.Element, selFilter, "Select multiple Groups").ToList();

                List<ElementId> groupsIds = new List<ElementId>();

                foreach(Reference r in groupsReference)
                {
                    Element e = CachedDoc.GetElement(r);
                    groupsIds.Add(e.Id);
                }

                FilteredElementCollector elements = new FilteredElementCollector(CachedDoc, CachedDoc.ActiveView.Id);
                elements.OfClass(typeof(FamilyInstance));

                List<FamilyInstance> elementsInGroups = new List<FamilyInstance>();

                StringBuilder sb = new StringBuilder();

                foreach (FamilyInstance inst in elements)
                {
                    ElementId e = inst.GroupId;

                    if (groupsIds.Contains(e)) {
                        elementsInGroups.Add(inst);
                        string groupName = CachedDoc.GetElement(e).Name;
                        sb.AppendLine(string.Format("element: {0} is of group: {1}",inst.Name,groupName));
                    }
                }

                TaskDialog.Show("Revit" , sb.ToString());

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                return Result.Failed;
            }
        }


        public class GroupSelectionFilter : ISelectionFilter
        {
            public bool AllowElement(Element e)
            {
                return (e.Category.Id.IntegerValue.Equals(
                  (int)BuiltInCategory.OST_IOSModelGroups));
            }
            public bool AllowReference(Reference r, XYZ p)
            {
                return false;
            }
        }
        #endregion
    }
}

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

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 3 of 5

Anonymous
Not applicable

Thanks, Mustafa,

This makes sends, and it looks like it will work.  The only issue that I can see, is that if I have about a hundred groups, each with quite a few elements, this could start to get slow, if I want to deal with things dynamically.  Maybe I can build an index and then just keep it up to date as things are modified.

 

I will give it a try and let you know what happens...

 

Message 4 of 5

Anonymous
Not applicable

I think this wont work for nested groups. The nested group will be a element in the parent group. But the elements in the nested group are not children of the parent group but children of the nested group. I posted somewhere in this forum a snippet to recursivly get all the elements in a model group, containing the elements of nested groups as well.

0 Likes
Message 5 of 5

JimJia
Alumni
Alumni
I think Mustafa's code is the solution to find element in group, we can use this method for nested group.

regarding nest group post mentioned by Michal-sk, I guess he means this page:
http://forums.autodesk.com/t5/revit-api/recursive-get-elements-in-group/m-p/6241156/highlight/true#M...

Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes