RevitPythonShell: Object Oriented Programming

<<< Return to Nathan's Revit API Notebook

Introduction

As our programs become more complex, we may want to make our lives easier by creating reusable classes. Welcome to the world of Object Oriented Programming. The examples here are a basic introduction to OOP in the context of the Revit API and RevitPythonShell.

Creating a Simple Class

Classes will change how we structure a RevitPythonShell script from the basic code structure I used in the previous examples.

Defining a Class with Methods

Classes in Python follow a simple structure.

  1. Define the class using class followed by a name
  2. Set some methods within the class using def and a method name
  3. The self variable is a reference to the instance of the class when it is called (more on this later)
#defining a new class called 'MyRevitClass' with two methods
class MyRevitClass:
 
    #first method 'AddSomething'
    def AddSomething(self, num1, num2):
 
    #second method 'NameSomething'
    def NameSomething(self, name):

Adding Properties

After we have defined some methods, we can define properties which we will be able to access.

class MyRevitClass:
 
    #first method 'AddSomething'
    def AddSomething(self, num1, num2):
 
        #'add' property
        self.add = num1+num2
 
    #second method 'NameSomething'
    def NameSomething(self, name):
 
        #'name' property
        self.name = name

Main Function

To keep our code clean, we can establish the 'main' function. While we simply write code after the class has been established, defining the main function allows use to keep our code clean and let other programmers know 'this is where the action is'!

For example:

#MAIN FUNCTION
def main():
    #do something here
 
#the __name__ variable equals __main__ by default
if __name__=="__main__":
        #run main
    main()

Instantiating an Object

Once the class is defined, we can assign an instance of the object to a variable within our main code.

def main():
    #declaring a variable named 'rev' to a new instance of 'MyRevitClass'
    rev = MyRevitClass()

Accessing Methods and Properties

After we have declared the variable with a new object instance, we can access the methods and properties.

def main():
    #declaring a variable named 'rev' to a new instance of 'MyRevitClass'
    rev = MyRevitClass()    
 
    #calling methods
    rev.AddSomething(3,4)
    rev.NameSomething('Mobius')
 
    #calling properties and printing them in the console
    print rev.add
    print rev.name

Summary

If you were to place this within a template script, it would look like this…

  1. Import libraries
  2. Define the class
  3. Execute main code
import clr
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
 
#DEFINE A CLASS ----------------------------------------
class MyRevitClass:
 
    #first method 'AddSomething'
    def AddSomething(self, num1, num2):
        #'add' property
        self.add = num1+num2
 
    #second method 'NameSomething
    def NameSomething(self, name):
        #'name' property
        self.name = name
 
#MAIN CODE HERE ----------------------------------------
 
def main():
    #start a Revit transaction
    t = Transaction(doc, 'Simple class usage.')
 
    t.Start()
 
    #assign instance of MyRevitClass object
    rev = MyRevitClass()
 
    #calling methods
    rev.AddSomething(3,4)
    rev.NameSomething('Mobius')
 
    #calling properties and printing them to the console.
    print rev.add
    print rev.name
 
    t.Commit()
 
if __name__=="__main__":
    main()
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License