import 'dart:developer'; import 'package:fis_measure/interfaces/process/calculators/values.dart'; import 'package:fis_measure/interfaces/process/items/item.dart'; import 'package:fis_measure/interfaces/process/items/item_feature.dart'; import 'package:fis_measure/interfaces/process/items/item_metas.dart'; import 'package:fis_measure/interfaces/process/items/terms.dart'; import 'package:fis_measure/interfaces/process/items/types.dart'; import 'package:fis_measure/process/unit/convert/convert.dart'; import 'package:fis_measure/process/workspace/measure_data_controller.dart'; import 'package:fis_measure/values/unit_desc.dart'; import 'package:fis_measure/view/result/results_panel.dart'; import 'package:get/get.dart'; import 'package:vid/us/vid_us_unit.dart'; class FeatureValueDescConverter { final IMeasureItemFeature feature; FeatureValueDescConverter(this.feature); String idPlaceStr = ' '; String idStr = ' '; String kidIdPlaceStr = ' '; String kidIdStr = ' '; bool _isCurrURM = false; List generate(int idLength) { idPlaceStr = ' '.padRight(idLength, ' '); idStr = feature.id.toString().padRight(idLength, ' '); _isCurrURM = feature.refItem.meta.name.contains("URM"); final List arr = []; List mainLines = []; if ([ MeasureTypes.URMShellDenMeasure, MeasureTypes.URMShellVelMeasure, MeasureTypes.URMShellDenVelMeasure ].contains(feature.refItem.meta.measureType)) { mainLines = _findSpecialRimFeatureLines(feature); } else { mainLines = _findFeatureLines(feature); } arr.addAll(mainLines); final ref = feature.refItem; if (ref is ITopMeasureItem && ref.canChildOutputSelf) { _buildChildOutputs(arr, ref.childItems); } return arr; } void _buildChildOutputs(List arr, List childItems) { for (var i = 0; i < childItems.length; i++) { kidIdPlaceStr = ' '.padRight(2, ' '); kidIdStr = " ${i + 1}".padRight(2, ' '); final child = childItems[i]; for (final feature in child.measuredFeatures) { arr.addAll(_pickFeatureLines(feature)); } if (child.feature != null) { arr.addAll(_pickFeatureLines(child.feature!)); } } } List _pickFeatureLines(IMeasureItemFeature feature) { final lines = _findKidFeatureLines(feature); return lines; } ResultLine? _findChildLine(IMeasureItemFeature feature) { if (feature.value == null) return null; final value = feature.value!; final valueStr = _pickValueStr(value); final name = feature.refItem.description; return ResultLine(label: ' $idPlaceStr $name', value: valueStr); } List _findKidFeatureLines(IMeasureItemFeature feature) { final count = feature.values.length; final itemName = _findMetaDisplayName(feature.refItem.meta, true); if (count == 0) { return []; } List arr = []; for (var i = 0; i < count; i++) { final val = feature.values[i]; final meta = val.meta; final valueStr = _pickValueStr(val); if (i == 0) { arr.add(ResultLine(label: "$kidIdStr $itemName", value: valueStr)); } else { final name = _findOutputMetaDisplayName( meta, feature.featureStyle.showBriefAnnotation); arr.add(ResultLine(label: "$kidIdPlaceStr $name", value: valueStr)); } } return arr; } List _findFeatureLines(IMeasureItemFeature feature) { final count = feature.values.length; final itemName = _findMetaDisplayName( feature.refItem.meta, feature.featureStyle.showBriefAnnotation); if (count == 0) { return [ResultLine(label: '$idStr $itemName')]; } List arr = []; for (var i = 0; i < count; i++) { final val = feature.values[i]; final meta = val.meta; final valueStr = _pickValueStr(val); if (i == 0) { arr.add(ResultLine(label: "$idStr $itemName", value: valueStr)); } else { final name = _findOutputMetaDisplayName( meta, feature.featureStyle.showBriefAnnotation); arr.add(ResultLine(label: "$idPlaceStr $name", value: valueStr)); } } return arr; } List _findSpecialRimFeatureLines(IMeasureItemFeature feature) { final count = feature.values.length; final itemName = _findMetaDisplayName( feature.refItem.meta, feature.featureStyle.showBriefAnnotation); if (count == 0) { return [ResultLine(label: '$idStr $itemName')]; } List arr = []; for (var i = 0; i < count; i++) { final val = feature.values[i]; final meta = val.meta; final valueStr = _pickValueStr(val); if (i == 0) { arr.add(ResultLine(label: "$idStr $itemName", value: valueStr)); } else { if (meta.description == MeasureTerms.Area) { final name = _findOutputMetaDisplayName( meta, feature.featureStyle.showBriefAnnotation); arr.add(ResultLine(label: "$idPlaceStr $name", value: valueStr)); } } } return arr; } String _pickValueStr(ValueBase value) { if (value is FloatValue) { final floatVal = _roundDouble(value.value!, 6); if (floatVal.isNaN) { return ''; } final unit = value.unit; // final targetUnit = getUnitFromConfig(value.metaUnit); final targetVal = UnitValueConverter.convert(unit, value.metaUnit, floatVal); final targetValRound = _roundDoubleToString(targetVal, value.meta.fractionalDigits); final unitStr = UnitDescriptionMap.getDesc(value.metaUnit); return '$targetValRound $unitStr'; } else if (value is StringValue) { return value.value!; } return ''; } String _findMetaDisplayName(ItemMeta meta, [bool showBriefAnnotation = false]) { if (showBriefAnnotation) { if (meta.briefAnnotation.isNotEmpty) { return meta.briefAnnotation; } } return meta.description; } String _findOutputMetaDisplayName(ItemOutputMeta meta, [bool showBriefAnnotation = false]) { if (showBriefAnnotation) { if (meta.briefAnnotation != null && meta.briefAnnotation!.isNotEmpty) { return meta.briefAnnotation!; } } return meta.description; } double _roundDouble(double value, [int digits = 2]) { final digitsStr = value.toStringAsFixed(digits); final result = double.parse(digitsStr); return result; } String _roundDoubleToString(double value, [int digits = 2]) { final digitsStr = value.toStringAsFixed(digits); return digitsStr; } VidUsUnit getUnitFromConfig(VidUsUnit unit) { try { final measureData = Get.find(); if (distanceUnits.contains(unit)) { if (_isCurrURM) { return VidUsUnitMap.getUnit( measureData.measureSystemSetting.uRMDistanceUnit.index); } else { return VidUsUnitMap.getUnit( measureData.measureSystemSetting.distanceUnit.index); } } if (areaUnits.contains(unit)) { return VidUsUnitMap.getUnit( measureData.measureSystemSetting.areaUnit.index); } if (velocityUnits.contains(unit)) { return VidUsUnitMap.getUnit( measureData.measureSystemSetting.velocityUnit.index); } if (timeUnits.contains(unit)) { return VidUsUnitMap.getUnit( measureData.measureSystemSetting.timeUnit.index); } return unit; } catch (e) { log('getUnitFromConfig error: $e'); return unit; } } static final List distanceUnits = [ VidUsUnit.mm, VidUsUnit.cm, VidUsUnit.um, VidUsUnit.inch, VidUsUnit.ft, ]; static final List areaUnits = [ VidUsUnit.mm2, VidUsUnit.cm2, VidUsUnit.m2, ]; static final List velocityUnits = [ VidUsUnit.mms, VidUsUnit.cms, VidUsUnit.ms, ]; static final List timeUnits = [ VidUsUnit.msec, VidUsUnit.s, VidUsUnit.hour, VidUsUnit.minute, ]; static final List volumeUnits = [ VidUsUnit.cm3, VidUsUnit.ml, VidUsUnit.mm3, VidUsUnit.L, ]; static final List weightUnits = [ VidUsUnit.kg, VidUsUnit.g, VidUsUnit.mg, ]; static final List pressureUnits = [ VidUsUnit.cm3s, VidUsUnit.mls, VidUsUnit.mlmin, VidUsUnit.Lmin, ]; }