]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/SketchSolver/SketchSolver_ConstraintMultiRotation.cpp
Salome HOME
Add tools
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMultiRotation.cpp
index ea4372fa648560509db0dfd30dde79a149fc4d96..ed1550d41e10ba1c740b3678f8e4956467aa3d11 100644 (file)
 
 #include <math.h>
 
-static double squareDistance(
-    StoragePtr theStorage, const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
-{
-  Slvs_Entity aPoint1 = theStorage->getEntity(thePoint1);
-  Slvs_Entity aPoint2 = theStorage->getEntity(thePoint2);
-  double x1 = theStorage->getParameter(aPoint1.param[0]).val;
-  double y1 = theStorage->getParameter(aPoint1.param[1]).val;
-  double x2 = theStorage->getParameter(aPoint2.param[0]).val;
-  double y2 = theStorage->getParameter(aPoint2.param[1]).val;
-  return (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2);
-}
-
 void SketchSolver_ConstraintMultiRotation::getAttributes(
     Slvs_hEntity& theCenter, double& theAngle,
     std::vector< std::vector<Slvs_hEntity> >& thePoints,
@@ -138,134 +126,29 @@ void SketchSolver_ConstraintMultiRotation::process()
     mySlvsConstraints.push_back(aConstraint.h);
   }
 
-  // Keep all objects unchanged (only initial object may be changed by user)
-  myCircsAndCopies.clear();
-  std::vector<std::vector<Slvs_hEntity> >::const_iterator anEntIt = anEntitiesAndCopies.begin();
-  std::vector<Slvs_hEntity>::const_iterator aCpIt;
-  for (; anEntIt != anEntitiesAndCopies.end(); ++anEntIt) {
-    std::vector<Slvs_hEntity> aCircs;
-    aCpIt = anEntIt->begin();
-    // Obtain initial points
-    Slvs_Entity anInitial = myStorage->getEntity(*aCpIt);
-    if (anInitial.type == SLVS_E_POINT_IN_2D || anInitial.type == SLVS_E_POINT_IN_3D)
-      myInitialPoints.insert(anInitial.h);
-    else {
-      for (int i = 0; i < 4 && anInitial.point[i] != SLVS_E_UNKNOWN; i++)
-        myInitialPoints.insert(anInitial.point[i]);
-    }
-
-    // Fix the copies
-    for (++aCpIt; aCpIt != anEntIt->end(); ++aCpIt) {
-      const Slvs_Entity& anEntity = myStorage->getEntity(*aCpIt);
-      std::vector<Slvs_hConstraint> aNewConstr;
-      if (anEntity.type == SLVS_E_CIRCLE) {
-        aCircs.push_back(anEntity.distance);
-        // for circles we fix only center
-        aNewConstr = myStorage->fixEntity(anEntity.point[0]);
-      } else
-        aNewConstr = myStorage->fixEntity(*aCpIt);
-      if (anEntity.type == SLVS_E_ARC_OF_CIRCLE)
-        aCircs.push_back(anEntity.h);
-      mySlvsConstraints.insert(mySlvsConstraints.end(), aNewConstr.begin(), aNewConstr.end());
-    }
-
-    if (!aCircs.empty()) {
-      if (anInitial.type == SLVS_E_CIRCLE)
-        aCircs.insert(aCircs.begin(), anInitial.distance);
-      else
-        aCircs.insert(aCircs.begin(), anInitial.h);
-      myCircsAndCopies.push_back(aCircs);
-    }
-  }
-
+  myAdjusted = false;
+  processEntities(anEntitiesAndCopies);
   adjustConstraint();
 }
 
