Salome HOME
External edges color should not be changed when sketch is fully constrained.
[modules/shaper.git] / src / PartSet / PartSet_OverconstraintListener.cpp
index 41ee6b2a22905cea2c9cb0e237de0c2f5dc87dab..2fd930ca72643356bb5882aed156e81e801d269d 100755 (executable)
@@ -5,24 +5,30 @@
 // Author:      Vitaly SMETANNIKOV
 
 #include <ModelAPI_Tools.h>
+#include <ModelAPI_AttributeString.h>
 
 #include "PartSet_OverconstraintListener.h"
 #include <PartSet_Module.h>
 #include <PartSet_SketcherMgr.h>
 #include <PartSet_SketcherReentrantMgr.h>
 
+#include "XGUI_CustomPrs.h"
+#include "XGUI_Displayer.h"
 #include "XGUI_ModuleConnector.h"
+#include "XGUI_OperationMgr.h"
+#include "XGUI_Tools.h"
 #include "XGUI_Workshop.h"
-#include "XGUI_Displayer.h"
-#include "XGUI_CustomPrs.h"
 
 #include "SketcherPrs_SymbolPrs.h"
 #include "SketchPlugin_SketchEntity.h"
+#include "SketchPlugin_MacroArcReentrantMessage.h"
+#include "SketchPlugin_Sketch.h"
 
 #include "Events_Loop.h"
 
 #include <GeomAPI_IPresentable.h>
 #include <ModelAPI_Events.h>
+#include <ModelAPI_EventReentrantMessage.h>
 #include <ModuleBase_Tools.h>
 
 #include <QString>
@@ -30,7 +36,7 @@
 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
 
 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
-: myWorkshop(theWorkshop), myIsFullyConstrained(false)
+: myWorkshop(theWorkshop), myIsActive(false), myIsFullyConstrained(false)
 {
   Events_Loop* aLoop = Events_Loop::loop();
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
@@ -39,23 +45,46 @@ PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorks
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
 
-  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
+  aLoop->registerListener(this, ModelAPI_EventReentrantMessage::eventId());
+  aLoop->registerListener(this, SketchPlugin_MacroArcReentrantMessage::eventId());
+}
+
+void PartSet_OverconstraintListener::setActive(const bool& theActive)
+{
+  if (myIsActive == theActive)
+    return;
+
+  myIsActive = theActive;
+  myIsFullyConstrained = false; /// returned to default state, no custom color for it
+
+  if (myIsActive) {
+    PartSet_Module* aModule = module();
+    CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
+    if (aSketch.get()) {
+      std::string aDOFMessage = aSketch->string(SketchPlugin_Sketch::SOLVER_DOF())->value();
+      myIsFullyConstrained = QString(aDOFMessage.c_str()).contains("DoF = 0");
+    }
+  }
 }
 
 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
                                                     std::vector<int>& theColor)
 {
+  if (!myIsActive)
+    return;
+
   if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
     theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
   }
   if (myIsFullyConstrained) {
     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
     // only entity features has custom color when sketch is fully constrained
-    if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
-      PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
-      CompositeFeaturePtr aCompositeFeature = aModule->sketchMgr()->activeSketch();
+    if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind()) &&
+        !PartSet_SketcherMgr::isExternalFeature(aFeature)) {
+      PartSet_Module* aModule = module();
+      CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
       // the given object is sub feature of the current sketch(created or edited)
-      if (ModelAPI_Tools::compositeOwner(aFeature) == aCompositeFeature)
+      if (ModelAPI_Tools::compositeOwner(aFeature) == aSketch)
         theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
     }
   }
@@ -64,6 +93,9 @@ void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
 void PartSet_OverconstraintListener::processEvent(
                                                  const std::shared_ptr<Events_Message>& theMessage)
 {
+  if (!myIsActive)
+    return;
+
 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
   bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
   int aCount = 0;
@@ -123,16 +155,38 @@ void PartSet_OverconstraintListener::processEvent(
       }
     }
   }
-  else if (anEventID == Events_Loop::eventByName(EVENT_OBJECT_CREATED)) {
-    PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
-    PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
-    if (aReentrantMgr->isInternalEditActive()) {
-      std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
-            std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
-      std::set<ObjectPtr> anObjects = aUpdMsg->objects();
-      aReentrantMgr->appendCreatedObjects(anObjects);
+  else if (anEventID == ModelAPI_EventReentrantMessage::eventId() ||
+           anEventID == SketchPlugin_MacroArcReentrantMessage::eventId()) {
+    // the message is sent to sketcher reentrant manager only if the kind of feature
+    // sender is equal to kind of the current operation. E.g. Horizontal create operation
+    // is active. Sketch Line feature is changed, so execute is called, it will send message
+    // This Line's message should not be processed, as the reentrant operation is not for Line
+    std::string aCurrentFeatureKind;
+    ModuleBase_Operation* anOperation =
+                XGUI_Tools::workshop(myWorkshop)->operationMgr()->currentOperation();
+    if (anOperation) {
+      ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                                           (anOperation);
+      if (aFOperation) {
+        FeaturePtr aFeature = aFOperation->feature();
+        if (aFeature.get())
+          aCurrentFeatureKind = aFeature->getKind();
+      }
+    }
+    if (theMessage->sender()) {
+      ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theMessage->sender());
+      if (aSender) {
+        FeaturePtr aFeatureSender =
+          std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
+        if (aFeatureSender.get() && aFeatureSender->getKind() == aCurrentFeatureKind) {
+          PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
+          PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
+          aReentrantMgr->setReentrantMessage(theMessage);
+        }
+      }
     }
   }
+
 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
   aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
   qDebug(QString("RESULT: current objects count = %1:%2\n")
@@ -199,10 +253,9 @@ void PartSet_OverconstraintListener::redisplayObjects(
   aLoop->flush(EVENT_DISP);
 }
 
-XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
+PartSet_Module* PartSet_OverconstraintListener::module() const
 {
-  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
-  return aConnector->workshop();
+  return dynamic_cast<PartSet_Module*>(myWorkshop->module());
 }
 
 #ifdef _DEBUG