private void NumerateInGroups(List<ElInGroup> elementsInGroups, Document doc)
{
var transactionName = Language.GetFunctionLocalName(LangItem, new ModPlusConnector().LName);
if (string.IsNullOrEmpty(transactionName))
transactionName = LangItem;
using (TransactionGroup transactionGroup = new TransactionGroup(doc))
{
transactionGroup.Start($"{transactionName}: Groups");
foreach (IGrouping<string, ElInGroup> grouping in elementsInGroups.GroupBy(e => e.GroupName))
{
List<List<ElementId>> elements = new List<List<ElementId>>();
// ungroup and delete
Transaction transaction = new Transaction(doc);
transaction.Start("Ungroup");
foreach (ElInGroup elInGroup in grouping)
{
var group = (Group)doc.GetElement(new ElementId(elInGroup.GroupId));
var grpElements = group.UngroupMembers().ToList();
elements.Add(grpElements);
}
transaction.Commit();
transaction.Start("Numerate");
foreach (ElInGroup elInGroup in grouping)
{
foreach (var pair in elInGroup.Elements)
{
if (pair.Key.LookupParameter(_parameterName) is Parameter parameter)
{
parameter.Set(pair.Value);
}
}
}
transaction.Commit();
transaction.Start("Create new group");
foreach (List<ElementId> elementIds in elements)
{
Group newGroup = doc.Create.NewGroup(elementIds);
newGroup.GroupType.Name = grouping.Key;
}
transaction.Commit();
//foreach (List<ElementId> elementIds in elements)
//{
// transaction.Start("Create new group");
// Group newGroup = doc.Create.NewGroup(elementIds);
// var groupType = new FilteredElementCollector(doc)
// .OfClass(typeof(GroupType))
// .Cast<GroupType>()
// .FirstOrDefault(g => g.Name == grouping.Key);
// if (groupType != null)
// newGroup.GroupType = groupType;
// else newGroup.GroupType.Name = grouping.Key;
// transaction.Commit();
//}
transaction.Dispose();
}
transactionGroup.Assimilate();
}
}