-void SketchSolver_ConstraintMultiRotation::update(ConstraintPtr theConstraint)
+void SketchSolver_ConstraintMultiRotation::updateLocal()
 {
-  cleanErrorMsg();
-  if (!theConstraint || theConstraint == myBaseConstraint) {
-    AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-        myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
-    AttributeIntegerPtr aNbCopies = myBaseConstraint->integer(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID());
-    if (anInitialRefList->size() != myNumberOfObjects || aNbCopies->value() != myNumberOfCopies) {
-      remove(myBaseConstraint);
-      process();
-      return;
-    }
-  }
-
-  // update angle value
-  myAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+  double aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
       myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
-
-  SketchSolver_Constraint::update();
-}
-
-bool SketchSolver_ConstraintMultiRotation::remove(ConstraintPtr theConstraint)
-{
-  cleanErrorMsg();
-  if (theConstraint && theConstraint != myBaseConstraint)
-    return false;
-  bool isFullyRemoved = true;
-  std::vector<Slvs_hEntity>::iterator aCIter = mySlvsConstraints.begin();
-  for (; aCIter != mySlvsConstraints.end(); aCIter++)
-   isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
-  mySlvsConstraints.clear();
-
-  std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.begin();
-  for (; aFeatIt != myFeatureMap.end(); aFeatIt++)
-    myStorage->removeEntity(aFeatIt->second);
-  myStorage->removeUnusedEntities();
-
-  std::map<FeaturePtr, Slvs_hEntity> aFeatureMapCopy = myFeatureMap;
-
-  if (isFullyRemoved) {
-    myFeatureMap.clear();
-    myAttributeMap.clear();
-    myValueMap.clear();
-  } else
-    cleanRemovedEntities();
-
-  // Restore initial features
-  std::map<FeaturePtr, Slvs_hEntity>::iterator aFIter = aFeatureMapCopy.begin();
-  for (; aFIter != aFeatureMapCopy.end(); ++aFIter)
-  {
-    if (myFeatureMap.find(aFIter->first) != myFeatureMap.end())
-      continue; // the feature was not removed
-    Slvs_hEntity anEntity = myGroup->getFeatureId(aFIter->first);
-    if (anEntity != SLVS_E_UNKNOWN)
-      myFeatureMap[aFIter->first] = anEntity;
-  }
-
-  // Clear list of rotated points
-  myPointsAndCopies.clear();
-  myInitialPoints.clear();
-
-  return true;
-}
-
-void SketchSolver_ConstraintMultiRotation::addFeature(FeaturePtr theFeature)
-{
-  SketchSolver_Constraint::addFeature(theFeature);
-
-  std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.find(theFeature);
-  if (aFeatIt == myFeatureMap.end())
-    return;
-
-  // store list of points of the feature
-  const Slvs_Entity& theEntity = myStorage->getEntity(aFeatIt->second);
-  for (int i = 0; i < 4; i++)
-    if (theEntity.point[i] != SLVS_E_UNKNOWN)
-      myPointsJustUpdated.insert(theEntity.point[i]);
+  if (fabs(myAngle - aValue) > tolerance)
+    myAdjusted = false;
+  // update angle value
+  myAngle = aValue;
 }
 
 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
 {
-  if (abs(myAngle) < tolerance) {
+  if (fabs(myAngle) < tolerance) {
     myStorage->setNeedToResolve(false);
     return;
   }
+  if (myAdjusted)
+    return;
 
   std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
   std::list<Slvs_Constraint>::const_iterator aCoIt;
@@ -290,102 +173,39 @@ void SketchSolver_ConstraintMultiRotation::adjustConstraint()
 
   // Obtain coordinates of rotation center
   Slvs_Entity aRotCenter = myStorage->getEntity(myRotationCenter);
-  double aCenterXY[2];
-  for (int i = 0; i < 2; i++)
-    aCenterXY[i] = myStorage->getParameter(aRotCenter.param[i]).val;
-
-  double cosA = cos(myAngle * PI / 180.0);
-  double sinA = sin(myAngle * PI / 180.0);
+  myCenterCoord[0] = myStorage->getParameter(aRotCenter.param[0]).val;
+  myCenterCoord[1] = myStorage->getParameter(aRotCenter.param[1]).val;
 
-  double aVec[2]; // coordinates of vector defining a direction from rotation center to a point
+  myRotationVal[0] = sin(myAngle * PI / 180.0);
+  myRotationVal[1] = cos(myAngle * PI / 180.0);
 
-  // Update positions of all points to satisfy angles
-  std::vector< std::vector<Slvs_hEntity> >::const_iterator aPointsIter = myPointsAndCopies.begin();
-  std::vector<Slvs_hEntity>::const_iterator aCopyIter;
-  for (; aPointsIter != myPointsAndCopies.end(); ++aPointsIter) {
-    aCopyIter = aPointsIter->begin();
-    const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
-    for (int i = 0; i < 2; i++)
-      aVec[i] = myStorage->getParameter(anInitial.param[i]).val - aCenterXY[i];
-
-    // if the point is coincident with another one which is temporary fixed (moved by user),
-    // we will update its position correspondingly
-    Slvs_hConstraint aFixed;
-    for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt) {
-      if ((aCoIt->ptA == anInitial.h && myInitialPoints.find(aCoIt->ptB) != myInitialPoints.end()) ||
-          (aCoIt->ptB == anInitial.h && myInitialPoints.find(aCoIt->ptA) != myInitialPoints.end())) {
-        Slvs_hEntity anOtherId = aCoIt->ptA == anInitial.h ? aCoIt->ptB : aCoIt->ptA;
-        if (!myStorage->isTemporary(aFixed) &&
-            myPointsJustUpdated.find(anOtherId) == myPointsJustUpdated.end())
-          continue; // nothing to change
-
-        const Slvs_Entity& anOtherPnt = myStorage->getEntity(anOtherId);
-        for (int i = 0; i < 2; i++) {
-          Slvs_Param anInitParam = myStorage->getParameter(anInitial.param[i]);
-          const Slvs_Param& anOtherParam = myStorage->getParameter(anOtherPnt.param[i]);
-          anInitParam.val = anOtherParam.val;
-          myStorage->updateParameter(anInitParam);
-          aVec[i] = anOtherParam.val - aCenterXY[i];
-        }
-      }
-    }
-
-    // update copied points
-    aCopyIter = aPointsIter->begin();
-    for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
-      // rotate direction
-      double aTemp = aVec[0] * cosA - aVec[1] * sinA;
-      aVec[1] = aVec[0] * sinA + aVec[1] * cosA;
-      aVec[0] = aTemp;
+  SketchSolver_ConstraintMulti::adjustConstraint();
+}
 
-      const Slvs_Entity& aTarget = myStorage->getEntity(*aCopyIter);
-      for (int i = 0; i < 2; i++) {
-        Slvs_Param aParam = myStorage->getParameter(aTarget.param[i]);
-        aParam.val = aCenterXY[i] + aVec[i];
-        myStorage->updateParameter(aParam);
-      }
-    }
-  }
+void SketchSolver_ConstraintMultiRotation::getRelative(
+    double theAbsX, double theAbsY, double& theRelX, double& theRelY)
+{
+  theRelX = theAbsX - myCenterCoord[0];
+  theRelY = theAbsY - myCenterCoord[1];
+}
 
