CLI open a revit project

simple proof of concept, I ran it on a single file

here is the code to count stuffs and write it to a csv then open it

from pyrevit import revit, DB, forms, HOST_APP
import os
import csv

uidoc = HOST_APP.uiapp.OpenAndActivateDocument(__models__[0])
doc = uidoc.Document

wall_count = DB.FilteredElementCollector(doc).OfCategory(DB.BuiltInCategory.OST_Walls).WhereElementIsNotElementType().GetElementCount()

# export wall count to csv in same folder
csv_path = os.path.join(os.path.dirname(__file__), 'wall_count.csv')
with open(csv_path, 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['wall_count', wall_count])
    print('Wall count written to csv: {}'.format(csv_path))
    # open the csv file
    os.startfile(csv_path)

don’t try and use the revit UI or forms, or any kind of taskdialog unless you allow them in the CLI command

pyrevit run "C:\test.py" "H:\whateverFileName.rvt"

3 Likes