Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Recover.cpp
index e521c3a296f4fb00d9a6fc1817c385df66313446..5dd6ad2fbd7a1fa053e285a9259490540d57042c 100644 (file)
@@ -1,8 +1,21 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
-
-// File:        FeaturesPlugin_Recover.cpp
-// Created:     29 Jul 2016
-// Author:      Natalia ERMOLAEVA
+// Copyright (C) 2014-2023  CEA/DEN, EDF R&D
+//
+// 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 "FeaturesPlugin_Recover.h"
 
 #include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_AttributeBoolean.h>
+#include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Validator.h>
 #include <ModelAPI_Result.h>
+#include <ModelAPI_ResultBody.h>
 #include <ModelAPI_Tools.h>
+#include <GeomAlgoAPI_Copy.h>
+#include <GeomAlgoAPI_Tools.h>
 
 FeaturesPlugin_Recover::FeaturesPlugin_Recover()
+  : myClearListOnTypeChange(true)
 {
 }
 
@@ -24,105 +42,59 @@ void FeaturesPlugin_Recover::initAttributes()
 {
   data()->addAttribute(BASE_FEATURE(), ModelAPI_AttributeReference::typeId());
   data()->addAttribute(RECOVERED_ENTITIES(), ModelAPI_AttributeRefList::typeId());
-  data()->addAttribute(PERSISTENT(), ModelAPI_AttributeBoolean::typeId());
 
-  myPersistent = boolean(PERSISTENT())->value();
-  synchronizeRegistered();
+  data()->addAttribute(METHOD(), ModelAPI_AttributeString::typeId());
+  if (!string(METHOD())->isInitialized()) {
+    myClearListOnTypeChange = false;
+    data()->blockSendAttributeUpdated(true, false);
+    string(METHOD())->setValue(METHOD_DEFAULT());
+    data()->blockSendAttributeUpdated(false, false);
+    myClearListOnTypeChange = true;
+  }
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), METHOD());
 }
 
 void FeaturesPlugin_Recover::execute()
 {
-  synchronizeRegistered();
-}
-
-void FeaturesPlugin_Recover::attributeChanged(const std::string& theID)
-{
-  synchronizeRegistered();
-}
-
-void FeaturesPlugin_Recover::synchronizeRegistered()
-{
-  FeaturePtr aBase = baseFeature();
-  bool aNewPersistent = boolean(PERSISTENT())->value();
-  if (aNewPersistent != myPersistent || myCurrentBase != aBase)
-    clearRegistered();
+  std::string anError;
+  int aResultIndex = 0;
+  AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
+  for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
+    ObjectPtr anObj = aRecovered->object(anIndex);
+    if (!anObj.get())
+      continue;
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
+    if (!aResult.get())
+      continue;
+    GeomShapePtr aShape = aResult->shape();
+    if (!aShape.get())
+      continue;
 
-  std::set<ObjectPtr> aRecoveredInList;
-  // add unconcealed which are not in the myRegistered map
-  if (isStable() && !isDisabled()) { // if unstable, clear any unconcealment effect
-    AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
-    for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
-      ObjectPtr anObj = aRecovered->object(anIndex);
-      aRecoveredInList.insert(anObj);
-      if (myRegistered.find(anObj) == myRegistered.end()) {
-        // not found, so register a new
-        ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
-        if (aRes.get()) { // this may be on first update after "open"
-          ModelAPI_Session::get()->validators()->registerUnconcealment(
-            aRes, aNewPersistent ? FeaturePtr() : aBase);
-          myRegistered.insert(anObj);
-        }
-      }
+    // Copy shape.
+    std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(aShape));
+    // Check that algo is done.
+    if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCopyAlgo, getKind(), anError)) {
+      setError(anError);
+      return;
     }
-  }
-  // remove unconcealed which are not in the stored list, but in the map
-  std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
-  while(aMyReg != myRegistered.end()) {
-    if (aRecoveredInList.find(*aMyReg) == aRecoveredInList.end()) {
-      ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
-      ModelAPI_Session::get()->validators()->disableUnconcealment(
-        aRes, aNewPersistent ? FeaturePtr() : myCurrentBase);
-      myRegistered.erase(aMyReg);
-      aMyReg = myRegistered.begin(); // restart iteration because after erase iterator may be bad
-    } else {
-      aMyReg++;
-    }
-  }
-  myCurrentBase = aBase;
-  myPersistent = aNewPersistent;
-}
 
-void FeaturesPlugin_Recover::erase()
-{
-  // clears myRegistered before all information is destroyed
-  clearRegistered();
-  ModelAPI_Feature::erase();
-}
+    // Store result.
+    ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
+    aResultBody->store(aCopyAlgo->shape());//, aCopyAlgo.shape());
 
-bool FeaturesPlugin_Recover::setStable(const bool theFlag)
-{
-  bool aRes = ModelAPI_Feature::setStable(theFlag);
-  synchronizeRegistered();
-  return aRes;
-}
+    aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::VERTEX);
+    aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::EDGE);
+    aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::FACE);
 
-bool FeaturesPlugin_Recover::setDisabled(const bool theFlag)
-{
-  bool aRes = ModelAPI_Feature::setDisabled(theFlag);
-  synchronizeRegistered();
-  return aRes;
-}
+    setResult(aResultBody, aResultIndex);
+    ++aResultIndex;
+  }
 
-FeaturePtr FeaturesPlugin_Recover::baseFeature()
-{
-  // for the current moment it can be result of feature of feature: GUI is not debugged
-  ObjectPtr aBaseObj = reference(BASE_FEATURE())->value();
-  FeaturePtr aResult;
-  if (aBaseObj.get() == NULL)
-    return aResult;
-  aResult = std::dynamic_pointer_cast<ModelAPI_Feature>(aBaseObj);
-  if (aResult.get() == NULL)
-    aResult = aBaseObj->document()->feature(std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObj));
-  return aResult;
+  removeResults(aResultIndex);
 }
 
-void FeaturesPlugin_Recover::clearRegistered()
+void FeaturesPlugin_Recover::attributeChanged(const std::string& theID)
 {
-  std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
-  for(; aMyReg != myRegistered.end(); aMyReg++) {
-    ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
-    ModelAPI_Session::get()->validators()->disableUnconcealment(
-      aRes, myPersistent ? FeaturePtr() : myCurrentBase);
-  }
-  myRegistered.clear();
+  if (theID == METHOD() && myClearListOnTypeChange)
+    reflist(RECOVERED_ENTITIES())->clear();
 }