βΌοΈBLACK FRIDAY DEAL 20% OFF. Ends in:
Feb 19, 2024
How to Place Views on Sheets with Revit API?
Stop Dragging Your Views on Sheets in Revit! Automate with Revit API instead! I will show you how to place multiple views on sheets by using pyRevit.
Intro
So, you might be wondering how to automate placing views on sheets in Autodesk Revit?
Sometimes that might be a very time-consuming task. But also, if you are into automation, there is a good chance that you created hundreds of views and now you need to place them on sheet.
That's exactly what we did in the previous lesson - Stop Wasting your Time on Revit Sections. Automate with Revit API!.
I showed you how to create hundreds of Elevations, Cross-Sections and Plans for all unique Window Types in the project.
And now we will continue and place all of them on individual sheets.
π§ Brainstorming
So how are we going to do that?
Let's brainstorm and write out all the steps we will go through in this lesson:
1οΈβ£ Get All Views + Filter
2οΈβ£ Sort Views for Placing on Sheets
3οΈβ£ Iterate and Create New Sheet
4οΈβ£ Place Views on Sheets
5οΈβ£ Handle Errors during placement (with SubTransaction)
6οΈβ£ Rename Sheets
Sounds like a lot of steps, but most of them will be short snippets.
So let's begin writing this code.
1οΈβ£ Get All Views
First of all, we are going to get all views in the project by using FilteredElementCollector, and then we need to filter only the ones that we want to place on sheets.
In my case, it's quite simple. I gave them all a 'py_' prefix when they were created with another script. So now I can easily grab them by checking if they have this prefix.
π‘You might need to adjust this step to get views with your own logic! It depends on your view management.
π You Can Get FilteredElementCollector PDF Guide with explanation and examples if you are new to this.
2οΈβ£ Sort Views based on Window Type
As I've mentioned earlier I am working with hundreds of Window sections.
There are 3 types of sections that I created in the previous lesson:
Window Elevation
Window Cross-Section
Window Plan (Made with Section Too)
And they all have the same view name except for the suffix, which indicates which one of this is it.
It has the following naming pattern:
Here is the Screenshot as well:
So it will be fairly easy to sort my views. But you might need to use different structure and logic, depending on how you organize your views in Revit!
I will use defaultdict with a nested dictionary for that! Keep in mind that this sorting method will only work if you have 3 sections for each of your windows! Otherwise, you need to ensure you have error handling as well.
Here is my desired output example
π And here is the Code Snippet to do that.
π‘ Keep in mind that this step will be different because we probably name and organize our views differently.
π Also don't forget to preview if you get views correctly before going to the next step!
Here is my code snippet since I used dictionaries for collecting my views.
π And here is the Output.
3οΈβ£ Iterate and Create New Sheet
Next, let's iterate through these views and start creating new ViewSheets.
To create a new ViewSheet we can use its Create Method.
It takes 2 arguments:
Document
TitleBlock.Id
We always have doc variable and the easiest way to get default TitleBlock is by using doc.GetDefaultFamilyTypeId
π‘And since we are making a change to the project, we also need to use Transaction.
We need to Start and Commit, and all changes have to be between these statements.
π I will also get my plan, elevation and cross-section from the dictionary so we can use it later on.
β οΈ Keep in mind that we might not want to produce hundreds of sheets while we are in development stage.
So I will add a counter to my iteration and break out of the loop after I iterated 10 times like this:
β Now we can test it and see if we create empty sheets for each iteration.
4οΈβ£ Place Views on Sheets
Now we will place views on sheets.
To place views on sheets we need to create a new instance of a Viewport. You can check its Create method that has the following arguments:
Document
Sheet.Id
View.Id
Location Point (XYZ)
We already have the first 3, so we only need to define Location Points for our views.
To keep it simple, I will hardcode these values for views so they are always placed in the same location.
I've placed views manually on 1 sheet, and then looked in RevitLookup at Viewport.GetBoxCenter values.
And then here is how we can create our Viewports:
π Let's test if we get any errors, and in my case I got an error that some sections couldn't be placed on the sheet. And it's because they are already placed! So we need to add some error handling here!
5οΈβ£ Handle Errors
I will use Viewport.CanAddViewToSheet method to test if view can be placed.
But it need doc, view_sheet_id and view_id arguments, so we need to create our sheet first.
I will use SubTransaction here.
This will allow me to
- Create the sheet
- Check if view can be placed, and Commit SubTransaction if it can.
- Or Rollback and undo creation of the sheet where we can't place any views.
π‘ There are easier ways to handle this error, but I wanted to use SubTransaction here!
Here is the Code:
6οΈβ£ Rename Sheets
The last simple step is to rename our sheets.
It's very simple because we can write new values to SheetNumber and Name properties like this:
β¨Final Code
Lastly, let's put it all together in a single code, so you can easily copy and adjust it to your own needs!
β¨οΈHappy Coding!
Join Newsletter
π© You will be added to Revit API Newsletter
Join Us!
which is already read by 7400+ people!
Ready to become Revit Hero for your office? Learn Revit API!
Join this comprehensive course that will guide you step by step on how to create your dream tools for Revit that save time.