Parameter of Revit API – 51: Parameter Converter – FamilyParameter to SharedPa
We talked about programmatically converting parameters from one to another manually before. Now let’s see how Parameter Converter of RevitAddinWizard can assist us to hit the target with just a few mouse clicks. The Parameter Converter can be found from either Revit Addin Coder sub-menu under the Tools or from Revit Addin Coder toolbar.When the menu item or the tool button is clicked, the Parameter Converter window will show up:
http://spiderinnet.typepad.com/.a/6a013489b64e31970c014e88d9bfe4970d-800wi
If the FamilyParameter (to SharedParameter) Converter is selected from the check list box and a class is chosen from the combo box, a FamilyParameter to SharedParameter Converter will be created and added into the class automatically. The entire converter code and necessary namespaces and class declarations have been appended below:
#region Namespacesusing System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;#endregion
namespace RevitAddinCS1
{
class ParameterConverter
{
public class RawFamilyParameterInfo
{
publicstring Name { get; set; }
publicstring ElementID { get; set; }
publicstring GUID { get; set; }
publicstring BuiltinID { get; set; }
publicBuiltInParameterGroup Group { get; set; }
publicParameterType Type { get; set; }
publicStorageType Storage { get; set; }
publicstring Unit { get; set; }
publicbool Shared { get; set; }
publicbool ReadOnly { get; set; }
publicbool Instance { get; set; }
publicbool Reporting { get; set; }
publicbool FormulaDetermined { get; set; }
publicbool CanAssignFormula { get; set; }
} public static string RawGetDUTString(FamilyParameter p)
{
string unitType = string.Empty;
try { unitType = p.DisplayUnitType.ToString(); }
catch { }
return unitType;
} public static List<RawFamilyParameterInfo> RawGetFamilyParametersInfo(Document doc)
{
List<RawFamilyParameterInfo> paramList =
(from FamilyParameter p in doc.FamilyManager.Parameters
select new RawFamilyParameterInfo
{
Name = p.Definition.Name,
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 = RawGetDUTString(p),
Shared = (p.Definition as ExternalDefinition) != null,
ReadOnly = p.IsReadOnly,
Instance = p.IsInstance,
Reporting = p.IsReporting,
FormulaDetermined = p.IsDeterminedByFormula,
CanAssignFormula = p.CanAssignFormula,
}).ToList();
return paramList;
}
public static string RawParametersInfoToCSVString<T>(List<T> infoList, ref string title)
{
StringBuilder sb = new StringBuilder();
PropertyInfo[] propInfoArrary = typeof(T).GetProperties();
foreach (PropertyInfo pi in propInfoArrary)
{
title += pi.Name + ",";
}
title = title.Remove(title.Length - 1);
foreach (T info in infoList)
{
foreach (PropertyInfo pi in propInfoArrary)
{
object obj = info.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, info, null);
IList list = obj as IList;
if (list != null)
{
string str = string.Empty;
foreach (object e in list)
{
str += e.ToString() + ";";
}
str = str.Remove(str.Length - 1);
sb.Append(str + ",");
}
else
{
sb.Append((obj == null ? string.Empty : obj.ToString()) + ",");
}
}
sb.Remove(sb.Length - 1, 1).Append(Environment.NewLine);
}
return sb.ToString();
}
public static List<T> RawConvertSetToList<T>(IEnumerable set)
{
List<T> list = (from T p in set select p).ToList<T>();
return list;
}
public static Definition RawCreateSharedParameter(RvtApplication app, string name, string group, ParameterType type, bool visible)
{
DefinitionFile defFile = app.OpenSharedParameterFile();
if (defFile == null) throw new Exception("No SharedParameter File!");
DefinitionGroup dg = RawConvertSetToList<DefinitionGroup>(defFile.Groups).FirstOrDefault(g => g.Name == group);
if (dg == null) dg = defFile.Groups.Create(group);
Definition def = RawConvertSetToList<Definition>(dg.Definitions).FirstOrDefault(d => d.Name == name);
if (def != null) return def; //dg.Definitions.Erase(def); //ReadOnly Exception!!
def = dg.Definitions.Create(name, type, visible);
return def;
}
public static void ConvertFamilyParameterInfoToSharedParameter(Document familyDoc, RawFamilyParameterInfo info, bool convertSharedOnly, bool visibilityOverride)
{
if (info.Type == ParameterType.Invalid)
{
info.Type = ParameterType.Text;
}
if (convertSharedOnly)
{
if (info.Shared)
{
RawCreateSharedParameter(familyDoc.Application, info.Name, "For_" + familyDoc.Title, info.Type, visibilityOverride);
}
}
else
{
RawCreateSharedParameter(familyDoc.Application, info.Name, "For_" + familyDoc.Title, info.Type, visibilityOverride);
}
}
public static void ConvertFamilyParameterToSharedParameter(Document familyDoc, string name, bool convertSharedOnly, bool visibilityOverride)
{
List<RawFamilyParameterInfo> pList = RawGetFamilyParametersInfo(familyDoc);
List<RawFamilyParameterInfo> pListOfInterest = pList.FindAll(p => p.Name == name);
foreach (RawFamilyParameterInfo info in pListOfInterest)
{
ConvertFamilyParameterInfoToSharedParameter(familyDoc, info, convertSharedOnly, visibilityOverride);
}
}
public static void ConvertAllFamilyParametersToSharedParameters(Document familyDoc, bool convertSharedOnly, bool visibilityOverride)
{
List<RawFamilyParameterInfo> pList = RawGetFamilyParametersInfo(familyDoc);
foreach (RawFamilyParameterInfo info in pList)
{
ConvertFamilyParameterInfoToSharedParameter(familyDoc, info, convertSharedOnly, visibilityOverride);
}
}
}
}
We would not create test command to extercise these parameter converter code here. In case needed, please refer to earlier posts for ideas and code examples.
As mentioned, Parameter Converter of RevitAddinCodercan create all these code automatically in a second.
顶起来…………
谢谢BIM大神…
楼主辛苦……
非常感谢!!
页:
[1]