Building on this previous post, here is how to retrieve elements that are stored in a SelectionFilterElement. After getting the elements, the code below shows how to select them in the Revit UI.
public void GetSelectionFilterElements()
{
Document doc = this.ActiveUIDocument.Document;
UIDocument uidoc = new UIDocument(doc);
// Get the SelectionFilterElement named "My Selection Filter" with this LINQ statement
SelectionFilterElement filter = (from f in new FilteredElementCollector(doc)
.OfClass(typeof(SelectionFilterElement))
.Cast<SelectionFilterElement>()
where f.Name == "My Selection Filter" select f).First();
// Get ids of elements in the filter
ICollection<ElementId> filterIds = filter.GetElementIds();
// Report # of elements to user
TaskDialog.Show("Elements in filter", filterIds.Count.ToString());
// ToString() must be used here because Count returns an integer and TaskDialog.Show requires a string
// This hasn't been needed in other examples because when you concatenate a string with an integer
// the whole thing automatically becomes a string. So ToString() would not be needed if I did:
// "Number of elements is " + fitlerIds.Count
// Now select the elements in "My Selection Filter" in the Revit UI
// Create a SelElemenSet object
SelElementSet selSet = SelElementSet.Create();
foreach (ElementId id in filterIds) // loop through the elements in the filter
{
Element e = doc.GetElement(id); // get the element for each id
selSet.Add(e); // add the element to the SelElementSet
}
// Use the SelElementSet to set the elements shown as selected in the Revit UI
uidoc.Selection.Elements = selSet;
// Refresh the active view so that the change in selection higlighting is shown
uidoc.RefreshActiveView();
}
Hello I got the problem to highlight the elements pls help me: my code is like this:
FilteredElementCollector c = new FilteredElementCollector(doc).OfClass(typeof(Family));
FilteredElementCollector familyCollector11 = new FilteredElementCollector(doc);
familyCollector.OfClass(typeof(FamilySymbol));
List GlobalStrings22 = new List();
Family family2 = null;
FamilySymbol symbol4 = null;
SelElementSet selElements = uidoc.Selection.Elements;
SelElementSet selSet = SelElementSet.Create();
foreach (Family f in c)
{
family2 = f;
foreach (FamilySymbol s in family2.Symbols)
{
symbol4 = s;
string ss = s.Name.ToString();
Element ele = s as Element;
foreach (Material material in s.Materials)
{
if (material.Name.ToString() == Class1.GlobalVar22.ToString())
{ ElementId id = s.Id as ElementId;
Element e = doc.GetElement(id);
selSet.Add(e);
GlobalStrings22.Add(material.Name.ToString());
}
} break;
}
}
uidoc.Selection.Elements = selSet;
uidoc.RefreshActiveView();
Hi
What is the problem you are having with this code?
elements are not highlighting……..like if i click the door in list box the i want to highlight all doors..
Here is some code that will highlight all instances of the Double-Flush door family. Can you adapt this to your needs?
public void highlightDoor()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
IList elementList = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).Cast().Where(e => e.Symbol.Family.Name == “Double-Flush”).ToList();
SelElementSet selSet = SelElementSet.Create();
foreach (FamilyInstance e in elementList)
{
selSet.Add(e);
}
uidoc.Selection.Elements = selSet;
uidoc.RefreshActiveView();
}
thank u for reply 🙂
IList elementList = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).Cast().Where(e => e.Symbol.Family.Name
this statement giving some error like tis
Error 2 ‘Autodesk.Revit.DB.FilteredElementCollector’ does not contain a definition for ‘Cast’ and no extension method ‘Cast’ accepting a first argument of type ‘Autodesk.Revit.DB.FilteredElementCollector’ could be found (are you missing a using directive or an assembly reference?) C:\Users\p\Documents\Visual Studio 2010\Projects\door\door\Class1.cs 174 43 door
Add this to the section of using statements near the top of your file
using System.Linq;
hi i cant highlight the wall….plz tell me how to highlights walls
Could you provide a sample of your code that is not having the desired effect?
i add tat also tat was giving same error
i got the output the code like tis :FilteredElementCollector collector= new FilteredElementCollector(doc).WhereElementIsNotElementType() .WhereElementIsViewIndependent() .OfClass(typeof(FamilyInstance)) ;
thank you so much 🙂
hi i cant highlight the wall….plz tell me how to highlights walls
Hi Harry,
I’m adding this to a foreach loop iterating through all FilterElements but this linq expression doesn’t allow for FilterElements with no elements. How would I go about fixing this?
public void GetAllElementInFilters()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
string info = “”;
string fName = “”;
int feId = 0;
IList feColl = new FilteredElementCollector(doc)
.OfClass(typeof(FilterElement))
.Cast()
.ToList();
foreach (FilterElement fe in feColl) {
feName = fe.Name;
feId = fe.Id.IntegerValue;
info += “ID: ” + feId + ” – Name: ” + feName + “\n”;
SelectionFilterElement filter = (from f in new FilteredElementCollector(doc)
.OfClass(typeof(SelectionFilterElement))
.Cast()
where f.Name == fe.Name select f).First();
ICollection filterIds = filter.GetElementIds();
if(filterIds.Count > 0){
foreach (ElementId id in filterIds) {
Element e = doc.GetElement(id);
FamilyInstance fi = e as FamilyInstance;
FamilySymbol fs = fi.Symbol;
Family fm = fs.Family;
info += fi.Id.ToString() + ” – ” + fm.Name + ” – ” + fi.Name + “\n”;
}
info += “\n”;
filterIds.Clear();
}
}
TaskDialog.Show(“Filter Info…”, info);
}
Many Thanks in advance,
Which line is causing the problem?
From what I can see when I step into the code is that when it gets to the linq statement (and the Filter is empty)
SelectionFilterElement filter = (from f in new FilteredElementCollector(doc)
.OfClass(typeof(SelectionFilterElement))
.Cast()
where f.Name == fe.Name select f).First();
it jumps right past everything else to the TaskDialog which in turn fails saying that there no elements in the sequence. Not sure why this happens, but I’m quite new to linq. I tried filtering if SelectionFilterElement.IsEmpty but this didn’t work either.
I did delete the empty Filters in the document and it worked fine, would a Try/Catch be better here where if the Try fails I can just skip this part and proceed as if it’s empty?
cheers,
Hi Harry,
I figured out what the issue was.
I was going through all FilterElements including ParameterFilterElements and this was where it was failing. So I enumerated through the SelectionFilteredElements only and this worked, then I enumerated the ParameterFilteredElements separately so I could apply the FilterRules for each Filter. Just need to filter out all the extra unnecessary elements that are not actual model elements! ; )
public void GetAllElemsInFilters()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
string info = “User Selected Filters:” + “\n” + “\n”;
IEnumerable sfeEnum = new FilteredElementCollector(doc)
.OfClass(typeof(SelectionFilterElement))
.Cast();
foreach (SelectionFilterElement sfe in sfeEnum) {
info += “Id: ” + sfe.Id.ToString() + ” Name: ” + sfe.Name + “\n”;
IEnumerable eIds = sfe.GetElementIds();
foreach (ElementId eId in eIds) {
Element e = doc.GetElement(eId);
info += “—– ID: ” + e.Id.ToString() + ” Name: ” + e.Name + ” —–” + “\n”;
}
}
info += “\n” + “Parameter Controlled Filters:” + “\n” + “\n”;
IEnumerable pfeEnum = new FilteredElementCollector(doc)
.OfClass(typeof(ParameterFilterElement))
.Cast();
foreach (ParameterFilterElement pfe in pfeEnum) {
info += “Id: ” + pfe.Id.ToString() + ” Name: ” + pfe.Name + “\n”;
IList fRule = pfe.GetRules().ToList();
ElementParameterFilter paramFilter = new ElementParameterFilter(fRule,false);
FilteredElementCollector fColl = new FilteredElementCollector(doc);
IList elemList = fColl.WherePasses(paramFilter).ToElements();
if(elemList.Count > 0){
foreach (Element e in elemList) {
info += “—– ID: ” + e.Id.ToString() + ” Name: ” + e.Name + ” —–” + “\n”;
}
}
else{
info += “—–No Elements In Filter—–” + “\n”;
}
}
TaskDialog.Show(“Test”, info);
}
Cheers,