// // (C) Copyright 2003-2008 by Autodesk, Inc. // // Permission to use, copy, modify, and distribute this software in // object code form for any purpose and without fee is hereby granted, // provided that the above copyright notice appears in all copies and // that both that copyright notice and the limited warranty and // restricted rights notice below appear in all supporting // documentation. // // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. // // Use, duplication, or disclosure by the U.S. Government is subject to // restrictions set forth in FAR 52.227-19 (Commercial Computer // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable. // using System; using System.ComponentModel; using System.Collections.Generic; using System.Reflection; using Autodesk.Revit; using Autodesk.Revit.Parameters; namespace Revit.SDK.Samples.ProjectInfo.CS { /// /// Type converter for wrapper classes /// public class WrapperConverter : ExpandableObjectConverter { #region Methods /// /// 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); } /// /// Converts to string. If value is null, convert it to "(null)". /// if value has a "Name" property, returns its name. otherwise, returns "(...)". /// /// 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 "(null)"; // get its name Type type = value.GetType(); string wrapperType = type.ToString(); MethodInfo mi = type.GetMethod("get_Name", new Type[0]); if (mi != null) { return mi.Invoke(value, new object[0]).ToString(); } // if no name return "(...)"; } return base.ConvertTo(context, culture, value, destinationType); } #endregion }; /// /// Type converter for Element /// public class ElementConverter : 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. /// /// Element collection retrieved through filtering current Revit document elements public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { Type targetType = context.PropertyDescriptor.PropertyType; // using type filter to get the target type objects Autodesk.Revit.TypeFilter typeFilter = RevitStartInfo.RevitApp.Create.Filter.NewTypeFilter(targetType, true); ElementIterator elementIterator = RevitStartInfo.RevitDoc.get_Elements(typeFilter); // create a list List list = new List(); elementIterator.Reset(); while (elementIterator.MoveNext()) { list.Add(elementIterator.Current as Element); } 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 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)) { Type targetType = context.PropertyDescriptor.PropertyType; // using type filter to get the target type objects Autodesk.Revit.TypeFilter typeFilter = RevitStartInfo.RevitApp.Create.Filter.NewTypeFilter(targetType, true); ElementIterator elementIterator = RevitStartInfo.RevitDoc.get_Elements(typeFilter); elementIterator.Reset(); // find one element using its name while (elementIterator.MoveNext()) { if((elementIterator.Current as Element).Name == text) { return elementIterator.Current; } } } 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 string.Empty; Element element = value as Element; if (element != null) { string elementName = string.Empty; try { elementName = element.Name; } catch { } return elementName; } } return base.ConvertTo(context, culture, value, destinationType); } #endregion }; /// /// 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 TypeConverter.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.get_Constructions(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 }; /// /// Converter for Enumeration types of RevitAPI /// public abstract class RevitEnumConverter : EnumConverter { #region Fields /// /// Dictionary contains enum and string map /// Dictionary m_map = null; #endregion #region Properties /// /// Gets the enum-string map /// protected abstract Dictionary EnumMap { get; } #endregion #region Constructors /// /// Initialize private variables /// /// Enumeration type public RevitEnumConverter(Type type) : base(type) { m_map = this.EnumMap; } #endregion #region Methods /// /// Returns a collection of standard values from the default context for the /// data type this type converter is designed for. /// /// All enum items public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return new StandardValuesCollection(m_map.Keys); } /// /// Returns an enum item 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 /// An enum item public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { object enumValue = value; string valueText = value.ToString(); foreach (KeyValuePair pair in m_map) { if (pair.Value == valueText) { enumValue = pair.Key.ToString(); } } return base.ConvertFrom(context, culture, enumValue); } /// /// Convert enum item 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. /// Corresponding string related with the enum item public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { object enumValue = base.ConvertTo(context, culture, value, destinationType); object enumObject = Enum.Parse(this.EnumType, enumValue.ToString()); return m_map[enumObject]; } #endregion }; /// /// Converter for BuildingType /// public class BuildingTypeConverter : RevitEnumConverter { #region Constructors /// /// Constructor /// /// enumeration type public BuildingTypeConverter(Type type) : base(type) { } #endregion #region Properties /// /// Gets the enum-string map /// protected override Dictionary EnumMap { get { return RevitStartInfo.BuildingTypeMap; } } #endregion }; /// /// Converter for ServiceType /// public class ServiceTypeConverter : RevitEnumConverter { #region Constructors /// /// Constructor /// /// enumeration type public ServiceTypeConverter(Type type) : base(type) { } #endregion #region Properties /// /// Gets the enum-string map /// protected override Dictionary EnumMap { get { return RevitStartInfo.ServiceTypeMap; } } #endregion }; /// /// Converter used to convert TimeZone /// public class TimeZoneConverter : 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. /// /// Element collection retrieved through filtering current Revit document elements public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return new StandardValuesCollection(RevitStartInfo.TimeZones); } #endregion }; }