Salome HOME
Merge remote-tracking branch 'origin/EDF_2019'
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintAngle.cpp
index 3d402214f7b48fa2583e8e1686482ee0f6c9f9b0..137acd0cdebab5ef7d9d51b737bba25ce6fd5d6f 100644 (file)
@@ -1,14 +1,31 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
-
-// File:    SketchPlugin_ConstraintAngle.cpp
-// Created: 19 August 2015
-// Author:  Artem ZHIDKOV
+// Copyright (C) 2014-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
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "SketchPlugin_ConstraintAngle.h"
 #include <SketchPlugin_Line.h>
+#include <SketchPlugin_Tools.h>
+#include <SketcherPrs_Tools.h>
 
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeInteger.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
 
 #include <GeomDataAPI_Point2D.h>
 
@@ -42,11 +59,19 @@ void SketchPlugin_ConstraintAngle::initAttributes()
   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
 
-  data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID(), ModelAPI_AttributeDouble::typeId());
-  data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
+  data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID(),
+                       ModelAPI_AttributeDouble::typeId());
+  data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(),
+                       ModelAPI_AttributeInteger::typeId());
 
-  data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID(), ModelAPI_AttributeBoolean::typeId());
-  data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID(), ModelAPI_AttributeBoolean::typeId());
+  data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID(),
+                       ModelAPI_AttributeBoolean::typeId());
+  data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID(),
+                       ModelAPI_AttributeBoolean::typeId());
+
+  data()->addAttribute(SketchPlugin_ConstraintAngle::LOCATION_TYPE_ID(),
+                       ModelAPI_AttributeInteger::typeId());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
 }
 
 void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
@@ -61,8 +86,10 @@ void SketchPlugin_ConstraintAngle::execute()
 {
   std::shared_ptr<ModelAPI_Data> aData = data();
 
-  std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
-  std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
+  std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA =
+    aData->refattr(SketchPlugin_Constraint::ENTITY_A());
+  std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB =
+    aData->refattr(SketchPlugin_Constraint::ENTITY_B());
   if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
     return;
 
@@ -72,9 +99,10 @@ void SketchPlugin_ConstraintAngle::execute()
   if (!anAttrValue->isInitialized()) {
     double anAngle = calculateAngle();
     anAttrValue->setValue(anAngle);
-    updateAngleValue();
+    updateConstraintValueByAngleValue();
   }
-  // the value should to be computed here, not in the getAISObject in order to change the model value
+  // the value should to be computed here, not in the
+  // getAISObject in order to change the model value
   // inside the object transaction. This is important for creating a constraint by preselection.
   // The display of the presentation in this case happens after the transaction commit
   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
@@ -88,8 +116,10 @@ AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious
   if (!sketch())
     return thePrevious;
 
-  AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane(),
+  AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch(),
                                                             thePrevious);
+  if (anAIS.get() && !thePrevious.get())
+    SketchPlugin_Tools::setDimensionColor(anAIS);
   return anAIS;
 }
 
@@ -103,25 +133,32 @@ void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
   if (!aLineA || !aLineB)
     return;
 
-  if (theID == SketchPlugin_Constraint::ENTITY_A() || 
+  if (theID == SketchPlugin_Constraint::ENTITY_A() ||
       theID == SketchPlugin_Constraint::ENTITY_B()) {
-    std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
-        ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
-    if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
+    AttributeDoublePtr aValueAttr = real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
+    AttributeDoublePtr aConstrValueAttr = real(SketchPlugin_Constraint::VALUE());
+    // only if one of attributes is not initialized, try to compute the current value
+    if (!aValueAttr->isInitialized() || !aConstrValueAttr->isInitialized()) {
+      if (aValueAttr->isInitialized() && !aConstrValueAttr->isInitialized())
+        // initialize base value of constraint
+        updateConstraintValueByAngleValue();
       double anAngle = calculateAngle();
       aValueAttr->setValue(anAngle);
-      updateAngleValue();
+      updateConstraintValueByAngleValue();
     }
   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
     // Recalculate flyout point in local coordinates
     // coordinates are calculated according to the center of shapes intersection
     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
-        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
 
     std::shared_ptr<ModelAPI_Data> aData = data();
     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
-    FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
-    FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
+    FeaturePtr aLineA =
+      SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
+    FeaturePtr aLineB =
+      SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
 
     // Intersection of lines
     std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
@@ -138,11 +175,16 @@ void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
     double anAngle = calculateAngle();
-    aValueAttr->setValue(anAngle);
-    updateAngleValue();
+    if (aValueAttr->text().empty())
+      aValueAttr->setValue(anAngle);
+    else {
+      aValueAttr = std::dynamic_pointer_cast<
+        ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::VALUE()));
+      aValueAttr->setValue(anAngle);
+    }
   }
   else if (theID == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) {
-    updateAngleValue();
+    updateConstraintValueByAngleValue();
   }
 }
 
