]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/SketchSolver/SketchSolver_Manager.cpp
Salome HOME
Dimensions move using ModelAPI_ObjectMovedMessage mechanism. Move by deltas is obsole...
[modules/shaper.git] / src / SketchSolver / SketchSolver_Manager.cpp
index 5571e29276bbe8a1dc4ab5f6fe47935280de1d49..506c8e46d99a9341a1dc2c983eae606e5f88f27c 100644 (file)
@@ -1,17 +1,33 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:    SketchSolver_Manager.cpp
-// Created: 08 May 2014
-// Author:  Artem ZHIDKOV
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// 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>
+//
 
 #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>
 #include <ModelAPI_Validator.h>
+#include <SketchPlugin_Constraint.h>
 #include <SketchPlugin_Sketch.h>
 
 /// Global constraint manager object
@@ -52,8 +68,6 @@ SketchSolver_Manager::SketchSolver_Manager()
   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_MOVED));
 
-  ////Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
-  ////Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_PREPARED));
 }
 
@@ -74,13 +88,17 @@ bool SketchSolver_Manager::groupMessages()
 void SketchSolver_Manager::processEvent(
   const std::shared_ptr<Events_Message>& theMessage)
 {
+  bool needToResolve = false;
+  bool isUpdateFlushed = false;
+  bool isMovedEvt = false;
+
   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
   // are collected and must be processed by the solver
   if (theMessage->eventID() == aSketchPreparedEvent) {
     flushGrouped(anUpdateEvent);
-    return;
+    needToResolve = true;
   }
 
   if (myIsComputed)
@@ -88,19 +106,12 @@ void SketchSolver_Manager::processEvent(
   myIsComputed = true;
 
   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)
-      || theMessage->eventID() == anUpdateEvent
-      || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
+      || theMessage->eventID() == anUpdateEvent) {
     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
     std::set<ObjectPtr> aFeatures = anUpdateMsg->objects();
 
-    bool isUpdateFlushed = stopSendUpdate();
-
-    bool 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;
+    isUpdateFlushed = stopSendUpdate();
 
     // update sketch features only
     std::set<ObjectPtr>::iterator aFeatIter;
@@ -110,28 +121,28 @@ void SketchSolver_Manager::processEvent(
       if (!aFeature || aFeature->isMacro())
         continue;
 
-      hasProperFeature = updateFeature(aFeature, isMovedEvt) || hasProperFeature;
-    }
-
-    if (isMovedEvt && !hasProperFeature) {
-      // in this iteration it will compute nothing, so, no problem with recursion
-      // it is important that solver flushes signal updated after processing move signal as there
-      // is optimization that relies on this update, might be found by key "optimization"
-      myIsComputed = false;
+      updateFeature(aFeature);
     }
 
-    // Solve the set of constraints
-    bool needToUpdate = resolveConstraints();
+  } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
+    std::shared_ptr<ModelAPI_ObjectMovedMessage> aMoveMsg =
+        std::dynamic_pointer_cast<ModelAPI_ObjectMovedMessage>(theMessage);
 
-    // Features may be updated => now send events, but for all changed at once
-    if (isUpdateFlushed)
-      allowSendUpdate();
+    ObjectPtr aMovedObject = aMoveMsg->movedObject();
+    std::shared_ptr<GeomDataAPI_Point2D> aMovedPoint =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMoveMsg->movedAttribute());
 
-    myIsComputed = false;
+    const std::shared_ptr<GeomAPI_Pnt2d>& aFrom = aMoveMsg->originalPosition();
+    const std::shared_ptr<GeomAPI_Pnt2d>& aTo = aMoveMsg->currentPosition();
 
-    // send update for movement in any case
-    if (needToUpdate || isMovedEvt)
-      Events_Loop::loop()->flush(anUpdateEvent);
+    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 =
@@ -158,19 +169,30 @@ void SketchSolver_Manager::processEvent(
         (*aGroupIter)->repairConsistency();
         ++aGroupIter;
       }
-
-      resolveConstraints();
     }
     myIsComputed = false;
   }
+
+  // resolve constraints if needed
+  bool needToUpdate = needToResolve && resolveConstraints();
+  releaseFeaturesIfEventsBlocked();
+
+  // Features may be updated => now send events, but for all changed at once
+  if (isUpdateFlushed)
+    allowSendUpdate();
+
+  myIsComputed = false;
+
+  // send update for movement in any case
+  if (needToUpdate || isMovedEvt)
+    Events_Loop::loop()->flush(anUpdateEvent);
 }
 
 // ============================================================================
-//  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.
@@ -186,13 +208,72 @@ 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()));
+    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
@@ -240,11 +321,17 @@ bool SketchSolver_Manager::resolveConstraints()
   for (; aGroupIter != myGroups.end(); ++aGroupIter) {
     if ((*aGroupIter)->resolveConstraints())
       needToUpdate = true;
-    (*aGroupIter)->blockEvents(false);
   }
   return needToUpdate;
 }
 
+void SketchSolver_Manager::releaseFeaturesIfEventsBlocked() const
+{
+  std::list<SketchGroupPtr>::const_iterator aGroupIter = myGroups.begin();
+  for (; aGroupIter != myGroups.end(); ++aGroupIter)
+    (*aGroupIter)->blockEvents(false);
+}
+
 bool SketchSolver_Manager::stopSendUpdate() const
 {
 static const Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);