Salome HOME
Issue #231 Deletion of a feature
authornds <natalia.donis@opencascade.com>
Thu, 15 Jan 2015 08:28:39 +0000 (11:28 +0300)
committersbh <sergey.belash@opencascade.com>
Fri, 30 Jan 2015 08:22:26 +0000 (11:22 +0300)
Message box correction to show it only once with the list of refefenced features.

src/Model/Model_Document.cpp
src/Model/Model_Document.h
src/ModelAPI/ModelAPI_Document.h
src/PartSetPlugin/PartSetPlugin_Remove.cpp
src/SketchPlugin/SketchPlugin_Sketch.cpp
src/XGUI/XGUI_Workshop.cpp

index ec10e79632777634a6ca203b35b9d2f7edced0b1..67339fdf3bf8361575550d1e8768f54ad2c18657 100644 (file)
@@ -574,22 +574,30 @@ static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced, co
   return aResult;
 }
 
-void Model_Document::removeFeature(FeaturePtr theFeature, const bool theCheck)
+void Model_Document::refsToFeature(FeaturePtr theFeature,
+                                   std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs,
+                                   const bool isSendError)
 {
-  if (theCheck) {
-    // check the feature: it must have no depended objects on it
-    std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
-    for(; aResIter != theFeature->results().cend(); aResIter++) {
-      std::shared_ptr<Model_Data> aData = 
-        std::dynamic_pointer_cast<Model_Data>((*aResIter)->data());
-      if (aData && !aData->refsToMe().empty()) {
-        Events_Error::send(
-          "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
-        return;
-      }
+  // check the feature: it must have no depended objects on it
+  std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
+  for(; aResIter != theFeature->results().cend(); aResIter++) {
+    ResultPtr aResult = (*aResIter);
+    std::shared_ptr<Model_Data> aData = 
+      std::dynamic_pointer_cast<Model_Data>(aResult->data());
+    if (aData && !aData->refsToMe().empty()) {
+      FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
+      if (aFeature.get() != NULL)
+        theRefs.insert(aFeature);
     }
   }
+  if (!theRefs.empty() && isSendError) {
+    Events_Error::send(
+      "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
+  }
+}
 
+void Model_Document::removeFeature(FeaturePtr theFeature/*, const bool theCheck*/)
+{
   std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
   if (aData) {
     TDF_Label aFeatureLabel = aData->label().Father();
index a766b981e7bfeaf81b34118840c17d80a4792160..cb99181f9ddb8a9ce6b1d2233d0f6c508701edb6 100644 (file)
@@ -78,10 +78,17 @@ class Model_Document : public ModelAPI_Document
   //! \param theID creates feature and puts it in the document
   MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID);
 
+  //! Return a list of features, which refers to the feature
+  //! \param theFeature a feature
+  //! \param theRefs a list of reference features
+  //! \param isSendError a flag whether the error message should be send
+  MODEL_EXPORT virtual void refsToFeature(FeaturePtr theFeature,
+                                          std::set<FeaturePtr>& theRefs,
+                                          const bool isSendError = true);
+
   //! Removes the feature from the document (with result)
-  //! \param theFeature the feature to be removed
-  //! \param theCheck if it is false, do not check the references
-  MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature, const bool theCheck = true);
+  //! \param theFeature a removed feature
+  MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
 
   //! Returns the existing feature by the label
   //! \param theLabel base label of the feature
index d6a7ce0bf07baa7561d851a3f0370338b51d76af..bf1fa3832d33882d18817394b605a244e24a81cc 100644 (file)
@@ -12,6 +12,7 @@
 #include <memory>
 #include <vector>
 #include <list>
+#include <set>
 
 class ModelAPI_Feature;
 class ModelAPI_Object;
@@ -44,9 +45,17 @@ public:
   //! \param theID creates feature and puts it in the document (if it is not action)
   virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID) = 0;
 
+  //! Return a list of features, which refers to the feature
+  //! \param theFeature a feature
+  //! \param theRefs a list of features
+  //! \param isSendError a flag whether the error message should be send
+  virtual void refsToFeature(std::shared_ptr<ModelAPI_Feature> theFeature,
+                             std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs,
+                             const bool isSendError = true) = 0;
+
   //! Removes the feature from the document
-  virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature,
-                             const bool theCheck = true) = 0;
+  //! \param theFeature a feature to be removed
+  virtual void removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature) = 0;
 
   ///! Adds a new sub-document by the identifier, or returns existing one if it is already exist
   virtual std::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID) = 0;
