Revit-API-08

/

/

/

revit-api/08-chnages-in-revit-api

How make changes with Revit API?

...

Revit-API-08

/

/

/

revit-api/08-chnages-in-revit-api

How make changes with Revit API?

...

Summary

Introduction

Hi everybody. Welcome back. As I mentioned in the previous lesson in this one I'm gonna show you how to make changes to rated API. And this is not that complicated. But most importantly I want you to reassure you that it's very safe for rated API beginners to explore API because you have to keep this in mind. T, you cannot make any changes to your projects unless you use transaction class. Transaction class is like a guardian of your project. It doesn't allow any accidental changes to make any changes. We have to create transaction. We have to start and commit it and all the changes have to be in between. And this is made. So beginners are not afraid to explore re API and that you cannot make any kinda unwanted changes in your projects. You can only make changes that are really intended because you literally have to kind of say, okay, I'm gonna make a change from here to here and here's the code for the change and this is what I'm gonna show you just now.

Opening the Documentation

So we're gonna use the same project and first of all, let's open documentation. So here is a documentation I'm gonna use another year. It's irrelevant and I'm gonna look for transaction class. You can see that transaction context likes objects that guard any change made in REIT model. This is great. Now let's just scroll through. They give you over complicated example. For some reason this is not worth it. Instead we're gonna go directly to constructor. And you can see normally when you create it, you would use this method where you define the document, which is the project that you're working on. If you have five projects open, each one of them gonna have different dock string and obviously you need to provide the correct one. And the string is the name of the change. Actually, let's open the method. It's gonna be much easier. So this refers to the project and this is gonna be the name of the change which is gonna be shown in the Ando menu. We can also read right here, this is the name of transaction. The name will appear in the ando menu once transaction is successfully committed. Right? So this is simple.

The Transaction Class

Now in methods, you pretty much just need two methods. You need to know that you need to start it and you need to towards it, commit it. A rollback, sometimes you might use it, but if you don't use it and for example, you crash, this is what's gonna happen by default, it automatically will roll back. Meaning if you're gonna create transaction, you're gonna start it, commit it. And in between you make you try to make some changes. Let's say that you wanna create five walls, you made four, but somehow you failed on the fifth one. And it's also nothing to be worried about because once you're gonna get any warning or errors, it just means that your transaction is gonna be rolled back, meaning that nothing is gonna be saved. And I'm gonna go back to the state before you started making transaction. Alright? Okay, I think I'm a little overcomplicating it. So let's just go and have a look.

Setting Up the Script

So we're gonna go and work with the same uh, wall and I'm gonna use exactly the same script we used in the previous lesson. I'm just gonna comment out maybe that part right here. I just want to maybe take my built-in parameters and that's gonna be fine. Everything is gonna be same for all the other parameters. So here I have a bunch of parameters and let's some set some values. So to set values to any of the parameters we need to grab the parameter. I'm gonna use the set method, right? So this one is gonna be for the comments. This one is gonna be for the types here I'm gonna write new type comments value. Then here it's gonna be new comments, value. And here we're gonna write, I dunno, 50 whatever, 69. Nice. Alright, we have these values. So I'm gonna write here, set new values.

First of all, let's just try to set it as it is. I told you that nothing is gonna happen because we need to use transaction, but let's just see what kind of error do we get. And first of all, it tells you here that the parameter is read only and this refers to the area parameter. 'cause obviously you cannot just select the wall, go to, I dunno where it is somewhere, find here an area, then click and change it. It's reporting parameter. So we cannot change that one. That's okay. We are gonna get some other one in a moment. But now let's just comment it out. Now let's just try, try it again. Now we're setting comments and type comments and look at that. This is a very common error, nothing to be afraid of. Attempt to modify the model outside of transaction. Outside of transaction. So what do you need? You need transaction. Here is how to do this. Very, very simple.

Creating and Using a Transaction

I'm gonna create transaction to do this. I use transaction class, provide document and some change name here. Then you need T stored and T commit and then call changes here. So you just have to be between start and commit. So I'm gonna take that one and put it right here between start and commit statements. I also like to put here the lock emojis. And again, if you wanna make the emoji you hold window key dot. I'm gonna get this menu right here. Lock if I just spell it correctly and then I can select one of these.

t = new Transaction(doc, "Some Change");
t.Start();  // 🔓

// ... (set parameters here)

t.Commit(); // 🔒

Alright, now let's just see if we are getting it. So I'm gonna select the wall. First of all, you can see here's the comments right now it's EF value. Now if I'm going to click on that one, pick the wall, you see something happened. And then if I'm gonna select right here, it's gonna be new. Uh, let me just make it bigger. Right here it says new comments, value. So we changed the value and the type somewhere here it's gonna be uh, where is it? Type comments here. New type comments. Value. So we changed this parameter.

Important Warning: Parameter Types

Now I wanna show you something that might save you literally a lot of hours. It's so simple but it's so stupid at the same time. Have a look. Sometimes you're gonna have large scripts and you're gonna get values as a string. Then you convert it to integer, maybe flow back and forth and eventually you're gonna set the value. And let's say that in the comments. I wanted to set a value. I dunno value, it's gonna be I something like, I dunno. Let's take the same value like this 15, 69. Now I'm gonna take that one and I'm gonna put it here. In this case it's obvious that I'm doing something wrong because you can see it's a flow. This is a, this is a string parameter. But see what's gonna happen. I'm gonna go to my project, I'm going to run the button and select the wall. Okay, everything went fine, no errors, but then I select it and nothing changed. I got no warnings, no errors and nothing changed. This is really confusing. I literally spent hours once on this because I was just like, I had really big script. I was going back and forth and I thought that something is broken. Turns out that when you're trying to set values to your parameter, obviously you need to provide it in the right type, but for some reason it doesn't give you warnings. If you provide the wrong type, it just ignored it. So if I'm gonna write here number, if I'm gonna write here element id, it's not gonna let me know that I'm doing something wrong. It's just not gonna do anything. So you have to make sure that you provide here correct values. In this case I could set it as a string like this. Then let me just see, I'm just gonna find here free. Now again, everything looks normal but this time it's gonna work 'cause this time I'm providing text to a text parameter.

