LearnRevitAPI

Aug 15, 2023

Save all Families from Project

Often time we want to save our loaded families from Revit projects. Doing it manually is not only boring, but might become time consuming, so let's look at a better approach to save our time with Revit API.

💡 Let's learn how to save all loaded families outside the project.

Output Folder

First of all let's prepare the main folder where each family will be saved.

I like to use folder where my script is located (PATH_SCRIPT), and then we can create folders: "Families", "Revit Year" and "Project's name" to sort it properly.

🫳 Get All Families

Next, let's get all our loaded families in the project. We can use  .OfClass(Family) to get all families. Also we need to filter out editable families which can be saved outside and not native Revit families. 

📁 Prepare filepaths for families

Once we got our families, we need to prepare filepaths for each family. We will use PATH_FAMILY, which we defined earlier, and then we will add a folder for its Category and the family name itself.

📁 os.makedirs

Once we have the filepaths, we need to make sure that these folders exists before we try to save a family there. For that we are going to use os.path.exists to check if folder has already been created and os.makedirs if we need to create one.

⬇️ Open and Save Family

We have prepared everything we needed. The last step is to open each family and save it to the path we prepared. For that we are going to use EditFamily and SaveAs methods

💡Also don't forget to close your families to avoid a mess in Revit.

👇 Here is the code Snippet for that

📂 Open Final Location

Lastly, I like to open the folder where all families are saved, so users don't have to endlessly scroll through folders looking for it. We can use os.startfile for that. 

💡Just make sure to keep it outside of the loop so users won't end up with hundreds of open folders.

⚠️NB! Possible Warnings ⚠️

While using this code you will also encounter warnings related to Families. It will pause your script execution until warnings are addressed. It is still quite fast to manually sit and click on each warning, but it's not ideal!

I am going to show you how to suppress warnings in a separate newsletter. For now I hope it won't cause you too much trouble to address them manually.

👀 Final Code

Enjoyed Newsletter?

Leave a Testimonial Here.

⌨️ Happy Coding!
Erik Frits

Join Newsletter

📩 You will be added to Revit API Newsletter

Join Us!

which is already read by 6700+ people!