Dimensions.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. namespace InABox.Core
  4. {
  5. public abstract class Dimensions<TLink,TUnit> : EnclosedEntity, IDimensions
  6. where TLink : DimensionUnitLink<TUnit>, new()
  7. where TUnit : DimensionUnit, new()
  8. {
  9. public Dimensions() : base() { }
  10. public Dimensions(Func<BaseObject> entity) : base(entity) { }
  11. [EditorSequence(1)]
  12. [RequiredColumn]
  13. [Caption("Sizing", IncludePath = false)]
  14. public abstract TLink Unit { get; set; }
  15. public IDimensionUnit GetUnit() => Unit;
  16. [DoubleEditor(Visible = Visible.Hidden)]
  17. [EditorSequence(2)]
  18. [Caption("Quantity", IncludePath = false)]
  19. [RequiredColumn]
  20. public abstract double Quantity { get; set; }
  21. [DoubleEditor(Visible = Visible.Hidden)]
  22. [EditorSequence(3)]
  23. [Caption("Length", IncludePath = false)]
  24. [RequiredColumn]
  25. public abstract double Length { get; set; }
  26. [DoubleEditor(Visible = Visible.Hidden)]
  27. [EditorSequence(4)]
  28. [Caption("Width", IncludePath = false)]
  29. [RequiredColumn]
  30. public abstract double Width { get; set; }
  31. [DoubleEditor(Visible = Visible.Hidden)]
  32. [EditorSequence(5)]
  33. [Caption("Height", IncludePath = false)]
  34. [RequiredColumn]
  35. public abstract double Height { get; set; }
  36. [DoubleEditor(Visible = Visible.Hidden)]
  37. [EditorSequence(6)]
  38. [Caption("Weight", IncludePath = false)]
  39. [RequiredColumn]
  40. public abstract double Weight { get; set; }
  41. [DoubleEditor(Visible = Visible.Optional, Editable = Editable.Hidden)]
  42. [Caption("Value", IncludePath = false)]
  43. [EditorSequence(7)]
  44. [RequiredColumn]
  45. public abstract double Value { get; set; }
  46. [TextBoxEditor(Visible = Visible.Default, Editable=Editable.Hidden)]
  47. [EditorSequence(8)]
  48. [Caption("Unit Size", IncludePath = false)]
  49. [RequiredColumn]
  50. public abstract String UnitSize { get; set; }
  51. protected override void Init()
  52. {
  53. base.Init();
  54. Unit = Activator.CreateInstance(typeof(TLink), new object[] { new Func<BaseObject>(LinkedEntity) }) as TLink;
  55. Unit.PropertyChanged += (s, e) =>
  56. {
  57. if(e.PropertyName == "ID")
  58. {
  59. DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.ID);
  60. }
  61. else if(e.PropertyName == "Formula")
  62. {
  63. DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.Formula);
  64. }
  65. else if(e.PropertyName == "Format")
  66. {
  67. DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.Format);
  68. }
  69. };
  70. }
  71. private bool bCalculating = false;
  72. private static Column<Dimensions<TLink,TUnit>> unitid = new Column<Dimensions<TLink,TUnit>>(x => x.Unit.ID);
  73. private static Column<Dimensions<TLink,TUnit>> quantity = new Column<Dimensions<TLink,TUnit>>(x => x.Quantity);
  74. private static Column<Dimensions<TLink,TUnit>> length = new Column<Dimensions<TLink,TUnit>>(x => x.Length);
  75. private static Column<Dimensions<TLink,TUnit>> width = new Column<Dimensions<TLink,TUnit>>(x => x.Width);
  76. private static Column<Dimensions<TLink,TUnit>> height = new Column<Dimensions<TLink,TUnit>>(x => x.Height);
  77. private static Column<Dimensions<TLink,TUnit>> weight = new Column<Dimensions<TLink,TUnit>>(x => x.Weight);
  78. private static Column<Dimensions<TLink,TUnit>> sizeformula = new Column<Dimensions<TLink,TUnit>>(x => x.Unit.Formula);
  79. private static Column<Dimensions<TLink,TUnit>> sizeformat = new Column<Dimensions<TLink,TUnit>>(x => x.Unit.Format);
  80. protected override void DoPropertyChanged(string name, object? before, object? after)
  81. {
  82. base.DoPropertyChanged(name, before, after);
  83. if (bCalculating)
  84. return;
  85. bCalculating = true;
  86. try
  87. {
  88. if (unitid.IsEqualTo(name))
  89. Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
  90. else if (quantity.IsEqualTo(name))
  91. Calculate(after, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
  92. else if (length.IsEqualTo(name))
  93. Calculate(Quantity, after, Width, Height, Weight, Unit.Formula, Unit.Format);
  94. else if (width.IsEqualTo(name))
  95. Calculate(Quantity, Length, after, Height, Weight, Unit.Formula, Unit.Format);
  96. else if (height.IsEqualTo(name))
  97. Calculate(Quantity, Length, Width, after, Weight, Unit.Formula, Unit.Format);
  98. else if (weight.IsEqualTo(name))
  99. Calculate(Quantity, Length, Width, Height, after, Unit.Formula, Unit.Format);
  100. else if (sizeformula.IsEqualTo(name))
  101. Calculate(Quantity, Length, Width, Height, Weight, after as string, Unit.Format);
  102. else if (sizeformat.IsEqualTo(name))
  103. Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, after as string);
  104. }
  105. finally
  106. {
  107. bCalculating = false;
  108. }
  109. }
  110. public void Set(IDimensionUnit unit, double quantity, double length, double width, double height, double weight)
  111. {
  112. bCalculating = true;
  113. try
  114. {
  115. Unit.ID = unit.ID;
  116. Unit.HasQuantity = unit.HasQuantity;
  117. Unit.HasLength = unit.HasLength;
  118. Unit.HasWidth = unit.HasWidth;
  119. Unit.HasHeight = unit.HasHeight;
  120. Unit.HasWeight = unit.HasWeight;
  121. Unit.Code = unit.Code;
  122. Unit.Description = unit.Description;
  123. Unit.Formula = unit.Formula;
  124. Unit.Format = unit.Format;
  125. Quantity = quantity;
  126. Length = length;
  127. Width = width;
  128. Height = height;
  129. Weight = weight;
  130. Calculate(quantity, length, width, height, weight, unit.Formula, unit.Format);
  131. }
  132. finally
  133. {
  134. bCalculating = false;
  135. }
  136. }
  137. private void Calculate(object? quantity, object? length, object? width, object? height, object? weight, string? formula, string? format)
  138. {
  139. if (Evaluate<double>(formula, quantity, length, width, height, weight, out double value))
  140. Value = value;
  141. if (Evaluate<String>(format, quantity, length, width, height, weight, out string unitsize))
  142. UnitSize = unitsize;
  143. }
  144. private bool Evaluate<T>(string? formula, object? quantity, object? length, object? width, object? height, object? weight, out T result)
  145. {
  146. if (!String.IsNullOrWhiteSpace(formula))
  147. {
  148. var variables = new Dictionary<string, object?>()
  149. {
  150. { "Quantity", Convert.ToDouble(quantity) },
  151. { "Length", Convert.ToDouble(length) },
  152. { "Width", Convert.ToDouble(width) },
  153. { "Height", Convert.ToDouble(height) },
  154. { "Weight", Convert.ToDouble(weight) }
  155. };
  156. try
  157. {
  158. var expr = new CoreExpression(formula);
  159. var eval = expr.Evaluate(variables);
  160. result = (T)CoreUtils.ChangeType(eval,typeof(T));
  161. return true;
  162. }
  163. catch (Exception e)
  164. {
  165. Logger.Send(LogType.Information, "", String.Format("Error in Formula: [{0}] ({1})", formula, e.Message));
  166. result = default(T);
  167. return false;
  168. }
  169. }
  170. result = default(T);
  171. return true;
  172. }
  173. public void CopyFrom(IDimensions source, bool observing = false)
  174. {
  175. if (!observing)
  176. SetObserving(false);
  177. Unit.ID = source.GetUnit().ID;
  178. Unit.Synchronise(source.GetUnit());
  179. Quantity = source.Quantity;
  180. Length = source.Length;
  181. Width = source.Width;
  182. Height = source.Height;
  183. Weight = source.Weight;
  184. Value = source.Value;
  185. UnitSize = source.UnitSize;
  186. if (!observing)
  187. SetObserving(true);
  188. }
  189. }
  190. }