|
| 1 | +using RedButton.Common.Core.CollectionExtensions; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using Tekla.Structures.Model; |
| 6 | + |
| 7 | +namespace RedButton.Common.TeklaStructures.Model.Properties |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Extract properties for Tekla objects. |
| 11 | + /// </summary> |
| 12 | + public class PropertiesExtractor |
| 13 | + { |
| 14 | + private ModelObject _modelObject; |
| 15 | + private Hashtable _values; |
| 16 | + |
| 17 | + private List<TeklaPropertyString> _stringAttributes; |
| 18 | + private List<TeklaPropertyDouble> _doubleAttributes; |
| 19 | + private List<TeklaPropertyInt> _integerAttributes; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// .ctor |
| 23 | + /// </summary> |
| 24 | + /// <param name="modelObject"></param> |
| 25 | + public PropertiesExtractor(ModelObject modelObject) |
| 26 | + { |
| 27 | + _modelObject = modelObject; |
| 28 | + _stringAttributes = new List<TeklaPropertyString>(); |
| 29 | + _doubleAttributes = new List<TeklaPropertyDouble>(); |
| 30 | + _integerAttributes = new List<TeklaPropertyInt>(); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Add string attribute |
| 35 | + /// </summary> |
| 36 | + /// <param name="attribute">Attribute name</param> |
| 37 | + public TeklaPropertyString AddStringAttribute(string attribute) |
| 38 | + { |
| 39 | + var property = new TeklaPropertyString(attribute, null); |
| 40 | + _stringAttributes.Add(property); |
| 41 | + return property; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Add double attribute |
| 46 | + /// </summary> |
| 47 | + /// <param name="attribute">Attribute name</param> |
| 48 | + public TeklaPropertyDouble AddDoubleAttribute(string attribute) |
| 49 | + { |
| 50 | + var property = new TeklaPropertyDouble(attribute, null); |
| 51 | + _doubleAttributes.Add(property); |
| 52 | + return property; |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Add integer attribute |
| 57 | + /// </summary> |
| 58 | + /// <param name="attribute">Attribute name</param> |
| 59 | + public TeklaPropertyInt AddIntegerAttribute(string attribute) |
| 60 | + { |
| 61 | + var property = new TeklaPropertyInt(attribute, null); |
| 62 | + _integerAttributes.Add(property); |
| 63 | + return property; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Refresh report properties |
| 68 | + /// </summary> |
| 69 | + public void ExtractProperties() |
| 70 | + { |
| 71 | + if (_modelObject == null) |
| 72 | + { |
| 73 | + _values = new Hashtable(); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + var valuesCount = _stringAttributes.Count + _doubleAttributes.Count + _integerAttributes.Count; |
| 78 | + |
| 79 | + _values = new Hashtable(valuesCount); |
| 80 | + |
| 81 | + _modelObject.GetAllReportProperties(_stringAttributes.Select(a => a.Name).ToArrayList(), |
| 82 | + _doubleAttributes.Select(a => a.Name).ToArrayList(), |
| 83 | + _integerAttributes.Select(a => a.Name).ToArrayList(), |
| 84 | + ref _values); |
| 85 | + |
| 86 | + foreach (var property in _stringAttributes) |
| 87 | + property.Value = GetExtractValue<string>(property.Name); |
| 88 | + |
| 89 | + foreach (var property in _doubleAttributes) |
| 90 | + property.Value = GetExtractValue<double>(property.Name); |
| 91 | + |
| 92 | + foreach (var property in _integerAttributes) |
| 93 | + property.Value = GetExtractValue<int>(property.Name); |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Get value |
| 98 | + /// </summary> |
| 99 | + /// <typeparam name="T">Type</typeparam> |
| 100 | + /// <param name="propertyName">Property name</param> |
| 101 | + /// <returns></returns> |
| 102 | + private T GetExtractValue<T>(string propertyName) |
| 103 | + { |
| 104 | + var val = _values[propertyName]; |
| 105 | + return (T)val; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments