Message 1 of 16
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I recently came across an idea for creating a class to handle group editing in the api and tried making my own. The code succeeds in modifying the group and perpetuating the changes to existing groups, but the original group ends up being duplicated. Am I correct in assuming that Doc.Create.NewGroup would group the list of elements without placing an additional group instance?
Here's the class I set up:
public class GroupEditor
{
Document _doc;
Group _group;
GroupType _groupType;
List<ElementId> _members;
double _orientation;
public GroupEditor(Group group)
{
_group = group;
_groupType = _group.GroupType;
_doc = _group.Document;
_orientation = _groupType.LookupParameter("Original_Orientation").AsDouble();
}
public List<ElementId> Members { get => _members; }
public void StartEditing()
{
_members = _group.UngroupMembers().ToList();
_doc.Regenerate();
}
public void AddElements(List<ElementId> list)
{
_members.AddRange(list);
}
public void AddElements(ElementId id)
{
_members.Add(id);
}
public void RemoveElements(List<ElementId> list)
{
_members.RemoveAll(x => list.Contains(x));
}
public void RemoveElements(ElementId id)
{
_members.Remove(id);
}
public void FinishEditing()
{
_doc.Regenerate();
_group = _doc.Create.NewGroup(_members);
_doc.Regenerate();
FilteredElementCollector collector = new FilteredElementCollector(_doc);
List<Group> groups = collector.OfClass(typeof(Group)).Cast<Group>().Where(x => x.GroupType.Name == _groupType.Name && x.Id != _group.Id).ToList();
foreach (Group g in groups)
g.GroupType = _group.GroupType;
string name = _groupType.Name;
_doc.Delete(_groupType.Id);
_group.GroupType.Name = name;
_group.GroupType.LookupParameter("Original_Orientation").Set(_orientation);
}
}
I tried adding doc.Regenerate in a few places to see if that would help but that wasn't the issue. If y'all have any thoughts, I'm all ears. Thanks!
Solved! Go to Solution.