Salome HOME
Updated copyright comment
[modules/shaper.git] / src / SketchSolver / SketchSolver_Manager.cpp
index f0d6eb21045fab8c72a8063df93e48500e594248..8bebc53c4247037984ef7b9c14fb112e7c6903f4 100644 (file)
@@ -1,19 +1,32 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:    SketchSolver_Manager.cpp
-// Created: 08 May 2014
-// Author:  Artem ZHIDKOV
+// Copyright (C) 2014-2024  CEA, EDF
+//
+// 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
+//
 
 #include "SketchSolver_Manager.h"
 #include "SketchSolver_Error.h"
 
 #include <Events_Loop.h>
 #include <GeomDataAPI_Point2D.h>
+#include <GeomDataAPI_Point2DArray.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
@@ -30,6 +43,64 @@ static bool isFeatureValid(FeaturePtr theFeature)
   return aFactory->validate(theFeature);
 }
 
+typedef std::map<int, std::shared_ptr<SketchPlugin_Feature>> IndexedFeatureMap;
+
+static void featuresOrderedByCreation(const std::set<ObjectPtr>& theOriginalFeatures,
+                                      IndexedFeatureMap& theOrderedFeatures)
+{
+  std::set<ObjectPtr>::iterator aFeatIter = theOriginalFeatures.begin();
+  for (; aFeatIter != theOriginalFeatures.end(); aFeatIter++) {
+    std::shared_ptr<SketchPlugin_Feature> aFeature =
+        std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
+    if (aFeature && !aFeature->isMacro() && aFeature->data() && aFeature->data()->isValid()) {
+      theOrderedFeatures[aFeature->data()->featureId()] = aFeature;
+    }
+  }
+}
+
+static void featuresOrderedByType(const std::set<ObjectPtr>& theOriginalFeatures,
+                                  IndexedFeatureMap& theOrderedFeatures,
+                                  CompositeFeaturePtr& theSketch)
+{
+  int aFeatureIndex = 0;
+  int aConstraintIndex = (int)theOriginalFeatures.size();
+
+  std::set<ObjectPtr>::iterator aFeatIter = theOriginalFeatures.begin();
+  for (; aFeatIter != theOriginalFeatures.end(); aFeatIter++) {
+    std::shared_ptr<SketchPlugin_Feature> aFeature =
+        std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
+    if (aFeature) {
+      if (!aFeature->isMacro() && aFeature->data() && aFeature->data()->isValid()) {
+        std::shared_ptr<SketchPlugin_Constraint> aConstraint =
+            std::dynamic_pointer_cast<SketchPlugin_Constraint>(aFeature);
+        if (aConstraint)
+          theOrderedFeatures[++aConstraintIndex] = aFeature;
+        else
+          theOrderedFeatures[++aFeatureIndex] = aFeature;
+      }
+    }
+    else {
+      CompositeFeaturePtr aSketch =
+          std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIter);
+      if (aSketch && aSketch->getKind() == SketchPlugin_Sketch::ID())
+        theSketch = aSketch;
+    }
+  }
+}
+
+static void setPoint(AttributePtr theAttribute,
+                     const int thePointIndex,
+                     const std::shared_ptr<GeomAPI_Pnt2d> theValue)
+{
+  AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
+  AttributePoint2DArrayPtr aPointArrayAttr =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(theAttribute);
+  if (aPointAttr)
+    aPointAttr->setValue(theValue);
+  else if (aPointArrayAttr && thePointIndex >= 0)
+    aPointArrayAttr->setPnt(thePointIndex, theValue);
+}
+
 
 
 // ========================================================
@@ -54,7 +125,10 @@ 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));
+  Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_GET_DOF_OBJECTS));
 }
 
 SketchSolver_Manager::~SketchSolver_Manager()
@@ -78,6 +152,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
@@ -91,55 +166,36 @@ void SketchSolver_Manager::processEvent(
     return;
   myIsComputed = true;
 
-#ifdef SUPPORT_NEW_MOVE
-  if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)
-      || theMessage->eventID() == anUpdateEvent) {
-#else
-  if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)
-      || theMessage->eventID() == anUpdateEvent
-      || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
-#endif
+  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();
 
-#ifndef SUPPORT_NEW_MOVE
-    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;
-#endif
-
     // 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;
-
-#ifdef SUPPORT_NEW_MOVE
-      updateFeature(aFeature);
-#else
-      hasProperFeature = updateFeature(aFeature, isMovedEvt) || hasProperFeature;
-#endif
+    const std::set<ObjectPtr>& aFeatures = anUpdateMsg->objects();
+    IndexedFeatureMap anOrderedFeatures;
+    CompositeFeaturePtr aSketchFeature;
+    // try to keep order as features were created if there are several created features: #2229
+    if (theMessage->eventID() == aCreatedEvent && aFeatures.size() > 1) {
+      featuresOrderedByCreation(aFeatures, anOrderedFeatures);
+    } else { // order is not important, just process features before constraints
+      featuresOrderedByType(aFeatures, anOrderedFeatures, aSketchFeature);
     }
 
-#ifndef SUPPORT_NEW_MOVE
-    if (isMovedEvt && hasProperFeature)
-      needToResolve = true;
+    IndexedFeatureMap::iterator aFeat;
+    for (aFeat = anOrderedFeatures.begin(); aFeat != anOrderedFeatures.end(); aFeat++) {
+      updateFeature(aFeat->second);
+    }
+    updateSketch(aSketchFeature);
 
-#else
   } 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());
+    AttributePtr aMovedAttribute = aMoveMsg->movedAttribute();
+    int aMovedPoint = aMoveMsg->movedPointIndex();
 
     const std::shared_ptr<GeomAPI_Pnt2d>& aFrom = aMoveMsg->originalPosition();
     const std::shared_ptr<GeomAPI_Pnt2d>& aTo = aMoveMsg->currentPosition();
@@ -150,21 +206,21 @@ void SketchSolver_Manager::processEvent(
           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 (aMovedAttribute)
+      needToResolve = moveAttribute(aMovedAttribute, aMovedPoint, aFrom, aTo);
 
-#endif
   } 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()) {
@@ -182,6 +238,35 @@ void SketchSolver_Manager::processEvent(
     }
     myIsComputed = false;
   }
+  else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_GET_DOF_OBJECTS)) {
+    std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
+      std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
+    std::set<ObjectPtr> aObjects = anUpdateMsg->objects();
+    if (aObjects.size() == 1) {
+      std::set<ObjectPtr>::const_iterator aIt;
+      for (aIt = aObjects.cbegin(); aIt != aObjects.cend(); aIt++) {
+        CompositeFeaturePtr aFeature =
+            std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aIt);
+        if (aFeature) {
+          SketchGroupPtr aGroup = findGroup(aFeature);
+
+          std::set<ObjectPtr> aFreeFeatures;
+          aGroup->underconstrainedFeatures(aFreeFeatures);
+
+          std::list<ObjectPtr> aFeatures;
+          std::set<ObjectPtr>::const_iterator aIt;
+          for (aIt = aFreeFeatures.cbegin(); aIt != aFreeFeatures.cend(); ++aIt) {
+            aFeatures.push_back(*aIt);
+          }
+
+          // send features to GUI
+          static const Events_ID anEvent = Events_Loop::eventByName(EVENT_DOF_OBJECTS);
+          ModelAPI_EventCreator::get()->sendUpdated(aFeatures, anEvent);
+          Events_Loop::loop()->flush(anEvent);
+        }
+      }
+    }
+  }
 
   // resolve constraints if needed
   bool needToUpdate = needToResolve && resolveConstraints();
@@ -198,16 +283,30 @@ void SketchSolver_Manager::processEvent(
     Events_Loop::loop()->flush(anUpdateEvent);
 }
 
