store data

store data

aclarke
Advocate Advocate
751 Views
2 Replies
Message 1 of 3

store data

aclarke
Advocate
Advocate

This is a two part question:

1) ExtensibleStorage:  What are some examples of what you use them for?  No code examples, I am looking for ideas.

 

2) In the image below, the first listbox contains conduit instance parameters,  I will move over the chosen parameters to the second list box and I want to save the second list for future use, and I want to create multiple saves with different names containing different selections.  Also any saved maybe updated, changed.

I started with the ExtensibleStorage.  But I am not sure if this is the correct usage for ExtensibleStorage because...

  • I want to update the list and resave if needed

So I figure, erase and recreate the schema each time, but I don't understand the GUID, because if I try to make my own outside of a GUID Generator, it doesn't work.  Keeps complaining about hexidecimal characters.  So I don't know how to create a GUID on the fly by code, or if it is even possible, or even a good idea.

 

How would a seasoned coder tackle this?

 

Thanksimage.png

 

0 Likes
Accepted solutions (1)
752 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Alan, 

 

Extensible storage can be used by your add-in to save any data you like.

 

Revit leaves your data pretty much alone, and the user has no access to it.

 

For more info and examples, please refer to The Building Coder topic group:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.23

 

The application you describe sounds like a sensible use of it.

 

What exactly do you want to store?

 

A list of strings? A single long concatenated string? A list of ids of selected objects?

 

Any of those will fit just fine.

 

However, you definitely do not erase and recreaqte the schema every time.

 

In fact, you cannot recreate a schema, ever. A schema is created once only and can NEVER be changed.

   

However, you can change the data you store under that schema as much and often as you like.

 

Please refer to the articles in the topic group for all the nitty-gritty details./

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 3

aclarke
Advocate
Advocate

Hi Jeremy, thanks for your reply

Below is what I came up with to create ExtensibleStorage from a listbox

 

t.Start();

List<string> myList = new List<string>() //<--This list will be fed by a form listbox
{
    "item_1",
    "item_2",
    "item_3",
    "item_4",
    "item_5"
};

foreach (Conduit conduit in myFilter.myConduits(doc))
{
    myXStorage.AddSchemaEntity(myXStorage.mySchema("someName", myList), myList, conduit);
}

t.Commit();

...

public class myXStorage
    {
        //-----------------------------------------------------------------------------------------
        //  Build Schema
        //-----------------------------------------------------------------------------------------

        public static Schema mySchema(string schemaName, List<string> fieldNames)
        {
            Guid schemaGuid = new Guid($"{myRandomHex(8)}-{myRandomHex(4)}-{myRandomHex(4)}-{myRandomHex(4)}-{myRandomHex(12)}");

            SchemaBuilder schemaBuilder = new SchemaBuilder(schemaGuid);

            schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
            schemaBuilder.SetWriteAccessLevel(AccessLevel.Public);

            schemaBuilder.SetSchemaName(schemaName); //<--SchemaName
            //schemaBuilder.SetDocumentation("Description of this schema"); //<--SchemaDescription

            foreach (string item in fieldNames)
            {
                FieldBuilder fieldBuilder = schemaBuilder.AddSimpleField(item, typeof(string));
            }

            Schema schema = schemaBuilder.Finish();

            return schema;
        }

        //-----------------------------------------------------------------------------------------
        //  Attach schema to element
        //-----------------------------------------------------------------------------------------

        public static void AddSchemaEntity(Schema schema, List<string> fieldNames, Element element)
        {
            Entity entity = new Entity(schema);
            foreach (string item in fieldNames)
            {
                Field fieldName = schema.GetField(item);//<--FieldName
            }
            //entity.Set(FieldName1, "Field Value, this is type String");//<--FieldValue, not needed for this application
            element.SetEntity(entity);
        }

        //-----------------------------------------------------------------------------------------
        //  Create a random hexdecimals
        //-----------------------------------------------------------------------------------------

        private static Random random = new Random();
        private static string myRandomHex(int length)
        {
            const string hexnums = "ABCDEF123456789";
            string randomhex = new string(Enumerable.Repeat(hexnums, length)
                .Select(q => q[random.Next(q.Length)]).ToArray());
            return randomhex;
        }

    }

 

  1. I noticed that if I filter conduit elements and only retrieve the first index element for the collection, my ExtensibleStorage is only assigned to the first conduit(element)… If this element(conduit) is deleted, so goes the XStorage and its information???  ß is this correct, is gone from the project???
  2. Also, if I don’t call the first index and assign the XStorage the entire conduit collection, only the conduits that are already created and in the model get this XStorage data but new conduits do not… So future conduit will need to be assigned this XStorage???  ß is this correct?
  3. If I only assign my XStorage to one conduit, I potensially can loose my XStorage info, if this one conduit is deleted
  4. If I assign my XStorage to all conduit, I potentially can bloat my model with unnecessary excess XStorage data, when really all I need is one place to house my data and call it as needed?
  5. Can I create an invisible ‘Element’ or better yet assign it to the application or document object, I only need one constant element that cannot be seen or deleted to house my XStorage? I am thinking to apply the storage to a view, such as the start view or BIMangerView, that should never be deleted…  but still can be!!
  6. When you say, “do not erase and recreate the schema every time” my thoughts where more along the lines of, keeping up with the schemaID number, when I Need to update I find the schemaID, delete it and create the XStorage again, only with a different random GUID and again, keep up with the GUID for next time to update and repeat.
  7. If I do erase the schema by GUID, is it fully gone?

I have downloaded the PDF and I will read UpgradeSchema on pg11.

 

Thanks

0 Likes