EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 649|回复: 1
打印 上一主题 下一主题

Parameter of Revit API – 8: Values Read and Write

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12404

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-9 12:25:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Revit Parameter can store a few different data types including integer, double, string and ElementId just as the StorageType enumeration indicates. Of course, the None one does not really mean anything for data storage.

In this post, let’s see how to read Parameter values and write them with different approaches when applicable.  The following code can read all Parameter values from a selected Element such as a Wall:
Element element = SelElement(cmdData.Application.ActiveUIDocument.Selection).Element;
using (StreamWriter sw = new StreamWriter(@"c:\ParametersValue.txt"))
{
    sw.WriteLine(string.Format("{0, -25} {1,-10} {2, -10} {3, -20} {4, -10} {5, -25}",
        "Name", "Integer", "Double", "String", "ElementId", "ValueString"));
    foreach (Parameter p in element.Parameters)
    {
        sw.WriteLine(string.Format("{0, -25} {1,-10} {2, -10} {3, -20} {4, -10} {5, -25}",
            p.Definition.Name,
            (p.StorageType == StorageType.Integer ? p.AsInteger().ToString() : @"N/A"),
            (p.StorageType == StorageType.Double ? Math.Round(p.AsDouble(), 2).ToString() : @"N/A"),
            (p.StorageType == StorageType.String ? p.AsString() : @"N/A"),
            (p.StorageType == StorageType.ElementId ? p.AsElementId().IntegerValue.ToString() : @"N/A"),
            p.AsValueString()));
        if (!p.IsShared && (p.Definition as InternalDefinition).BuiltInParameter == BuiltInParameter.WALL_TOP_OFFSET)
        {
            p.SetValueString("1\'6\"");
        }
    }
}
The help method SelElement which can be created with the Object Picker in no time has also been appended below to make the example code complete:

        public static Reference SelElement(Selection selection)
        {
            Reference picked = selection.PickObject(ObjectType.Element, "Please select an element");
            return picked;
        }
The Parameter class provides different methods to convert the internal data to the right storage type. As shown in the code, each such method should be called based on the StorageType of the Parameter; otherwise exception may just be thrown out.
The Parameter also provides another method, AsValueString(), to interpret the Double type data with unit information. The good thing is that for other data types it would return an empty string rather than throw out some exceptions and this will surely make the coding path smoother.
One more good thing is that a corresponding write method is also provided, SetValueString(). As demonstrated, it accepts both data and unit information in a string argument and will convert it properly to the internal value and unit. The string "1\'6\"" in the example code will be properly interpreted as one foot and six inches by the Revit Parameter API.
And the better part is that it supports different units for a single parameter. For example, if a value in centimeter is provided like the following:
                p.SetValueString("25 cm");
the value 25 will be properly interpreted in centimeter and converted to the right internal unit (feet). Please just keep in mind to use the right unit abbreviations that the SetValueString() accepts and the Revit supports, otherwise something unexpected will happen for sure.
The Revit Parameter API also provides methods to set values individually for each supported data type:
Set(ElementId)
Set(Double)
Set(Int32)
Set(String)
A kind reminder once again: please do not forget to check the StorageType and the IsReadOnly flag before trying to write to any of parameters using the above methods.
If a wall is selected, the ParametersValue.txt file containing its parameter value information may look like:
Name                      Integer    Double     String   ElementId  ValueString
Length                    N/A        26.15      N/A      N/A        26' - 1 105/128"
Room Bounding             0          N/A        N/A      N/A              
Area                      N/A        259.94     N/A      N/A        259.94 SF
Top is Attached           0          N/A        N/A      N/A            
Related to Mass           0          N/A        N/A      N/A              
Top Extension Distance    N/A        0          N/A      N/A        0' - 0"
Phase Demolished          N/A        N/A        N/A      -1               
Mark                      N/A        N/A        Mark     N/A            
Comments                  N/A        N/A        Test     N/A            
Phase Created             N/A        N/A        N/A      21885            
Base Offset               N/A        0          N/A      N/A        0' - 0"
Top Constraint            N/A        N/A        N/A      118280           
Base is Attached          0          N/A        N/A      N/A               
Top Offset                N/A        0          N/A      N/A        0' - 0"
Volume                    N/A        252.8      N/A      N/A        252.80 CF
Location Line             0          N/A        N/A      N/A              
Base Constraint           N/A        N/A        N/A      30                 
Base Extension Distance   N/A        0          N/A      N/A        0' - 0"
Structural Usage          0          N/A        N/A      N/A              
Unconnected Height        N/A        11.5       N/A      N/A        11' - 6"
RevitAddinWizardprovides a few coders to manage Revit Parameter in various ways.


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对
工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言

4

主题

832

帖子

1368

积分

BIM经理

Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6

积分
1368
2F
发表于 2014-2-20 15:00:41 | 只看该作者
路过!!! 不发表意见……
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|EaBIM网 ( 苏ICP备2020058923号-1  苏公网安备32011502011255号

GMT+8, 2024-11-16 13:47

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表