0

I am looking for a way to save in a list the instances of my class (ArduraRepresentacion) that have properties and a constructor.

public class ArmaduraRepresentacion
    {
        Document doc;
        View vistaBarra;
        Rebar barraRefuerzo;
        List<CurveElement> listaCurvas;
        List<TextNote> listaTextos;
        XYZ posicion;
        List<Int32> listaCurvasId = new List<int>();
        List<Int32> listaTextosId = new List<int>();

        public ArmaduraRepresentacion(Document doc, View vista, Rebar barra)
        {
            this.doc = doc;            
            this.vistaBarra = vista;
            this.barraRefuerzo = barra;
        }

        public List<Int32> ListaCurvasId
        {
            get { return listaCurvasId; }

            set { this.listaCurvasId = value; }
        }
    }

I read about extensible storage and also about data storage that is provided by the Revit API. One of the limitations that can be observed is the types of data that can be stored.

I'm looking into serialization in binary, Json and in XML. But I have some concerns because I make references to RevitAPI classes and I don't think they are serializable.

My doubts is: The correct approach would be to create an entity for each property of my class and save it in a revit object?

Any help is welcome no matter how small.

I tried to use the Newtonsoft.json library to serialize my class instances but revit crashes, this crash is not a revit problem.

kevinzurro
  • 13
  • 7
  • To serialize and deserialize the Revit `XYZ` class with Json.NET, see [Deserialize Json XYZ Point](https://stackoverflow.com/q/25471890/3744182). – dbc Feb 20 '23 at 19:34
  • That being said, do not use `BinaryFormatter`. It is being [obsoleted and removed](https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/5.0/binaryformatter-serialization-obsolete). And for another reason not to use `BinaryFormatter` see [this answer](https://stackoverflow.com/a/25455393) by Matt to [Deserialize Revit API](https://stackoverflow.com/q/25414843). If you want to go with binary consider [tag:protobuf-net]. – dbc Feb 20 '23 at 19:36
  • This question may be off-topic for Stack Overflow as asked. Firstly, opinion-based question like *What is the best approach I can take to save my class objects?* are generally off-topic. Secondly, you are asking three separate questions but the format for questions on stack overflow is [one question per post](https://meta.stackexchange.com/q/222735). Try to break your post down into discrete answerable questions as defined by the [help center](https://stackoverflow.com/help/on-topic). – dbc Feb 20 '23 at 19:42
  • Thanks @dbc for your help. I'm will go to divide the post into several specific questions. – kevinzurro Feb 20 '23 at 20:44

1 Answers1

0

You say, you have some concerns because of references to Revit API classes and you don't think they are serializable.

Well, true, the classes per se are not serrializable. However, that is probably not your meaning or intention at all. Presumably, if you wish to persist something in the Revit BIM and maintain references to other BIM objects, the referenced objects will be Revit Element instances. All Revit BIM objects are Element instances.

Element instances can be references within the model using their ElementId. Extensible storage provides support for serialising ElementId, and even support for automatically updating references and relationships established using them, cf. Handling of ElementId Data in EStorage.

I would certainly encourage you to use extensible storage, and simply store the member data you require to reconstruct your ArmaduraRepresentacion instance.

Jeremy Tammik
  • 7,333
  • 2
  • 12
  • 17
  • Thanks jeremy for guiding. I re-read all possible posts and found the answer by applying Victor Chekalin's [effortless extensible storage](https://thebuildingcoder.typepad.com/blog/2013/05/effortless-extensible-storage.html#4). – kevinzurro Apr 01 '23 at 02:03
  • A personal question is: The best way to store a Dictionary is in the [ProjectInformation element](https://thebuildingcoder.typepad.com/blog/2012/05/devblog-devcamp-element-and-project-wide-data.html#4) or in an instance of the DataStorage class? This item is not related to any element in the project, I think someone on the development team recommended that you stop using singleton instances. Cheers – kevinzurro Apr 01 '23 at 02:09
  • Definitely in your own `DataStorage` element. Definitely not in `ProjectInfo`. They did not recommend avoiding singleton instances. That is a very valid and useful and often appropriate pattern. They did recommend not using `ProjectInfo`: https://thebuildingcoder.typepad.com/blog/2015/02/extensible-storage-in-a-worksharing-environment.html – Jeremy Tammik Apr 02 '23 at 07:17