前言
很多时候,需要往Revit的文件里面放自己的数据。这里简单介绍一下 Revit API 如何处理数据的保存和读取,并且这些数据是绑定到具体的构件上的。另外,也可以自己创建一个 DataStorage 的 element,把信息存储在它里面,这样就不用依赖于某个具体的构件了。
实例
创建一个数据结构,绑定到墙的实例上,存储值,然后再把它读出来。
创建SchemaBuilder
SchemaBuilder schemaBuilder = new SchemaBuilder(new Guid("720080CB-DA99-40DC-9415-E53F280AA1F0"));
schemaBuilder.SetReadAccessLevel(AccessLevel.Public); // allow anyone to read the object
schemaBuilder.SetWriteAccessLevel(AccessLevel.Vendor); // restrict writing to this vendor only
schemaBuilder.SetVendorId("ADSK"); // required because of restricted write-access
schemaBuilder.SetSchemaName("WireSpliceLocation");
创建SchemaBuilder的Field
可以设定 Field 的类型、单位和文档说明。
FieldBuilder fieldBuilder = schemaBuilder.AddSimpleField("WireSpliceLocation", typeof(XYZ)); // create a field to store an XYZ
fieldBuilder.SetUnitType(UnitType.UT_Length);
fieldBuilder.SetDocumentation("A stored location value representing a wiring splice in a wall.");
完成SchemaBuilder的创建
Schema schema = schemaBuilder.Finish(); // register the Schema object
创建一个entity
每一个 Entity 对应到一个 Field,在 Entity 里面可以设置它的值。
Entity entity = new Entity(schema); // create an entity (object) for this schema (class)
Field fieldSpliceLocation = schema.GetField("WireSpliceLocation"); // get the field from the schema
entity.Set<XYZ>(fieldSpliceLocation, dataToStore, DisplayUnitType.DUT_METERS); // set the value for this entity
存储和读取
一个具体的 Entity 可以绑定到某一个具体的构件上面,这里是墙。
wall.SetEntity(entity); // store the entity in the element
// get the data back from the wall
Entity retrievedEntity = wall.GetEntity(schema);
XYZ retrievedData = retrievedEntity.Get<XYZ>(schema.GetField("WireSpliceLocation"), DisplayUnitType.DUT_METERS);
完整代码
// 创建一个数据结构,绑定到墙的实例上,存储值,然后再把它读出来。
void StoreDataInWall(Wall wall, XYZ dataToStore)
{
using (Transaction createSchemaAndStoreData = new Transaction(wall.Document, "tCreateAndStore"))
{
createSchemaAndStoreData.Start();
SchemaBuilder schemaBuilder = new SchemaBuilder(new Guid("720080CB-DA99-40DC-9415-E53F280AA1F0"));
schemaBuilder.SetReadAccessLevel(AccessLevel.Public); // allow anyone to read the object
schemaBuilder.SetWriteAccessLevel(AccessLevel.Vendor); // restrict writing to this vendor only
schemaBuilder.SetVendorId("ADSK"); // required because of restricted write-access
schemaBuilder.SetSchemaName("WireSpliceLocation");
FieldBuilder fieldBuilder = schemaBuilder.AddSimpleField("WireSpliceLocation", typeof(XYZ)); // create a field to store an XYZ
fieldBuilder.SetUnitType(UnitType.UT_Length);
fieldBuilder.SetDocumentation("A stored location value representing a wiring splice in a wall.");
Schema schema = schemaBuilder.Finish(); // register the Schema object
Entity entity = new Entity(schema); // create an entity (object) for this schema (class)
Field fieldSpliceLocation = schema.GetField("WireSpliceLocation"); // get the field from the schema
entity.Set<XYZ>(fieldSpliceLocation, dataToStore, DisplayUnitType.DUT_METERS); // set the value for this entity
wall.SetEntity(entity); // store the entity in the element
// get the data back from the wall
Entity retrievedEntity = wall.GetEntity(schema);
XYZ retrievedData = retrievedEntity.Get<XYZ>(schema.GetField("WireSpliceLocation"), DisplayUnitType.DUT_METERS);
createSchemaAndStoreData.Commit();
}
}
更多信息
Schema.ListFields 列出 schema 中所有的 field,按字母排序。
Schema.ListSchemas 列出所有的 schema,这是一个静态方法。
Schema.Lookup 通过 GUID 查找 schema,这是一个静态方法。