Can anyone help me with this code:
I`m trying to select the shared elements from a nested family. But, I need the code to include the parent family in the selection. I tryind the elementSet.Add command to see if it would work, but no succes. Anyone can help? I`m new with code.
Thanks!
public Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref string message, ElementSet elements) { UIApplication app = revit.Application; Document doc = app.ActiveUIDocument.Document; Autodesk.Revit.UI.Selection.SelElementSet elementSet = app.ActiveUIDocument.Selection.Elements; Autodesk.Revit.UI.Selection.SelElementSet subSet = Autodesk.Revit.UI.Selection.SelElementSet.Create(); foreach (Element e in elementSet) { elementSet.Add(e); FamilyInstance fi = e as FamilyInstance; if (fi != null) { ICollection<ElementId> ids = fi.GetSubComponentIds(); if (ids.Count > 0) { foreach (ElementId id in ids) { Element sube = doc.GetElement(id); subSet.Add(sube); } } else subSet.Add(e); } else { AssemblyInstance ai = e as AssemblyInstance; if (ai != null) { ICollection<ElementId> ids = ai.GetMemberIds(); foreach (ElementId id in ids) { Element sube = doc.GetElement(id); subSet.Add(sube); } } else { Group g = e as Group; if (g != null) { ICollection<ElementId> ids = g.GetMemberIds(); foreach (ElementId id in ids) { Element sube = doc.GetElement(id); subSet.Add(sube); } } } } } app.ActiveUIDocument.Selection.Elements = subSet; return Autodesk.Revit.UI.Result.Succeeded; } }
foreach (Element e in elementSet) { elementSet.Add(e);
You are trying to add an element in the list where it already exists...just can't understand what you are trying to achive. Also if I remember correctyl , .NET doesn't allow you to modify the enumerable while in foreach looping.
This loop is for selecting the subcomponents from the nested family selected in the project. However, I`d like to select also the hole hole. For an example: The nested family contains 2 elements. If I tab this elements manually, I will make a set of 2 elements. If I include the hole family at this selection, I will make a set of 3 elements. What I aim to do with this code id to select this elements (including the hole family) automatically. This already happens with the nested componentes, but not with the whole family.
Any help?
Hello,
It looks like you want to have the original family instance also as a part of the subSet selection set. I would suggest removing the code lines "elementSet.Add(e);" and "else subSet.Add(e);" and inside the "if (fi != null)" condition, just add subSet.Add(e);. Something like this:
if (fi != null) {
subSet.Add(e); ICollection<ElementId> ids = fi.GetSubComponentIds(); if (ids.Count > 0) { foreach (ElementId id in ids) { Element sube = doc.GetElement(id); subSet.Add(sube); } }
Hope this helps.
Cheers
Gopinath