Revit API 开发 (10): ExtensibleStorage 外部存储

前言

很多时候,需要往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,这是一个静态方法。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客BIM工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值