Only Selecting .RFA component files

Hello,

I feel like I’m almost complete with my first script, but I’m having a hard time finding something in the documents. I want to only allow the selection of family components. I can’t find anything similar to: DB.BuiltInCategory.OST_Grids. I can’t find any BuiltInCategory for ‘families’. Am I going about this the wrong way?

Thanks!

Aaron

Family is not a category, it is a class.
A family is ‘ofCategory’ db.builtincategory.ost_something

Can you share your selection method and be specific about what you mean by components: sub elements of a family instance, or just the family 8nstance place in the project alone?

I’m referring to an instance of a placed family. I’m piecing together different pieces of code while I’m learning Python. I have pasted the pertinent portion below. I’m essentially wanting the user to run the script, then select certain families which need to be exported. I don’t want them to be able to selected system elements (like walls, etc).

I started this script based off of the ‘toggle grid’ script from PyChilizer.

class CustomISelectionFilter(ISelectionFilter):
    def __init__(self, cat):
        self.categ = cat
    
    def AllowElement(self, e):
        if e.Category.Id.IntegerValue == int(self.categ):
            return True
        else:
            return False


try:
    while True:
       
        grid = doc.GetElement(revit.uidoc.Selection.PickObject(UI.Selection.ObjectType.Element, CustomISelectionFilter(DB.BuiltInCategory.OST_Grids), 'Pick a Family'))
        
        with revit.Transaction('Family Select'):
            print(grid.Id.IntegerValue)
        
except Exception as e:
    pass

The “pythonic way” to do this is to test if the element has attributes specific to the FamilyInstance class.

class FamilyInstancesSelector(ISelectionFilter):
   def AllowElement(self, e):
        try:
            _ = e.Symbol.Family.Name
            return True
        except AttributeError:
            return False

I didn’t test it, let me know if it doesn’t work.

Since you’re learning python (and programming in general, i guess), let me give you some random tips:

  • the “I” of “ISelectionFilter” stand for “Interface”, that is, something to follow to be able to talk to other objects; when you implement an interface with code you can lose the “I”, since yours is a concrete implementation.
  • the if condition: return True; else: return False is a long and convoluted way to do the same as return condition

Also: please remember to wrap the code in triple backticks (```) for better readability. I’ve done it for you, but keep it in mind for the future. Thanks

2 Likes

Thanks! I was on the right track…I eventually broke the element type into a string and looked for the substring of ‘family’. What you have shown above is obviously better.

I really appreciate everybody’s help.

My personal preference: hasattr

Examples
https://discourse.pyrevitlabs.io/search?q=hasattr
and
https://github.com/search?q=repo%3Aeirannejad%2FpyRevit+hasattr(+path%3A%2F^pyrevitlib\%2Fpyrevit\%2F%2F+&type=code

I almost always follow the EAFP principle: it’s easier to ask for forgivenes than permission, that’s why I suggested the try/except way.

At least in standard python, exceptions are cheap, so as long as the case catched is an… exception (= not so frequent), it is slightly faster than always check for the attribute presence (that is the C# way, LBYL: look before you leap!)

In this specific case, it is difficult to know how frequently the exceprion is raised, it depends on whether the end user understands the tool or not :sweat_smile:

1 Like