【Revit二次开发】模型中存储数据——参数和外部存储(Parameter, Schema and Entity)

news/2024/7/7 19:57:06

模型中存储数据

  • 参数
    • 读取
    • 写入
  • 外部存储
    • Schema
    • Entity
    • 快速获取外部存储参数

参数

在Revit平台API中,每个图元对象都有参数属性,它是隶属于图元所有属性的集合,在此集合中更改属性值。

  • 每个图元的参数都有一个与之关联的ElementId类型的ID
  • 大多数参数是Revit内置的
  • 还有一部分是存储在硬盘外部共享参数文件的共享参数

Revit API中参数相关的类

  • Autodesk.Revit.Parameters.BuiltInParameter枚举:Revit内建参数枚举
  • Parameter类: 参数
  • Defination类:参数定义类
  • ParameterType枚举: 参数类型枚举

通过Lookup获取参数,点击Parameter得到该元素所有参数
在这里插入图片描述
选择你想获取的参数,然后点击Definition
通过

读取

  1. e.Parameters

  2. e.GetParameters(string name)

  3. e.get_Parameter(BuiltInParameter parameterId)

  4. e.LookupParameter(string name)

写入

判断该参数是否是ReadOnly (p.IsReadOnly)

判断该参数交互时是否可修改(p.UserModifiable)

判断该参数的StorageType

将要写入该参数的值转换为相应的数据类型,然后Set().

外部存储

Revit API允许创建Schema类数据结构并将它们的实例附着到Revit模型中的任何Element。

Revit API中外部存储相关的类
Sechema:数据结构
SchemaBuilder:数据架构编辑器
Field:字段
FieldBuilder:字段编辑器
Entity:数据实体

Schema

using Autodesk.Revit.UI;
using System;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using System.Collections.Generic;

namespace ParameterBasicDemo
{

    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class CreateSharedParameterCmd : IExternalCommand
    {
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                string dllLocation = string.Empty;
                string sharedFile = string.Empty;

                dllLocation = "D:\\";
                sharedFile = Path.Combine(dllLocation, "SharedParameters.txt");
                //需要文件存在
                commandData.Application.Application.SharedParametersFilename = sharedFile;

                DefinitionFile defFile = null;
                defFile = commandData.Application.Application.OpenSharedParameterFile();
                using (Transaction tran = new Transaction(commandData.Application.ActiveUIDocument.Document))
                {
                    tran.Start("创建共享参数");
                    try
                    {
                        // 创建、获得共享参数
                        DefinitionGroup defGroup = defFile.Groups.Create("BasicDemo");
                        Definition def = defGroup.Definitions.get_Item("BasicDemoId");
                        ExternalDefinitionCreationOptions edcOpt = new ExternalDefinitionCreationOptions("BasicDemoId", ParameterType.Text);
                        //高版本不能用ParameterType要用ForgeTypeId(在SpecTypeId类中)
                        if (null == def)
                        {
                            def = defGroup.Definitions.Create(edcOpt);
                        }
                        // 获得墙类别集合
                        CategorySet cs = new CategorySet();
                        Category wallCategory =
                            commandData.Application.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory
                                .OST_Walls);
                        cs.Insert(wallCategory);
                        // 实例绑定
                        InstanceBinding instBd = commandData.Application.Application.Create.NewInstanceBinding(cs);
                        commandData.Application.ActiveUIDocument.Document.ParameterBindings.Insert(def, instBd);
                        if (TransactionStatus.Committed != tran.Commit())
                        {
                            TaskDialog.Show("共享参数", "事务提交失败!");
                        }
                    }
                    catch
                    {
                        tran.RollBack();
                        throw;
                    }
                }
                return Autodesk.Revit.UI.Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Autodesk.Revit.UI.Result.Failed;
            }
        }
    }
}

Entity

using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace ParameterBasicDemo
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class WallExternalStorageCmd : IExternalCommand
    {
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                Wall wall = PickWall(uidoc);
                StoreDataInWall(wall, XYZ.Zero);
                return Autodesk.Revit.UI.Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Autodesk.Revit.UI.Result.Failed;
            }
        }

        private Wall PickWall(UIDocument uidoc)
        {
            Wall wall = null;
            while (null == wall)
            {
                Reference reference = uidoc.Selection.PickObject(ObjectType.Element);
                wall = uidoc.Document.GetElement(reference.ElementId) as Wall;
            }
            return wall;
        }

        //创建一个数据结构,将其附加到墙上,用数据填充它,然后从墙上检索数据
        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); // 读权限:所有用户 
                schemaBuilder.SetWriteAccessLevel(AccessLevel.Vendor); // 可写权限:发行商
                schemaBuilder.SetVendorId("ADSK"); // 必须,由于可写权限为Vendor
                schemaBuilder.SetSchemaName("WireSpliceLocation");
                FieldBuilder fieldBuilder = schemaBuilder.AddSimpleField("WireSpliceLocation", typeof(XYZ)); // create a field to store an XYZ
                fieldBuilder.SetSpec(SpecTypeId.Length);
                fieldBuilder.SetDocumentation("A stored location value representing a wiring splice in a wall.");

                Schema schema = schemaBuilder.Finish(); // 注册外部存储的数据结构
                Entity entity = new Entity(schema); // 创建该数据结构类型的数据实体
                Field fieldSpliceLocation = schema.GetField("WireSpliceLocation"); // 获得数据结构的字段
                entity.Set<XYZ>(fieldSpliceLocation, dataToStore, UnitTypeId.Meters); // 设置数据实体的指定字段的值

                wall.SetEntity(entity); // 存储数据实体到墙元素

                // 读取墙上的外部数据
                Entity retrievedEntity = wall.GetEntity(schema);
                XYZ retrievedData = retrievedEntity.Get<XYZ>(schema.GetField("WireSpliceLocation"), UnitTypeId.Meters);
                createSchemaAndStoreData.Commit();
            }
        }
    }
}

