Thanks for the tip, but I just tried and regenerating doesn't help.
It looks like I need to end and restart the transaction.
This code shows that GroupType.Groups still reports the ungrouped group after doc.Regenerate() and finally disappears after committing and restarting a new transaction. I tried to get a new reference to the GroupType, hoping that avoiding some caching would help, but it didn't:
tx.Start("Test 1");
var elementIds = new List<ElementId>
{
Utils.CreateModelLine(new XYZ(0, 0, 0), new XYZ(1, 0, 0), doc).Id,
Utils.CreateModelLine(new XYZ(0, 1, 0), new XYZ(1, 1, 0), doc).Id,
Utils.CreateModelLine(new XYZ(0, 2, 0), new XYZ(1, 2, 0), doc).Id,
};
var group1 = doc.Create.NewGroup(elementIds);
var group2 = doc.Create.PlaceGroup(new XYZ(0, 5, 0), group1.GroupType);
var group3 = doc.Create.PlaceGroup(new XYZ(0, 10, 0), group1.GroupType);
Debug.Print(group1.GroupType.Groups.Size.ToString());
group2.UngroupMembers();
Debug.Print(group1.GroupType.Groups.Size.ToString());
doc.Regenerate();
Debug.Print(group1.GroupType.Groups.Size.ToString());
var groupType = new FilteredElementCollector(doc)
.OfClass(typeof(GroupType))
.Cast<GroupType>()
.FirstOrDefault(gt => gt.Name == group1.GroupType.Name);
Debug.Print(groupType.Groups.Size.ToString());
tx.Commit();
tx.Start("Test 2");
Debug.Print(groupType.Groups.Size.ToString());
groupType = new FilteredElementCollector(doc)
.OfClass(typeof(GroupType))
.Cast<GroupType>()
.FirstOrDefault(gt => gt.Name == group1.GroupType.Name);
Debug.Print(groupType.Groups.Size.ToString());
tx.Commit();
I tried using a SubTransaction, but didn't help.
At this point I do have a workaround, but I don't like to create transactions only because I can't rely on GroupType.Groups not reporting ungrouped groups.
Any advice?