converter.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import 'dart:developer';
  2. import 'package:fis_measure/interfaces/process/calculators/values.dart';
  3. import 'package:fis_measure/interfaces/process/items/item.dart';
  4. import 'package:fis_measure/interfaces/process/items/item_feature.dart';
  5. import 'package:fis_measure/interfaces/process/items/item_metas.dart';
  6. import 'package:fis_measure/interfaces/process/items/terms.dart';
  7. import 'package:fis_measure/interfaces/process/items/types.dart';
  8. import 'package:fis_measure/process/unit/convert/convert.dart';
  9. import 'package:fis_measure/process/workspace/measure_data_controller.dart';
  10. import 'package:fis_measure/values/unit_desc.dart';
  11. import 'package:fis_measure/view/result/results_panel.dart';
  12. import 'package:get/get.dart';
  13. import 'package:vid/us/vid_us_unit.dart';
  14. class FeatureValueDescConverter {
  15. final IMeasureItemFeature feature;
  16. FeatureValueDescConverter(this.feature);
  17. String idPlaceStr = ' ';
  18. String idStr = ' ';
  19. String kidIdPlaceStr = ' ';
  20. String kidIdStr = ' ';
  21. bool _isCurrURM = false;
  22. List<ResultLine> generate(int idLength) {
  23. idPlaceStr = ' '.padRight(idLength, ' ');
  24. idStr = feature.id.toString().padRight(idLength, ' ');
  25. _isCurrURM = feature.refItem.meta.name.contains("URM");
  26. final List<ResultLine> arr = [];
  27. List<ResultLine> mainLines = [];
  28. if ([
  29. MeasureTypes.URMShellDenMeasure,
  30. MeasureTypes.URMShellVelMeasure,
  31. MeasureTypes.URMShellDenVelMeasure
  32. ].contains(feature.refItem.meta.measureType)) {
  33. mainLines = _findSpecialRimFeatureLines(feature);
  34. } else {
  35. mainLines = _findFeatureLines(feature);
  36. }
  37. arr.addAll(mainLines);
  38. final ref = feature.refItem;
  39. if (ref is ITopMeasureItem && ref.canChildOutputSelf) {
  40. _buildChildOutputs(arr, ref.childItems);
  41. }
  42. return arr;
  43. }
  44. void _buildChildOutputs(List<ResultLine> arr, List<IMeasureItem> childItems) {
  45. for (var i = 0; i < childItems.length; i++) {
  46. kidIdPlaceStr = ' '.padRight(2, ' ');
  47. kidIdStr = " ${i + 1}".padRight(2, ' ');
  48. final child = childItems[i];
  49. for (final feature in child.measuredFeatures) {
  50. arr.addAll(_pickFeatureLines(feature));
  51. }
  52. if (child.feature != null) {
  53. arr.addAll(_pickFeatureLines(child.feature!));
  54. }
  55. }
  56. }
  57. List<ResultLine> _pickFeatureLines(IMeasureItemFeature feature) {
  58. final lines = _findKidFeatureLines(feature);
  59. return lines;
  60. }
  61. ResultLine? _findChildLine(IMeasureItemFeature feature) {
  62. if (feature.value == null) return null;
  63. final value = feature.value!;
  64. final valueStr = _pickValueStr(value);
  65. final name = feature.refItem.description;
  66. return ResultLine(label: ' $idPlaceStr $name', value: valueStr);
  67. }
  68. List<ResultLine> _findKidFeatureLines(IMeasureItemFeature feature) {
  69. final count = feature.values.length;
  70. final itemName = _findMetaDisplayName(feature.refItem.meta, true);
  71. if (count == 0) {
  72. return [];
  73. }
  74. List<ResultLine> arr = [];
  75. for (var i = 0; i < count; i++) {
  76. final val = feature.values[i];
  77. final meta = val.meta;
  78. final valueStr = _pickValueStr(val);
  79. if (i == 0) {
  80. arr.add(ResultLine(label: "$kidIdStr $itemName", value: valueStr));
  81. } else {
  82. final name = _findOutputMetaDisplayName(
  83. meta, feature.featureStyle.showBriefAnnotation);
  84. arr.add(ResultLine(label: "$kidIdPlaceStr $name", value: valueStr));
  85. }
  86. }
  87. return arr;
  88. }
  89. List<ResultLine> _findFeatureLines(IMeasureItemFeature feature) {
  90. final count = feature.values.length;
  91. final itemName = _findMetaDisplayName(
  92. feature.refItem.meta, feature.featureStyle.showBriefAnnotation);
  93. if (count == 0) {
  94. return [ResultLine(label: '$idStr $itemName')];
  95. }
  96. List<ResultLine> arr = [];
  97. for (var i = 0; i < count; i++) {
  98. final val = feature.values[i];
  99. final meta = val.meta;
  100. final valueStr = _pickValueStr(val);
  101. if (i == 0) {
  102. arr.add(ResultLine(label: "$idStr $itemName", value: valueStr));
  103. } else {
  104. final name = _findOutputMetaDisplayName(
  105. meta, feature.featureStyle.showBriefAnnotation);
  106. arr.add(ResultLine(label: "$idPlaceStr $name", value: valueStr));
  107. }
  108. }
  109. return arr;
  110. }
  111. List<ResultLine> _findSpecialRimFeatureLines(IMeasureItemFeature feature) {
  112. final count = feature.values.length;
  113. final itemName = _findMetaDisplayName(
  114. feature.refItem.meta, feature.featureStyle.showBriefAnnotation);
  115. if (count == 0) {
  116. return [ResultLine(label: '$idStr $itemName')];
  117. }
  118. List<ResultLine> arr = [];
  119. for (var i = 0; i < count; i++) {
  120. final val = feature.values[i];
  121. final meta = val.meta;
  122. final valueStr = _pickValueStr(val);
  123. if (i == 0) {
  124. arr.add(ResultLine(label: "$idStr $itemName", value: valueStr));
  125. } else {
  126. if (meta.description == MeasureTerms.Area) {
  127. final name = _findOutputMetaDisplayName(
  128. meta, feature.featureStyle.showBriefAnnotation);
  129. arr.add(ResultLine(label: "$idPlaceStr $name", value: valueStr));
  130. }
  131. }
  132. }
  133. return arr;
  134. }
  135. String _pickValueStr(ValueBase value) {
  136. if (value is FloatValue) {
  137. final floatVal = _roundDouble(value.value!, 6);
  138. if (floatVal.isNaN) {
  139. return '';
  140. }
  141. final unit = value.unit;
  142. // final targetUnit = getUnitFromConfig(value.metaUnit);
  143. final targetVal =
  144. UnitValueConverter.convert(unit, value.metaUnit, floatVal);
  145. final targetValRound =
  146. _roundDoubleToString(targetVal, value.meta.fractionalDigits);
  147. final unitStr = UnitDescriptionMap.getDesc(value.metaUnit);
  148. return '$targetValRound $unitStr';
  149. } else if (value is StringValue) {
  150. return value.value!;
  151. }
  152. return '';
  153. }
  154. String _findMetaDisplayName(ItemMeta meta,
  155. [bool showBriefAnnotation = false]) {
  156. if (showBriefAnnotation) {
  157. if (meta.briefAnnotation.isNotEmpty) {
  158. return meta.briefAnnotation;
  159. }
  160. }
  161. return meta.description;
  162. }
  163. String _findOutputMetaDisplayName(ItemOutputMeta meta,
  164. [bool showBriefAnnotation = false]) {
  165. if (showBriefAnnotation) {
  166. if (meta.briefAnnotation != null && meta.briefAnnotation!.isNotEmpty) {
  167. return meta.briefAnnotation!;
  168. }
  169. }
  170. return meta.description;
  171. }
  172. double _roundDouble(double value, [int digits = 2]) {
  173. final digitsStr = value.toStringAsFixed(digits);
  174. final result = double.parse(digitsStr);
  175. return result;
  176. }
  177. String _roundDoubleToString(double value, [int digits = 2]) {
  178. final digitsStr = value.toStringAsFixed(digits);
  179. return digitsStr;
  180. }
  181. VidUsUnit getUnitFromConfig(VidUsUnit unit) {
  182. try {
  183. final measureData = Get.find<MeasureDataController>();
  184. if (distanceUnits.contains(unit)) {
  185. if (_isCurrURM) {
  186. return VidUsUnitMap.getUnit(
  187. measureData.measureSystemSetting.uRMDistanceUnit.index);
  188. } else {
  189. return VidUsUnitMap.getUnit(
  190. measureData.measureSystemSetting.distanceUnit.index);
  191. }
  192. }
  193. if (areaUnits.contains(unit)) {
  194. return VidUsUnitMap.getUnit(
  195. measureData.measureSystemSetting.areaUnit.index);
  196. }
  197. if (velocityUnits.contains(unit)) {
  198. return VidUsUnitMap.getUnit(
  199. measureData.measureSystemSetting.velocityUnit.index);
  200. }
  201. if (timeUnits.contains(unit)) {
  202. return VidUsUnitMap.getUnit(
  203. measureData.measureSystemSetting.timeUnit.index);
  204. }
  205. return unit;
  206. } catch (e) {
  207. log('getUnitFromConfig error: $e');
  208. return unit;
  209. }
  210. }
  211. static final List<VidUsUnit> distanceUnits = [
  212. VidUsUnit.mm,
  213. VidUsUnit.cm,
  214. VidUsUnit.um,
  215. VidUsUnit.inch,
  216. VidUsUnit.ft,
  217. ];
  218. static final List<VidUsUnit> areaUnits = [
  219. VidUsUnit.mm2,
  220. VidUsUnit.cm2,
  221. VidUsUnit.m2,
  222. ];
  223. static final List<VidUsUnit> velocityUnits = [
  224. VidUsUnit.mms,
  225. VidUsUnit.cms,
  226. VidUsUnit.ms,
  227. ];
  228. static final List<VidUsUnit> timeUnits = [
  229. VidUsUnit.msec,
  230. VidUsUnit.s,
  231. VidUsUnit.hour,
  232. VidUsUnit.minute,
  233. ];
  234. static final List<VidUsUnit> volumeUnits = [
  235. VidUsUnit.cm3,
  236. VidUsUnit.ml,
  237. VidUsUnit.mm3,
  238. VidUsUnit.L,
  239. ];
  240. static final List<VidUsUnit> weightUnits = [
  241. VidUsUnit.kg,
  242. VidUsUnit.g,
  243. VidUsUnit.mg,
  244. ];
  245. static final List<VidUsUnit> pressureUnits = [
  246. VidUsUnit.cm3s,
  247. VidUsUnit.mls,
  248. VidUsUnit.mlmin,
  249. VidUsUnit.Lmin,
  250. ];
  251. }