The issue about Selection.PickObjects() method when picking group elements.

itsjakk3
Contributor

The issue about Selection.PickObjects() method when picking group elements.

itsjakk3
Contributor
Contributor

I've found that when picking group elements by API and by no-API (Manually picking objects in Revit),

there is the difference about accessibility of pickable elements.

 

I caught this while designing the method to ungroup all nested groups as separated elements by recursive method.

And then, I thought that this can lead users to see exceptions more.

 

So that if there is intention of this when designing API, it would be better if there is an option about picking.
(like an optional bool type parameter that make users only pick not nested parent group).

 

Actually, I'm new in this Revit API... So that it may be that I didn't find about the explanation of this on the forum or API or not.

 

---

 

1. First, I've created the nested groups.

 

2. Picking test.

1) By API : can pick all nested groups.

itsjakk3_0-1702431743306.png

 

2) By no-API : can pick only parent groups (the highest element of tree).

itsjakk3_1-1702431910051.png

Of course this way also can pick child groups by pressing tab key. But if I trying to ungroup it,
directly warning shows up.

 

3. And then, I've tried to ungroup them through the method below :

 

 

 

 public class ExplodeNestedGroup_NoDeletion_YesRecursive : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var uiapp = commandData.Application;
            var app = uiapp.Application;
            var uidoc = uiapp.ActiveUIDocument;
            var doc = uidoc.Document;

            var groupElementIds = uidoc.Selection.PickObjects(ObjectType.Element, new GroupFilter())
                .Select(e => e.ElementId)
                .ToList();
            UngroupAll(groupElementIds, doc);

            return Result.Succeeded;
        }

        public void UngroupAll(IEnumerable<ElementId> elemIds, Document doc)
        {
            using (var tr = new Transaction(doc, "Ungroup"))
            {

                foreach (ElementId elemId in elemIds)
                {
                    if (doc.GetElement(elemId) is Group groupElement)
                    {
                        tr.Start();
                        var ungroupedIds = groupElement.UngroupMembers();
                        tr.Commit();
                        UngroupAll(ungroupedIds, doc);
                    }
                }
            }
        }
    }

 

(for easier way to design the unit tests, I've set transaction starting just before ungrouping members.)

Filter Class is below:

public class GroupFilter : ISelectionFilter
    {
        public bool AllowElement(Element elem)
        {
            if (elem is Group)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            return false;
        }
    }

 

Then, the exception window shows up.

itsjakk3_2-1702432437686.png

 

I thought that if I add the deletion of parent element from document, it could be solved. 

 

 

 

if (doc.GetElement(elemId) is Group groupElement)
                    {
                        tr.Start();
                        var ungroupedIds = groupElement.UngroupMembers();
                        doc.Delete(elemId);
                        tr.Commit();
                        UngroupAll(ungroupedIds, doc);
                    }

 

 

 

 

 

BIM Engineer & Developer (Wannabe)
Reply
535 Views
4 Replies
Replies (4)

jeremy_tammik
Autodesk
Autodesk

I have asked the development team for you.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open

itsjakk3
Contributor
Contributor

@jeremy_tammik 

thank you for your reply!

 

BIM Engineer & Developer (Wannabe)
0 Likes

jeremy_tammik
Autodesk
Autodesk

I discussed this with them, and they say: What actually is the question or concern? The selection behavior? The ungrouping behavior? Both? The thread is informative but doesn't clearly ask a question or assert a problem. I suspect that it would be possible to write a selection filter that excludes nested groups, if desired. This would lead to behavior closer to other behaviour in Revit UI.

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes

itsjakk3
Contributor
Contributor

Sorry, my first post says not clear question and using keword 'issue' seems to offend development team. I didn't intend it...

I just want to make users unable to select nested group elements when they run my custom function.

 

---

I need to write more detailed explanation about this...


First, I wanted to make a function to ungroup all nested items that belong to group, like exploding all nested block on AutoCAD.

To do that, I tried recursive function. And I thought that the picking prompt (PickObjects()) should not allow
picking nested groups not to meet the exception BuiltInFailures.GroupFailures.DontEditNestedGroup. And created filter class that allows Group and applied to the method PickObjects().

 

(Filter class is here)

 

    public class GroupFilter : ISelectionFilter
    {
        public bool AllowElement(Element elem)
        {
            if (elem is Group)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            return false;
        }
    }

 


I expected this will users not to pick nested groups, like just picking elements without using API. But it allowed to pick nested groups, just allowed picking only Group elements whether it's nested or not.

And after reading your reply, I've tried to modify my filter class in several ways but could not find a way for that.


@jeremy_tammik wrote:

I discussed this with them, and they say: What actually is the question or concern? The selection behavior? The ungrouping behavior? Both? The thread is informative but doesn't clearly ask a question or assert a problem. I suspect that it would be possible to write a selection filter that excludes nested groups, if desired. This would lead to behavior closer to other behaviour in Revit UI.

   


I searched some properties or methods on revit 2024 api sdk that returns whether the group element is nested or not, but couldn't find.

BIM Engineer & Developer (Wannabe)
0 Likes