+// ============================================================================
+//  Function: updateSketch
+//  Purpose:  update sketch plane in appropriate group
+// ============================================================================
+bool SketchSolver_Manager::updateSketch(const CompositeFeaturePtr& theSketch)
+{
+  if (!theSketch)
+    return true;
+
+  bool isOk = true;
+  std::list<SketchGroupPtr>::const_iterator aGroupIt;
+  for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); ++aGroupIt)
+    if ((*aGroupIt)->getWorkplane() == theSketch) {
+      (*aGroupIt)->updateSketch(theSketch);
+      break;
+    }
+  return isOk;
+}
+
 // ============================================================================
 //  Function: updateFeature
 //  Purpose:  create/update constraint or feature in appropriate group
 // ============================================================================
-#ifdef SUPPORT_NEW_MOVE
 bool SketchSolver_Manager::updateFeature(const std::shared_ptr<SketchPlugin_Feature>& theFeature)
-#else
-bool SketchSolver_Manager::updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature,
-                                         bool theMoved)
-#endif
 {
   // Check feature validity and find a group to place it.
   // If the feature is not valid, the returned group will be empty.
@@ -223,16 +322,11 @@ bool SketchSolver_Manager::updateFeature(std::shared_ptr<SketchPlugin_Feature> t
   bool isOk = false;
   if (aConstraint)
     isOk = aGroup->changeConstraint(aConstraint);
-#ifndef SUPPORT_NEW_MOVE
-  else if (theMoved)
-    isOk = aGroup->moveFeature(theFeature);
-#endif
   else
     isOk = aGroup->updateFeature(theFeature);
   return isOk;
 }
 
-#ifdef SUPPORT_NEW_MOVE
 // ============================================================================
 //  Function: moveFeature
 //  Purpose:  move given feature in appropriate group
@@ -246,6 +340,20 @@ bool SketchSolver_Manager::moveFeature(
   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);
 }
@@ -255,30 +363,41 @@ bool SketchSolver_Manager::moveFeature(
 //  Purpose:  move given attribute in appropriate group
 // ============================================================================
 bool SketchSolver_Manager::moveAttribute(
-    const std::shared_ptr<GeomDataAPI_Point2D>& theMovedAttribute,
+    const std::shared_ptr<ModelAPI_Attribute>& theMovedAttribute,
+    const int theMovedPointIndex,
     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)
+  {
+    setPoint(theMovedAttribute, theMovedPointIndex, 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)
+  if (!aGroup) {
+    setPoint(theMovedAttribute, theMovedPointIndex, theTo);
     return false;
+  }
 
   aGroup->blockEvents(true);
-  return aGroup->movePoint(theMovedAttribute, theFrom, theTo);
+  return aGroup->movePoint(theMovedAttribute, theMovedPointIndex, theFrom, theTo);
 }
-#endif
 
 // ============================================================================
 //  Function: findGroup
 //  Purpose:  search groups of entities interacting with given feature
 // ============================================================================
 SketchGroupPtr SketchSolver_Manager::findGroup(
-    std::shared_ptr<SketchPlugin_Feature> theFeature)
+  std::shared_ptr<SketchPlugin_Feature> theFeature)
 {
   if (!isFeatureValid(theFeature))
     return SketchGroupPtr(); // do not process wrong features
@@ -294,17 +413,21 @@ SketchGroupPtr SketchSolver_Manager::findGroup(
       break;
     }
   }
+  return findGroup(aSketch);
+}
 
-  if (!aSketch)
+SketchGroupPtr SketchSolver_Manager::findGroup(CompositeFeaturePtr theSketch)
+{
+  if (!theSketch)
     return SketchGroupPtr(); // not a sketch's feature
 
   std::list<SketchGroupPtr>::const_iterator aGroupIt;
   for (aGroupIt = myGroups.begin(); aGroupIt != myGroups.end(); ++aGroupIt)
-    if ((*aGroupIt)->getWorkplane() == aSketch)
+    if ((*aGroupIt)->getWorkplane() == theSketch)
       return *aGroupIt;
 
   // group for the sketch does not created yet
-  SketchGroupPtr aNewGroup = SketchGroupPtr(new SketchSolver_Group(aSketch));
+  SketchGroupPtr aNewGroup = SketchGroupPtr(new SketchSolver_Group(theSketch));
   myGroups.push_back(aNewGroup);
   return aNewGroup;
 }