Salome HOME
Merge commit 'f709219506b7cd587e94abc5ebed18d629df92d8'
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ExportPart.cpp
index cc4af14c6ac7e97874ce8c6a1a5d6c27fb28975b..dee52674dbf530876040068161ed0c212becf7a9 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2022  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
@@ -47,7 +47,10 @@ static void collectConstructions(DocumentPtr theDocument, std::list<FeaturePtr>&
 // Returns true if all features can be exported.
 static bool verifyExport(const std::list<FeaturePtr>& theFeatures,
                          std::list<FeaturePtr>& theExternalReferences,
-                         std::list<FeaturePtr>& theExportedParts);
+                         std::list<FeaturePtr>& theExportedParts,
+                         std::list<FeaturePtr>& theReferredParts);
+// Collect names of features as a single string
+static std::wstring namesOfFeatures(const std::list<FeaturePtr>& theFeatures);
 
 
 ExchangePlugin_ExportPart::ExchangePlugin_ExportPart()
@@ -95,38 +98,30 @@ void ExchangePlugin_ExportPart::execute()
   if (aFeaturesToExport.back()->getKind() == ExchangePlugin_ExportPart::ID())
     aFeaturesToExport.pop_back();
 
-  std::list<FeaturePtr> anExternalLinks, aReferredParts;
-  if (!verifyExport(aFeaturesToExport, anExternalLinks, aReferredParts)) {
+  std::list<FeaturePtr> anExternalLinks, anExportedParts, aReferredParts;
+  if (!verifyExport(aFeaturesToExport, anExternalLinks, anExportedParts, aReferredParts)) {
     if (!anExternalLinks.empty()) {
-      // collect names of features as a string
-      std::ostringstream aListOfFeatures;
-      for (std::list<FeaturePtr>::iterator anIt = anExternalLinks.begin();
-           anIt != anExternalLinks.end(); ++anIt) {
-        if (anIt != anExternalLinks.begin())
-          aListOfFeatures << ", ";
-        aListOfFeatures << "'" << (*anIt)->name() << "'";
-      }
+      std::wstring aListOfFeatures = namesOfFeatures(anExternalLinks);
 
       std::string aMessage = "The selected results were created using external references "
                              "outside of this Part from features %1. "
                              "Please, remove these references or select another "
                              "sub-set of results to be able to export.";
-      Events_InfoMessage(getKind(), aMessage).arg(aListOfFeatures.str()).send();
+      Events_InfoMessage(getKind(), aMessage).arg(aListOfFeatures).send();
     }
     if (!aReferredParts.empty()) {
-      // collect names of parts as a string
-      std::ostringstream aListOfParts;
-      for (std::list<FeaturePtr>::iterator anIt = aReferredParts.begin();
-           anIt != aReferredParts.end(); ++anIt) {
-        if (anIt != aReferredParts.begin())
-          aListOfParts << ", ";
-        aListOfParts << "'" << (*anIt)->name() << "'";
-      }
+      std::wstring aListOfParts = namesOfFeatures(aReferredParts);
 
       std::string aMessage = "The selected results were created using references "
-                             "to results of Parts %1. Please, remove these references "
+                             "to the results of Parts: %1. Please, remove these references "
                              "or select another sub-set of results to be able to export.";
-      Events_InfoMessage(getKind(), aMessage).arg(aListOfParts.str()).send();
+      Events_InfoMessage(getKind(), aMessage).arg(aListOfParts).send();
+    }
+    if (!anExportedParts.empty()) {
+      std::wstring aListOfParts = namesOfFeatures(anExportedParts);
+
+      std::string aMessage = "The export of Part's result is forbidden (%1).";
+      Events_InfoMessage(getKind(), aMessage).arg(aListOfParts).send();
     }
     // should not export anything
     aFeaturesToExport.clear();
@@ -142,6 +137,14 @@ void ExchangePlugin_ExportPart::execute()
 
 // ================================     Auxiliary functions     ===================================
 
+static bool isCoordinate(FeaturePtr theFeature)
+{
+  return !theFeature->isInHistory() &&
+          (theFeature->getKind() == ConstructionPlugin_Point::ID() ||
+           theFeature->getKind() == ConstructionPlugin_Axis::ID() ||
+           theFeature->getKind() == ConstructionPlugin_Plane::ID());
+}
+
 static void allReferencedFeatures(const std::set<FeaturePtr>& theFeatures,
                                   std::set<FeaturePtr>& theReferencedFeatures)
 {
@@ -158,7 +161,8 @@ static void allReferencedFeatures(const std::set<FeaturePtr>& theFeatures,
       for (std::list<ObjectPtr>::iterator anObjIt = aRIt->second.begin();
            anObjIt != aRIt->second.end(); ++anObjIt) {
         FeaturePtr aFeature = ModelAPI_Feature::feature(*anObjIt);
-        if (aFeature && theReferencedFeatures.find(aFeature) == theReferencedFeatures.end())
+        if (aFeature && !isCoordinate(aFeature) &&
+            theReferencedFeatures.find(aFeature) == theReferencedFeatures.end())
           aReferences.insert(aFeature);
       }
     }
@@ -168,20 +172,21 @@ static void allReferencedFeatures(const std::set<FeaturePtr>& theFeatures,
     allReferencedFeatures(aReferences, theReferencedFeatures);
 }
 
-static bool isCoordinate(FeaturePtr theFeature)
-{
-  return !theFeature->isInHistory() &&
-          (theFeature->getKind() == ConstructionPlugin_Point::ID() ||
-           theFeature->getKind() == ConstructionPlugin_Axis::ID() ||
-           theFeature->getKind() == ConstructionPlugin_Plane::ID());
-}
-
 void collectFeatures(DocumentPtr theDocument,
                      AttributeSelectionListPtr theSelected,
                      std::list<FeaturePtr>& theExport)
 {
   theExport = theDocument->allFeatures();
 
+  // remove all features after the current one
+  FeaturePtr aCurrentFeature = theDocument->currentFeature(false);
+  std::list<FeaturePtr>::iterator anIt = theExport.begin();
+  for (; anIt != theExport.end(); ++anIt)
+    if (*anIt == aCurrentFeature) {
+      theExport.erase(++anIt, theExport.end());
+      break;
+    }
+
   if (!theSelected || theSelected->size() == 0) {
     // nothing is selected, return all features of the document
     return;
@@ -191,15 +196,15 @@ void collectFeatures(DocumentPtr theDocument,
   std::set<FeaturePtr> aFeaturesToExport;
   for (int anIndex = 0, aSize = theSelected->size(); anIndex < aSize; ++anIndex) {
     AttributeSelectionPtr aCurrent = theSelected->value(anIndex);
-    FeaturePtr aCurrentFeature = ModelAPI_Feature::feature(aCurrent->context());
-    if (aCurrentFeature)
-      aFeaturesToExport.insert(aCurrentFeature);
+    FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurrent->context());
+    if (aCurFeature)
+      aFeaturesToExport.insert(aCurFeature);
   }
   // recursively collect all features used for the selected results
   allReferencedFeatures(aFeaturesToExport, aFeaturesToExport);
 
   // remove the features which are not affect the selected results
-  std::list<FeaturePtr>::iterator anIt = theExport.begin();
+  anIt = theExport.begin();
   while (anIt != theExport.end()) {
     if (aFeaturesToExport.find(*anIt) == aFeaturesToExport.end()) {
       std::list<FeaturePtr>::iterator aRemoveIt = anIt++;
@@ -234,7 +239,8 @@ void collectConstructions(DocumentPtr theDocument, std::list<FeaturePtr>& theExp
 
 bool verifyExport(const std::list<FeaturePtr>& theFeatures,
                   std::list<FeaturePtr>& theExternalReferences,
-                  std::list<FeaturePtr>& theExportedParts)
+                  std::list<FeaturePtr>& theExportedParts,
+                  std::list<FeaturePtr>& theReferredParts)
 {
   for (std::list<FeaturePtr>::const_iterator anIt = theFeatures.begin();
        anIt != theFeatures.end(); ++anIt) {
@@ -258,11 +264,23 @@ bool verifyExport(const std::list<FeaturePtr>& theFeatures,
             theExternalReferences.push_back(*anIt);
           // feature refers to result of a part
           if (aFeature->getKind() == PartSetPlugin_Part::ID())
-            theExportedParts.push_back(*anIt);
+            theReferredParts.push_back(*anIt);
         }
       }
     }
   }
 
-  return theExternalReferences.empty() && theExportedParts.empty();
+  return theExternalReferences.empty() && theExportedParts.empty() && theReferredParts.empty();
+}
+
+std::wstring namesOfFeatures(const std::list<FeaturePtr>& theFeatures)
+{
+  std::wostringstream aListOfFeatures;
+  for (std::list<FeaturePtr>::const_iterator anIt = theFeatures.begin();
+       anIt != theFeatures.end(); ++anIt) {
+    if (anIt != theFeatures.begin())
+      aListOfFeatures << ", ";
+    aListOfFeatures << "'" << (*anIt)->name() << "'";
+  }
+  return aListOfFeatures.str();
 }