@@ -172,17 +214,25 @@ double SketchPlugin_ConstraintAngle::calculateAngle()
     bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
     std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
     bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
-    anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
+    anAng = std::shared_ptr<GeomAPI_Angle2d>(
+      new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
   }
   double anAngle = anAng->angleDegree();
+  std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
+      ModelAPI_AttributeDouble>(data()->attribute(VALUE()));
+  std::shared_ptr<ModelAPI_AttributeDouble> anAngleValueAttr = std::dynamic_pointer_cast<
+      ModelAPI_AttributeDouble>(data()->attribute(ANGLE_VALUE_ID()));
+  if (!aValueAttr->isInitialized())
+    aValueAttr->setValue(anAngle);
   /// an angle value should be corrected by the current angle type
-  anAngle = getAngleForType(anAngle);
+  anAngle = getAngleForType(anAngleValueAttr->text().empty() ?
+                            anAngle : anAngleValueAttr->value());
   boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(anAng->isReversed(0));
   boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(anAng->isReversed(1));
   return anAngle;
 }
 
-double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle)
+double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle, bool isPreviousValueObtuse)
 {
   double anAngle = theAngle;
 
@@ -195,9 +245,13 @@ double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle)
       anAngle = theAngle;
     break;
     case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
-      anAngle = 180.0 - theAngle;
-      if (anAngle < 0)
-        anAngle += 360;
+      if (theAngle > 180 || isPreviousValueObtuse)
+        anAngle = theAngle - 180.0;
+      else
+        anAngle = 180.0 - theAngle;
+
+      if (anAngle < 0.0)
+        anAngle += 360.0;
     }
     break;
     case SketcherPrs_Tools::ANGLE_BACKWARD:
@@ -209,33 +263,21 @@ double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle)
   return anAngle;
 }
 
-void SketchPlugin_ConstraintAngle::updateAngleValue()
+void SketchPlugin_ConstraintAngle::updateConstraintValueByAngleValue()
 {
   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
     ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
   double anAngle = aValueAttr->value();
 
   /// an angle value should be corrected by the current angle type
-  anAngle = getAngleForType(anAngle);
   aValueAttr = std::dynamic_pointer_cast<
                   ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
+  if (!aValueAttr->isInitialized())
+    calculateAngle();
+  anAngle = getAngleForType(anAngle, aValueAttr->value() > 180.0);
   aValueAttr->setValue(anAngle);
 }
 
-void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
-{
-  std::shared_ptr<ModelAPI_Data> aData = data();
-  if (!aData->isValid())
-    return;
-
-  myFlyoutUpdate = true;
-  std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
-      GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
-  aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
-  myFlyoutUpdate = false;
-}
-
-
 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
 {
   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
@@ -245,7 +287,8 @@ bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
 
   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
                            GeomDataAPI_Point2D>(attribute(theAttributeId));
-  if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
+  if (aFlyOutAttr->isInitialized() &&
+      (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance))
     return false;
 
   DataPtr aData = data();
@@ -267,9 +310,19 @@ bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
   if (aStartA->distance(aEndA) < tolerance)
     return false;
 
+  std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      aLineB->attribute(SketchPlugin_Line::START_ID()));
+  std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      aLineB->attribute(SketchPlugin_Line::END_ID()));
+
+  std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
+  std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
+  if (aStartB->distance(aEndB) < tolerance)
+    return false;
+
   myFlyoutUpdate = true;
-  double aX = (aStartA->x() + aEndA->x()) / 2.;
-  double aY = (aStartA->y() + aEndA->y()) / 2.;
+  double aX = (aStartA->x() + aEndA->x() + aStartB->x() + aEndB->x()) / 4.;
+  double aY = (aStartA->y() + aEndA->y() + aStartB->y() + aEndB->y()) / 4.;
 
   aFlyOutAttr->setValue(aX, aY);
   myFlyoutUpdate = false;