using System; using System.Collections.Generic; using System.ComponentModel; using Autodesk.Revit.DB; namespace Revit.SDK.Samples.ProjectInfo.CS { /// /// Converts City with string /// public class CityConverter : TypeConverter { public static List Cities; public const string UserDefined = "User Defined"; static CityConverter() { Cities = new List(); foreach (City city in RevitStartInfo.RevitApp.Cities) { Cities.Add(city); } } #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. /// /// Element collection retrieved through filtering current Revit document elements public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return new StandardValuesCollection(Cities); } /// /// Converts from string. /// /// Converted string public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } /// /// Converts to string. /// /// Converted string public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return destinationType.Equals(typeof(string)) || base.CanConvertTo(context, destinationType); } /// /// Returns an element from a string contains its id /// /// 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 an element /// An element if the element exists, otherwise null public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { string text = value as string; if (!string.IsNullOrEmpty(text)) { foreach (City city in Cities) { if (city.Name == text) return city; } } return base.ConvertFrom(context, culture, value); } /// /// Returns element name. /// returns empty string if value is null or Element.Name throws an exception. /// /// 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. /// Converted string 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 UserDefined; City city = value as City; if (city != null) return city.Name; else return null; } return base.ConvertTo(context, culture, value, destinationType); } #endregion } }