In this article, let us look at a new code that Revit Addin Wizard (RevitAddinWizard) provides, Element Data Extension. It is to create the super easy and cool Element Data Operator and/or Element Data Extension.
It can be found from both the Revit Addin Coder menu group under Tools and the toolbar with the same name from right click context menu. After the menu item or tool icon is clicked, the Element Data Extension coder window will show up:
If both the Element Data Operator class and the Element Data Extension class options are checked off as above, both help classes will be created. Here they are.
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 RevitAddinCS7
{
public class SuperEasyCoolDataOperator2
{
private const string SingleStringFiledName = "SingleStringForAll";
public static void SetDataToElement(string id, object o, Element e)
{
SchemaBuilder sb = new SchemaBuilder(new Guid(id));
sb.SetSchemaName(id.Replace("-", String.Empty));
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 GetDataFromElement<T>(string id, Element e)
{
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);
}
}
}
}
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 RevitAddinCS7
{
public static class ElementDataExtension2
{
private const string SingleStringFiledName = "SingleStringForAll";
//Sample usage: e.SetData2("DA4AAE5A-4EE1-45A8-B3E8-F790C84CC44F", complexData);
public static void SetData(this Element e, string id, object o)
{
SchemaBuilder sb = new SchemaBuilder(new Guid(id));
sb.SetSchemaName(id.Replace("-", String.Empty));
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);
}
}
}
//Sample usage: ComplexDataSample complexData = e.GetData2<ComplexDataSample>("DA4AAE5A-4EE1-45A8-B3E8-F790C84CC44F");
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);
}
}
}
}
In terms of how to use the super easy and cool Element Data Operator and Element Data Extension coders, please refer to earlier posts for detailed instructions and code examples.
RevitAddinWizard will provide more cool coders and widgets in the future. Please stay tuned.
Recent Comments