Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to filter a specific element?

7 REPLIES 7
Reply
Message 1 of 8
ferreirasou_gabriell
525 Views, 7 Replies

how to filter a specific element?

I would like, for learning purposes, to know how I can select a specific element type.

For example:

Coletor FilteredElementCollector       = novo  FilteredElementCollector ( doc ); 
     coletor.OfCategory ( BuiltInCategory .OST_Doors );     coletor.OfClass ( typeof ( FamilySymbol ) );

This command returns me all types of doors that belong to the OST_Doors category. I want to know if there is a way for me to select a specific port within that category.

7 REPLIES 7
Message 2 of 8

Hi

 

If you want only one element, just get the element by id. If you want to filter by value, then you can do it with a simple loop.

Message 3 of 8

Hi @ferreirasou_gabriell,

 

You can try this sample code below. I have not tried it myself, but I think this is what you are looking for

 

 

FilteredElementCollector coletor = new FilteredElementCollector(doc);
coletor.OfClass(typeof(FamilySymbol));
coletor.OfCategory(BuiltInCategory.OST_Doors);
doors = coletor.ToElements();
if (0 == doors.Count)
{
    throw new InvalidOperationException("There is no doors of the family.");
}

foreach (Element element in doors)
{
    //write your code here for the element
}

 

  Moturi George,     Developer Advocacy and Support,  ADN Open
Message 4 of 8

I'm not sure if I understand your code, but I want one that returns a specific type of element, for example: "Generic - 90mm Brick" which is a type of element within the OST_Walls category. I want to pergar this one to create her or something like that.

Message 5 of 8

ok that is not the initial question 🙂

 

if you want to filter by type of wall, first get the collector for wall instance (make sure there is no ElementType, but only instance). Then filter with a loop for wall with the same name string as the one you like.

Message 6 of 8
Gabriel.7taios
in reply to: Yien_Chao

Sorry, I think I expressed myself badly then. How would the code look? I'm noob in revit API, as in programming.

I was basically thinking about this logic, but I still don't know how to apply it

 

 

 

Message 7 of 8

Hi @ferreirasou_gabriell 

 

I am not sure I understand your question but if I did, you can try any of the examples below:

 

Example 1

 

View myActiveView = doc.ActiveView;
FilteredElementCollector wallfilter = new FilteredElementCollector(doc, myActiveView.Id);
List<Element> swwalls = wallfilter.OfCategory(BuiltInCategory.OST_Walls).ToList();
var elements = from element in wallfilter
    where element.Name.Contains("Generic - 90mm Brick")
    select element;
foreach (var element in elements)
{
	TaskDialog.Show("Elements",element.Name);
}

 

 

 

Example 2

 

View myActiveView = doc.ActiveView;
FilteredElementCollector coletor = new FilteredElementCollector(doc, myActiveView.Id);
coletor.OfCategory(BuiltInCategory.OST_Walls);
var walls = coletor.ToElements();
if (0 == walls.Count)
{
    throw new InvalidOperationException("There is no walls.");
}

foreach (Element element in walls)
{
    //write your code here for the element
    if(element.Name.Contains("Generic - 90mm Brick")){
    	TaskDialog.Show("Element", element.Name);
    }
}

 

 

  Moturi George,     Developer Advocacy and Support,  ADN Open
Message 8 of 8

Use the Of category filter to select a specific family category, and .WhereElementIsElementType() to search among types "Sympols" only, finally the  .Where(e=> e.Name.ToLower().Contains("0915")) to filter by name and replace "0915" by your desired name in lower case. @ferreirasou_gabriell 
 

  FamilySymbol FilterDoors(Document doc)
        {
            var myDoorsCollector = new FilteredElementCollector(doc)
                                   .OfCategory(BuiltInCategory.OST_Doors)
                                   .WhereElementIsElementType()
                                   .Where(e=> e.Name.ToLower().Contains("0915"))
                                   .First() as FamilySymbol;
            return myDoorsCollector;
        }

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


Autodesk Design & Make Report