Saving and loading script data

I’m trying to create a button to save my favorite views in the project with Shift-click and open them with a regular click. I’m struggling to use get_document_data_file though. Does anyone have any code examples for writing and reading data using this tool?

I would rather store document specific data in the document itself, preferrably in ExtensibleStorage

1 Like

Thank you, I didn’t even realize that was a thing! So would you add a boolean schema set to True for the favorited views? And then just search all the views for that schema when you want to open the favorites? I’m worried that will reduce the responsiveness of the primary command. I guess I’d prefer to save the favorite views as a string of element ids, but I’m not sure what element I’d save it on.

I’ve done something similar and stored the data with Extensible Storage using a schema that contains an array field of ElementId type. This data can then be saved as an entity to a DataStorage Element. That data can be recalled and overwritten to load and save views.

The extensible storage process is a little complex and convoluted in my opinion, but it works well when you get it working.

Yeah, that is one way of doing it, however my solution does it using a DataStorage element that has a simple string field in the ExtensibleStorage schema, where I store the list of ElementId’s as a json array. Using a datastorage object is also better in workshared environment, because you can’t edit the schema of elements borrowed by someone else. The best way is to use multiple DataStorage elements per different users.

I’ve created a free addin for this as an exercise: Favourite Views | Revit | Autodesk App Store
It is slower (the json string method) in theory, but never head performance issues with it yet.

Ok so this may be a bit of a novice question, but schemas are basically hidden parameters (or rather, one schema can contain many sets of hidden variables), right? So what Guid do you provide when setting up the schema? Do I just make a random one and use it for anything else in my toolbar as well? How do I know it’s not already taken?

Yes they are hardcoded sets of some specific hidden parameters, with much less options than normal parameters. Their main problem is once you create the schema you can’t edit it, thats why I use a string field, with json values, it is much more flexible, but slightly slower.
Regarding the guid, yes, you have to make up a random one, you have almost no chance making up something that is already used by someone else, especially in your session.

Thanks for the advice! Only now is it occurring to me that schemas and extensible storage may not be the best option since I’ll want different users to have their own list of favorites. I suppose I could save them in a JSON format as you have suggested, but that’s a bit outside my wheelhouse. I was able to find some examples of the pyRevit get_document_data_file by searching the pyRevit GitHub though. I’ll try to remember to update here if/when I find the time to build this.

Some great points thumDer. As an alternative to json, I like using pickle to dump data to a string.

1 Like