0

The line of code where the fieldbuilder is constructed assigns a type that can be stored as a string, int, XYZ etc.

The developer documentation doesn't include the "BuiltInCategory" type. I need to write this List<BuiltInCategory> to the "projectinfo object" for storage. Do I need to cast the List<BuiltinCategory> to a string List<string> for storage and then convert back for retrieval? Will that work?

public void StoreDataInProjectInfo(Document doc, Element projectInfoElement)
    {
        Schema schema = Schema.Lookup(SchemaGuid);

        IList<BuiltInCategory> BuiltincatStringList = new List<BuiltInCategory>();

        BuiltincatStringList.Add("1");
        BuiltincatStringList.Add("2");


        using (Transaction transEstorage = new Transaction(doc))
        {
            transEstorage.Start("add estorage list");


        if (null == schema)
        {
            SchemaBuilder sb = new SchemaBuilder(new Guid("AF5E4C3E-C2E2-493B-8236-BA0F5E323887"));

            //public accesibility
            sb.SetReadAccessLevel(AccessLevel.Public);
            sb.SetWriteAccessLevel(AccessLevel.Public);

This is the fieldbuilder list that needs a type the code fails on execution and reports an incorrect "type."

            //Storage Filled for Cat List
            ***FieldBuilder fb = sb.AddArrayField("UserCategoryList", typeof(BuiltInCategory))***; 




            fb.SetDocumentation("A Set Of Categories to be worksetted");

            //set schema name and register
            sb.SetSchemaName("UserCategoryList");
            schema = sb.Finish();
        }


        // Create an entity (object) for this schema (class) 
        Entity entity = new Entity(schema);

        // Get the field from the schema 
        Field userWSCategoryList = schema.GetField("UserCategoryList");

        entity.Set<IList<BuiltInCategory>>(userWSCategoryList, BuiltincatStringList);


        //Entity storage on Element
        projectInfoElement.SetEntity(entity);



        // Read back the data from the wall 
        Entity retrievedEntity = projectInfoElement.GetEntity(schema);

        IList<BuiltInCategory> retrievedData = retrievedEntity.Get<List<BuiltInCategory>>(schema.GetField("UserCategoryList"));


        transEstorage.Commit();
            }

    }
mbarthelemy
  • 12,465
  • 4
  • 41
  • 43
Bimtopian
  • 37
  • 1
  • 5

2 Answers2

0

I'd recommend casting the BuiltInCategory to an int. This converts the BuiltInCategory to its underlying element ID value.

This should be pretty straightforward - simply replace all instances of BuiltInCategory in your code above with int.

To cast a BuiltInCategory to an int:

BuiltInCategory builtInCategory;
int builtInCategoryId = (int)builtInCategory;

And the other way:

BuiltInCategory builtInCategory = (BuiltInCategory)builtInCategoryId;

This will be more consistent than trying to convert values to a string. If you ever have to deal with multiple languages in your code, you would likely run into issues using a string.

Colin Stark
  • 591
  • 3
  • 15
  • 1
    The "ElementId," type can be used according to the Revit developer documentation in implementing a schema. – Bimtopian May 21 '14 at 07:05
  • Hi, I managed to store the builtincategory as Ilist data, however now I can't convert the retrieved data back to a IList. This is the error-- Cannot convert type 'System.Collections.Generic.List' to 'Autodesk.Revit.DB.BuiltInCategory' – Bimtopian May 21 '14 at 09:57
0

Thanx cs 1088,

I basically used casting a List to an array List as you correctly proposed and I finally after much mucking about realised I needed to use this structure below to cast the IList back to a BIC after retrieving the array from the Schema.

userCheckedbuiltInCategory = retrievedData.Cast().ToList();

Bimtopian
  • 37
  • 1
  • 5