Salome HOME
Task 2.5 Show where the remaining degrees of freedom are (issue #2997)
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMiddle.cpp
index 6680004c8f633395b340b231f0ca80381b6c4976..893747131c8025a66372d12b38bb231336a53ed8 100644 (file)
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+// 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 <SketchSolver_ConstraintMiddle.h>
 #include <PlaneGCSSolver_ConstraintWrapper.h>
+#include <PlaneGCSSolver_EdgeWrapper.h>
+#include <PlaneGCSSolver_PointWrapper.h>
+#include <PlaneGCSSolver_Storage.h>
+#include <PlaneGCSSolver_Tools.h>
 #include <PlaneGCSSolver_UpdateCoincidence.h>
 
+static bool isArc(EntityWrapperPtr theEntity)
+{
+  return theEntity->type() == ENTITY_ARC || theEntity->type() == ENTITY_ELLIPTIC_ARC;
+}
+
 void SketchSolver_ConstraintMiddle::getAttributes(
     EntityWrapperPtr& theValue,
     std::vector<EntityWrapperPtr>& theAttributes)
 {
   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
+
+  // create auxiliary point if middle point on arc is specified
+  if (isArc(theAttributes[2])) {
+    std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
+        std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
+
+    myOddPoint = GCSPointPtr(new GCS::Point);
+    myOddPoint->x = aStorage->createParameter();
+    myOddPoint->y = aStorage->createParameter();
+    theAttributes[1] = PointWrapperPtr(new PlaneGCSSolver_PointWrapper(myOddPoint));
+  }
+}
+
+bool SketchSolver_ConstraintMiddle::remove()
+{
+  if (myOddPoint) {
+    std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
+        std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
+
+    GCS::SET_pD aParams;
+    aParams.insert(myOddPoint->x);
+    aParams.insert(myOddPoint->y);
+    aStorage->removeParameters(aParams);
+  }
+  return SketchSolver_ConstraintCoincidence::remove();
 }
 
 void SketchSolver_ConstraintMiddle::notify(const FeaturePtr&      theFeature,
                                            PlaneGCSSolver_Update* theUpdater)
 {
+  if (theFeature == myBaseConstraint && myInSolver) {
+    // the constraint is already being updated,
+    // update the middle point parameter if the constraint is "point-on-arc".
+    if (myOddPoint) {
+      EntityWrapperPtr anArcEntity =
+          isArc(myAttributes.front()) ? myAttributes.front() : myAttributes.back();
+      EdgeWrapperPtr anArcEdge =
+          std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(anArcEntity);
+      if (anArcEdge) {
+        std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anArcEdge->entity());
+        if (anArc) {
+          // recalculate parameters of middle point according to arc
+          *myOddPoint->x = (*anArc->startAngle + *anArc->endAngle) * 0.5;
+          *myOddPoint->y = (*anArc->endAngle - *anArc->startAngle) * 0.5;
+        }
+        else {
+          std::shared_ptr<GCS::ArcOfEllipse> aEllArc =
+              std::dynamic_pointer_cast<GCS::ArcOfEllipse>(anArcEdge->entity());
+          if (aEllArc) {
+            // recalculate parameters of middle point according to arc
+            *myOddPoint->x = (*aEllArc->startAngle + *aEllArc->endAngle) * 0.5;
+            *myOddPoint->y = (*aEllArc->endAngle - *aEllArc->startAngle) * 0.5;
+          }
+        }
+      }
+    }
+    return;
+  }
+
   PlaneGCSSolver_UpdateCoincidence* anUpdater =
       static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
-  bool isAccepted = anUpdater->checkCoincidence(myAttributes.front(), myAttributes.back());
+  bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
   if (isAccepted) {
     if (!myInSolver) {
       myInSolver = true;
@@ -25,13 +105,8 @@ void SketchSolver_ConstraintMiddle::notify(const FeaturePtr&      theFeature,
         // remove previously adde constraint
         myStorage->removeConstraint(myBaseConstraint);
         // merge divided constraints into single object
-        std::list<GCSConstraintPtr> aGCSConstraints;
-        std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
-            std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(myMiddle);
-        aGCSConstraints.push_back(aConstraint->constraints().front());
-        aConstraint =
-            std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(mySolverConstraint);
-        aGCSConstraints.push_back(aConstraint->constraints().front());
+        std::list<GCSConstraintPtr> aGCSConstraints = myMiddle->constraints();
+        aGCSConstraints.push_front(mySolverConstraint->constraints().front());
 
         myMiddle = ConstraintWrapperPtr();
         mySolverConstraint = ConstraintWrapperPtr(
@@ -42,8 +117,8 @@ void SketchSolver_ConstraintMiddle::notify(const FeaturePtr&      theFeature,
     }
   } else {
     if (myInSolver) {
-      myInSolver = false;
       myStorage->removeConstraint(myBaseConstraint);
+      myInSolver = false;
     }
 
     if (!myMiddle) {
@@ -52,10 +127,11 @@ void SketchSolver_ConstraintMiddle::notify(const FeaturePtr&      theFeature,
           std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(mySolverConstraint);
       std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
 
-      myMiddle = ConstraintWrapperPtr(
-          new PlaneGCSSolver_ConstraintWrapper(aGCSConstraints.front(), CONSTRAINT_MIDDLE_POINT));
       mySolverConstraint = ConstraintWrapperPtr(
-          new PlaneGCSSolver_ConstraintWrapper(aGCSConstraints.back(), CONSTRAINT_MIDDLE_POINT));
+        new PlaneGCSSolver_ConstraintWrapper(aGCSConstraints.front(), CONSTRAINT_MIDDLE_POINT));
+      aGCSConstraints.pop_front();
+      myMiddle = ConstraintWrapperPtr(
+          new PlaneGCSSolver_ConstraintWrapper(aGCSConstraints, CONSTRAINT_MIDDLE_POINT));
 
       // send middle constraint only
       myStorage->addConstraint(myBaseConstraint, myMiddle);