Salome HOME
Update constraints defined by parameters (issue #976)
[modules/shaper.git] / src / SketchSolver / SketchSolver_Group.cpp
index c965b8ee98e11f690d6f06b899160864e33142a7..35f90dc7464da1d81e8f74aa9d4a26a4113556e8 100644 (file)
@@ -147,11 +147,12 @@ Slvs_hEntity SketchSolver_Group::getFeatureId(FeaturePtr theFeature) const
   Slvs_hEntity aResult = SLVS_E_UNKNOWN;
   if (!myFeatureStorage)
     return aResult;
+  // Obtain regular constraints interacting with the feature and find its ID
   std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theFeature);
   if (aConstraints.empty())
     return aResult;
   std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
-  for (; aConstrIter != aConstraints.end(); aConstrIter++) {
+  for (; aConstrIter != aConstraints.end(); ++aConstrIter) {
     ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
     if (aCIter == myConstraints.end())
       continue;
@@ -159,7 +160,11 @@ Slvs_hEntity SketchSolver_Group::getFeatureId(FeaturePtr theFeature) const
     if (aResult != SLVS_E_UNKNOWN)
       return aResult;
   }
-  return SLVS_E_UNKNOWN;
+  // The feature is not found, check it in the temporary constraints
+  std::set<SolverConstraintPtr>::iterator aTmpCIter = myTempConstraints.begin();
+  for (; aTmpCIter != myTempConstraints.end() && aResult == SLVS_E_UNKNOWN; ++aTmpCIter)
+    aResult = (*aTmpCIter)->getId(theFeature);
+  return aResult;
 }
 
 // ============================================================================
@@ -172,9 +177,8 @@ Slvs_hEntity SketchSolver_Group::getAttributeId(AttributePtr theAttribute) const
   Slvs_hEntity aResult = SLVS_E_UNKNOWN;
   if (!myFeatureStorage)
     return aResult;
+  // Obtain regular constraints interacting with the attribute and find its ID
   std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theAttribute);
-  if (aConstraints.empty())
-    return aResult;
   std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
   for (; aConstrIter != aConstraints.end(); aConstrIter++) {
     ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
@@ -184,7 +188,16 @@ Slvs_hEntity SketchSolver_Group::getAttributeId(AttributePtr theAttribute) const
     if (aResult != SLVS_E_UNKNOWN)
       return aResult;
   }
-  return SLVS_E_UNKNOWN;
+  // The attribute is not found, check it in the temporary constraints
+  std::set<SolverConstraintPtr>::const_iterator aTmpCIter = myTempConstraints.begin();
+  for (; aTmpCIter != myTempConstraints.end() && aResult == SLVS_E_UNKNOWN; ++aTmpCIter)
+    aResult = (*aTmpCIter)->getId(theAttribute);
+  // Last chance to find attribute in parametric constraints
+  std::map<AttributePtr, SolverConstraintPtr>::const_iterator aParIter =
+      myParametricConstraints.find(theAttribute);
+  if (aParIter != myParametricConstraints.end())
+    aResult = aParIter->second->getId(theAttribute);
+  return aResult;
 }
 
 // ============================================================================
@@ -199,7 +212,7 @@ bool SketchSolver_Group::changeConstraint(
   if (myWorkplaneID == SLVS_E_UNKNOWN)
     return false;
 
-  if (!theConstraint)
+  if (!theConstraint || !theConstraint->data())
     return false;
 
   if (!checkFeatureValidity(theConstraint))
@@ -288,6 +301,34 @@ bool SketchSolver_Group::changeConstraint(
     myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
   myFeatureStorage->changeConstraint(theConstraint);
 
+  // Check the attributes of constraint are given by parametric expression
+  std::list<AttributePtr> anAttributes =
+      theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
+  std::list<AttributePtr>::iterator anAttrIt = anAttributes.begin();
+  for (; anAttrIt != anAttributes.end(); ++anAttrIt) {
+    AttributeRefAttrPtr aRefAttr =
+        std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIt);
+    if (!aRefAttr || aRefAttr->isObject())
+      continue;
+    std::shared_ptr<GeomDataAPI_Point2D> aPoint =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
+    if (!aPoint || (aPoint->textX().empty() && aPoint->textY().empty()))
+      continue;
+
+    std::map<AttributePtr, SolverConstraintPtr>::iterator aFound =
+        myParametricConstraints.find(aRefAttr->attr());
+    if (aFound == myParametricConstraints.end()) {
+      SolverConstraintPtr aConstraint =
+          SketchSolver_Builder::getInstance()->createParametricConstraint(aRefAttr->attr());
+      if (!aConstraint)
+        continue;
+      aConstraint->setGroup(this);
+      aConstraint->setStorage(myStorage);
+      myParametricConstraints[aRefAttr->attr()] = aConstraint;
+    } else
+      aFound->second->update();
+  }
+
   return true;
 }
 
@@ -335,6 +376,31 @@ bool SketchSolver_Group::updateFeature(std::shared_ptr<SketchPlugin_Feature> the
     aSolConIter->second->addFeature(theFeature);
     myChangedConstraints.insert(aSolConIter->first);
   }