Example: Changing Base Offset

If I would work with numbers, let's say, I dunno, base offset. I also gonna change to metric because I have no idea what's going on. Uh, so it's somewhere here. I think it's, it was length. Gonna set it to centimeters. Yeah, now it's in centimeters. Now we are gonna go and we're gonna get the built-in parameter. So, so I'm gonna call it p wall base equals element. Uh, get parameter, build the in parameter. I have no idea what's the name of it. So we're gonna have a look. Select it. Go to add-ins, Revit lookup snoop selection. Go to parameters, find your parameter. But actually we need not the base constraint but base offset. This is important. And here we can see it takes wall base offset or if you're gonna go inside here it is wall base offset. That's what we need. So let's go and write it here I'm gonna write B uh, base offset equals elements get parameter built in parameter. And it was, was it base offset wall base offset, wall base offset. Okay, we got the parameter. Now let's just set the, I dunno we can use the same. This value, that's okay. Now I'm gonna comment it out. These two, I could also leave. It doesn't matter. I just want you to focus on the numeric value for now.

// Example snippet for base offset
Parameter p_base_offset = elem.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);

t.Start();  // 🔓
p_base_offset.Set(69.0);
t.Commit(); // 🔒

Now let's go and test it. So in parid hacker parameter basics, we click on here. Well it changed obviously, well obviously it changed the geometer of the wall, which I did not think about. But let's say, okay, unjoin elements. Yeah, I pretty much destroyed the wall. So let's just go, go back and set the value a little bit less. 1 69 is fine. I'm gonna click on it. Click here, you can see jumped. That's okay. And join, our wall is here and I set this value. But notice it says 51, 51. This is strange, right? I'm gonna go to add-ins. Nope, selection. We're gonna look again in parameters. We're gonna look for the base offset and look at that. Here the value is correct, but here it's incorrect. Do you know what's the difference? I'll just give you a moment just to think about why does it, why does it say 51 here and 1 69 here? Here's why this value is in fit. This value is in centimeters. So as I mentioned, Ravit uses fit as internal units. So if you wanna set this one as centimeters, we need to first convert it and then put it in here as fit actually otherwise we take the value in centimeters, we convert it into fit value and then that's the value that we are gonna provide here in set method.

Working with Units

So let's just go to code. I think I made confusing. So this value is gonna be in centimeters and now we need value in feet again, we're gonna use unit U tools. Convert this case too internal 'cause we want to take this value centimeters and we want to convert it into unit type id. Um, we're gonna take centimeters. So in this case what's happening is that you provide this value and you say here, this value is in centimeters and it'll convert it to internal units and now we're gonna provide it here. Now the moment of truth, let's go and test it. Pirate, hacker, this button, this wall, something happened. I selected. Now I can see here is the value in centimeter that I actually wanted. 1 69, right? Nice. Uh, so if I'm gonna select it, go to add-ins in selection. Just wanna show you once again, uh, parameters base offset. This time here is gonna be some weird value, but you don't care. 'cause if you work in metric, this is what you wanna see. This is what you got. Everything works. What's happening here is just internal values. Just keep in mind that you have to convert your values. And in general, I also wanna show you that when you work with parameters, especially in the beginning, you'll constantly jump between this menu and your code back and forth. You can open it. Okay, comments Uhhuh, here is the value. This is what I'm supposed to be getting. I'm not getting this. You go back and forth. You can check here, go back. Also use chat GPT, it's really useful.

double value_cm = 1.69;
double value_ft = UnitUtils.ConvertToInternalUnits(value_cm, UnitTypeId.Centimeters);

t.Start();  // 🔓
p_base_offset.Set(value_ft);
t.Commit(); // 🔒

Performance Considerations

And that's the basics about how to make changes in Revit api. All you need to know is that if you wanna make any changes, you create transaction, you start, you commit it. And in between you write all the code that make Changes. If few knows that, I wanna tell you do not put it inside of uh, loops. Let's say for I in range of a hundred, you create a wall, don't make transaction inside. 'cause this is gonna drastically reduce the performance of your uh, script because every time you commit transaction it have to update the uh, database of Revit. And on large projects, as you know, if you worked on like 500 megabytes or even a gigabyte, then you know how slow it is. You just make change across 10 walls and then you can sit there for like literally 30 seconds. And this is when transaction commit happens. You each have to wait the same time just to update the database. However, if you're gonna put it outside, you can make all the changes you want and then you will just update database once. That's gonna save you a lot of time.

Conclusion

Alright, this concept is done and I'm waiting for you in the next lesson where we're gonna look at another exciting co concept that you really need to know. I wish you happy coding. Oh, and by the way, before going to the next lesson, don't forget to practice. Just grab some elements, try to set some values, and just practice, practice, practice. This is really important if you wanna learn REIT API. Now again, happy coding. See you soon. Bye.

HomeWork

⌨️ Happy Coding!

Questions:

What should I write in __init__.py file?

What should I write in __init__.py file?

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API