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

萧闫子 发表于 2014-1-9 12:24:36

Parameter of Revit API – 12: FamilyParameter Information

Revit family parameter (FamilyParameter) has more information that the model Parameter. Besides those common parameter properties like BuiltInParameterGroup, ParameterType, StorageType, and DisplayUnitType, the FamilyParameter also has some information about whether it is for report purpose only (IsReporting), instance applicable or type applicable (IsInstance), formula assignable (CanAssignFormula), and so on.
In this post, let’s see how to collect the information of FamilyParameter from a family document and put it together into a good place which is ready to use for any possible purposes.The following help classes and methods will store and handle the FamilyParameter information regarding a family document:      public class FamilyParameterInfo
      {
            public string Name { get; set; }
            //public string Value { get; set; } // Parameter Only!
            public string ElementID { get; set; }
            public string GUID { get; set; }
            public string BuiltinID { 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; }    // Both but different handling!
            public bool Instance { get; set; }// FamilyParameter Only!
            public bool Reporting { get; set; }// FamilyParameter Only!
            public bool FormulaDetermined { get; set; } // FamilyParameter Only!
            public bool CanAssignFormula { get; set; }
            public bool ReadOnly { get; set; }
      }      public static string GetDUTString(FamilyParameter p)
      {
            string unitType = string.Empty;
            try { unitType = p.DisplayUnitType.ToString(); }
            catch { }            return unitType;
      }      public static List<FamilyParameterInfo> GetFamilyParametersInfo(Document doc)
      {
            List<FamilyParameterInfo> paramList =
                (from FamilyParameter p in doc.FamilyManager.Parameters
               select new FamilyParameterInfo
               {
                     Name = p.Definition.Name,
                     //Value = p.AsValueString(),   //N/A
                     ElementID = p.Id.IntegerValue.ToString(),
                     GUID = (p.Definition as ExternalDefinition) != null ?
                        (p.Definition as ExternalDefinition).GUID.ToString() : string.Empty,
                     BuiltinID = (p.Definition as InternalDefinition) != null ?
                        (p.Definition as InternalDefinition).BuiltInParameter.ToString() : string.Empty,
                     Group = p.Definition.ParameterGroup,
                     Type = p.Definition.ParameterType,
                     Storage = p.StorageType,
                     Unit = GetDUTString(p),
                     Shared = (p.Definition as ExternalDefinition) != null,
                     Instance = p.IsInstance,
                     Reporting = p.IsReporting,
                     FormulaDetermined = p.IsDeterminedByFormula,
                     CanAssignFormula = p.CanAssignFormula,
                     ReadOnly = p.IsReadOnly,
               }).ToList();            return paramList;
      }And the following code will convert the informative object List into a single string:
      public string ParametersInfoToCSVString(List<FamilyParameterInfo> infoList, ref string title)
      {
            StringBuilder sb = new StringBuilder();
            PropertyInfo[] propInfoArrary = typeof(FamilyParameterInfo).GetProperties();
            foreach (PropertyInfo pi in propInfoArrary)
            {
                title += pi.Name + ",";
            }
            title = title.Remove(title.Length - 1);            foreach (FamilyParameterInfo 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 FamilyParameter information of an opened family document to a CSV file.
                …
                if (CachedDoc.IsFamilyDocument)
                {
                  List<FamilyParameterInfo> famParamsInfo = GetFamilyParametersInfo(CachedDoc);
                  using (StreamWriter sw = new StreamWriter(@"c:\FamilyParametersInfo.csv"))
                  {
                        string title = string.Empty;
                        string rows = ParametersInfoToCSVString(famParamsInfo, ref title);
                        sw.WriteLine(title);
                        sw.Write(rows);
                  }
                }
                …Finally the CSV file can be read into a spreadsheet of Excel.http://spiderinnet.typepad.com/.a/6a013489b64e31970c01538e1a7540970b-800wi

From the output, we can notice something that is not straightforward in the FamilyParameter API.
[*]A FamilyParameter can be neither a built-in parameter (as indicated by the INVALID value of the BuiltInParameter enumeration) nor a shared Parameter (as indicated by the null ExternalDefinition or empty GUID)!
[*]The sign of the ElementId integer value of the FamilyParameter can be used to indicate whether it has a valid BuiltInParameter value or not!
[*]The IsDeterminedByFormula seems to always return FALSE or has nothing to do with the CanAssignFormula property!
The informative object list can be used for any other purposes as well for sure.FamilyParameter Infoer of RevitAddinWizardcan help do all of these in a configurable and flexible way.

老鼠仔CH 发表于 2014-2-20 15:00:57

顶起来…………

页: [1]
查看完整版本: Parameter of Revit API – 12: FamilyParameter Information