Autodesk® Revit® 2012 was first release that included a new API called Extensible Storage, which allows API users to store custom data with the Revit model. This blogpost will cover the basics of working with the Extensible Storage API over two posts.
To use the Extensible Storage feature, we first define our own data container, add data to it, and attach its instance to an element in a Revit model. This functionality provides a structured way of handling and storing data.
Defining Schema and Fields
To use the Extensible Storage API, we first need to define a new schema. A schema is a description of the data structure that is stored in a Revit document, and is similar to a Class in object-oriented programming language. A schema contains identity information, documentation, and a list of fields of the data structure.
The SchemaBuilder class is used to create and populate a schema. Any particular Revit model can have more than one schema, and to uniquely identify each schema, GUID is used. The constructor of the SchemaBuilder class accepts this GUID as a parameter. Schema name is another required field; after creating a schema, you must provide a name to the schema, using SchemaBuilder.SetSchemaName() method. The SchemaBuilder class also provides a method to add additional description (called documentation) for the schema.
Once we create an instance of the SchemaBuilder class, we can set the read and write access levels to this schema which help determine who can read or write data into the schema. The schema data can be configured to be used by all users, or by a specific application vendor or by just a specific application from a vendor. If we set the access level to any setting other than Public, we need to set the Vendor Id on the SchemaBuilder class. The following lines of code shows how to create a new SchemaBuilder object, set the access levels and set the name and documentation of the schema that will be created using this SchemaBuilder object:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.ExtensibleStorage;
namespace ExtensibleStorageSample
{
[Regeneration(RegenerationOption.Manual)]
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
Guid schemaGuid = new Guid(
"0DC954AE-ADEF-41c1-8D38-EB5B8465D255");
#region IExternalCommand Members
public Result Execute(
ExternalCommandData commandData,
ref string message, ElementSet elements)
{
// Create Transaction for working with schema
Transaction trans = new Transaction(
commandData.Application.ActiveUIDocument.Document,
"ExtensibleStore");
trans.Start();
// Select a wall
Reference wallRef = commandData.Application.ActiveUIDocument.Selection.PickObject(
Autodesk.Revit.UI.Selection.ObjectType.Element);
Wall wall =
commandData.Application.ActiveUIDocument.Document.GetElement(
wallRef)
as Wall;
// Create a schema builder
SchemaBuilder builder = new SchemaBuilder(schemaGuid);
// Set read and write access levels
builder.SetReadAccessLevel(AccessLevel.Public);
builder.SetWriteAccessLevel(AccessLevel.Public);
// Note: if this was set as vendor or application,
// we would have addtionally required to use SetVendorId
// Set name to this schema builder
builder.SetSchemaName("WallSocketLocation");
builder.SetDocumentation(
"Data store for socket related info in a wall");
trans.Commit();
return Result.Succeeded;
}
#endregion
}
}
Next, we need to create the descriptions of the specific data that will be contained in the schema. These descriptions are called fields. Fields can be defined as the description of a specific data within a Schema and are similar to property of a class in object-oriented programming. Field contains the name, documentation, access control, types, and units. This is used as a key to access corresponding data on a Revit element.
Just like we used SchemaBuilder helper class to create a schema, we use the FieldBuilder helper class to create a new field. This class helps create a template for the fields. The SchemaBuilder object itself provides methods like AddSimpleField() which returns an instance of FieldBuilder class, and the parameters for this method include name and type of data to be stored. Additionally, we can set the description (called documentation), unit types, etc. The following lines of code show how to use FieldBuilder helper class to create a new field and set the name, unit type, etc.
// Create field1

Leave a Reply