To overwrite parameters via the Revit API, we need to get the parameter, then pass it a value of the type it expects.
If you don't already have it, I would recommend installing Revit Lookup. This tool is always useful, but for what we're doing here, I'd go so far as to say it's virtually essential.
In this example, let's pretend we have a big meeting in Germany, requiring us to change all our room names. We'll start by creating a simple dictionary of the room names we need to change.
| eng_to_de_roomnames = { |
| "Bedroom": "Schlafzimmer", |
| "Living Room": "Wohnzimmer", |
| "Kitchen": "Kueche" |
| } |
Then, we'll collect all the rooms in our document and iterate through them.
(NOTE: When working iterating through rooms, it's always a good idea to check that the room has a location before trying to do anything to it. This is because when a room is deleted from a view, it is not simultaneously deleted from a project, and the script may throw an error if you try to treat it like a regular room)
| doc = DocumentManager.Instance.CurrentDBDocument |
| rooms = Fec(doc).OfCategory(Bic.OST_Rooms).WhereElementIsNotElementType().ToElements() |
| |
| for room in rooms: |
| if room.Location and room.get_BoundingBox(doc.ActiveView) and isinstance(room.Location, LocationPoint): |
We now need to get each room's name parameter. There are a couple of ways to do this. The first is to use LookupParameter() and simply pass in the parameter name ("Name", in our case).
That might work in some cases, but generally speaking, it's not great practice, because we might have multiple parameters with that name, and the script will just take whichever it finds first.
When working with built in parameters, it's always best to use get_Parameter() and pass in the built in name, which is unique. To find the built in name of a parameter, use Revit Lookup.
Click on a room in your project, then click "Snoop Selection". The subsequent pop-up window has a bunch of fascinating info in it, but in our case, we'll jump straight to "Parameters".
Another pop-up window will list all the parameters associated with our element. We'll navigate to "Name", where we see that the built in name for the parameter is "ROOM_NAME".
We also see that it's stored as a string. This will be important later - we can only give a parameter values of the type it expects.
With all that in mind, let's get our name parameter and it's current value. We'll also check if the the current room name shows up in our eng_to_de_roomnames dictionary, and if it does, we'll get the corresponding German name.
| |
| name_parameter = room.get_Parameter(BuiltInParameter.ROOM_NAME) |
| name_parameter_value = name_parameter.AsString() |
| |
| if name_parameter_value in eng_to_de_roomnames: |
| target_name = eng_to_de_roomnames[name_parameter_value] |
We now have all we need to overwrite our parameter. We'll use the Set() method.
| |
| TransactionManager.Instance.EnsureInTransaction(doc) |
| name_parameter.Set(target_name) |
| TransactionManager.Instance.TransactionTaskDone() |
And that's it! Our German clients should now be able to read our plans.
Comments
Post a Comment