Salome HOME
Delete key regression corrections: in previous implementation sketch entities did...
[modules/shaper.git] / src / SketchSolver / SketchSolver_Group.cpp
index 0959722eeb56e68471b3524971f9b638d3517752..34fb4d17b525179faf294141fff76a7bb9c23f5e 100644 (file)
@@ -77,7 +77,7 @@ static void sendMessage(const char* theMessageName)
 SketchSolver_Group::SketchSolver_Group(
     std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
     : myID(GroupIndexer::NEW_GROUP()),
-      myPrevSolved(true)
+      myPrevResult(STATUS_UNKNOWN)
 {
   // Initialize workplane
   myWorkplaneID = EID_UNKNOWN;
@@ -172,28 +172,15 @@ bool SketchSolver_Group::changeConstraint(
   return true;
 }
 
-
-void SketchSolver_Group::updateConstraints()
+static void updateMultiConstraints(ConstraintConstraintMap& theConstraints, FeaturePtr theFeature)
 {
-  std::set<SolverConstraintPtr> aPostponed; // postponed constraints Multi-Rotation and Multi-Translation
-
-  ConstraintConstraintMap::iterator anIt = myConstraints.begin();
-  for (; anIt != myConstraints.end(); ++anIt) {
-    if (myChangedConstraints.find(anIt->first) == myChangedConstraints.end())
-      continue;
-    if (anIt->first->getKind() == SketchPlugin_MultiRotation::ID() ||
-        anIt->first->getKind() == SketchPlugin_MultiTranslation::ID())
-      aPostponed.insert(anIt->second);
-    else
-      anIt->second->update();
+  ConstraintConstraintMap::iterator aCIt = theConstraints.begin();
+  for (; aCIt != theConstraints.end(); ++aCIt) {
+    if ((aCIt->second->getType() == CONSTRAINT_MULTI_ROTATION ||
+         aCIt->second->getType() == CONSTRAINT_MULTI_TRANSLATION)
+        && aCIt->second->isUsed(theFeature))
+      std::dynamic_pointer_cast<SketchSolver_ConstraintMulti>(aCIt->second)->update(true);
   }
-
-  // Update postponed constraints
-  std::set<SolverConstraintPtr>::iterator aSCIter = aPostponed.begin();
-  for (; aSCIter != aPostponed.end(); ++aSCIter)
-    (*aSCIter)->update();
-
-  myChangedConstraints.clear();
 }
 
 bool SketchSolver_Group::updateFeature(FeaturePtr theFeature)
@@ -201,9 +188,20 @@ bool SketchSolver_Group::updateFeature(FeaturePtr theFeature)
   if (!checkFeatureValidity(theFeature))
     return false;
 
-  myStorage->blockEvents(true);
+  bool isBlocked = myStorage->isEventsBlocked();
+  if (!isBlocked)
+    myStorage->blockEvents(true);
+
   myStorage->refresh(true);
-  return myStorage->update(theFeature);
+  bool isUpdated = myStorage->update(theFeature);
+
+  updateMultiConstraints(myConstraints, theFeature);
+
+  // events were not blocked before, the feature has not been updated,
+  // so it is necessary to revert blocking
+  if (!isUpdated && !isBlocked)
+    myStorage->blockEvents(false);
+  return isUpdated;
 }
 
 void SketchSolver_Group::moveFeature(FeaturePtr theFeature)
@@ -214,22 +212,16 @@ void SketchSolver_Group::moveFeature(FeaturePtr theFeature)
   myStorage->blockEvents(true);
   myStorage->refresh(true);
 
-  // Secondly, search attributes of the feature in the list of the Multi constraints and update them
-  ConstraintConstraintMap::iterator aCIt = myConstraints.begin();
-  for (; aCIt != myConstraints.end(); ++aCIt) {
-    if ((aCIt->second->getType() == CONSTRAINT_MULTI_ROTATION ||
-         aCIt->second->getType() == CONSTRAINT_MULTI_TRANSLATION)
-        && aCIt->second->isUsed(theFeature))
-      std::dynamic_pointer_cast<SketchSolver_ConstraintMulti>(aCIt->second)->update(true);
-  }
-
-  // Then, create temporary rigid constraint
+  // Then, create temporary Fixed constraint
   SolverConstraintPtr aConstraint = aBuilder->createMovementConstraint(theFeature);
   if (!aConstraint)
     return;
   aConstraint->process(myStorage, getId(), getWorkplaneId());
   if (aConstraint->error().empty())
     setTemporary(aConstraint);
+
+  // Secondly, search attributes of the feature in the list of the Multi constraints and update them
+  updateMultiConstraints(myConstraints, theFeature);
 }
 
 // ============================================================================
@@ -277,9 +269,6 @@ bool SketchSolver_Group::updateWorkplane()
 // ============================================================================
 bool SketchSolver_Group::resolveConstraints()
 {
-  if (!myChangedConstraints.empty())
-    updateConstraints();
-
   bool aResolved = false;
   bool isGroupEmpty = isEmpty();
   if (myStorage->isNeedToResolve() && !isGroupEmpty) {
@@ -289,6 +278,7 @@ bool SketchSolver_Group::resolveConstraints()
     mySketchSolver->setGroup(myID);
     mySketchSolver->calculateFailedConstraints(false);
     myStorage->initializeSolver(mySketchSolver);
+    mySketchSolver->prepare();
 
     SketchSolver_SolveStatus aResult = STATUS_OK;
     try {
@@ -316,28 +306,32 @@ bool SketchSolver_Group::resolveConstraints()
     } catch (...) {
 //      Events_Error::send(SketchSolver_Error::SOLVESPACE_CRASH(), this);
       getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(SketchSolver_Error::SOLVESPACE_CRASH());
-      if (myPrevSolved) {
+      if (myPrevResult == STATUS_OK || myPrevResult == STATUS_UNKNOWN) {
         // the error message should be changed before sending the message
         sendMessage(EVENT_SOLVER_FAILED);
-        myPrevSolved = false;
+        myPrevResult = STATUS_FAILED;
       }
+      mySketchSolver->undo();
       return false;
     }
     if (aResult == STATUS_OK || aResult == STATUS_EMPTYSET) {  // solution succeeded, store results into correspondent attributes
       myStorage->refresh();
-      if (!myPrevSolved) {
+      if (myPrevResult != STATUS_OK || myPrevResult == STATUS_UNKNOWN) {
         getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue("");
         // the error message should be changed before sending the message
         sendMessage(EVENT_SOLVER_REPAIRED);
-        myPrevSolved = true;
+        myPrevResult = STATUS_OK;
       }
-    } else if (!myConstraints.empty()) {
+    } else {
+      mySketchSolver->undo();
+      if (!myConstraints.empty()) {
 //      Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
-      getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(SketchSolver_Error::CONSTRAINTS());
-      if (myPrevSolved) {
-        // the error message should be changed before sending the message
-        sendMessage(EVENT_SOLVER_FAILED);
-        myPrevSolved = false;
+        getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(SketchSolver_Error::CONSTRAINTS());
+        if (myPrevResult != aResult || myPrevResult == STATUS_UNKNOWN) {
+          // the error message should be changed before sending the message
+          sendMessage(EVENT_SOLVER_FAILED);
+          myPrevResult = aResult;
+        }
       }
     }
 
@@ -512,52 +506,14 @@ void SketchSolver_Group::removeTemporaryConstraints()
 // ============================================================================
 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
 {
-  bool isFullyRemoved = true;
   ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
   for (; aCIter != myConstraints.end(); aCIter++)
     if (aCIter->first == theConstraint) {
-      if (!aCIter->second->remove()) // the constraint is not fully removed
-        isFullyRemoved = false;
+      aCIter->second->remove(); // the constraint is not fully removed
       break;
     }
-  if (aCIter == myConstraints.end())
-    return;
-
-  if (isFullyRemoved)
+  if (aCIter != myConstraints.end())
     myConstraints.erase(aCIter);
-  else if (aCIter != myConstraints.end() &&
-           aCIter->first->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
-    // Update multicoincidence
-    std::list<ConstraintPtr> aMultiCoinc;
-    SolverConstraintPtr aCoincidence = aCIter->second;
-    while (aCIter != myConstraints.end()) {
-      if (aCIter->second != aCoincidence) {
-        ++aCIter;
-        continue;
-      }
-      if (aCIter->first != theConstraint)
-        aMultiCoinc.push_back(aCIter->first);
-      aCIter->second->remove();
-      ConstraintConstraintMap::iterator aRemoveIt = aCIter++;
-      myConstraints.erase(aRemoveIt);
-    }
-
-    std::list<ConstraintPtr>::iterator anIt = aMultiCoinc.begin();
-    for (; anIt != aMultiCoinc.end(); ++anIt)
-      changeConstraint(*anIt);
-  }
-}
-
-// ============================================================================
-//  Function: isComplexConstraint
-//  Class:    SketchSolver_Group
-//  Purpose:  verifies the constraint is complex, i.e. it needs another constraints to be created before
-// ============================================================================
-bool SketchSolver_Group::isComplexConstraint(FeaturePtr theConstraint)
-{
-  return theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID() ||
-         theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID() ||
-         theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID();
 }
 
 // ============================================================================