Message 1 of 7
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi everyone,
I have made three generic methods for extensible storage, a get and two set methods (one for arrays and simple field and one for maps).
Problem however is that I get an error when getting the storage:
Autodesk.Revit.Exceptions.ArgumentException: 'The Field belongs to a different Schema from this Entity, or this Entity is invalid.
Parameter name: field'
Can somebody help me with making these methods work. Because in the long run it will save a lot of time if methodes can be used for different types.
This is the code I have so far:
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
using(var tx = new Transaction(_doc, "Test"))
{
tx.Start();
var e = _doc.GetElement(id);
var elementInfo = new Dictionary<string, string>
{
{ "Test1", "Hello World!" },
{ "Test2", "Hello World!" },
{ "Test3", "Hello World!" }
};
var extensibleElementInfo = GetStorageData<Dictionary<string,string>>(e, _elementInfo, _guidElementInfo);
SetStorageData<string, string>(e, elementInfo, _elementInfo, _guidElementInfo);
tx.Commit();
}
return Result.Succeeded;
}
private static T GetStorageData<T>(Element e, string fieldName, Guid guid)
{
var schema = GetSchema(guid);
if(schema != null)
{
//try
//{
var entity = e.GetEntity(schema);
return entity.Get<T>(schema.GetField(fieldName));
//}
//catch { }
}
return default(T);
}
private void SetStorageData<T>(Element e, T data, string schemaName, Guid guid)
{
var schema = GetSchema(guid);
Entity entity = null;
if(schema == null)
{
var schemaBuilder = new SchemaBuilder(guid);
schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
schemaBuilder.SetWriteAccessLevel(AccessLevel.Vendor);
schemaBuilder.SetVendorId("BimBuildings");
schemaBuilder.SetSchemaName(schemaName);
try
{
if (typeof(T) != typeof(string) && typeof(IEnumerable).IsAssignableFrom(typeof(T)))
schemaBuilder.AddArrayField(schemaName, typeof(T));
else
schemaBuilder.AddSimpleField(schemaName, typeof(T));
}
catch (Autodesk.Revit.Exceptions.ArgumentException)
{
Message.Display("The type provided is not storable.", WindowType.Error);
}
schema = schemaBuilder.Finish();
entity = new Entity(schema);
}
else
{
entity = e.GetEntity(schema);
}
var fieldSet = schema.GetField(schemaName);
entity.Set(fieldSet, data);
}
private void SetStorageData<TKey, TValue>(Element e, IDictionary<TKey, TValue> data, string schemaName, Guid guid)
{
var schema = GetSchema(guid);
Entity entity = null;
if (schema == null)
{
var schemaBuilder = new SchemaBuilder(guid);
schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
schemaBuilder.SetWriteAccessLevel(AccessLevel.Vendor);
schemaBuilder.SetVendorId("BimBuildings");
schemaBuilder.SetSchemaName(schemaName);
schemaBuilder.AddMapField(schemaName, typeof(TKey), typeof(TValue));
schema = schemaBuilder.Finish();
entity = new Entity(schema);
}
else
{
entity = e.GetEntity(schema);
}
var fieldSet = schema.GetField(schemaName);
entity.Set(fieldSet, data);
}
Solved! Go to Solution.