LearnRevitAPI

Feb 2, 2024

How to Automate Sections?

Creating sections in Revit is boring, and adjusting them to see only the right element is even worse.

So, I want to show you how to automate section creation with the Revit API, and I will try to break it down as simply as I can.

💡 You can see Full Video Tutorial on YouTube!

💡 I will use Windows in my Example.

How to create Sections in Revit?

Let's start with the basics, How do we create a Section?
To create a ViewSection we need to use CreateSection method.

It takes 3 arguments that you see in the screenshot 👆

First 2 are not an issue, but the 3rd one will involve multiple steps to address the position and rotation of our elements. 

🧠What do we need?

🧠 Let's brainstorm before we start looking at the code.

1️⃣ Firstly, we need to get Windows of each Type
2️⃣ Then Get Window Origin Point for Location
3️⃣ Create Vector to follow the wall's direction
4️⃣ Get Window Sizes 
5️⃣ Prepare Transform (Location + XYZ Vectors)
6️⃣ Create SectionBox
7️⃣ Create Section!

💡 There will be a few minor additional steps like naming views and transactions, but overall these are the main steps.

Let's begin.

1️⃣ Get and Sort Windows

First of all, we need some elements to create sections for. 

I will be working with windows, but you will be able to apply the same logic to other elements, as long as you understand the concept. 

There are different ways to get a single window of each type. However, I decided to iterate through all the windows and combine their Family and Type Names to use as a key in a dictionary.


💡 I also filter out windows that are hosted only in walls!


2️⃣ Window Origin / 3️⃣Wall Vector


Then we can start iterating through all the windows and begin obtaining the Origin and Vector. 

The Origin is simple - just use the Window.Location.Point. 

For the Vector, we need to create a vector with the same direction as the wall where the window is hosted.
It's fairly simple; we need to get the curve and then we can create a vector from its Start and End points, as shown in the snippet.

4️⃣Window Sizes

Now, let's get the sizes of our windows so we can create a matching section area. This will depend on the families that you are using. 

In my case, I use the German Library from Autodesk, and there are two parameters used:

- Regular Height (Type, Built-In)
- Shared 'Hohe ab FBOK' (Type, Shared

💡 In your case, make sure to adjust to your families!

5️⃣Prepare Transform

So far, everything has been relatively straightforward. Now, we need to flex our brain muscle a bit, especially if you've never worked with vectors and the Transform class. 

First, it's essential to understand the Transform class. This object holds information about location and rotation with XYZ vectors and is used to move and rotate objects in space. 

We can already create a BoundingBox with the correct size of a window, but we need to ensure it's placed where our window is located and rotate it to match its direction.

Additionally, to create a Transform instance, we need to use the Identity property. As I understand, this is a common design pattern in 3D software for Transforms, therefore it's not a constructor or method.

A few notes:

💡 Normalize a vector means we adjust its length to a single unit. This is necessary during calculations when scale is not important.

Setting .Origin is straightforward; it takes a single XYZ point.

Then, consider the Vectors. Here, the approach will depend on how you want to rotate your section. For instance, we will have to change these values depending on whether we want an Elevation, CrossSection, or Plan view.

I'll explain using the Elevation example:

  • BasisX - This is the vector we created along the hosted Wall.

  • BasisY- That's just a vertical vector (BasisZ).

  • BasisZ- Here, we calculate the CrossProduct between the first two vectors.

💡CrossProduct - This calculates a new vector, which is perpendicular to the first two vectors. However, it will only work if they are also perpendicular to each other.

Think of the Transform Gizmo👇.

6️⃣Create SectionBox

Creating a BoundingBoxXYZ is relatively straightforward. It has a constructor that requires no arguments, which returns a box with dimensions Min(-100,-100,-100) and Max(100,100,100).

For those who have never worked with bounding boxes, they are cubes defined by two opposite points (Min/Max).

Since the default origin is placed at (0,0,0), we need to create a box using positive values to move right and negative values to move left. The same principle applies for moving forwards/backwards and up/down.

Once you create a BoundingBoxXYZ with the correct size and offset, the next step is to apply the transformation that was defined earlier. It's just a Transform property, so assign our transformation we made earlier.

7️⃣Create ViewSection

Now we are almost finished!

We've created our SectionBox argument, and the only thing left is to obtain the ViewSection TypeId.

For this, we can use the GetDefaultElementTypeId method, which takes the ElementTypeGroup enumeration. Within this enumeration, there is an option - ViewTypeSection.

✨Final Code

So, we've covered all the steps, and now it's time to put everything together.

The final code will be available on my LearnRevitAPI blog in a few days. Sorry, it's a big lesson, so I need some time to write it out completely.

🎦 Follow Tutorial


Meanwhile, it's best if you follow my tutorial on YouTube.

It's a bit long, but there is a reason why it's 30 minutes long...

I wanted to explain certain concepts in more depth so that you don't just blindly copy my code, but actually understand every step that I wrote and apply it to other elements.

This will enable you to automate your sections across different categories!

So, I hope you will enjoy the video!

Enjoyed Newsletter?

Leave a Testimonial Here.

⌨️ Happy Coding!
Erik Frits

Join Newsletter

📩 You will be added to Revit API Newsletter

Join Us!

which is already read by 6700+ people!