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

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

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

Parameter of Revit API – 4: Parameter Information

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

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

积分
12404

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-9 12:27:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

Revit Parameter has a lot of relevant information such as group, type, unit, storage type, shared or native/built-in, read only or writable, and the most important ones name, value and id (BuiltInParameter value or GUID), but they are scattered around in a few different API objects such as the Definition (which has two derivatives InternalDefinition and ExternalDefinition to address different kinds of Parameter instances).

In this post, let’s see how to collect all these pieces of information from an Element and put them together into a good place to make it ready to use for any possible purposes.
The following code can find all the information from a selected Element:
                Element element = SelElement(cmdData.Application.ActiveUIDocument.Selection).Element;
                List<undefinedarameter> paramList = ParametersOf(element);
                string str = string.Empty;
                foreach (Parameter p in paramList)
                {
                    //The following has to be done because the DUT will throw out exceptions many times!
                    string unitType = string.Empty;
                    try { unitType = p.DisplayUnitType.ToString(); }
                    catch { }
                 
                    str += string.Format("{0}\tPG:{1}\tPT:{2}\tST:{3}\tDUT:{4}\n\tSH:{5}\tID:{6}\tRO:{7}\n",
                        p.Definition.Name,
                        p.Definition.ParameterGroup,
                        p.Definition.ParameterType,
                        p.StorageType,
                        unitType,
                        p.IsShared,
                        p.IsShared ? p.GUID.ToString()undefinedp.Definition as InternalDefinition).BuiltInParameter.ToString(),
                        p.IsReadOnly);
                }
                MessageBox.Show(str, "Information of Element Parameters");
As commented in the code, the DisplayUnitType property of the Parameter object looks very special. It throws out exceptions very often and this will interrupt the whole process. The only way for programmers to address the issue is to use a try/catch block as demonstrated but that is definitely not something so cool. It is somewhat due to the fact that the DisplayUnitType enumeration lacks a value to indicate that DisplayUnitType does not make any sense in some situations just like the INVALID value in the BuiltInParameterGroup enumeration or Invalid in ParameterType.

Anyway we have a way to work around the issue and the above code can successfully print out the Parameter information like:


As mentioned earlier, we’d like to have a nice place to keep all the information so that we can use it in an easy way when necessary. The following help classes and methods will store all the Parameter information of an Element into a List of an informative object:

        public class ParameterInfo
        {
            public string Name { get; set; }
            public string Value { get; set; }
            public BuiltInParameterGroup Group { get; set; }
            public ParameterType Type { get; set; }
            public StorageType Storage { get; set; }
            public string Unit { get; set; } //DisplayUnitType doesn't work!
            public bool Shared { get; set; }
            public string ID { get; set; }
            public bool ReadOnly { get; set; }
        }
        public static List<undefinedarameterInfo> GetParametersInfo(Element e)
        {
            List<undefinedarameterInfo> paramList =
                (from Parameter p in e.Parameters
                 select new ParameterInfo
                 {
                     Name = p.Definition.Name,
                     Value = p.AsValueString(),
                     Group = p.Definition.ParameterGroup,
                     Type = p.Definition.ParameterType,
                     Storage = p.StorageType,
                     Unit = GetDUTString(p),
                     Shared = p.IsShared,
                     ReadOnly = p.IsReadOnly,
                     ID = p.IsShared ? p.GUID.ToString() :
                     (p.Definition as InternalDefinition).BuiltInParameter.ToString()
                 }).ToList();
            return paramList;
        }

And the following code will convert the informative object List into a single string:

        public string ParametersInfoToCSVString(List<undefinedarameterInfo> infoList, ref string title)
        {
            StringBuilder sb = new StringBuilder();
            PropertyInfo[] propInfoArrary = typeof(ParameterInfo).GetProperties();
            foreach (PropertyInfo pi in propInfoArrary)
            {
                title += pi.Name + ",";
            }
            title = title.Remove(title.Length - 1);
            
            foreach (ParameterInfo info in infoList)
            {
                foreach (PropertyInfo pi in propInfoArrary)
                {
                    object obj = info.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, info, null);
                    sb.Append( (obj == null ? string.Empty : obj.ToString()) + ",");
                }
                sb.Remove(sb.Length - 1, 1).Append(Environment.NewLine);
            }
            return sb.ToString();
        }
Then we can write all the Parameter information of a selected Element to a CSV file, for example:
        public static Reference SelElement(Selection selection)
        {
            Reference picked = selection.PickObject(ObjectType.Element, "undefinedlease select an element");
            return picked;
        }
                …
                Element element = SelElement(cmdData.Application.ActiveUIDocument.Selection).Element;
                List<undefinedarameterInfo> paramsInfo = GetParametersInfo(element);
                using (StreamWriter sw = new StreamWriter(@"c:\ParametersInfo.csv"))
                {
                    string title = string.Empty;
                    string rows = ParametersInfoToCSVString(paramsInfo, ref title);
                    sw.WriteLine(title);
                    sw.Write(rows);
                }
                …
Finally the CSV file can be read into a spreadsheet of Excel for example:



The informative object list can be used for any other purposes as well for sure.

Parameter Infoer of RevitAddinWizardcan help do all of these in a configurable and flexible way.


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对
工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-16 15:26

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

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