Resources
NB! There are 3 more videos on the bottom
I've left you 3 code snippets for your homework and you can also watch short videos with solutions.
Summary
Translate C# to Python
You've seen the basic differences between C# and python, and I hope that you agree that it's not so different afterall.
And in this lesson we will go through various examples and try them out ourselves. The goal is to show you that it's not that complicated to take C# snippets and translate them to Python.
So let's open RevitAPI Documentation and go through a few examples
Example 1 - FEC.OfClass
Let's begin with one the most used classes - FIlteredElementCollector.
This class is used for getting elements from our projects and applying various filters. Don't worry, we will cover it in great detail in its own module. For now just focus on translating C# to Python.
We will look at one of its most used methods - OfClass because it's very simple.
Here is the C# Example:
In general, we just need to get rid of a bunch of C# stuff like
Type Hinting
'new' keyword
; semi-colons
Comments
And then it will be a python code.
Let's also print the results to see what we get.
How to Read Results
When you print your elements you might see a confusing message. Just ignore the beginning, it's about memory alocation that is used by python engine.
You just interested in:
What is the Type of Element (Autodesk.Revit.DB.Level)
What is the Value (view.Name in this case)
Example 2 - Wall.Create
Next let's have a look at Wall.Create() method and have a look at the structure inside of Revit API Docs and how to translate C# to python.
If you are going to look at Wall.Create methods, you will notice that there are multiple methods available. You can read their descriptions to understand the difference, but I will take the simplest one, that creates a wall using default wall style.
Once you select something in Revit API Docs, you will see more information about it on the right.
On the top you will see where is it coming from and a general description of selected Class/Method/Property…
Then, most importantly you will see Syntax example.
Syntax will be provided in multiple languages, but not in python. Unfortunately python is not officially supported by Autodesk, but that shouldn't discourage you from using it. It's very simple to read it, let me explain.
In case of Wall.Create method, parameters are quite self-explanatory. But if you need more information about them, you can scroll down to Parameters section to get a little description about each of them.
You will also see what kind of value is being returned.
How to use Syntax?
To use a method we usually just need to find C# Syntax Snippet, so we know what arguments needs to be provided and what is being returned.
As I mentioned, turning C# snippet into python is not hard. In this case we just need to get rid of type hinting and use Class name to call this method.
👇 You can see an example below how to turn this example into python.
C# Method Example
Now, let's have a look at the code example provided for this method on how to use it. It's very simple and again, we just need to get rid of all the same things as before.
💡 You can ask ChatGPT or similar services to translate C# to python and provide snippet that you found. In most cases it does a decent job with translation.
But here is the screenshot of all the things we need to get rid of to turn this C# into Python manually.
Here is the final Python Code:
How to test this snippet
We can also test the snippet, to make sure it works. Since this is a function we just need to gather all arguments and provide it to the function.
Also we will have to use Transaction, but more on that in a moment…
To use this python function we need to get doc and level variables and create a Transaction, so we can make changes to the active document.
💡 Transactions are used to make changes in Revit projects. We create an instance of a transaction and then .Start() and Commit(), and all the changes have to be in between these statements.
Transactions are like guardians that make sure no unintentional changes are made. It's great for beginners, because you know you can't change anything unless you use it. There also going to be a module about Transactions to go in depth.
💡 doc.GetDefaultElementTypeId - This method is great to get default type id of various elements. It takes one argument ElementTypeGroup, which is Enumaration.
Enumeration means that you can select from pre-defined options like WallType, FloorType…
Example #3: Selection
Let's have a look at one more example in Selection class. This is an important class because you will need to use it to get user selection, set new selection or even prompt user to pick some elements.
You will also notice that you can find examples not only for methods, but even for the classes itself. There is a big example, but don't worry, there is a lot of things applicable only to C#, which we can ignore.
I will get rid of a lot of code and just take the code from try statement:
Hopefully, you can already see the common pattern of how to turn it into python.
Here is the translated snippet. I've also adjusted print statements to be more pythonic as you can see.
Alright, enough watching me doing it. It's time for you to practice!
HomeWork
In the first version of this lesson, people said that I didn't provide enough examples, so I will give you more homework with solutions. So you can practice and watch me do it even more.
Try to translate yourself before you watch solutions. This is how you will learn the most.
P.S.
💡You might need to implement a Transaction to change projects!
Place your code between Start and Commit if you want to make changes in the project.
⌨️ Happy Coding!
Task #1: Set Override Graphics
Translate this little function I found on one of the blogs.
It's a simple function that will prompt element selection and change its ProjectionLineColor overrides. Let's compare the snippets, and you will see how little has to change to work in python.
Solution #1
💡It's worth noting that I took doc and uidoc outside of the function, because we would normally have it that way.
Task #2: Create Ceiling
Let's have a look at the 2nd example - Ceiling.Create.
💡This method will be available in Revit API 2022+ only. Funny enough, we couldn't create ceilings before Revit 2022…
By now you know how to understand this syntax.
Element with Ceiling type will be returned
It takes 4 arguments written in orange
You can see respective types of each argument on the left
The snippet might look more complicated, but it just involved more steps to define the geometry of a Ceiling.
It creates 4 points
Use these points for creating Lines
Add this lines to CurveLoop()
Gets default Ceiling Type
Create a Ceiling
Also to use this function, we would need to use Transaction, like this:
Luckily, there is also an example of this method, so we just need to translate it to python to understand how it's used.
Let's compare the snippets:
Solution #2
Task #3: Create Group
Solution #3
More Examples (if you really want it)
If you want to practice even more, you can check these simple examples:
1️⃣ doc.Paint
2️⃣ ViewPlan.Create
3️⃣ View.Duplicate
There are also plenty of example in Revit API Developer's Guide
Conclusion
That was a long lesson… But that's what people has asked me, so here you go.
In general I hope that it helped you to understand how to convert C# to Python.