import os
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import *
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application
rvt_year = 'RVT_{}'.format(int(app.VersionNumber))
PATH_SCRIPT = os.path.dirname(__file__)
PATH_FAMILY = os.path.join(PATH_SCRIPT,
'Families',
rvt_year,
doc.Title)
all_families = FilteredElementCollector(doc)\
.OfClass(Family)\
.ToElements()
loaded_families = [f for f in all_families if f.IsEditable]
for f in loaded_families:
if f.IsEditable:
cat = f.FamilyCategory.Name
name = f.Name
f_dir = os.path.join(PATH_FAMILY, cat)
f_filepath = os.path.join(f_dir, name) + '.rfa'
if not os.path.exists(f_dir):
os.makedirs(f_dir)
try:
f_doc = doc.EditFamily(f)
if f_doc and f_doc.IsFamilyDocument:
if not os.path.exists(f_filepath):
f_doc.SaveAs(f_filepath)
finally:
f_doc.Close(False)
os.startfile(PATH_FAMILY)