Salome HOME
Correct misprint
[modules/shaper.git] / src / SketchSolver / SketchSolver_Manager.cpp
index c4a9969a523d6db83c5c412f6bc47d7839c2bdcd..8e976d02b9dd8d370fc3f515ddd47be4e2c30900 100644 (file)
 // 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<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
 //
 
 #include "SketchSolver_Manager.h"
 #include "SketchSolver_Error.h"
 
 #include <Events_Loop.h>
+#include <GeomDataAPI_Point2D.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_Session.h>
@@ -91,6 +93,7 @@ void SketchSolver_Manager::processEvent(
   bool isUpdateFlushed = false;
   bool isMovedEvt = false;
 
+  static const Events_ID aCreatedEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
   static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
   static const Events_ID aSketchPreparedEvent = Events_Loop::eventByName(EVENT_SKETCH_PREPARED);
   // sketch is prepared for resolve: all the needed events
@@ -104,46 +107,71 @@ void SketchSolver_Manager::processEvent(
     return;
   myIsComputed = true;
 
-  if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)
-      || theMessage->eventID() == anUpdateEvent
-      || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
+  if (theMessage->eventID() == aCreatedEvent || theMessage->eventID() == anUpdateEvent) {
     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
-    std::set<ObjectPtr> aFeatures = anUpdateMsg->objects();
 
     isUpdateFlushed = stopSendUpdate();
 
-    isMovedEvt = theMessage->eventID()
-          == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
-
-    // Shows that the message has at least one feature applicable for solver
-    bool hasProperFeature = false;
-
     // update sketch features only
-    std::set<ObjectPtr>::iterator aFeatIter;
-    for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
-      std::shared_ptr<SketchPlugin_Feature> aFeature =
-          std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
-      if (!aFeature || aFeature->isMacro())
-        continue;
-
-      hasProperFeature = updateFeature(aFeature, isMovedEvt) || hasProperFeature;
+    const std::set<ObjectPtr>& aFeatures = anUpdateMsg->objects();
+    // try to keep order as features were created if there are several created features: #2229
+    if (theMessage->eventID() == aCreatedEvent && aFeatures.size() > 1) {
+      std::map<int, std::shared_ptr<SketchPlugin_Feature>> anOrderedFeatures;
+      std::set<ObjectPtr>::iterator aFeatIter;
+      for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
+        std::shared_ptr<SketchPlugin_Feature> aFeature =
+            std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
+        if (aFeature && !aFeature->isMacro() && aFeature->data() && aFeature->data()->isValid()) {
+          anOrderedFeatures[aFeature->data()->featureId()] = aFeature;
+        }
+      }
+      std::map<int, std::shared_ptr<SketchPlugin_Feature>>::iterator aFeat;
+      for(aFeat = anOrderedFeatures.begin(); aFeat != anOrderedFeatures.end(); aFeat++) {
+        updateFeature(aFeat->second);
+      }
+    } else { // order is not important
+      std::set<ObjectPtr>::iterator aFeatIter;
+      for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
+        std::shared_ptr<SketchPlugin_Feature> aFeature =
+            std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
+        if (aFeature && !aFeature->isMacro())
+          updateFeature(aFeature);
+      }
     }
 
-    if (isMovedEvt && hasProperFeature)
-      needToResolve = true;
+  } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
+    std::shared_ptr<ModelAPI_ObjectMovedMessage> aMoveMsg =
+        std::dynamic_pointer_cast<ModelAPI_ObjectMovedMessage>(theMessage);
+
+    ObjectPtr aMovedObject = aMoveMsg->movedObject();
+    std::shared_ptr<GeomDataAPI_Point2D> aMovedPoint =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMoveMsg->movedAttribute());
+
+    const std::shared_ptr<GeomAPI_Pnt2d>& aFrom = aMoveMsg->originalPosition();
+    const std::shared_ptr<GeomAPI_Pnt2d>& aTo = aMoveMsg->currentPosition();
+
+    if (aMovedObject) {
+      FeaturePtr aMovedFeature = ModelAPI_Feature::feature(aMovedObject);
+      std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+          std::dynamic_pointer_cast<SketchPlugin_Feature>(aMovedFeature);
+      if (aSketchFeature && !aSketchFeature->isMacro())
+        needToResolve = moveFeature(aSketchFeature, aFrom, aTo);
+    } else if (aMovedPoint)
+      needToResolve = moveAttribute(aMovedPoint, aFrom, aTo);
 
   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
     std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleteMsg =
       std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
