LearnRevitAPI
Sep 18, 2024
1️⃣ First Revit Add-in
Many of you think that it's very hard to create custom Revit add-ins... And I want to break that stereotype. Let's create your first tool and get you in a developer's flow. Because once you will create your first tool, you will want to create even more!
Click Here to watch watch Video Tutorial:
Your First pyRevit Button: An Easy Step-by-Step Tutorial
📁 Prepare a new Pushbutton
First, let's prepare a pushbutton, where we will write the code.
📁 Create .pushbuttonCreate a new button inside the Dev.panel by duplicating an existing button.Rename it to FirstButton.pushbuttonInside this pushbutton, you should have:script.pyicon.png
📄 Script.py
For the script.py file, use the code from template.min in pyRevit StarterKit/Templates.
It's very helpful to always start with a template or a copy of an existing tool.
👆Here is my simple Template
💡 Over time, you will adjust this template to your own needs.
Reload pyRevit
Next, Reload pyRevit, to see this new button appear in your toolbar.
💡If you can't see your new button after reloading:Make sure you placed them in the correct folders and have lowercase suffixes without misspellings:
📁CustomNAME.extension
📁CustomNAME.tab
📁Dev.panel
📁FirstButton.pushbutton
📄script.py
🖼️icon.png
Brainstorming
📝Get a piece of paper and a pen before you open your code editor.
We’ll brainstorm a simple tool to rename our views with Find/Replace logic. This will help introduce various Revit API concepts.
First of all, let's break down the idea into smaller, manageable code steps. This is necessary as it helps you think like a programmer and discover any hidden steps to avoid errors.
It doesn't need to be beautiful! It just have to make sense to you.
Here is how I would write the steps for the tool that I've just mentioned to you.While writing the steps, I've also marked with red the potential issue that we need to address (Unique View Names).💡 I tend to rewrite these steps over and over until they make sense. It helps me to brainstorm better solutions.
Now we know the steps, we can start coding.
1. Get Views
First of all we need to select Views.
1️⃣ Try to get the currently selected views in ProjectBrowser
2️⃣ If none selected, we will use pyrevit.forms.select_views. This will create a UI form where user can select multiple views
3️⃣ Lastly, we will check if views were selected or otherwise, we stop execution of the script.
👆Here is the code snippet:
2. Define Rules for Renaming
Next, we need to think about the rules for renaming.
At first, hardcode find/replace values to create a working script, then about a UI form.
💡Make sure it works before making it pretty.
2+. Ask User for Rules
When ready, use this snippet to create a FlexForm with rpw package.
This will create a nice form where a user can quickly provide any values they want for the script. Super useful.
This will create UI form
3. Transaction
Next, we need to start a Transaction.
Transactions are necessary to make any changes in your Revit projects. And that's great news for beginners, because it means that you can't make any accidental changes without Transaction.
To define a Transaction, you should provide a Document and the name of the change, that will be shown in Revit's undo menu.
Then, use Start and Commit methods, and write code that makes changes between these statements.
Here is how to create Transaction.
💡 Don't worry If you make any errors during Transaction, it will just be Rollbacked (cancelled) and nothing will happen to your project.
4. Rename Views
Lastly, we can rename our views.
Creating a new name is very simple, but we have to make sure that it's unique.
The easiest way is to use try/except statement in a loop until we succeed. And if we catch an error, we will keep adding '*' symbol in the end of the name until it's unique.
This will set unique new names to your selected views
🥳And Congratulations!🎉
🔨You've just created a new tool!!
Now you know how to create new tools in pyRevit, and it's just the beginning. Start small, and focus on quantity over quality at first. Over time, you will create so many tools and have so many code examples that creating new tools will get easy.
In my case, I can create new tools where I copy 80% of the code from the previous tools because I have so many examples already.
And so will you.
💪 Keep creating more cool tools and Happy Coding!
Join Newsletter
📩 You will be added to Revit API Newsletter
Join Us!
which is already read by 7400+ people!
◼️BLACK FRIDAY DEAL 20% OFF. Ends in: