Browse Source

fix bug 0008872&0008877 measure clear&undo

melon.yin 2 years ago
parent
commit
2ed8de75f1

+ 19 - 10
lib/process/calcuators/curve.dart

@@ -28,8 +28,8 @@ class AreaPerimeterEllipseCal extends Calculator<Ellipse, double> {
   }
 }
 
-class AreaPerimeterCal extends Calculator<Trace, double> {
-  AreaPerimeterCal(Trace ref) : super(ref);
+class AreaPerimeterCal extends Calculator<AreaItemAbstract, double> {
+  AreaPerimeterCal(AreaItemAbstract ref) : super(ref);
 
   @override
   void calculate() {
@@ -39,13 +39,22 @@ class AreaPerimeterCal extends Calculator<Trace, double> {
     final viewport = feature.hostVisualArea!.viewport!;
     final points = feature.innerPoints.map((e) => viewport.convert(e)).toList();
 
-    double threshold = 0;
-    // if (ref.Threshold.HasValue)
-    // {
-    //     threshold = ref.Threshold.Value;
-    // }
-    double perimeter = calcPerimeter(points, ref.feature!.isClosed, threshold);
-    double area = clacArea(points);
+    double area;
+    double perimeter;
+    if ((feature.splineTension - 0).abs() > 0) {
+      // TODO: CreateSpline - Polyline.cs 850
+      perimeter = 0;
+      area = 0;
+    } else {
+      double threshold = 0.0;
+      // TODO:
+      // if (ref.Threshold.HasValue)
+      // {
+      //     threshold = ref.Threshold.Value;
+      // }
+      area = calcArea(points);
+      perimeter = calcPerimeter(points, feature.isClosed, threshold);
+    }
 
     for (var output in ref.meta.outputs) {
       if (output.name == MeasureTerms.Perimeter) {
@@ -60,7 +69,7 @@ class AreaPerimeterCal extends Calculator<Trace, double> {
     // updateFloatValue(value);
   }
 
-  static double clacArea(List<DPoint> points) {
+  static double calcArea(List<DPoint> points) {
     if (points.isEmpty) {
       return 0;
     }

+ 1 - 81
lib/process/primitives/polyline.dart

@@ -22,7 +22,7 @@ class Polyline extends AreaItemAbstract with AutoSnapMixin {
 
   static Polyline createAreaPerimeter(ItemMeta meta, [IMeasureItem? parent]) {
     Polyline polygon = Polyline(meta, parent);
-    polygon.calculator = _AreaPerimeterCalc(polygon);
+    polygon.calculator = AreaPerimeterCal(polygon);
     return polygon;
   }
 
@@ -125,83 +125,3 @@ class PolylineFeature extends AreaItemFeatureAbstract {
     }
   }
 }
-
-class _AreaPerimeterCalc extends Calculator<Polyline, double> {
-  _AreaPerimeterCalc(Polyline ref) : super(ref);
-
-  @override
-  void calculate() {
-    if (ref.feature == null) return;
-
-    final feature = ref.feature!;
-    final viewport = feature.hostVisualArea!.viewport!;
-    final points = feature.innerPoints.map((e) => viewport.convert(e)).toList();
-
-    feature.values.clear();
-
-    double area;
-    double perimeter;
-    if ((feature.splineTension - 0).abs() > 0) {
-      // TODO: CreateSpline - Polyline.cs 850
-      perimeter = 0;
-      area = 0;
-    } else {
-      double threshold = 0.0;
-      area = calcArea(points);
-      perimeter = calcPerimeter(points, feature.isClosed, threshold);
-    }
-    for (var output in ref.meta.outputs) {
-      if (output.name == MeasureTerms.Perimeter) {
-        var value = roundDouble(perimeter, output.fractionalDigits);
-        feature.updateFloatValue(output, value, output.unit);
-      } else if (output.name == MeasureTerms.Area) {
-        var value = roundDouble(area, output.fractionalDigits);
-        feature.updateFloatValue(output, value, output.unit);
-      }
-    }
-  }
-
-  static double calcArea(List<DPoint> points) {
-    if (points.isEmpty) {
-      return 0;
-    }
-
-    double sum = 0;
-    var ax = points[0].x;
-    var ay = points[0].y;
-    for (var i = 1; i < points.length - 1; i++) {
-      var bx = points[i].x;
-      var by = points[i].y;
-      var cx = points[i + 1].x;
-      var cy = points[i + 1].y;
-      sum += ax * by - ay * bx + ay * cx - ax * cy + bx * cy - cx * by;
-    }
-    return (-sum / 2).abs();
-  }
-
-  static double calcPerimeter(
-    List<DPoint> points,
-    bool isClosed,
-    double threshold,
-  ) {
-    final len = points.length;
-    if (points.length < 2) {
-      return 0;
-    }
-
-    double sum = 0;
-    for (int i = 1, j = 0; j < len && i < len; i++) {
-      var length = (points[i] - points[j]).length;
-      if ((length).abs() > threshold) {
-        sum += length;
-        j = i;
-      }
-    }
-
-    if (isClosed) {
-      sum += (points[len - 1] - points[0]).length;
-    }
-
-    return sum;
-  }
-}

+ 0 - 1
lib/process/workspace/application.dart

@@ -437,7 +437,6 @@ class Application implements IApplication {
       );
       return;
     }
-    print("Unknown measure type: $name");
     activeMeasureItem = null;
   }
 

+ 21 - 7
lib/process/workspace/recorder.dart

@@ -37,6 +37,24 @@ class MeasureRecorder implements IMeasureRecorder {
 
   /// 撤销一次操作
   bool undoOnce() {
+    _finishLast();
+    return _undoOnce();
+  }
+
+  /// 清除所有记录
+  void clear() {
+    if (_records.isEmpty) return;
+
+    _finishLast();
+
+    final len = _records.length;
+    for (var i = 0; i < len; i++) {
+      _undoOnce();
+    }
+    _resetId();
+  }
+
+  bool _undoOnce() {
     final record = _records.last;
     bool removed = false;
     if (record.recordType == MeasureRecordType.measure) {
@@ -51,13 +69,9 @@ class MeasureRecorder implements IMeasureRecorder {
     return removed;
   }
 
-  /// 清除所有记录
-  void clear() {
-    final len = _records.length;
-    for (var i = 0; i < len; i++) {
-      undoOnce();
-    }
-    _resetId();
+  void _finishLast() {
+    _application.activeAnnotationItem?.finishLast();
+    _application.activeMeasureItem?.finishOnce();
   }
 
   bool _undoOnceMeasure(_MeasureModel record) {