最近被revit的外部扩展存储搞得死去活来,作为日后再次使用的预防针,此处随手留下印记,以作警示。
首先我们知道外部扩展存储ExtensibleStorage是revit提供给revit二次开发人员用于存储外部数据的接口,这使得很多公司可以在rvt文件中存储其自定义的数据。
对,这东西看上去很美好,但是!!
存储的过程十分的诡异,具体怎么诡异呢,我以自身经历举个例子。
(我也想弄成本地文件,但revit的这个存储功能的隐蔽性和二次开发的原因,导致用扩展存储比常规数据存储方案更优,比如减少io操作啦诸如此类)
这两天遇到一个需求,公司本身已经有很多自身的数据了,但因各种理由需要将其存入revit中供其他模块调用,起初看了一下叶先生的相关博客了解到了如何使用,具体不赘述,链接如下
https://blog.csdn.net/joexiongjin/article/details/7776552
看上去很简单,好的,那我们开始吧。
首先要存储数据,需要建立视图Schema,而schema与构件绑定使用entity,entity里面要存储数据要定义域field,再在field中确定存储的数值类型type。嗯,看着挺麻烦不过问题不大,和类定义很像。
entity可以理解为存储类,field就是类的变量,二field需要确定类型,和类定义没区别,甚至schema可以简单的理解为命名空间,也就是类的上一级(这里不搞太复杂,毕竟不喜欢b乎大佬们那种术语满天飞装得飞起)。
而每一个guid固定一个schema,使用get,set来存取数据,到这里是不是觉得ok了?
接下来,实际使用的时候,鬼畜的事情来了!
首先,简单的数据存储一般是没问题的,比如存一些用户信息等,最开始由于存储的都是string,bool,int这一类的数据(对你没看错,我说的就是几个常用的数据类型),所以还没觉得有什么,无非就是guid的管理比较麻烦。但是!
以上我可没提到过浮点数,对没错!唯独浮点数类型的例外(float,double:喵喵喵???)
最近做revit内置属性窗用于设置自定义数据,于是碰到了这样一种属性“地下比例”,它是一个0~1的浮点数。于是乎按照之前的理解存了一个double类型的field,数值的区间限制在外部的getset中处理,这些不难我就不贴代码了,跟着叶老师的代码抄作业都能抄明白。
编好之后试运行,选择构件,revit里鸦雀无声,属性窗纹丝不动(老子就是不出数据,来打我呀!)
随手套个TryCatch是个好习惯,我错了。。。
再次运行,打开调试模式,跳到断点里查看以下错误信息???
Autodesk.Revit.Exceptions.InvalidOperationException: Units are required for field FieldTest1 at Autodesk.Revit.DB.ExtensibleStorage.SchemaBuilder.Finish() …
提示说我们的定义域需要个单位,这什么鬼???叶老师没说还需要单位啊。
图中也可以看到,field的定义里也没有重载函数,那么是什么情况呢?翻了一下FieldBuilder的函数,于是呢
对没错,添加完field之后,利用fieldBuilder还需要指定一下unittype,这个unittype是什么呢?其实就是我们业务中经常使用到的单位,跳转到枚举类里看一下就能明白,无非就是质量单位、长度单位等等,但是。。。
1 public enum UnitType 2 { 3 UT_Undefined = -2, 4 UT_Custom = -1, 5 UT_Length = 0, 6 UT_Area = 1, 7 UT_Volume = 2, 8 UT_Angle = 3, 9 UT_Number = 4, 10 UT_SheetLength = 5, 11 UT_SiteAngle = 6, 12 UT_HVAC_Density = 7, 13 UT_HVAC_Energy = 8, 14 UT_HVAC_Friction = 9, 15 UT_HVAC_Power = 10, 16 UT_HVAC_Power_Density = 11, 17 UT_HVAC_Pressure = 12, 18 UT_HVAC_Temperature = 13, 19 UT_HVAC_Velocity = 14, 20 UT_HVAC_Airflow = 15, 21 UT_HVAC_DuctSize = 16, 22 UT_HVAC_CrossSection = 17, 23 UT_HVAC_HeatGain = 18, 24 UT_Electrical_Current = 19, 25 UT_Electrical_Potential = 20, 26 UT_Electrical_Frequency = 21, 27 UT_Electrical_Illuminance = 22, 28 UT_Electrical_Luminous_Flux = 23, 29 UT_Electrical_Power = 24, 30 UT_HVAC_Roughness = 25, 31 UT_Force = 26, 32 UT_LinearForce = 27, 33 UT_AreaForce = 28, 34 UT_Moment = 29, 35 UT_ForceScale = 30, 36 UT_LinearForceScale = 31, 37 UT_AreaForceScale = 32, 38 UT_MomentScale = 33, 39 UT_Electrical_Apparent_Power = 34, 40 UT_Electrical_Power_Density = 35, 41 UT_Piping_Density = 36, 42 UT_Piping_Flow = 37, 43 UT_Piping_Friction = 38, 44 UT_Piping_Pressure = 39, 45 UT_Piping_Temperature = 40, 46 UT_Piping_Velocity = 41, 47 UT_Piping_Viscosity = 42, 48 UT_PipeSize = 43, 49 UT_Piping_Roughness = 44, 50 UT_Stress = 45, 51 UT_UnitWeight = 46, 52 UT_ThermalExpansion = 47, 53 UT_LinearMoment = 48, 54 UT_LinearMomentScale = 49, 55 UT_ForcePerLength = 50, 56 UT_ForceLengthPerAngle = 51, 57 UT_LinearForcePerLength = 52, 58 UT_LinearForceLengthPerAngle = 53, 59 UT_AreaForcePerLength = 54, 60 UT_Piping_Volume = 55, 61 UT_HVAC_Viscosity = 56, 62 UT_HVAC_CoefficientOfHeatTransfer = 57, 63 UT_HVAC_Airflow_Density = 58, 64 UT_Slope = 59, 65 UT_HVAC_Cooling_Load = 60, 66 UT_HVAC_Cooling_Load_Divided_By_Area = 61, 67 UT_HVAC_Cooling_Load_Divided_By_Volume = 62, 68 UT_HVAC_Heating_Load = 63, 69 UT_HVAC_Heating_Load_Divided_By_Area = 64, 70 UT_HVAC_Heating_Load_Divided_By_Volume = 65, 71 UT_HVAC_Airflow_Divided_By_Volume = 66, 72 UT_HVAC_Airflow_Divided_By_Cooling_Load = 67, 73 UT_HVAC_Area_Divided_By_Cooling_Load = 68, 74 UT_WireSize = 69, 75 UT_HVAC_Slope = 70, 76 UT_Piping_Slope = 71, 77 UT_Currency = 72, 78 UT_Electrical_Efficacy = 73, 79 UT_Electrical_Wattage = 74, 80 UT_Color_Temperature = 75, 81 UT_DecSheetLength = 76, 82 UT_Electrical_Luminous_Intensity = 77, 83 UT_Electrical_Luminance = 78, 84 UT_HVAC_Area_Divided_By_Heating_Load = 79, 85 UT_HVAC_Factor = 80, 86 UT_Electrical_Temperature = 81, 87 UT_Electrical_CableTraySize = 82, 88 UT_Electrical_ConduitSize = 83, 89 UT_Reinforcement_Volume = 84, 90 UT_Reinforcement_Length = 85, 91 UT_Electrical_Demand_Factor = 86, 92 UT_HVAC_DuctInsulationThickness = 87, 93 UT_HVAC_DuctLiningThickness = 88, 94 UT_PipeInsulationThickness = 89, 95 UT_HVAC_ThermalResistance = 90, 96 UT_HVAC_ThermalMass = 91, 97 UT_Acceleration = 92, 98 UT_Bar_Diameter = 93, 99 UT_Crack_Width = 94, 100 UT_Displacement_Deflection = 95, 101 UT_Energy = 96, 102 UT_Structural_Frequency = 97, 103 UT_Mass = 98, 104 UT_Mass_per_Unit_Length = 99, 105 UT_Moment_of_Inertia = 100, 106 UT_Surface_Area = 101, 107 UT_Period = 102, 108 UT_Pulsation = 103, 109 UT_Reinforcement_Area = 104, 110 UT_Reinforcement_Area_per_Unit_Length = 105, 111 UT_Reinforcement_Cover = 106, 112 UT_Reinforcement_Spacing = 107, 113 UT_Rotation = 108, 114 UT_Section_Area = 109, 115 UT_Section_Dimension = 110, 116 UT_Section_Modulus = 111, 117 UT_Section_Property = 112, 118 UT_Structural_Velocity = 113, 119 UT_Warping_Constant = 114, 120 UT_Weight = 115, 121 UT_Weight_per_Unit_Length = 116, 122 UT_HVAC_ThermalConductivity = 117, 123 UT_HVAC_SpecificHeat = 118, 124 UT_HVAC_SpecificHeatOfVaporization = 119, 125 UT_HVAC_Permeability = 120, 126 UT_Electrical_Resistivity = 121, 127 UT_MassDensity = 122, 128 UT_MassPerUnitArea = 123, 129 UT_Pipe_Dimension = 124, 130 UT_PipeMass = 125, 131 UT_PipeMassPerUnitLength = 126 132 }
这么多枚举实际上和我要存储的数据基本不沾边,UT_Number看上去很美好但其实质是整数类型,还好有个custom可以解决问题,剩下的都可以不用管了。
然而,设置了单位之后,再跑一下下,哎?怎么还是没反应?
TryCatch大法好。。。
Autodesk.Revit.Exceptions.ArgumentException: The displayUnits value is not compatible with the field description. …
这次是displayUnits,按句子直接意思就是说“嘿,屌丝,你丫的存储的数值与你定义的不是一个玩意儿!”
红果果的嘲讽。。。
我定了什么呢?double类型,存储的时候输入的也是个double数值,反复查了n遍代码也没什么不同,然后想起了unittype,custom顾名思义自定义类型,但是这个displayunits又在哪里设置呢?
网上查了,群里大佬们也问过,么得反应!这东西要么少有人用,要么懒得发帖子,行吧,好歹最终还是让我找到了相关文章,不过是全英文的。
具体就不翻译了,没英语基础的用谷歌机翻也能看懂,大意就是说这东西存的时候,唯独double类型及其相关的数据(实际上基本浮点数类型都要算),或者部分复杂的数据在创建field时需要单独指定单位类型(UnitType),而在存的时候又要指定配套的DisplayUnitType,最鬼畜的是,取数据的时候也要指定DisplayUnitType(我以后只存字符串行不,revit大哥Orz.....)
于是乎,在对数据的定义,存储和取数据三个地方都设定好单位类型之后,我终于搞定了!!!
个人建议,如果非要使用外部扩展存储数据的话,尽量不存浮点类型的数据,实在不行用字符串存,取出来之后转换一下都比直接存方便,至于autodesk那帮大佬为何把这东西这么设定,咱也不敢说,咱也不敢问QAQ
原文:https://www.cnblogs.com/FireFlyWings/p/12193207.html