Recursive get elements in Group

Recursive get elements in Group

Anonymous
Not applicable
3,809 Views
6 Replies
Message 1 of 7

Recursive get elements in Group

Anonymous
Not applicable

I would like to get all Elements in a Model Group. The catch is, even elements which are in a nested group of the Model Group given. Is there an existing method i could use?

0 Likes
Accepted solutions (1)
3,810 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
Does Group.GetMemberIds() contain a flat list of ALL the elementids recursivly?
0 Likes
Message 3 of 7

Anonymous
Not applicable

First draft to getting Elements recursive from a Group

 

        public static List<RevitElement> GetMembersRecursive(Document d, RevitGroup g, List<RevitElement> r)
        {
            List<RevitElement> elems = g.GetMemberIds().Select(q => d.GetElement(q)).ToList();

            foreach (RevitElement el in elems)
            {
                if (el.GetType() == typeof(RevitGroup))
                {
                    Upload.GetMembersRecursive(d, el as RevitGroup, r);
                    continue;
                }
                
                r.Add(el);
            }

            return r;
        }
0 Likes
Message 4 of 7

OR_AND_NO
Advocate
Advocate

Could You please explain what is Upload in Your code?

0 Likes
Message 5 of 7

Anonymous
Not applicable
Accepted solution

Upload is just the class name. 

 

You could use it without the class name.

 

 

class GroupUtil
    {
        public static List<Element> GetMembersRecursive(Document d, Group g, List<Element> r = null)
        {
            if (r == null)
            {
                r = new List<Element>();
            }

            List<Element> elems = g.GetMemberIds().Select(q => d.GetElement(q)).ToList();

            foreach (Element el in elems)
            {
                if (el.GetType() == typeof(Group))
                {
                    GetMembersRecursive(d, el as Group, r);
                    continue;
                }
                
                r.Add(el);
            }

            return r;
        }
    }

 

Message 6 of 7

DavidsDesk
Advocate
Advocate

I'm working on the same problem and find this post very helpful.  I have a DocumentChanged event argument, but I'm getting an error LIST<Element> does not contain definition for "GetMembersRecursive."

 

    public class OnGroupTrans : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            return Execute(commandData.Application);
        }
        
        public Result Execute(UIApplication uiapp)
        {
            // Get the handle of current document.
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uiapp.ActiveUIDocument.Document;

            // Get the element selection of current document.
            Selection selection = uidoc.Selection;
            ElementId selectedIds = (ElementId)selection.GetElementIds();
            Group selectedGroup = (Group)doc.GetElement(selectedIds);

            // Get a list of group elements
            ElementClassFilter filter = new ElementClassFilter(typeof(Group));
            List<Element> groupElem = (List<Element>)selectedGroup.GetDependentElements(filter);
            List<Element> groupMem = groupElem.GetMembersRecursive();

            return Result.Succeeded;
        }
    }
0 Likes
Message 7 of 7

asnow_DSARCSVY7W37
Explorer
Explorer

Why does 

"query": "'property.name.Edited by' != null and 'property.name.Edited by' != ''"

return

"results": [
                            {
                                "name": "Edited by",
                                "value": null
                            }

?

Is the 'and' looking for an =is= like some other graphQL implementations use?
0 Likes