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!

Wednesday, April 6, 2011

List Parameters of Current Selection

This is another post about using the RevitPythonShell (RPS) for accessing the Revit API. I assume you are using a variation of the default InitScript, as that provides a handy selection variable.

The following session in the RPS shell assumes you have selected a wall in your Revit model. It then provides a list of the names of all parameters on the walls type:

>>>w

<Autodesk.Revit.DB.Wall object at 0x000000000000002B [Autodesk.Revit.DB.Wall]>
>>>wt = doc.get_Element(w.GetTypeId())

>>>plist = list(wt.Parameters)

>>>pnames = [p.Definition.Name for p in plist]

>>>pnames

['Description', 'DPV:DpvConstruction', 'Fire Rating', 'Structure', 'Assembly Description', 'Type Mark', 'Cost', 'Coarse Scale Fill Color', 'URL', 'Function', 'Width', 'Wrapping at Inserts', 'Assembly Code', 'Type Comments', 'Coarse Scale Fill Pattern', 'Keynote', 'Model', 'Wrapping at Ends', 'Manufacturer']
>>>