Thursday, April 14, 2011

Listing Window Dimensions with RevitPythonShell

I wrote this quick script this morning and thought I should share:

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

doc = __revit__.ActiveUIDocument.Document

import revitutil

def main():
    windows = revitutil.listWindows(doc)

    for w in windows:
        instance_width = revitutil.meters(w.get_Parameter(BuiltInParameter.WINDOW_WIDTH).AsDouble())
        instance_height = revitutil.meters(w.get_Parameter(BuiltInParameter.WINDOW_HEIGHT).AsDouble())

        wt = doc.get_Element(w.GetTypeId())

        type_width = revitutil.meters(wt.get_Parameter(BuiltInParameter.WINDOW_WIDTH).AsDouble())
        type_height = revitutil.meters(wt.get_Parameter(BuiltInParameter.WINDOW_HEIGHT).AsDouble())

        rough_width = revitutil.meters(wt.get_Parameter(BuiltInParameter.FAMILY_ROUGH_WIDTH_PARAM).AsDouble())
        rough_height = revitutil.meters(wt.get_Parameter(BuiltInParameter.FAMILY_ROUGH_WIDTH_PARAM).AsDouble())

        wid = w.Id.IntegerValue
        wname = w.Name

        print "%(wid)d\t%(wname)s\t%(instance_width).2f\t%(instance_height).2f\t%(type_width)s\t%(type_height).2f\t%(rough_width).2f\t%(rough_height).2f" % locals()


if __name__ == '__main__':
    import traceback
    try:
        main()
    except:
        traceback.print_exc()

It doesn't do much more than you could do with a window schedule in the Revit GUI, except that I couldn't figure out how to distinguish instance width and type width in window schedules. As I was trying to debug some code that made that distinction, I needed some goggles! Out pops RPS to the rescue ;)

One thing I'd like to point out is the structure of the script. I tend to write my canned commands in RPS following this structure:

  • import stuff and create a doc variable (I normally just copy the contens of the InitScript for this)
  • define a main() function that handles the code
  • use the if __name__ == '__main__' idiom to wrap calling the main function in a try block

The reason for using the try block is traceback.print_exc() which prints a nice error message to the console that helps debugging!

No comments:

Post a Comment