How can I get the equivalent behaviour for the user Interface Select all instances in entire project?

How can I get the equivalent behaviour for the user Interface Select all instances in entire project?

Anonymous
Not applicable
390 Views
3 Replies
Message 1 of 4

How can I get the equivalent behaviour for the user Interface Select all instances in entire project?

Anonymous
Not applicable

How can I get the equivalent behaviour of the Revit User Interface:

right click on an element then "Select all instances",  then "in entire project"

in the Revit API?

 

Of course this behaviour is independent of the the class of the element. For instance it could be a Wall Element (HostObject), a Door (FamilyInstance).

 

Select All Instances in Entire Project.jpg

 

Many thanks in advance!!

 

Kind regards,

Alexandros

0 Likes
391 Views
3 Replies
Replies (3)
Message 2 of 4

kevinaugustino
Contributor
Contributor

If you have an ElementType, I'm pretty sure you can just filter on BuiltInParameter.ELEM_TYPE_PARAM.

 

public static FilteredElementCollector GetInstancesFromCurrentSelection(UIDocument uiDoc)
{
    Document doc = uiDoc.Document;

    ICollection<ElementId> selectedIds = uiDoc.Selection.GetElementIds();
    if (!selectedIds.Any())
        throw new InvalidOperationException("No elements selected");

    Element instance = doc.GetElement(selectedIds.First());
    ElementType type = doc.GetElement(instance.GetTypeId()) as ElementType;

    return GetInstances(doc, type);
}

public static FilteredElementCollector GetInstances(Document doc, ElementType type)
{
    ElementId typeParamId = new ElementId(BuiltInParameter.ELEM_TYPE_PARAM);
    FilterRule typeIdRule = ParameterFilterRuleFactory.CreateEqualsRule(typeParamId, type.Id);
    ElementFilter typeFilter = new ElementParameterFilter(typeIdRule);
    FilteredElementCollector collector = new FilteredElementCollector(doc).WhereElementIsNotElementType().WherePasses(typeFilter);
    return collector;
}
0 Likes
Message 3 of 4

Anonymous
Not applicable

As I wrote above, the user Interface behaviour works both FamilyInstance and HostObject classes.

 

How can we also achieve that?

 

0 Likes
Message 4 of 4

BenoitE&A
Collaborator
Collaborator

I would build a List<Category> (constant) and then filter on the chosen category using the classical 

Category _myCat; // Chosen category from your UI
FilteredElementCollector collector = new FilteredElementCollector(_myDoc)
.OfCategory(_myCat);

The use of the List<Category> is to reduce the number of categories shown in your UI.


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
0 Likes