using System; using System.Collections.Generic; using System.ComponentModel; using Autodesk.Revit.DB; using ConstructionType = Autodesk.Revit.DB.Analysis.ConstructionType; namespace Revit.SDK.Samples.ProjectInfo.CS { /// /// Converter for ConstructionWrapper /// public class ConstructionWrapperConverter : TypeConverter { #region Methods /// /// Returns whether this object supports a standard set of values that can be /// picked from a list, using the specified context. /// /// true public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } /// /// Returns whether the collection of standard values returned from System.ComponentModel.TypeConverter.GetStandardValues() /// is an exclusive list. /// /// true public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return true; } /// /// Returns a collection of standard values from the default context for the /// data type this type converter is designed for. /// /// ConstructionWrapper collection depends on current context public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { List list = new List(); // convert property name to ConstructionType ConstructionType constructionType = (ConstructionType)Enum.Parse(typeof(ConstructionType), context.PropertyDescriptor.Name); // convert instance to MEPBuildingConstructionWrapper MEPBuildingConstructionWrapper mEPBuildingConstruction = context.Instance as MEPBuildingConstructionWrapper; // get all Constructions from MEPBuildingConstructionWrapper and add them to a list foreach (Construction con in mEPBuildingConstruction.GetConstructions(constructionType)) { list.Add(new ConstructionWrapper(con)); } // sort the list list.Sort(); return new StandardValuesCollection(list); } /// /// Can convert from string /// /// An ITypeDescriptorContext that provides a format context. /// A Type that represents the type you want to convert from. /// true if sourceType is string, otherwise false public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } /// /// Can be converted to string /// /// true if destinationType is string, otherwise false public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return destinationType.Equals(typeof(System.String)) || base.CanConvertTo(context, destinationType); } /// /// Returns a ConstructionWrapper from a string /// /// An System.ComponentModel.ITypeDescriptorContext /// that provides a format context. /// An optional System.Globalization.CultureInfo. /// If not supplied, the current culture is assumed. /// string to be converted to /// A ConstructionWrapper from the StandardValues public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { string text = value as string; if (!string.IsNullOrEmpty(text)) { foreach (ConstructionWrapper con in this.GetStandardValues(context)) { if (con.Name == text) { return con; } } } return base.ConvertFrom(context, culture, value); } /// /// Convert object to string /// /// An ITypeDescriptorContext that provides a format context. /// A CultureInfo. If null is passed, the current culture is assumed. /// The Object to convert. /// The Type to convert the value parameter to. /// empty string if current construction is null, otherwise construction name public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } if (destinationType == typeof(string)) { if (value == null) return string.Empty; ConstructionWrapper construction = value as ConstructionWrapper; if (construction != null) { return construction.Name; } } return base.ConvertTo(context, culture, value, destinationType); } #endregion }; }