index d77712bb34a01cd5c025da5b44165a95fc525dc8..0c47368fc1eeb4ea8c56f0a99c44e78b33c3eb1a 100644 (file)
@@ -11,6 +11,7 @@
 #include <ModelAPI_AttributeDocRef.h>
 #include <ModelAPI_ResultPart.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Feature.h>
 
 void PartSetPlugin_Remove::execute()
 {
@@ -28,7 +29,10 @@ void PartSetPlugin_Remove::execute()
       if (aFeature) {
         // do remove
         aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value()->close();
-        aRoot->removeFeature(aFeature);
+        std::set<std::shared_ptr<ModelAPI_Feature> > aRefFeatures;
+        aRoot->refsToFeature(aFeature, aRefFeatures);
+        if (aRefFeatures.empty())
+          aRoot->removeFeature(aFeature);
       }
     }
   }
index 8e9644e459fac19d312d3fd04a9281abf99902df..024bda4b473b92fc0f3298be8a6d80b914894535 100644 (file)
@@ -266,8 +266,8 @@ void SketchPlugin_Sketch::erase()
   for (; anIt != aFeatures.end(); anIt++) {
     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
     if (aFeature) {
-       // subs are referenced from sketch, but must be removed for sure, so not checkings
-      document()->removeFeature(aFeature, false);
+      // subs are referenced from sketch, but must be removed for sure, so not checkings
+      document()->removeFeature(aFeature);
     }
   }
   ModelAPI_CompositeFeature::erase();
index 4022d7fc70d4e4cb0750393b73cddf4e75dbbad9..4eae595781c199d3aa7d6eb02c24abd02ad7c6f6 100644 (file)
@@ -1259,31 +1259,62 @@ void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
 void XGUI_Workshop::deleteObjects(const QObjectPtrList& theList)
 {
   QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
-  QMessageBox::StandardButton aRes = QMessageBox::warning(
-      aDesktop, tr("Delete features"), tr("Seleted features will be deleted. Continue?"),
-      QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
-  // ToDo: definbe deleting method
-  if (aRes == QMessageBox::Yes) {
-    SessionPtr aMgr = ModelAPI_Session::get();
-    aMgr->startOperation();
-    foreach (ObjectPtr aObj, theList)
-    {
-      ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
-      if (aPart) {
-        DocumentPtr aDoc = aPart->document();
-        if (aDoc == aMgr->activeDocument()) {
-          aDoc->close();
-        }
-        //aMgr->moduleDocument()->removeFeature(aPart->owner());
-      } else {
-        FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
-        if (aFeature)
-          aObj->document()->removeFeature(aFeature);
+
+  std::set<FeaturePtr> aRefFeatures;
+  foreach (ObjectPtr aObj, theList)
+  {
+    ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
+    if (aPart) {
+      // TODO: check for what there is this condition. It is placed here historicaly because
+      // ther is this condition during remove features.
+    } else {
+      FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
+      if (aFeature) {
+        aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
       }
     }
-    myDisplayer->updateViewer();
-    aMgr->finishOperation();
   }
+
+  if (!aRefFeatures.empty()) {
+    QStringList aRefNames;
+    std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
+                                         aLast = aRefFeatures.end();
+    for (; anIt != aLast; anIt++) {
+      FeaturePtr aFeature = (*anIt);
+      std::string aFName = aFeature->data()->name().c_str();
+      std::string aName = (*anIt)->name().c_str();
+      aRefNames.append((*anIt)->name().c_str());
+    }
+    QString aNames = aRefNames.join(", ");
+
+    QMessageBox::StandardButton aRes = QMessageBox::warning(
+        aDesktop, tr("Delete features"),
+        QString(tr("Selected features are used in the following features: %1.\
+These features will be deleted also. Would you like to continue?")).arg(aNames),
+        QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
+    if (aRes != QMessageBox::Yes)
+      return;
+  }
+
+  SessionPtr aMgr = ModelAPI_Session::get();
+  aMgr->startOperation();
+  foreach (ObjectPtr aObj, theList)
+  {
+    DocumentPtr aDoc = aObj->document();
+    ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
+    if (aPart) {
+      if (aDoc == aMgr->activeDocument()) {
+        aDoc->close();
+      }
+    } else {
+      FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
+      if (aFeature) {
+        aDoc->removeFeature(aFeature);
+      }
+    }
+  }
+  myDisplayer->updateViewer();
+  aMgr->finishOperation();
 }
 
 //**************************************************************