-    const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
+    const std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>& aFeatureGroups =
+      aDeleteMsg->groups();
 
     // Find SketchPlugin_Sketch::ID() in groups.
     // The constraint groups should be updated when an object removed from Sketch
-    std::set<std::string>::const_iterator aFGrIter;
+    std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator aFGrIter;
     for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
-      if (aFGrIter->compare(ModelAPI_ResultConstruction::group()) == 0 ||
-          aFGrIter->compare(ModelAPI_Feature::group()) == 0)
+      if (aFGrIter->second == ModelAPI_ResultConstruction::group() ||
+        aFGrIter->second == ModelAPI_Feature::group())
         break;
 
     if (aFGrIter != aFeatureGroups.end()) {
@@ -178,11 +206,10 @@ void SketchSolver_Manager::processEvent(
 }
 
 // ============================================================================
-//  Function: changeConstraintOrEntity
-//  Purpose:  create/update the constraint or the feature and place it into appropriate group
+//  Function: updateFeature
+//  Purpose:  create/update constraint or feature in appropriate group
 // ============================================================================
-bool SketchSolver_Manager::updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature,
-                                         bool theMoved)
+bool SketchSolver_Manager::updateFeature(const std::shared_ptr<SketchPlugin_Feature>& theFeature)
 {
   // Check feature validity and find a group to place it.
   // If the feature is not valid, the returned group will be empty.
@@ -198,13 +225,75 @@ bool SketchSolver_Manager::updateFeature(std::shared_ptr<SketchPlugin_Feature> t
   bool isOk = false;
   if (aConstraint)
     isOk = aGroup->changeConstraint(aConstraint);
-  else if (theMoved)
-    isOk = aGroup->moveFeature(theFeature);
   else
     isOk = aGroup->updateFeature(theFeature);
   return isOk;
 }
 
+// ============================================================================
+//  Function: moveFeature
+//  Purpose:  move given feature in appropriate group
+// ============================================================================
+bool SketchSolver_Manager::moveFeature(
+    const std::shared_ptr<SketchPlugin_Feature>& theMovedFeature,
+    const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
+    const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
+{
+  SketchGroupPtr aGroup = findGroup(theMovedFeature);
+  if (!aGroup)
+    return false;
+
+  std::shared_ptr<SketchPlugin_Constraint> aConstraint =
+      std::dynamic_pointer_cast<SketchPlugin_Constraint>(theMovedFeature);
+  if (aConstraint)
+  {
+    std::shared_ptr<GeomDataAPI_Point2D> aPntAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
+      (aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+    if (aPntAttr)
+    {
+      aPntAttr->setValue(theTo);
+      Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+    }
+    return true;
+  }
+
+  aGroup->blockEvents(true);
+  return aGroup->moveFeature(theMovedFeature, theFrom, theTo);
+}
+
+// ============================================================================
+//  Function: moveAttribute
+//  Purpose:  move given attribute in appropriate group
+// ============================================================================
+bool SketchSolver_Manager::moveAttribute(
+    const std::shared_ptr<GeomDataAPI_Point2D>& theMovedAttribute,
+    const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
+    const std::shared_ptr<GeomAPI_Pnt2d>& theTo)
+{
+  FeaturePtr anOwner = ModelAPI_Feature::feature(theMovedAttribute->owner());
+  std::shared_ptr<SketchPlugin_Constraint> aConstraint =
+      std::dynamic_pointer_cast<SketchPlugin_Constraint>(anOwner);
+  if (aConstraint)
+  {
+    theMovedAttribute->setValue(theTo);
+    Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+    return true;
+  }
+
+  std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+      std::dynamic_pointer_cast<SketchPlugin_Feature>(anOwner);
+  SketchGroupPtr aGroup;
+  if (aSketchFeature)
+    aGroup = findGroup(aSketchFeature);
+  if (!aGroup) {
+    theMovedAttribute->setValue(theTo);
+    return false;
+  }
+
+  aGroup->blockEvents(true);
+  return aGroup->movePoint(theMovedAttribute, theFrom, theTo);
+}
+
 // ============================================================================
 //  Function: findGroup
 //  Purpose:  search groups of entities interacting with given feature