Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchAPI / SketchAPI_ConstraintAngle.cpp
index 50bbbb946ef93b285bea8e85887568b142c9b971..6128fb600c5d860e4adc6e20185d62f2a13668bf 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019  CEA/DEN, EDF R&D
+// Copyright (C) 2019-2023  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
@@ -28,6 +28,8 @@
 #include <SketchPlugin_ConstraintAngle.h>
 #include <SketchPlugin_Line.h>
 
+#include <SketcherPrs_Tools.h>
+
 #include <cmath>
 
 SketchAPI_ConstraintAngle::SketchAPI_ConstraintAngle(
@@ -74,34 +76,47 @@ static void calculatePossibleValuesOfAngle(FeaturePtr theFeature,
 
   std::shared_ptr<GeomAPI_Angle2d> anAng(new GeomAPI_Angle2d(
       aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
-  theAngleDirect = anAng->angleDegree();
+  theAngleDirect = fabs(anAng->angleDegree());
+  theAngleComplementary = 180.0 - theAngleDirect;
   theAngleBackward = 360.0 - theAngleDirect;
-
-  if (theAngleDirect > 180.0)
-    theAngleComplementary = theAngleDirect - 180.0;
-  else
-    theAngleComplementary = 180.0 - theAngleDirect;
 }
 
 static std::string angleTypeToString(FeaturePtr theFeature)
 {
   static const double TOLERANCE = 1.e-7;
+  static const std::string THE_ANGLE_DIRECT("Direct");
+  static const std::string THE_ANGLE_SUPPLEMENTARY("Supplementary");
+  static const std::string THE_ANGLE_BACKWARD("Backward");
+
   double anAngleDirect, anAngleComplmentary, anAngleBackward;
   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 (aDirectDiff > TOLERANCE) {
+  if (isDirect && aDirectDiff < TOLERANCE) {
+    aType = THE_ANGLE_DIRECT;
+  }
+  else if (isComplementary && aComplementaryDiff < TOLERANCE)
+    aType = THE_ANGLE_SUPPLEMENTARY;
+  else if (isBackward && aBackwardDiff < TOLERANCE)
+    aType = THE_ANGLE_BACKWARD;
+  else {
     if (aComplementaryDiff < aDirectDiff && aComplementaryDiff < aBackwardDiff)
-      aType = "Complementary";
+      aType = THE_ANGLE_SUPPLEMENTARY;
     else if (aBackwardDiff < aDirectDiff && aBackwardDiff < aComplementaryDiff)
-      aType = "Backward";
+      aType = THE_ANGLE_BACKWARD;
+    else
+      aType = THE_ANGLE_DIRECT;
   }
   return aType;
 }
@@ -114,10 +129,10 @@ void SketchAPI_ConstraintAngle::dump(ModelHighAPI_Dumper& theDumper) const
 
   // calculate angle value as it was just applied to the attributes
   FeaturePtr aBase = feature();
-  std::string aSetterSuffix = angleTypeToString(aBase);
 
   const std::string& aSketchName = theDumper.parentName(aBase);
-  theDumper << aBase << " = " << aSketchName << "." << "setAngle" << aSetterSuffix << "(";
+  theDumper.name(aBase, false, true, true); // mark constraint as dumped
+  theDumper << aSketchName << "." << "setAngle(";
 
   bool isFirstAttr = true;
   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
@@ -132,5 +147,6 @@ void SketchAPI_ConstraintAngle::dump(ModelHighAPI_Dumper& theDumper) const
   if (aValueAttr && aValueAttr->isInitialized())
     theDumper << ", " << aValueAttr;
 
-  theDumper << ")" << std::endl;
+  std::string aType = angleTypeToString(aBase);
+  theDumper << ", type = \"" << aType << "\")" << std::endl;
 }