Salome HOME
Issue #3086: Avoid crash when FeatureInfo is null.
[modules/shaper.git] / src / SketchAPI / SketchAPI_ConstraintAngle.cpp
index cdfa37b6dcd2b94283290938f4925625c7c11bf7..9d7bf8c461624a42bc42c358410b577ffc147158 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-20xx  CEA/DEN, EDF R&D
+// Copyright (C) 2019  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "SketchAPI_ConstraintAngle.h"
@@ -29,6 +28,8 @@
 #include <SketchPlugin_ConstraintAngle.h>
 #include <SketchPlugin_Line.h>
 
+#include <SketcherPrs_Tools.h>
+
 #include <cmath>
 
 SketchAPI_ConstraintAngle::SketchAPI_ConstraintAngle(
@@ -91,16 +92,31 @@ static std::string angleTypeToString(FeaturePtr theFeature)
   calculatePossibleValuesOfAngle(theFeature, anAngleDirect, anAngleComplmentary, anAngleBackward);
 
   AttributeDoublePtr aValueAttr = theFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
+  double aDirectDiff = fabs(aValueAttr->value() - anAngleDirect);
+  double aComplementaryDiff = fabs(aValueAttr->value() - anAngleComplmentary);
+  double aBackwardDiff = fabs(aValueAttr->value() - anAngleBackward);
+
+  AttributeIntegerPtr aTypeAttr = theFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID());
+  bool isDirect = aTypeAttr->value() == SketcherPrs_Tools::ANGLE_DIRECT;
+  bool isComplementary = aTypeAttr->value() == SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
+  bool isBackward = aTypeAttr->value() == SketcherPrs_Tools::ANGLE_BACKWARD;
 
+  // find the minimal difference
   std::string aType;
-  if (fabs(aValueAttr->value() - anAngleDirect) < TOLERANCE) {
+  if (isDirect && aDirectDiff < TOLERANCE) {
     // Nothing to do.
     // This case is empty and going the first to check the direct angle before the others.
   }
-  else if (fabs(aValueAttr->value() - anAngleComplmentary) < TOLERANCE)
+  else if (isComplementary && aComplementaryDiff < TOLERANCE)
     aType = "Complementary";
-  else if (fabs(aValueAttr->value() - anAngleBackward) < TOLERANCE)
+  else if (isBackward && aBackwardDiff < TOLERANCE)
     aType = "Backward";
+  else {
+    if (aComplementaryDiff < aDirectDiff && aComplementaryDiff < aBackwardDiff)
+      aType = "Complementary";
+    else if (aBackwardDiff < aDirectDiff && aBackwardDiff < aComplementaryDiff)
+      aType = "Backward";
+  }
   return aType;
 }