-  std::list<Slvs_Constraint> aDiamConstr;
-  for (aPointsIter = myCircsAndCopies.begin(); aPointsIter != myCircsAndCopies.end(); ++aPointsIter) {
-    aCopyIter = aPointsIter->begin();
-    const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
-    if (anInitial.type == SLVS_E_DISTANCE) {
-      const Slvs_Param& anInitRad = myStorage->getParameter(anInitial.param[0]);
-      for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
-        const Slvs_Entity& aCopy = myStorage->getEntity(*aCopyIter);
-        Slvs_Param aCopyRad = myStorage->getParameter(aCopy.param[0]);
-        aCopyRad.val = anInitRad.val;
-        myStorage->updateParameter(aCopyRad);
-      }
-    } else if (anInitial.type == SLVS_E_ARC_OF_CIRCLE) {
-      const Slvs_Entity& aCenterEnt = myStorage->getEntity(anInitial.point[0]);
-      const Slvs_Entity& aStartEnt = myStorage->getEntity(anInitial.point[1]);
+void SketchSolver_ConstraintMultiRotation::getAbsolute(
+    double theRelX, double theRelY, double& theAbsX, double& theAbsY)
+{
+  theAbsX = theRelX + myCenterCoord[0];
+  theAbsY = theRelY + myCenterCoord[1];
+}
 
-      if (aDiamConstr.empty())
-        aDiamConstr = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
-      // Calculate diameter of initial arc
-      double aDiam = 0.0;
-      for (int i = 0; i < 2; i++) {
-        double d = myStorage->getParameter(aStartEnt.param[i]).val -
-                   myStorage->getParameter(aCenterEnt.param[i]).val;
-        aDiam += d * d;
-      }
-      aDiam = sqrt(aDiam) * 2.0;
-      // Update the Diameter constraints of copied arcs
-      for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
-        std::list<Slvs_Constraint>::iterator aDCIt = aDiamConstr.begin();
-        for (; aDCIt != aDiamConstr.end(); ++aDCIt)
-          if (aDCIt->entityA == *aCopyIter) {
-            aDCIt->valA = aDiam;
-            myStorage->updateConstraint(*aDCIt);
-            aDiamConstr.erase(aDCIt);
-            break;
-          }
-      }
-    }
-  }
+void SketchSolver_ConstraintMultiRotation::transformRelative(double& theX, double& theY)
+{
+  // rotate direction
+  // myRotationVal[0] = sinA, myRotationVal[1] = cosA
+  double aTemp = theX * myRotationVal[1] - theY * myRotationVal[0];
+  theY = theX * myRotationVal[0] + theY * myRotationVal[1];
+  theX = aTemp;
+}
 
-  myPointsJustUpdated.clear();
+const std::string& SketchSolver_ConstraintMultiRotation::nameNbCopies()
+{
+  return SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID();
 }