快速获取外部存储参数

方法1
在这里插入图片描述

在LookUp上获取所需的外部存储的guid,得到schema,再得到entity,然后通过get方法输入参数的名称得到参数的值

Schema schema = Schema.Lookup(new Guid("{*****************}"));
Entity entity = ele.GetEntity(schema);
string data = entity.Get<string>(schema.GetField("id"));

方法2

            string data = null;
            IList<Guid> listGuids = ele.GetEntitySchemaGuids().ToList();
            foreach (Guid guid in listGuids)
            {
                Schema schema = Schema.Lookup(guid);
                if (schema.SchemaName == "schema名称"){
                    Entity entity = ele.GetEntity(schema);
                    data = entity.Get<string>(schema.GetField("参数名称"));
                }
            }

http://lihuaxi.xjx100.cn/news/145335.html

相关文章

外汇天眼:外汇走势图怎么看涨跌,怎么看外汇盘面走势图?

炒汇的难点&#xff0c;一是分辨行情是多头&#xff0c;空头或盘整形态&#xff0c;二是克服逆市操作的人性弱点&#xff0c;为此既要不断积累经验增加认知&#xff0c;又要对基本面技术面勤加研判&#xff0c;舍此并无捷径可走。 让趋势做你的朋友 外汇走势图怎么看涨跌 我…

flutter 图片加载缓存机制深入源码解析

flutter 图片加载缓存机制深入源码解析前言一、图片控件二、缓存管理三、新增缓存四、缓存清理五、图片加载六、滑动中处理总结前言 最近去看了一些flutter 中的图片加载机制的源码&#xff0c;在这里和大家分享一下。 目前在flutter 中&#xff0c;我们常用的Image 图片加载…

Java程序员不得不会的124道面试题(含答案)

1&#xff09;Java 中能创建 volatile 数组吗&#xff1f; 能&#xff0c;Java 中可以创建 volatile 类型数组&#xff0c;不过只是一个指向数组的引用&#xff0c;而不是整个数组。我的意思是&#xff0c;如果改变引用指向的数组&#xff0c;将会受到 volatile 的保护&#x…

服务器测试之linux下RAID/HBA管理命令汇总

** sas3ircu ** 对LSI3008阵列卡的管理&#xff0c;命令用法与sas2ircu类似。提供的为可执行文件无需安装 ./sas3ircu 0 locate 2:$A on ./sas3ircu -h 查看帮助信息 ./sas3ircu list 查看所有RAID控制器信息 ./sas3ircu 0 display 查看第一块RAID控制器、volume、物理磁…

html学习,html书写规范,骨架标签,图片标签,相对路径,html常用标签

每篇博文的浪漫主义&#xff1a; 22岁&#xff0c;去做了我想做的事&#xff5c;独自一人摩旅1万公里【22岁&#xff0c;去做了我想做的事&#xff5c;独自一人摩旅1万公里】 https://www.bilibili.com/video/BV1UD4y1475E/?share_sourcecopy_web&vd_source385ba0043075be…

DMA实践3:dmaengine的实验

前言 本次是第三篇。 第一篇&#xff0c;写一个通用框架&#xff0c;做到拿来就能用。 第二篇&#xff0c;实现mmap功能&#xff0c;内核中的read_buf和write_buf都映射到用户空间&#xff0c;然后呢。写read_buf和write_buf的最后一个字节为‘R’和W&#xff0c;然后再release…

二、mysql数据库的安装

mysql数据库的安装二、mysql数据库的安装1、yum安装2、源码安装mysql-5.53、源码安装mysql-5.74、二进制安装mysql5.7三、数据库的链接四、mysql的安装目录说明五、mysql的配置文件my.cnf详解六、mysql的启动与关闭1、mysql启动2、mysql的关闭3、mysql登陆二、mysql数据库的安装…

开源直播美颜SDK工具算法分析

说起美颜相信大家都不会陌生&#xff0c;因为在日常生活中我们经常会用到美颜相关的工具&#xff0c;特别是在互联网社交平台中&#xff0c;各式各样的美颜工具更是让人眼花缭乱。美颜&#xff0c;从一开始只有少数人才能用到的“高端技术”发展至如今的“全民美颜”时代&#…