The issue about Selection.PickObjects() method when picking group elements.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
2) By no-API : can pick only parent groups (the highest element of tree).
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.
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);
}