+
+  // Search attributes of the feature in the set of parametric constraints and update them
+  std::list<AttributePtr> anAttrList =
+      theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
+  std::list<AttributePtr>::iterator anAttrIt = anAttrList.begin();
+  for (; anAttrIt != anAttrList.end(); ++anAttrIt) {
+    std::map<AttributePtr, SolverConstraintPtr>::iterator aFound =
+        myParametricConstraints.find(*anAttrIt);
+    if (aFound != myParametricConstraints.end())
+      aFound->second->update();
+    else {
+      std::shared_ptr<GeomDataAPI_Point2D> aPoint =
+          std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anAttrIt);
+      if (aPoint && (!aPoint->textX().empty() || !aPoint->textY().empty())) {
+        // Create new parametric constraint
+        SolverConstraintPtr aConstraint =
+            SketchSolver_Builder::getInstance()->createParametricConstraint(*anAttrIt);
+        if (!aConstraint)
+          continue;
+        aConstraint->setGroup(this);
+        aConstraint->setStorage(myStorage);
+        myParametricConstraints[*anAttrIt] = aConstraint;
+      }
+    }
+  }
   return true;
 }
 
@@ -505,31 +571,39 @@ 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) {
+        // 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::SOLVESPACE_CRASH());
       return false;
     }
     if (aResult == SLVS_RESULT_OKAY) {  // solution succeeded, store results into correspondent attributes
       myFeatureStorage->blockEvents(true);
+      // First refresh parametric constraints to satisfy parameters
+      std::map<AttributePtr, SolverConstraintPtr>::iterator aParIter = myParametricConstraints.begin();
+      for (; aParIter != myParametricConstraints.end(); ++aParIter)
+        aParIter->second->refresh();
+      // Update all other constraints
       ConstraintConstraintMap::iterator aConstrIter = myConstraints.begin();
-      for (; aConstrIter != myConstraints.end(); aConstrIter++)
+      for (; aConstrIter != myConstraints.end(); ++aConstrIter)
         aConstrIter->second->refresh();
       myFeatureStorage->blockEvents(false);
       if (!myPrevSolved) {
+        getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue("");
+        // the error message should be changed before sending the message
         sendMessage(EVENT_SOLVER_REPAIRED);
         myPrevSolved = true;
       }
-      getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue("");
     } else 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());
     }
 
     aResolved = true;
@@ -654,7 +728,11 @@ bool SketchSolver_Group::isConsistent()
 // ============================================================================
 void SketchSolver_Group::removeTemporaryConstraints()
 {
+  std::set<SolverConstraintPtr>::iterator aTmpIt = myTempConstraints.begin();
+  for (; aTmpIt != myTempConstraints.end(); ++aTmpIt)
+    (*aTmpIt)->remove();
   myTempConstraints.clear();
+
   while (myStorage->numberTemporary())
     myStorage->deleteTemporaryConstraint();
   // Clean lists of removed entities in the storage
@@ -762,13 +840,21 @@ static double featureToVal(FeaturePtr theFeature)
     return 1.0; // features (arc, circle, line, point)
 
   const std::string& anID = aConstraint->getKind();
-  if (anID == SketchPlugin_ConstraintCoincidence::ID())
-    return 2.0;
+  if (anID == SketchPlugin_ConstraintCoincidence::ID()) {
+    AttributeRefAttrPtr anAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+        aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
+    AttributeRefAttrPtr anAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+        aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
+    if (anAttrA && anAttrB && (anAttrA->isObject() || anAttrB->isObject()))
+      return 2.0; // point-on-line and point-on-circle should go before points coincidence constraint
+    return 2.5;
+  }
   if (anID == SketchPlugin_ConstraintDistance::ID() ||
       anID == SketchPlugin_ConstraintLength::ID() ||
-      anID == SketchPlugin_ConstraintRadius::ID() ||
-      anID == SketchPlugin_ConstraintAngle::ID())
+      anID == SketchPlugin_ConstraintRadius::ID())
     return 3.0;
+  if (anID == SketchPlugin_ConstraintAngle::ID())
+    return 3.5;
   if (anID == SketchPlugin_ConstraintHorizontal::ID() ||
       anID == SketchPlugin_ConstraintVertical::ID() ||
       anID == SketchPlugin_ConstraintParallel::ID() ||
@@ -789,7 +875,7 @@ static double featureToVal(FeaturePtr theFeature)
   return 5.5;
 }
 
-static bool operator< (FeaturePtr theFeature1, FeaturePtr theFeature2)
+static bool isLess(FeaturePtr theFeature1, FeaturePtr theFeature2)
 {
   return featureToVal(theFeature1) < featureToVal(theFeature2);
 }
@@ -814,7 +900,7 @@ std::list<FeaturePtr> SketchSolver_Group::selectApplicableFeatures(const std::se
 
     // Find the place where to insert a feature
     for (aResIt = aResult.begin(); aResIt != aResult.end(); ++aResIt)
-      if (aFeature < *aResIt)
+      if (isLess(aFeature, *aResIt))
         break;
     aResult.insert(aResIt, aFeature);
   }