Dimensions.cs 8.3 KB

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