How to know the syntax of Revit API

Hey everyone, I’ve been learning Revit API for a couple of weeks now and I’m having some trouble with the syntax. The thing is, Revit API is composed of C#, Visual Basic, and Visual C++, but I’m only familiar with Python. It’s been challenging for me to figure out how to convert the syntax to Python. Do you have any tips or experiences that you can share with me to make it easier to learn? I would really appreciate your help. Thank you.

Hi @Luffy11,
Sorry for the late response.
I’m trying to put up a “Getting started” thread in which to put information like this, but it’s taking me longer that expected…
In the meantime, from the top of my head:

  • the usings at the top of the file equates to the imports in python; for those you also need to prepend import clr and then clr.AddReference(assembly_name) - you need to know where those usings come from; fortunately pyRevit already loads the revit assemblies, so you just need to from pyrevit.revit import DB, for example. For other libraries, chances are that there’s a python library that does the same thing, but you’ll have to translate the calls tocthat library accordingly.
  • you can ignore the namespace statement
  • curly brackets in c# are like the indentation in python (in fact, one thing I hate in c# is that the indentation is optional and can lead to hard tovread code)
  • drop the semicolons! If there are multiple instruction in one line, simply put them in separate lines (just a style suggestion, semicolons are valid in python, but rarely used)
  • static methods within static classes are just function, so you can write them at the “top level” of your script
  • private, public, internal keywords determine the ability to access the class/method/variable from the outside; we don’t do that in python (underscores at the beginning of the name, is just a convention, a sign that you shouldnt use that thing outsite its module)
  • variables declared in c# are preceeded by their type (or the var keyword), in python you should ignore the type (well, in python 3.7+ you can annotate the types, but that’s another story) and just write variable = value
  • in c# a class instance must be created with the new keyword, in python you just write my_instance = MyClass()
  • the foreach (var item in myList) loop translates to for item in lmy_ist:
  • the for (var i = 0; i < myList.Count; i++) can be written as for i, item in enumerate(my_list): with the added bonus that you already have the item and dont need to read it with my_lisi[i]
  • LINQ statement can be rewritten as list comprehensions/generators, and/or leveraging the itertools module
  • the using keyword for disposables doesn’t exist in python, but can be emulated via context managers (an example is pyRevit’s transaction class that handles the start and commit/rollback by itself)

That’s it for now, if you have trouble with specific code, fell free to post it here and we’ll try to help you translating it