What programming language should you choose? For Revit

, , , ,
Programming Languages Logos

A common question that I find myself answering often is: what should I learn to automate tasks and create my own Revit tools?

Usually, my answer goes on and on for at least 15 minutes, and if the lucky listener is still interested afterward, I would jump to the second part of my answer sharing personal experiences. Today, lucky reader, I am giving you the answer in the shortest time possible.

Among the many options available, I believe that the most accessible and suitable for someone who has never programmed before is Dynamo. Dynamo is a visual programming tool, meaning that instead of writing code in a text editor, we are connecting elements visually. And this is the key element of Dynamo, it is very intuitive and the learning curve is not steep.

For example, if we want to create a plan view in Revit, we only have to use two nodes while with more advanced options, it can get a bit more complicated:

After mastered Dynamo, something that you probably desire in Dynamo is the ability to condense code or use loops. This is the moment when you know you have to jump to the next step; choosing a textual programming language. To me, Python is just perfect. It is a language that can be used in so many other endeavors; automating tasks in Windows, databases, web scraping, data analysis, machine learning and so on.

There is another option at this stage, DesignScript, a language specifically developed for Dynamo. I personally don’t consider it a good option when you have Python. It all boils down to pragmatism, check the number of projects and jobs for Python and check the ones for DesignScript and you will get a clear picture.

Let’s check the code for creating view plans in Python:

Yes, I know what you are thinking, the code is much longer and complex than connecting those two nodes in Dynamo! Well, it is, no arguments. But bright side, when your code and tasks grow, this one has no dependencies and can be integrated into pyRevit.

As the last step, when you start considering performance and creating proprietary code, that is the moment when you start thinking about the .NET platform. And the beautiful part of .NET is that allows you to develop in any language compatible, IronPython, Ruby, Visual Basic, C++, C# among others.

C# is the choice here without much discussion. I would rather pick a language that is backed up by Microsoft, than any of the other ones that are entering in the legacy territory. C# is used in databases, web apps, desktop applications and so on. You cannot go wrong picking this one. Here is the code for the same task in C# used in a macro:

public void FloorPlan()
{
    // Store current doc in variable
    Document doc = this.Application.ActiveUIDocument.Document;
    
    // Collect level 1
    ElementId level1Id = new FilteredElementCollector(doc).OfClass(typeof(Level))
                    .Cast<Level>()
                    .FirstOrDefault(l => l.Name == "Level 1").Id;
    
    // Collect ViewFamilyType Id
    ElementId vftId = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType))
                        .Cast<ViewFamilyType>()
                        .FirstOrDefault(vft => vft.ViewFamily == ViewFamily.FloorPlan).Id;
    
    // Create transaction and start
    Transaction t = new Transaction(doc, "Create floor plan");
    t.Start();
    
    // Create floor plan
    ViewPlan vp = ViewPlan.Create(doc, vftId, level1Id);
    
    t.Commit();
        
    // Show result
    TaskDialog.Show("ViewPlan creation", vp.Name + " has been created");
}

Even though it looks shorter than Python, in order to create the tool in the toolbar it takes a bit longer. But it comes with the benefit of being compiled, which avoids code tinkering by users and runs faster.

As a final thought, don’t worry about what language or tool you pick, you will have to learn them at some point, so it is never going to be time lost. Python is really powerful for scripts, testing some ideas or getting stuff done in the least amount of time. C# is the best choice for something robust, deployment office-wide apps or even macros that run much faster than Dynamo or Python scripts.

2 responses to “What programming language should you choose? For Revit”

  1. Andrew Mole Avatar
    Andrew Mole

    Thanks for this article. Good advice.

    You could make the Python noticeably shorter than the C# version by converting the ‘for’ loops into list comprehensions:

    vftId = [vft.Id for vft in viewFamilyTypeColl if vft.ViewFamily == ViewFamily.FloorPlan][0]

    level1 = [f for f in levelsCollector if f.Name == “Level 1”][0]

    I am assuming that these for-loops should only return one answer, or that the first one satisfying the criteria is the best.

    1. BIMicon Avatar

      Absolutely, that is the purpose of my for-loops, the same as in C#. Sorry, I made Python not look so great compared to C# :(.
      Cannot believe it has been already 3 years since I wrote that snippet in Python, time flies.
      If you don’t mind me hijacking your comment, I would add that for efficiency, instead of looping through the entire list as I did, a break statement should have been added when the first element matching the condition was found.
      Also, to avoid the list comprehension running through the entire list, a generator can be used instead:
      next((f for f in levelsCollector if f.Name == “Level 1”), None)

      Thanks for sharing your thoughts Andrew!

Leave a Reply

Your email address will not be published. Required fields are marked *