private void SetAnnotationValue(SkylineObject skylineObject, AnnotationDef annotationDef, string strValue)
        {
            object value;

            if (string.IsNullOrEmpty(strValue))
            {
                value = null;
            }
            else
            {
                switch (annotationDef.Type)
                {
                case AnnotationDef.AnnotationType.number:
                    value = double.Parse(strValue, CultureInfo);
                    break;

                case AnnotationDef.AnnotationType.true_false:
                    if (false.ToString(CultureInfo) == strValue)
                    {
                        value = false;
                    }
                    else
                    {
                        value = true;
                    }
                    break;

                default:
                    value = strValue;
                    break;
                }
            }
            skylineObject.SetAnnotation(annotationDef, value);
        }
 private IEnumerable <string> GetRowValues(ExportAnnotationSettings settings,
                                           ElementHandler elementHandler, SkylineObject skylineObject)
 {
     foreach (var annotation in settings.AnnotationNames)
     {
         var annotationDef = elementHandler.FindAnnotation(annotation);
         if (annotationDef == null)
         {
             yield return(null);
         }
         else
         {
             yield return(FormatAnnotationValue(skylineObject, annotationDef));
         }
     }
     foreach (var property in settings.PropertyNames)
     {
         var propertyInfo = elementHandler.FindProperty(property);
         if (propertyInfo == null)
         {
             yield return(null);
         }
         else
         {
             yield return(propertyInfo.ValueToText(CultureInfo, propertyInfo.GetValue(skylineObject)));
         }
     }
 }
Exemple #3
0
        public object GetAnnotation(AnnotationDef annotationDef, Type skylineObjectType,
                                    SkylineObject skylineObject, Annotations annotations)
        {
            var expression = annotationDef.Expression;

            if (expression == null)
            {
                return(annotations.GetAnnotation(annotationDef));
            }

            ColumnSelector columnSelector = GetColumnSelector(skylineObjectType, expression.Column);

            if (!columnSelector.IsValid)
            {
                return(NAME_ERROR);
            }

            try
            {
                if (expression.AggregateOperation == null)
                {
                    return(ConvertAnnotationValue(annotationDef, columnSelector.GetSingleValue(skylineObject)));
                }

                return(ConvertAnnotationValue(annotationDef, columnSelector.AggregateValues(expression.AggregateOperation, skylineObject)));
            }
            catch (Exception)
            {
                return(ERROR_VALUE);
            }
        }
            public Annotations UpdateAnnotations(Annotations annotations, SkylineObject skylineObject)
            {
                foreach (var annotationDef in AnnotationDefs)
                {
                    annotations =
                        annotations.ChangeAnnotation(annotationDef, skylineObject.GetAnnotation(annotationDef));
                }

                return(annotations);
            }
        private string FormatAnnotationValue(SkylineObject skylineObject, AnnotationDef annotationDef)
        {
            var value = skylineObject.GetAnnotation(annotationDef);

            if (value == null || false.Equals(value))
            {
                return(string.Empty);
            }
            if (value is double d)
            {
                return(d.ToString(Formats.RoundTrip, CultureInfo));
            }
            return(value.ToString());
        }
        public void ReadAllAnnotations(CancellationToken cancellationToken, DsvFileReader fileReader)
        {
            var fieldNames         = fileReader.FieldNames;
            int locatorColumnIndex = fieldNames.IndexOf(COLUMN_LOCATOR);

            if (locatorColumnIndex < 0)
            {
                throw new InvalidDataException(string.Format(Resources.Columns_Columns_Missing_column___0__,
                                                             COLUMN_LOCATOR));
            }
            string[] row;
            while ((row = fileReader.ReadLine()) != null)
            {
                cancellationToken.ThrowIfCancellationRequested();
                ElementLocator elementLocator = ElementLocator.Parse(row[locatorColumnIndex]);
                var            elementRef     = ElementRefs.FromObjectReference(elementLocator);
                ElementHandler handler;
                if (!_elementHandlers.TryGetValue(elementRef.ElementType, out handler))
                {
                    throw ElementNotSupportedException(elementRef);
                }
                SkylineObject element = handler.FindElement(elementRef);
                if (element == null)
                {
                    throw ElementNotFoundException(elementRef);
                }
                for (int icol = 0; icol < fieldNames.Count; icol++)
                {
                    if (icol == locatorColumnIndex)
                    {
                        continue;
                    }
                    string            fieldName     = fieldNames[icol];
                    TextColumnWrapper propertyInfo  = null;
                    AnnotationDef     annotationDef = null;
                    if (fieldName.StartsWith(PROPERTY_PREFIX))
                    {
                        propertyInfo = handler.FindProperty(fieldName.Substring(PROPERTY_PREFIX.Length));
                    }
                    else if (fieldName.StartsWith(ANNOTATION_PREFIX))
                    {
                        annotationDef = handler.FindAnnotation(fieldName.Substring(ANNOTATION_PREFIX.Length));
                    }
                    if (propertyInfo == null && annotationDef == null)
                    {
                        propertyInfo = handler.FindProperty(fieldName);
                        if (propertyInfo == null)
                        {
                            annotationDef = handler.FindAnnotation(fieldName);
                        }
                    }
                    string fieldValue = row[icol];
                    if (propertyInfo == null && annotationDef == null)
                    {
                        if (string.IsNullOrEmpty(fieldValue))
                        {
                            continue;
                        }
                        throw AnnotationDoesNotApplyException(fieldName, elementRef);
                    }

                    if (propertyInfo != null)
                    {
                        object value = propertyInfo.ParseTextValue(CultureInfo, fieldValue);
                        propertyInfo.SetValue(element, value);
                    }
                    if (annotationDef != null)
                    {
                        SetAnnotationValue(element, annotationDef, fieldValue);
                    }
                }
            }
        }