Salome HOME
cosmétique
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMovement.cpp
index 7f4c8d78be74bc356fa3c967a41d612b9bbb1b0f..d9f46de4a0c2fc21f63b24ddf97957983508a07a 100644 (file)
+// Copyright (C) 2017-2022  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_ConstraintMovement.h>
 #include <SketchSolver_Error.h>
 #include <SketchSolver_Manager.h>
 
+#include <PlaneGCSSolver_EdgeWrapper.h>
+#include <PlaneGCSSolver_PointArrayWrapper.h>
+#include <PlaneGCSSolver_PointWrapper.h>
+
 #include <SketchPlugin_Arc.h>
 #include <SketchPlugin_Circle.h>
+#include <SketchPlugin_Ellipse.h>
+#include <SketchPlugin_EllipticArc.h>
 #include <SketchPlugin_Line.h>
 #include <SketchPlugin_Point.h>
 
 #include <GeomDataAPI_Point2D.h>
 
+#include <GeomAPI_Pnt2d.h>
+
+#include <cmath>
+
+static GCS::Point createGCSPoint(double* x, double* y)
+{
+  GCS::Point aPoint;
+  aPoint.x = x;
+  aPoint.y = y;
+  return aPoint;
+}
+
+
+SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
+  : SketchSolver_ConstraintFixed(ConstraintPtr()),
+    myMovedFeature(theFeature),
+    mySimpleMove(true)
+{
+}
+
+SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr theAttribute,
+                                                                 const int thePointIndex)
+  : SketchSolver_ConstraintFixed(ConstraintPtr()),
+    myDraggedAttribute(theAttribute),
+    myDraggedPointIndex(thePointIndex),
+    mySimpleMove(true)
+{
+  myMovedFeature = ModelAPI_Feature::feature(theAttribute->owner());
+}
+
+void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
+{
+  if (myMovedFeature)
+    myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
+}
 
 void SketchSolver_ConstraintMovement::process()
 {
   cleanErrorMsg();
-  if (!myBaseFeature || !myStorage || myGroupID == GID_UNKNOWN) {
+  if (!myMovedFeature || !myStorage) {
     // Not enough parameters are initialized
     return;
   }
 
-  ParameterWrapperPtr aValue;
-  getAttributes(aValue, myMovedEntities);
-  if (!myErrorMsg.empty() || myMovedEntities.empty()) {
+  mySolverConstraint = initMovement();
+  if (!myErrorMsg.empty() || !mySolverConstraint) {
     // Nothing to move, clear the feature to avoid changing its group
     // after removing the Movement constraint.
-    myBaseFeature = FeaturePtr();
+    myMovedFeature = FeaturePtr();
     return;
   }
+  myStorage->addMovementConstraint(mySolverConstraint);
+}
+
 
-  std::vector<EntityWrapperPtr>::iterator anEntIt = myMovedEntities.begin();
-  for (; anEntIt != myMovedEntities.end(); ++anEntIt)
-    fixFeature(*anEntIt);
+static bool isSimpleMove(FeaturePtr theMovedFeature, AttributePtr theDraggedPoint)
+{
+  bool isSimple = true;
+#ifdef CHANGE_RADIUS_WHILE_MOVE
+  if (theMovedFeature->getKind() == SketchPlugin_Circle::ID() ||
+      theMovedFeature->getKind() == SketchPlugin_Ellipse::ID())
+    isSimple = (theDraggedPoint.get() != 0);
+  else if (theMovedFeature->getKind() == SketchPlugin_Arc::ID() ||
+           theMovedFeature->getKind() == SketchPlugin_EllipticArc::ID()) {
+    isSimple = (theDraggedPoint.get() != 0 &&
+               (theDraggedPoint->id() == SketchPlugin_Arc::CENTER_ID() ||
+                theDraggedPoint->id() == SketchPlugin_EllipticArc::CENTER_ID()));
+  }
+#endif
+  return isSimple;
 }
 
+ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
+{
+  ConstraintWrapperPtr aConstraint;
+
+  // if the feature is copy, do not move it
+  std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+      std::dynamic_pointer_cast<SketchPlugin_Feature>(myMovedFeature);
+  if (!aSketchFeature || aSketchFeature->isCopy()) {
+    myStorage->setNeedToResolve(true);
+    return aConstraint;
+  }
+
+  EntityWrapperPtr anEntity = myDraggedAttribute ? myStorage->entity(myDraggedAttribute)
+                                                 : myStorage->entity(myMovedFeature);
+  if (!anEntity) {
+    myStorage->update(myMovedFeature, true);
+    anEntity = myDraggedAttribute ? myStorage->entity(myDraggedAttribute)
+                                  : myStorage->entity(myMovedFeature);
+    if (!anEntity)
+      return aConstraint;
+  }
+
+  mySimpleMove = isSimpleMove(myMovedFeature, myDraggedAttribute);
+
+  if (mySimpleMove) {
+    if (anEntity->type() == ENTITY_POINT_ARRAY) {
+      anEntity = std::dynamic_pointer_cast<PlaneGCSSolver_PointArrayWrapper>(anEntity)
+                 ->value(myDraggedPointIndex);
+    }
+    aConstraint = fixFeature(anEntity);
+  }
+  else {
+    if (myDraggedAttribute) // start or end point of arc has been moved
+      aConstraint = fixArcExtremity(anEntity);
+    else if (anEntity->type() == ENTITY_CIRCLE || anEntity->type() == ENTITY_ARC) {
+      // arc or circle has been moved
+      aConstraint = fixPointOnCircle(anEntity);
+    }
+    else if (anEntity->type() == ENTITY_ELLIPSE || anEntity->type() == ENTITY_ELLIPTIC_ARC) {
+      // ellipse or elliptic arc has been moved
+      aConstraint = fixPointOnEllipse(anEntity);
+    }
+  }
+
+  return aConstraint;
+}
 
-static std::list<EntityWrapperPtr> movedEntities(
-    EntityWrapperPtr theOld, StoragePtr theOldStorage,
-    EntityWrapperPtr theNew, StoragePtr theNewStorage)
+ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
+    const EntityWrapperPtr& theArcExtremity)
 {
-  bool isFullyMoved = true;
-  std::list<EntityWrapperPtr> aMoved;
-  if (theOld->isEqual(theNew))
-    return aMoved;
-
-  std::list<EntityWrapperPtr> anOldSubs = theOld->subEntities();
-  std::list<EntityWrapperPtr> aNewSubs = theNew->subEntities();
-  std::list<EntityWrapperPtr>::const_iterator anOldIt = anOldSubs.begin();
-  std::list<EntityWrapperPtr>::const_iterator aNewIt = aNewSubs.begin();
-  for (; anOldIt != anOldSubs.end() && aNewIt != aNewSubs.end(); ++anOldIt, ++aNewIt) {
-    std::list<EntityWrapperPtr> aMovedSubs = movedEntities(
-        *anOldIt, theOldStorage, *aNewIt, theNewStorage);
-    if ((*anOldIt)->type() == ENTITY_POINT && // check only the points to be moved (because arcs in PlaneGCS have scalar subs too)
-       (aMovedSubs.size() != 1 || aMovedSubs.front() != *anOldIt))
-      isFullyMoved = false;
-    aMoved.insert(aMoved.end(), aMovedSubs.begin(), aMovedSubs.end());
+  static const int nbParams = 4;
+  myFixedValues.reserve(nbParams); // moved point and center of arc
+
+  EdgeWrapperPtr aCircularEntity = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
+      myStorage->entity(myMovedFeature));
+  std::shared_ptr<GCS::Arc> anArc =
+      std::dynamic_pointer_cast<GCS::Arc>(aCircularEntity->entity());
+  std::shared_ptr<GCS::ArcOfEllipse> anEllArc =
+      std::dynamic_pointer_cast<GCS::ArcOfEllipse>(aCircularEntity->entity());
+
+  PointWrapperPtr aPoint =
+      std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
+
+  double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y, 0, 0 };
+  if (anArc) {
+    aParams[2] = anArc->center.x;
+    aParams[3] = anArc->center.y;
+  }
+  else if (anEllArc) {
+    aParams[2] = anEllArc->center.x;
+    aParams[3] = anEllArc->center.y;
   }
-  if (isFullyMoved) {
-    aMoved.clear();
-    aMoved.push_back(theOld);
+
+  std::list<GCSConstraintPtr> aConstraints;
+  for (int i = 0; i < nbParams; ++i) {
+    myFixedValues.push_back(*aParams[i]);
+    GCSConstraintPtr aNewConstraint(new GCS::ConstraintEqual(&myFixedValues[i], aParams[i]));
+    aNewConstraint->rescale(0.01);
+    aConstraints.push_back(aNewConstraint);
   }
-  return aMoved;
+
+  return ConstraintWrapperPtr(
+      new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
 }
 
+ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
+    const EntityWrapperPtr& theCircular)
+{
+  static const double scale = 0.01;
+  static const int nbParams = 4;
+  myFixedValues.reserve(nbParams); // moved point and center of arc/circle
+
+  EdgeWrapperPtr aCircularEntity =
+      std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theCircular);
+  std::shared_ptr<GCS::Circle> aCircular =
+      std::dynamic_pointer_cast<GCS::Circle>(aCircularEntity->entity());
 
-void SketchSolver_ConstraintMovement::getAttributes(
-    ParameterWrapperPtr& theValue,
-    std::vector<EntityWrapperPtr>& theAttributes)
+  // initialize fixed values
+  myFixedValues.push_back(*aCircular->center.x + *aCircular->rad);
+  myFixedValues.push_back(*aCircular->center.y);
+  myFixedValues.push_back(*aCircular->center.x);
+  myFixedValues.push_back(*aCircular->center.y);
+
+  // create a moved point
+  GCS::Point aPointOnCircle = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
+
+  std::list<GCSConstraintPtr> aConstraints;
+  // point-on-circle
+  GCSConstraintPtr aNewConstraint(
+      new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
+  aNewConstraint->rescale(scale);
+  aConstraints.push_back(aNewConstraint);
+  // fixed center (x)
+  aNewConstraint = GCSConstraintPtr(
+      new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
+  aNewConstraint->rescale(scale);
+  aConstraints.push_back(aNewConstraint);
+  // fixed center (y)
+  aNewConstraint = GCSConstraintPtr(
+      new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
+  aNewConstraint->rescale(scale);
+  aConstraints.push_back(aNewConstraint);
+
+  return ConstraintWrapperPtr(
+      new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
+}
+
+ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnEllipse(
+    const EntityWrapperPtr& theConic)
 {
-  // There will be build entities, according to the fixed feature, in the separate storage
-  // to check whether any point is moved
-  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
-  StoragePtr anOtherStorage = aBuilder->createStorage(myGroupID);
-  anOtherStorage->setSketch(myStorage->sketch());
-  if (!anOtherStorage->update(myBaseFeature, myGroupID))
-    return;
-  EntityWrapperPtr aNewEntity = anOtherStorage->entity(myBaseFeature);
-  EntityWrapperPtr anOldEntity = myStorage->entity(myBaseFeature);
+  static const double scale = 0.01;
+  static const int nbParams = 6;
+  myFixedValues.reserve(nbParams); // moved point; center and focus of ellipse
 
-  std::list<EntityWrapperPtr> aMoved;
-  if (aNewEntity && anOldEntity)
-    aMoved = movedEntities(anOldEntity, myStorage, aNewEntity, anOtherStorage);
+  EdgeWrapperPtr anEdge = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theConic);
+  std::shared_ptr<GCS::Ellipse> aConic = std::dynamic_pointer_cast<GCS::Ellipse>(anEdge->entity());
+
+  // major axis direction
+  double dx = *aConic->focus1.x - *aConic->center.x;
+  double dy = *aConic->focus1.y - *aConic->center.y;
+  double norm = sqrt(dx * dx + dy* dy);
+  if (norm < tolerance) {
+    dx = 1.0;
+    dy = 0.0;
+  }
   else {
-    // get attributes moved
-    std::list<AttributePtr> anAttrList =
-        myBaseFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
-    std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
-    for (; anIt != anAttrList.end(); ++anIt) {
-      aNewEntity = anOtherStorage->entity(*anIt);
-      anOldEntity = myStorage->entity(*anIt);
-      if (!aNewEntity || !anOldEntity)
-        continue;
-      std::list<EntityWrapperPtr> aMovedAttr = movedEntities(
-          anOldEntity, myStorage, aNewEntity, anOtherStorage);
-      aMoved.insert(aMoved.end(), aMovedAttr.begin(), aMovedAttr.end());
-    }
+    dx /= norm;
+    dy /= norm;
   }
-  theAttributes.clear();
-  theAttributes.insert(theAttributes.begin(), aMoved.begin(), aMoved.end());
+
+  double aMajorRad = aConic->getRadMaj();
+
+  // initialize fixed values
+  myFixedValues.push_back(*aConic->center.x + dx * aMajorRad);
+  myFixedValues.push_back(*aConic->center.y + dy * aMajorRad);
+  myFixedValues.push_back(*aConic->center.x);
+  myFixedValues.push_back(*aConic->center.y);
+  myFixedValues.push_back(*aConic->focus1.x);
+  myFixedValues.push_back(*aConic->focus1.y);
+
+  // create a moved point
+  GCS::Point aPointOnEllipse = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
+
+  std::list<GCSConstraintPtr> aConstraints;
+  // point-on-circle
+  GCSConstraintPtr aNewConstraint(
+    new GCS::ConstraintPointOnEllipse(aPointOnEllipse, *aConic));
+  aNewConstraint->rescale(scale);
+  aConstraints.push_back(aNewConstraint);
+  // fixed center (x)
+  aNewConstraint = GCSConstraintPtr(
+    new GCS::ConstraintEqual(&myFixedValues[2], aConic->center.x));
+  aNewConstraint->rescale(scale);
+  aConstraints.push_back(aNewConstraint);
+  // fixed center (y)
+  aNewConstraint = GCSConstraintPtr(
+    new GCS::ConstraintEqual(&myFixedValues[3], aConic->center.y));
+  aNewConstraint->rescale(scale);
+  aConstraints.push_back(aNewConstraint);
+  // focus on the major axis
+  GCS::Point aStartPoint = createGCSPoint(&myFixedValues[2], &myFixedValues[3]);
+  GCS::Point aEndPoint   = createGCSPoint(&myFixedValues[4], &myFixedValues[5]);
+  aNewConstraint = GCSConstraintPtr(
+    new GCS::ConstraintPointOnLine(aConic->focus1, aStartPoint, aEndPoint));
+  aNewConstraint->rescale(scale);
+  aConstraints.push_back(aNewConstraint);
+
+  return ConstraintWrapperPtr(
+    new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
 }
 
-bool SketchSolver_ConstraintMovement::remove()
+void SketchSolver_ConstraintMovement::startPoint(
+    const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
 {
-  cleanErrorMsg();
-  // Move fixed entities back to the current group
-  std::vector<EntityWrapperPtr>::iterator aMoveIt = myMovedEntities.begin();
-  for (; aMoveIt != myMovedEntities.end(); ++aMoveIt) {
-    if ((*aMoveIt)->baseAttribute())
-      myStorage->update((*aMoveIt)->baseAttribute(), myGroupID);
-    else if ((*aMoveIt)->baseFeature())
-      myStorage->update((*aMoveIt)->baseFeature(), myGroupID);
+  myStartPoint = theStartPoint;
+  if (!mySimpleMove) {
+    myFixedValues[0] = myStartPoint->x();
+    myFixedValues[1] = myStartPoint->y();
   }
+}
+
+void SketchSolver_ConstraintMovement::moveTo(
+    const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
+{
+  if (!myMovedFeature)
+    return; // nothing to move
+
+  double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
+                       theDestinationPoint->y() - myStartPoint->y() };
 
-  // Remove base feature
-  if (myBaseFeature)
-    myStorage->removeEntity(myBaseFeature);
-  return true;
+#ifdef CHANGE_RADIUS_WHILE_MOVE
+  int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
+#else
+  int aMaxSize =
+      myMovedFeature->getKind() == SketchPlugin_Line::ID() && !myDraggedAttribute ? 4 : 2;
+#endif
+  for (int i = 0; i < aMaxSize; ++i)
+    myFixedValues[i] += aDelta[i % 2];
 }