Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintManager.cpp
index 4a2cf08eee22be617261246917b57b5b0daf55fc..bd346fb35fe35fac2343b7ff27b2600dae9c9544 100644 (file)
@@ -1,8 +1,11 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 // File:    SketchSolver_ConstraintManager.cpp
 // Created: 08 May 2014
 // Author:  Artem ZHIDKOV
 
 #include "SketchSolver_ConstraintManager.h"
+#include <SketchSolver_ConstraintGroup.h>
 
 #include <Events_Loop.h>
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Object.h>
 #include <ModelAPI_ResultConstruction.h>
+#include <ModelAPI_Attribute.h>
 
 #include <SketchPlugin_Constraint.h>
+#include <SketchPlugin_ConstraintCoincidence.h>
 
 #include <SketchPlugin_Arc.h>
 #include <SketchPlugin_Circle.h>
 #include <SketchPlugin_Line.h>
 #include <SketchPlugin_Point.h>
 #include <SketchPlugin_Sketch.h>
+#include <SketchPlugin_Feature.h>
 
 #include <list>
+#include <set>
+#include <memory>
 
 // Initialization of constraint manager self pointer
 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::_self = 0;
@@ -41,6 +49,7 @@ SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::Instance()
 SketchSolver_ConstraintManager::SketchSolver_ConstraintManager()
 {
   myGroups.clear();
+  myIsComputed = false;
 
   // Register in event loop
   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
@@ -56,60 +65,76 @@ SketchSolver_ConstraintManager::~SketchSolver_ConstraintManager()
 
 // ============================================================================
 //  Function: processEvent
-//  Class:    SketchSolver_PluginManager
+//  Class:    SketchSolver_Session
 //  Purpose:  listen the event loop and process the message
 // ============================================================================
-void SketchSolver_ConstraintManager::processEvent(const Events_Message* theMessage)
+void SketchSolver_ConstraintManager::processEvent(
+  const std::shared_ptr<Events_Message>& theMessage)
 {
+  if (myIsComputed)
+    return;
   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)
       || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)
       || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
-    const ModelAPI_ObjectUpdatedMessage* anUpdateMsg =
-        dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
+    std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
+        std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
     std::set<ObjectPtr> aFeatures = anUpdateMsg->objects();
 
-    bool isModifiedEvt = theMessage->eventID()
+    bool isMovedEvt = theMessage->eventID()
         == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
-    if (!isModifiedEvt) {
+    if (isMovedEvt) {
       std::set<ObjectPtr>::iterator aFeatIter;
       for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
-        FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*aFeatIter);
+        std::shared_ptr<SketchPlugin_Feature> aSFeature = 
+            std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
+        if (aSFeature)
+          updateEntity(aSFeature);
+      }
+    } else {
+      std::set<ObjectPtr>::iterator aFeatIter;
+      // iterate sketchers fisrt to create all sketches before (on load may exist several sketches)
+      for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
+        FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFeatIter);
         if (!aFeature)
           continue;
-        // Only sketches and constraints can be added by Create event
         const std::string& aFeatureKind = aFeature->getKind();
         if (aFeatureKind.compare(SketchPlugin_Sketch::ID()) == 0) {
-          boost::shared_ptr<SketchPlugin_Feature> aSketch = boost::dynamic_pointer_cast<
-              SketchPlugin_Feature>(aFeature);
-          if (aSketch)
-            changeWorkplane(aSketch);
-          continue;
-        }
-        boost::shared_ptr<SketchPlugin_Constraint> aConstraint = boost::dynamic_pointer_cast<
-            SketchPlugin_Constraint>(aFeature);
-        if (aConstraint)
-          changeConstraint(aConstraint);
-        else {
-          // Sketch plugin features can be only updated
-          boost::shared_ptr<SketchPlugin_Feature> aSFeature = boost::dynamic_pointer_cast<
-              SketchPlugin_Feature>(aFeature);
-          if (aSFeature)
-            updateEntity(aSFeature);
+          std::shared_ptr<ModelAPI_CompositeFeature> aSketch = std::dynamic_pointer_cast<
+              ModelAPI_CompositeFeature>(aFeature);
+          changeWorkplane(aSketch);
         }
       }
+      // then get anything but not the sketch
+      // at first, add coincidence constraints, because they may be used by other constraints
+      for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
+        std::shared_ptr<SketchPlugin_Feature> aFeature = 
+          std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
+        if (!aFeature || aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
+          continue;
+        changeConstraintOrEntity(aFeature);
+      }
+      // after that, add all features except coincidence
+      for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
+        std::shared_ptr<SketchPlugin_Feature> aFeature = 
+          std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
+        if (!aFeature /*|| aFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()*/)
+          continue;
+        changeConstraintOrEntity(aFeature);
+      }
     }
 
     // Solve the set of constraints
-    resolveConstraints();
+    resolveConstraints(isMovedEvt); // send update for movement in any case
   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
-    const ModelAPI_ObjectDeletedMessage* aDeleteMsg =
-        dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
+    std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleteMsg =
+        std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
     const std::set<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;
     for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
-      if (aFGrIter->compare(ModelAPI_ResultConstruction::group()) == 0)
+      if (aFGrIter->compare(ModelAPI_ResultConstruction::group()) == 0 ||
+          aFGrIter->compare(ModelAPI_Feature::group()) == 0)
         break;
 
     if (aFGrIter != aFeatureGroups.end()) {
@@ -136,11 +161,11 @@ void SketchSolver_ConstraintManager::processEvent(const Events_Message* theMessa
 
 // ============================================================================
 //  Function: changeWorkplane
-//  Class:    SketchSolver_PluginManager
+//  Class:    SketchSolver_Session
 //  Purpose:  update workplane by given parameters of the sketch
 // ============================================================================
 bool SketchSolver_ConstraintManager::changeWorkplane(
-    boost::shared_ptr<SketchPlugin_Feature> theSketch)
+    std::shared_ptr<ModelAPI_CompositeFeature> theSketch)
 {
   bool aResult = true;  // changed when a workplane wrongly updated
   bool isUpdated = false;
@@ -156,7 +181,7 @@ bool SketchSolver_ConstraintManager::changeWorkplane(
   if (!isUpdated) {
     SketchSolver_ConstraintGroup* aNewGroup = new SketchSolver_ConstraintGroup(theSketch);
     // Verify that the group is created successfully
-    if (!aNewGroup->isBaseWorkplane(theSketch)) {
+    if (!aNewGroup->isBaseWorkplane(theSketch) || !aNewGroup->isWorkplaneValid()) {
       delete aNewGroup;
       return false;
     }
@@ -166,36 +191,46 @@ bool SketchSolver_ConstraintManager::changeWorkplane(
 }
 
 // ============================================================================
-//  Function: changeConstraint
-//  Class:    SketchSolver_PluginManager
-//  Purpose:  create/update the constraint and place it into appropriate group
+//  Function: changeConstraintOrEntity
+//  Class:    SketchSolver_Session
+//  Purpose:  create/update the constraint or the feature and place it into appropriate group
 // ============================================================================
-bool SketchSolver_ConstraintManager::changeConstraint(
-    boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
+bool SketchSolver_ConstraintManager::changeConstraintOrEntity(
+    std::shared_ptr<SketchPlugin_Feature> theFeature)
 {
-  // Search the groups which this constraint touches
+  // Search the groups which this feature touches
   std::set<Slvs_hGroup> aGroups;
-  findGroups(theConstraint, aGroups);
+  findGroups(theFeature, aGroups);
+
+  std::shared_ptr<SketchPlugin_Constraint> aConstraint = 
+      std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
 
   // Process the groups list
-  if (aGroups.size() == 0) {  // There are no groups applicable for this constraint => create new one
-    boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
+  if (aGroups.size() == 0) {
+    // There are no groups applicable for this constraint => create new one
+    // The group will be created only for constraints, not for features
+    if (!aConstraint) return false;
+    std::shared_ptr<ModelAPI_CompositeFeature> aWP = findWorkplane(aConstraint);
     if (!aWP)
       return false;
     SketchSolver_ConstraintGroup* aGroup = new SketchSolver_ConstraintGroup(aWP);
-    if (!aGroup->changeConstraint(theConstraint)) {
+    if (!aGroup->changeConstraint(aConstraint)) {
       delete aGroup;
       return false;
     }
     myGroups.push_back(aGroup);
     return true;
-  } else if (aGroups.size() == 1) {  // Only one group => add constraint into it
+  } else if (aGroups.size() == 1) {  // Only one group => add feature into it
     Slvs_hGroup aGroupId = *(aGroups.begin());
     std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
     for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
-      if ((*aGroupIter)->getId() == aGroupId)
-        return (*aGroupIter)->changeConstraint(theConstraint);
-  } else if (aGroups.size() > 1) {  // Several groups applicable for this constraint => need to merge them
+      if ((*aGroupIter)->getId() == aGroupId) {
+        // If the group is empty, the feature is not added (the constraint only)
+        if (!aConstraint && !(*aGroupIter)->isEmpty())
+          return (*aGroupIter)->changeEntityFeature(theFeature) != SLVS_E_UNKNOWN;
+        return (*aGroupIter)->changeConstraint(aConstraint);
+      }
+  } else if (aGroups.size() > 1) {  // Several groups applicable for this feature => need to merge them
     std::set<Slvs_hGroup>::const_iterator aGroupsIter = aGroups.begin();
 
     // Search first group
@@ -226,7 +261,9 @@ bool SketchSolver_ConstraintManager::changeConstraint(
       anOtherGroupIter = myGroups.begin() + aShiftOther;
     }
 
-    return (*aFirstGroupIter)->changeConstraint(theConstraint);
+    if (aConstraint)
+      return (*aFirstGroupIter)->changeConstraint(aConstraint);
+    return (*aFirstGroupIter)->changeEntityFeature(theFeature) != SLVS_E_UNKNOWN;
   }
 
   // Something goes wrong
@@ -235,11 +272,11 @@ bool SketchSolver_ConstraintManager::changeConstraint(
 
 // ============================================================================
 //  Function: updateEntity
-//  Class:    SketchSolver_PluginManager
+//  Class:    SketchSolver_Session
 //  Purpose:  update any element on the sketch, which is used by constraints
 // ============================================================================
 void SketchSolver_ConstraintManager::updateEntity(
-    boost::shared_ptr<SketchPlugin_Feature> theFeature)
+    std::shared_ptr<SketchPlugin_Feature> theFeature)
 {
   // Create list of attributes depending on type of the feature
   std::vector<std::string> anAttrList;
@@ -272,7 +309,7 @@ void SketchSolver_ConstraintManager::updateEntity(
     for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++) {
       if ((*aGroupIter)->isEmpty())
         continue;
-      boost::shared_ptr<ModelAPI_Attribute> anAttribute = boost::dynamic_pointer_cast<
+      std::shared_ptr<ModelAPI_Attribute> anAttribute = std::dynamic_pointer_cast<
           ModelAPI_Attribute>(theFeature->data()->attribute(*anAttrIter));
       (*aGroupIter)->updateEntityIfPossible(anAttribute);
     }
@@ -281,24 +318,24 @@ void SketchSolver_ConstraintManager::updateEntity(
   std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
     if (!(*aGroupIter)->isEmpty())
-      (*aGroupIter)->updateRelatedConstraints(theFeature);
+      (*aGroupIter)->updateRelatedConstraintsFeature(theFeature);
 }
 
 // ============================================================================
 //  Function: findGroups
-//  Class:    SketchSolver_PluginManager
-//  Purpose:  search groups of entities interacting with given constraint
+//  Class:    SketchSolver_Session
+//  Purpose:  search groups of entities interacting with given feature
 // ============================================================================
 void SketchSolver_ConstraintManager::findGroups(
-    boost::shared_ptr<SketchPlugin_Constraint> theConstraint,
+    std::shared_ptr<SketchPlugin_Feature> theFeature,
     std::set<Slvs_hGroup>& theGroupIDs) const
 {
-  boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
+  std::shared_ptr<ModelAPI_CompositeFeature> aWP = findWorkplane(theFeature);
 
   SketchSolver_ConstraintGroup* anEmptyGroup = 0;  // appropriate empty group for specified constraint
   std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
-    if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theConstraint)) {
+    if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theFeature)) {
       if (!(*aGroupIter)->isEmpty())
         theGroupIDs.insert((*aGroupIter)->getId());
       else if (!anEmptyGroup)
@@ -311,47 +348,67 @@ void SketchSolver_ConstraintManager::findGroups(
 }
 
 // ============================================================================
-//  Function: findWorkplaneForConstraint
-//  Class:    SketchSolver_PluginManager
-//  Purpose:  search workplane containing given constraint
+//  Function: findWorkplane
+//  Class:    SketchSolver_Session
+//  Purpose:  search workplane containing given feature
 // ============================================================================
-boost::shared_ptr<SketchPlugin_Feature> SketchSolver_ConstraintManager::findWorkplaneForConstraint(
-    boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
+std::shared_ptr<ModelAPI_CompositeFeature> SketchSolver_ConstraintManager
+::findWorkplane(std::shared_ptr<SketchPlugin_Feature> theFeature) const
 {
   // Already verified workplanes
-  std::set<boost::shared_ptr<SketchPlugin_Feature> > aVerified;
+  std::set<std::shared_ptr<ModelAPI_CompositeFeature> > aVerified;
 
   std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++) {
-    boost::shared_ptr<SketchPlugin_Feature> aWP = (*aGroupIter)->getWorkplane();
+    std::shared_ptr<ModelAPI_CompositeFeature> aWP = (*aGroupIter)->getWorkplane();
     if (aVerified.find(aWP) != aVerified.end())
       continue;
 
-    boost::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures = boost::dynamic_pointer_cast<
-        ModelAPI_AttributeRefList>(aWP->data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
-    std::list<ObjectPtr> aFeaturesList = aWPFeatures->list();
-    std::list<ObjectPtr>::const_iterator anIter;
-    for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
-      if (*anIter == theConstraint)
-        return aWP;  // workplane is found
+    DataPtr aData = aWP->data();
+    if (aData) {
+      std::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures = std::dynamic_pointer_cast<
+          ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
+      std::list<ObjectPtr> aFeaturesList = aWPFeatures->list();
+      std::list<ObjectPtr>::const_iterator anIter;
+      for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
+        if (*anIter == theFeature)
+          return aWP;  // workplane is found
+    }
     aVerified.insert(aWP);
   }
 
-  return boost::shared_ptr<SketchPlugin_Feature>();
+  return std::shared_ptr<ModelAPI_CompositeFeature>();
 }
 
 // ============================================================================
 //  Function: resolveConstraints
-//  Class:    SketchSolver_PluginManager
+//  Class:    SketchSolver_Session
 //  Purpose:  change entities according to available constraints
 // ============================================================================
-void SketchSolver_ConstraintManager::resolveConstraints()
+void SketchSolver_ConstraintManager::resolveConstraints(const bool theForceUpdate)
 {
+  myIsComputed = true;
+  bool needToUpdate = false;
+  static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
+  // to avoid redisplay of each segment on update by solver one by one in the viewer
+  bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
+  if (isUpdateFlushed) {
+    Events_Loop::loop()->setFlushed(anUpdateEvent, false);
+  }
+
   std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
-    (*aGroupIter)->resolveConstraints();
+    if ((*aGroupIter)->resolveConstraints())
+      needToUpdate = true;
 
-  // Features may be updated => send events
-  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+  // Features may be updated => now send events, but for all changed at once
+  if (isUpdateFlushed) {
+    Events_Loop::loop()->setFlushed(anUpdateEvent, true);
+  }
+  // Must be before flush because on "Updated" flush the results may be produced
+  // and the creation event is appeared with many new objects. If myIsComputed these
+  // events are missed in processEvents and some elements are not added.
+  myIsComputed = false;
+  if (needToUpdate || theForceUpdate)
+    Events_Loop::loop()->flush(anUpdateEvent);
 }
-