Salome HOME
Issue #3232: Delete automatic constraints if they cause conflicts
[modules/shaper.git] / src / PartSet / PartSet_OverconstraintListener.cpp
index 1b2b688fd1a55986faff84f982cbd827ab71d43b..a0eff2c34b011e8f26821d22bb0df2b9553379b9 100644 (file)
@@ -25,7 +25,6 @@
 #include <PartSet_SketcherMgr.h>
 #include <PartSet_SketcherReentrantMgr.h>
 
-#include "XGUI_CustomPrs.h"
 #include "XGUI_Displayer.h"
 #include "XGUI_ModuleConnector.h"
 #include "XGUI_OperationMgr.h"
@@ -36,6 +35,8 @@
 #include "SketchPlugin_SketchEntity.h"
 #include "SketchPlugin_MacroArcReentrantMessage.h"
 #include "SketchPlugin_Sketch.h"
+#include "SketchPlugin_ConstraintHorizontal.h"
+#include "SketchPlugin_ConstraintVertical.h"
 
 #include "Events_Loop.h"
 
@@ -45,6 +46,7 @@
 #include <ModuleBase_Tools.h>
 
 #include <QString>
+#include <QTimer>
 
 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
 
@@ -74,8 +76,13 @@ void PartSet_OverconstraintListener::setActive(const bool& theActive)
     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");
+      QString aDOFMessage(aSketch->string(SketchPlugin_Sketch::SOLVER_DOF())->value().c_str());
+      if (aDOFMessage.contains('=')) {
+        // to support old data
+        aDOFMessage =
+          aDOFMessage.right(aDOFMessage.length() - aDOFMessage.lastIndexOf('=')).trimmed();
+      }
+      myIsFullyConstrained = (aDOFMessage == "0");
     }
   }
 }
@@ -249,6 +256,21 @@ bool PartSet_OverconstraintListener::appendConflictingObjects(
   if (isUpdated)
     redisplayObjects(aModifiedObjects);
 
+  if (myConflictingObjects.size() == 1) {
+    // If the conflicting object is an automatic constraint caused the conflict
+    // then it has to be deleted
+    ObjectPtr aObj = *theConflictingObjects.cbegin();
+    FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
+    if (aFeature) {
+      std::string aType = aFeature->getKind();
+      if ((aType == SketchPlugin_ConstraintHorizontal::ID()) ||
+        (aType == SketchPlugin_ConstraintVertical::ID())) {
+        PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
+        QTimer::singleShot(5, aModule, SLOT(onConflictingConstraints()));
+      }
+    }
+  }
+
   return isUpdated;
 }
 
@@ -263,7 +285,6 @@ bool PartSet_OverconstraintListener::repairConflictingObjects(
     ObjectPtr anObject = *anIt;
     if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
       myConflictingObjects.erase(anObject);
-
       aModifiedObjects.insert(anObject);
     }
   }
@@ -279,7 +300,7 @@ void PartSet_OverconstraintListener::redisplayObjects(
 {
   static Events_Loop* aLoop = Events_Loop::loop();
 
-  static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
+  static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_VISUAL_ATTRIBUTES);
   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
 
   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();