Revit二次开发之扩展存储的使用

一、背景

小伙伴们在做Revit二次开发时,是否需要扩展存储呢?这里骑士给大家介绍三种使用方式,一个是依赖某个构件存储单个属性,一个是依赖某个构件存储文件,还有一个是不依赖于构件直接进行存储。

二、实现思路

2.1依赖构件单个属性存储

这里的例子,可以基于一个墙,添加一个额外的字符串信息

    [Transaction(TransactionMode.Manual)]
   public class StoreSingle: IExternalCommand
    {
        Guid guid = new Guid("69042559-379C-44D4-AF38-88241FBE77BF");

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            //选择项目中的一个墙
            Wall wall = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element)) as Wall;
            Transaction transaction = new Transaction(doc, "创建Storge");
            transaction.Start();
            //【1】建立存储框架(可以理解为类似于定义了一个类)
            SchemaBuilder builder = new SchemaBuilder(guid);
            //【1-1】权限设置
            builder.SetReadAccessLevel(AccessLevel.Public);
            builder.SetWriteAccessLevel(AccessLevel.Public);
            //【1-2】基本信息
            builder.SetSchemaName("WallStorge");
            builder.SetDocumentation("Storge Information in wall");
            //【1-3】创建字段
            FieldBuilder fieldBuilder = builder.AddSimpleField("stringValue", typeof(string));
            //【1-4】得到创建的框架
            Schema schema = builder.Finish();
            //【2】创建实体(可以理解为实例化这个类)
            Entity entity = new Entity(schema);
            Field stringValue = schema.GetField("stringValue");
            entity.Set<string>(stringValue, "字符串123456789");
            wall.SetEntity(entity);
            transaction.Commit();

            //【3】获取这个对象的值

            Schema currentScheme = Schema.Lookup(guid);
            Entity currentEntity = wall.GetEntity(currentScheme);
            string currentStringValue = currentEntity.Get<String>(currentScheme.GetField("stringValue"));

            TaskDialog.Show("提示信息", $"当前墙中存储的字符串的信息为{currentStringValue}");

            return Result.Succeeded;
        }
    }

2.2依赖构件文件存储

这种可以以二进制形式存储任何一个文件,比如我们把一个rvt存储到一个墙里面

    [Transaction(TransactionMode.Manual)]
    public class StoreFile : IExternalCommand
    {
        Guid guid = new Guid("69042559-379C-44D4-AF38-88241FBE77BF");

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            //选择项目中的一个墙
            Wall wall = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element)) as Wall;
            Transaction transaction = new Transaction(doc, "创建Storge");
            transaction.Start();    
            //【1】建立存储框架(可以理解为类似于定义了一个类)
            SchemaBuilder builder = new SchemaBuilder(guid);
            //【1-1】权限设置
            builder.SetReadAccessLevel(AccessLevel.Public);
            builder.SetWriteAccessLevel(AccessLevel.Public);
            //【1-2】基本信息
            builder.SetSchemaName("WallStorge");
            builder.SetDocumentation("Storge Information in wall");
            //【1-3】创建字段
            //文件名
            FieldBuilder fieldBuilder = builder.AddSimpleField("stringValue", typeof(string));
            //文件内容
            fieldBuilder = builder.AddArrayField("Data", typeof(byte));
            fieldBuilder.SetDocumentation("Store Data");
            //【1-4】得到创建的框架
            Schema schema = builder.Finish();
            //【2】创建实体(可以理解为实例化这个类)
            Entity entity = new Entity(schema);
            string fileName = @"xxx.rvt";
            Field stringValue = schema.GetField("stringValue");
            byte[] data = File.ReadAllBytes(fileName);
            entity.Set<string>(stringValue, fileName);
            entity.Set<IList<byte>>(schema.GetField("Data"), data);
            wall.SetEntity(entity);
            transaction.Commit();
            //【3】获取之前的路径
            Schema currentScheme = Schema.Lookup(guid);
            Entity currentEntity = wall.GetEntity(currentScheme);
            string currentStringValue = currentEntity.Get<String>(currentScheme.GetField("stringValue"));
            //【4】将这个文件还原回来
            byte[] currentData = currentEntity.Get<IList<byte>>(currentScheme.GetField("Data")).ToArray<byte>();
            File.WriteAllBytes(@"xxx\项目2.rvt", currentData);
            return Result.Succeeded;
        } 
       
    }

2.3不依赖构件存储

这种方式可以不局限于某个构件,而是在项目中存储这个参数

  [Transaction(TransactionMode.Manual)]
    public class StoreElement : IExternalCommand
    {
        Guid guid = new Guid("69042559-379C-44D4-AF38-88241FBE77FB");

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            Transaction transaction = new Transaction(doc, "创建Storge");
            transaction.Start();
            //全局存储单元
            DataStorage myElement = DataStorage.Create(doc);
            //【1】建立存储框架(可以理解为类似于定义了一个类)
            SchemaBuilder builder = new SchemaBuilder(guid);
            //【1-1】权限设置
            builder.SetReadAccessLevel(AccessLevel.Public);
            builder.SetWriteAccessLevel(AccessLevel.Public);
            //【1-2】基本信息
            builder.SetSchemaName("WallStorge");
            builder.SetDocumentation("Storge Information in wall");
            //【1-3】创建字段
            FieldBuilder fieldBuilder = builder.AddSimpleField("stringValue", typeof(string));
            //【1-4】得到创建的框架
            Schema schema = builder.Finish();
            //【2】创建实体(可以理解为实例化这个类)
            Entity entity = new Entity(schema);
            Field stringValue = schema.GetField("stringValue");
            entity.Set<string>(stringValue, "字符串123456789");
            myElement.SetEntity(entity);
            transaction.Commit();
            //【3】获取这个对象的值
           Schema currentScheme = Schema.Lookup(guid);
            //过滤找到这个单元
            Element CurrentElement = new FilteredElementCollector(doc).OfClass(typeof(DataStorage)).FirstElement();
            Entity currentEntity = CurrentElement.GetEntity(currentScheme);
            string currentStringValue = currentEntity.Get<String>(currentScheme.GetField("stringValue"));
            TaskDialog.Show("提示信息", $"当前文件中存储的字符串的信息为{currentStringValue}");
            return Result.Succeeded;
        }
    }

三、注意事项

需要Revit二次开发全流程教学 的朋友可以联系我qq:1056295111
1.大家好,我是黑夜の骑士,欢迎大家关注我的博客,笔者将持续输出BIM相关软件开发、移动互联网开发以及游戏编程干货;
2.欢迎加入建筑信息化开发交流群,获取更多开发资料 群号:711844216(满),二群群号:1016453207
3.欢迎关注微信公众号,“工程人的编程课堂”

  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值