Salome HOME
Update unit tests for the PlaneGCS solver. Bug fixes.
[modules/shaper.git] / src / SketchSolver / SketchSolver_Storage.cpp
index b8773d92d4f007f1598995fd4fadda70ec7c1ad1..8370549174a16d7acc09de33e539a67599f88123 100644 (file)
@@ -12,6 +12,7 @@
 #include <ModelAPI_AttributeRefList.h>
 #include <SketchPlugin_Arc.h>
 #include <SketchPlugin_Circle.h>
+#include <SketchPlugin_ConstraintRigid.h>
 
 
 /// \brief Verify two vectors of constraints are equal.
@@ -75,7 +76,7 @@ void SketchSolver_Storage::addConstraint(
   if (!theSolverConstraints.empty() || aFound == myConstraintMap.end())
     myConstraintMap[theConstraint] = theSolverConstraints;
   // block events if necessary
-  if (myEventsBlocked && theConstraint->data() && theConstraint->data()->isValid())
+  if (myEventsBlocked && theConstraint && theConstraint->data() && theConstraint->data()->isValid())
     theConstraint->data()->blockSendAttributeUpdated(myEventsBlocked);
 }
 
@@ -152,6 +153,11 @@ bool SketchSolver_Storage::update(FeaturePtr theFeature, const GroupID& theGroup
     // Secondly, convert feature
     BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
     GroupID aGroup = theGroup != GID_UNKNOWN ? theGroup : myGroupID;
+    // Check external feature
+    std::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
+        std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
+    if (aSketchFeature && aSketchFeature->isExternal())
+      aGroup = GID_OUTOFGROUP;
     aRelated = aBuilder->createFeature(theFeature, aSubs, aGroup);
     if (!aRelated)
       return false;
@@ -193,6 +199,11 @@ bool SketchSolver_Storage::update(AttributePtr theAttribute, const GroupID& theG
     }
     BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
     GroupID aGroup = theGroup != GID_UNKNOWN ? theGroup : myGroupID;
+    // Check attribute of external features
+    std::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
+        std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
+    if (aSketchFeature && aSketchFeature->isExternal())
+      aGroup = GID_OUTOFGROUP;
     aRelated = aBuilder->createAttribute(anAttribute, aGroup);
     if (!aRelated)
       return false;
@@ -295,6 +306,22 @@ static bool isUsed(EntityWrapperPtr theFeature, AttributePtr theSubEntity)
   return false;
 }
 
+static bool isUsed(ConstraintPtr theConstraint, AttributePtr theAttribute)
+{
+  if (!theConstraint || !theAttribute)
+    return false;
+  std::list<AttributePtr> anAttrList = theConstraint->data()->attributes(std::string());
+  std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
+  for (; anIt != anAttrList.end(); ++anIt) {
+    if (*anIt == theAttribute)
+      return true;
+    AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIt);
+    if (aRefAttr && !aRefAttr->isObject() && aRefAttr->attr() == theAttribute)
+      return true;
+  }
+  return false;
+}
+
 bool SketchSolver_Storage::isUsed(FeaturePtr theFeature) const
 {
   if (myFeatureMap.find(theFeature) != myFeatureMap.end())
@@ -333,10 +360,15 @@ bool SketchSolver_Storage::isUsed(AttributePtr theAttribute) const
   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
       aCIt = myConstraintMap.begin();
   std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
-  for (; aCIt != myConstraintMap.end(); ++aCIt)
+  for (; aCIt != myConstraintMap.end(); ++aCIt) {
     for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
       if (::isUsed(*aCWIt, anAttribute))
         return true;
+    // Additional check for the Fixed constraints, which have no wrapper associated.
+    if (aCIt->first->getKind() == SketchPlugin_ConstraintRigid::ID() &&
+        ::isUsed(aCIt->first, anAttribute))
+      return true;
+  }
   // check in features
   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
   for (; aFIt != myFeatureMap.end(); ++aFIt)
@@ -620,6 +652,21 @@ void SketchSolver_Storage::blockEvents(bool isBlocked)
   myEventsBlocked = isBlocked;
 }
 
+std::set<ObjectPtr> SketchSolver_Storage::getConflictingConstraints(SolverPtr theSolver) const
+{
+  std::set<ObjectPtr> aConflicting;
+  std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
+      aConstrIt = myConstraintMap.begin();
+  for (; aConstrIt != myConstraintMap.end(); ++aConstrIt) {
+    std::list<ConstraintWrapperPtr>::const_iterator anIt = aConstrIt->second.begin();
+    for (; anIt != aConstrIt->second.end(); ++anIt)
+      if (theSolver->isConflicting((*anIt)->id())) {
+        aConflicting.insert(aConstrIt->first);
+        break;
+      }
+  }
+  return aConflicting;
+}