Preserves global information
Exemplo n.º 1
0
 /// <summary>
 /// Returns element name.
 /// returns empty string if value is null or Element.Name throws an exception.
 /// </summary>
 /// <param name="context">An ITypeDescriptorContext that provides a format context. </param>
 /// <param name="culture">A CultureInfo. If null is passed, the current culture is assumed. </param>
 /// <param name="value">The Object to convert. </param>
 /// <param name="destinationType">The Type to convert the value parameter to. </param>
 /// <returns>Converted string</returns>
 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);
         }
         ElementId elementId = value as ElementId;
         if (elementId != null)
         {
             Element element = RevitStartInfo.GetElement(elementId);
             if (element != null)
             {
                 string elementName = string.Empty;
                 try
                 {
                     elementName = element.Name;
                 }
                 catch
                 {
                 }
                 return(elementName);
             }
         }
     }
     return(base.ConvertTo(context, culture, value, destinationType));
 }
Exemplo n.º 2
0
        /// <summary>
        /// Returns an element from a string contains its id
        /// </summary>
        /// <param name="context">An System.ComponentModel.ITypeDescriptorContext
        /// that provides a format context.</param>
        /// <param name="culture">An optional System.Globalization.CultureInfo.
        /// If not supplied, the current culture is assumed.</param>
        /// <param name="value">string to be converted to an element</param>
        /// <returns>An element if the element exists, otherwise null</returns>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string text = value as string;

            if (!string.IsNullOrEmpty(text))
            {
                StandardValuesCollection svc = GetStandardValues(context);
                foreach (ElementId elementId in svc)
                {
                    Element element = RevitStartInfo.GetElement(elementId);
                    if (element.Name == text)
                    {
                        return(element.Id);
                    }
                }
            }
            return(base.ConvertFrom(context, culture, value));
        }