Dimensions.cs 8.9 KB

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