The Revit API 2012 provides an official way to extend the Element data now. In the old days, we could only create some cumbersome invisible parameters and attach them to the elements where we want to store some extra data. Now the Extensible Storage API can directly address so basic a need for us and has much power.
However, as introduced and demonstrated before, unnecessary complexities, impositions, redundancies and inconsistencies are here and there. In case any of those points as mentioned repeatedly in early posts slips out of our minds, the data storage or retrieval would just fail. In addition, the out-of-box supported data types are just a few, far less than enough.
Things become super easy and cool with the Revit Element Data Extension. However, the first version of the Revit Element Data Extension does not support Dictionary type directly. Fortunately, the second version of the Revit Element Data Extension has got rid of the limitation, and it actually supports more types seamlessly.
Here is the Revit Element Data Extension 2:
using System;
using System.Xml;
using System.Runtime.Serialization;
using System.IO;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage; //Revit API 2012 only
namespace RevitAddinWizard
{
public static class ElementDataExtension2
{
private const string SingleStringFiledName = "SingleStringForAll";
public static void SetData(this Element e, string id, object o)
{
SchemaBuilder sb = new SchemaBuilder(new Guid(id));
sb.SetSchemaName(id.Replace("-", ""));
FieldBuilder fb = sb.AddSimpleField(SingleStringFiledName, typeof(string));
DataContractSerializer serializer = new DataContractSerializer(o.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, o);
ms.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(ms))
{
Entity ent = new Entity(sb.Finish());
ent.Set<string>(SingleStringFiledName, reader.ReadToEnd());
e.SetEntity(ent);
}
}
}
public static T GetData<T>(this Element e, string id)
{
Schema sch = Schema.Lookup(new Guid(id));
string s = e.GetEntity(sch).Get<string>(SingleStringFiledName);
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
using (XmlReader reader = XmlReader.Create(new StringReader(s)))
{
return (T)serializer.ReadObject(reader);
}
}
}
}
Here is some test code to write a Dictionary data to a selected Element using the Revit Element Data Extension 2:
Reference picked = CachedUiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element, "Pick a window to attach our data");
using (Transaction trans = new Transaction(CachedDoc, "AttachDataTo"))
{
trans.Start();
Dictionary<string, int> data = new Dictionary<string, int>()
{
{ "AAA", 111},
{ "BBB", 222},
{ "CCC", 333},
};
CachedDoc.GetElement(picked).SetData2("DA4AAE5A-4EE1-45A8-B3E8-F790C84CC44F", data);
trans.Commit();
}
Here is the code to read back the same Dictionary data from the same Element:
Reference picked = CachedUiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element, "Pick the window having our data");
using (Transaction trans = new Transaction(CachedDoc, "ReadDataFrom"))
{
trans.Start();
Dictionary<string, int> data = CachedDoc.GetElement(picked).GetData2<Dictionary<string, int>>("DA4AAE5A-4EE1-45A8-B3E8-F790C84CC44F");
string str = string.Empty;
int index = 0;
foreach (KeyValuePair<string, int> entry in data)
{
str += string.Format("Entry #{0}: {1}/{2}\n", ++index, entry.Key, entry.Value);
}
MessageBox.Show(str, "MyData");
trans.Commit();
}
If the above test code is put into an external command, we can happily verify that everything works just fine.
As can be seen, those complexities and impositions all go away. We do not have to specify the same schema GUIDs, scheme names, field names, data types, or unit types repeatedly. We do not have to care about all those intermediate objects such as Schema, SchemaBuilder, Field, FieldBuilder, or Entity anymore either.
What we need to care about now are what we really want, the Element itself, the data object, and the Schema identifier. If they are consistent with each other, it will be very hard to make the data storage or retrieval fail.
Life is becoming better and better. Huh?
The Revit Addin Wizard (RevitAddinWizard) is going to provide a coder to help generate Extensible Storage code.
Recent Comments