Salome HOME
Recalculate DoF when removing feature without constraints (issue #1456)
[modules/shaper.git] / src / InitializationPlugin / InitializationPlugin_Plugin.cpp
index b2ced2b57b3ef5372fa45dbaf9ddcb9a3b316240..b896d594ba9ddb904b52d57dc81f17b682e71389 100644 (file)
@@ -6,6 +6,7 @@
 #include <ModelAPI_Document.h>
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Result.h>
 
@@ -46,26 +47,29 @@ void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Mess
         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
     Events_Loop::loop()->send(aMsg);
 
-    aFeatures.push_back(createPoint(aDoc));
+    FeaturePtr aOrigin = createPoint(aDoc, "Origin", 0., 0., 0.);
+    aFeatures.push_back(aOrigin);
+    aFeatures.push_back(createAxis(aDoc, aOrigin, 100., 0., 0.));
+    aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 100., 0.));
+    aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 0., 100.));
     aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
-    aFeatures.push_back(createPlane(aDoc, 0., 1., 0.));
+    aFeatures.push_back(createPlane(aDoc, 0., -1., 0.));
     aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
     // for PartSet it is done outside of the transaction, so explicitly flush this creation
     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
 
     // hides the created features, the precondition is that the feature's results have been
     // already built, so the createPlane/Points method calls the execute function for the planes
-    static Events_ID HIDE_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE);
     std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
     for (; aFIter != aFeatures.cend(); aFIter++) {
       FeaturePtr aPlane = *aFIter;
       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
       std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
       for (; aRIter != aResults.cend(); aRIter++) {
-        ModelAPI_EventCreator::get()->sendUpdated(*aRIter, HIDE_DISP);
+        (*aRIter)->setDisplayed(false);
       }
     }
-    Events_Loop::loop()->flush(HIDE_DISP);
+    Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
 
     // the viewer update should be unblocked in order to avoid the features blinking before they are
     // hidden
@@ -92,33 +96,68 @@ FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double t
   aPlane->real("D")->setValue(0.);
 
   if (theX) {
-    aPlane->data()->setName("Y0Z");
+    aPlane->data()->setName("YOZ");
   } else if (theY) {
-    aPlane->data()->setName("X0Z");
+    aPlane->data()->setName("XOZ");
   } else if (theZ) {
-    aPlane->data()->setName("X0Y");
+    aPlane->data()->setName("XOY");
   }
   aPlane->setInHistory(aPlane, false);  // don't show automatically created feature in the features history
 
   // the plane should be executed in order to build the feature result immediatelly
   // the results are to be hidden in the plugin
   aPlane->execute();
+  // this flag is needed here to avoid setting it inside of the next transaction
+  // (may cause crash on redo of the first transaction in OCAF)
+  aPlane->data()->execState(ModelAPI_StateDone);
+  aPlane->firstResult()->data()->execState(ModelAPI_StateDone);
 
   return aPlane;
 }
 
-FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
+FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc, const std::string& theName,
+                                                    double theX, double theY, double theZ)
 {
   std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
-  aPoint->real("x")->setValue(0.);
-  aPoint->real("y")->setValue(0.);
-  aPoint->real("z")->setValue(0.);
-  aPoint->data()->setName("Origin");
+  aPoint->real("x")->setValue(theX);
+  aPoint->real("y")->setValue(theY);
+  aPoint->real("z")->setValue(theZ);
+  aPoint->data()->setName(theName);
   aPoint->setInHistory(aPoint, false);  // don't show automatically created feature in the features history
 
   // the point should be executed in order to build the feature result immediatelly
   // the results are to be hidden in the plugin
   aPoint->execute();
+  aPoint->data()->execState(ModelAPI_StateDone);
+  aPoint->firstResult()->data()->execState(ModelAPI_StateDone);
 
   return aPoint;
 }
+
+FeaturePtr InitializationPlugin_Plugin::createAxis(DocumentPtr theDoc, FeaturePtr theOrigin,
+                                                   double theX, double theY, double theZ)
+{
+  std::shared_ptr<ModelAPI_Feature> aAxis = theDoc->addFeature("Axis");
+  aAxis->string("CreationMethod")->setValue("AxisByPointAndDirection");
+
+  ResultPtr aResult = theOrigin->firstResult();
+  aAxis->selection("FirstPoint")->setValue(aResult, aResult->shape());
+
+  aAxis->real("X_Direction")->setValue(theX);
+  aAxis->real("Y_Direction")->setValue(theY);
+  aAxis->real("Z_Direction")->setValue(theZ);
+
+  if (theX != 0) {
+    aAxis->data()->setName("OX");
+  } else if (theY != 0) {
+    aAxis->data()->setName("OY");
+  } else if (theZ != 0) {
+    aAxis->data()->setName("OZ");
+  }
+  aAxis->setInHistory(aAxis, false);  // don't show automatically created feature in the features history
+  aAxis->execute();
+  aAxis->data()->execState(ModelAPI_StateDone);
+  aAxis->firstResult()->data()->execState(ModelAPI_StateDone);
+
+  return aAxis;
+}