Salome HOME
Fix for the issue #19726 : Placement failed because of wrong name in face selection.
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ImportPart.cpp
index 4a40de68c25028fc739ef0f9bcb9545040890b40..e63d5ee9c0109b8265e43e291f243e405b21da8e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2020  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
 
 #include <ExchangePlugin_ImportPart.h>
 
+#include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeStringArray.h>
 #include <ModelAPI_ResultPart.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Tools.h>
 
 #include <PartSetPlugin_Part.h>
 
 #include <map>
 #include <sstream>
 
+static const std::string THE_NEW_PART_STR("New Part");
+static const std::string THE_PART_SET_STR("PartSet");
+
 // Update names of imported features/results concurent with existing objects.
 static void correntNonUniqueNames(DocumentPtr theDocument, std::list<FeaturePtr>& theImported);
+// Find the document according to its name or create the new one.
+static DocumentPtr findDocument(DocumentPtr thePartSetDoc, const std::string& thePartName);
 
 ExchangePlugin_ImportPart::ExchangePlugin_ImportPart()
 {
@@ -38,8 +46,11 @@ ExchangePlugin_ImportPart::ExchangePlugin_ImportPart()
 void ExchangePlugin_ImportPart::initAttributes()
 {
   data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
+  data()->addAttribute(TARGET_PART_ID(), ModelAPI_AttributeInteger::typeId());
+  data()->addAttribute(TARGET_PARTS_LIST_ID(), ModelAPI_AttributeStringArray::typeId());
 }
 
+
 void ExchangePlugin_ImportPart::execute()
 {
   AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID());
@@ -49,35 +60,112 @@ void ExchangePlugin_ImportPart::execute()
     return;
   }
 
-  // load the file into the active document
+  // get the document where to import
+  AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID());
+  AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID());
   SessionPtr aSession = ModelAPI_Session::get();
-  DocumentPtr aDoc = document();
-  bool isPartSet = aDoc == aSession->moduleDocument();
+  DocumentPtr aDoc =
+      findDocument(aSession->moduleDocument(), aPartsAttr->value(aTargetAttr->value()));
+
+  // load the file into the document
   std::list<FeaturePtr> anImportedFeatures;
-  bool isOk = aDoc->import(aFilename.c_str(), anImportedFeatures, isPartSet);
-  if (!isOk && isPartSet) {
-    // there are features not appropriate for PartSet,
-    // create new part and load there
-    FeaturePtr aPartFeature = aDoc->addFeature(PartSetPlugin_Part::ID());
-    ResultPartPtr aPartResult;
-    if (aPartFeature) {
-      aPartFeature->execute();
-      aPartResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->lastResult());
-    }
-    if (aPartResult) {
-      aDoc = aPartResult->partDoc();
-      isOk = aDoc->import(aFilename.c_str(), anImportedFeatures);
-    }
-  }
-  if (isOk)
+  if (aDoc && aDoc->importPart(aFilename.c_str(), anImportedFeatures))
     correntNonUniqueNames(aDoc, anImportedFeatures);
   else
     setError("Cannot import the document.");
 }
 
+void ExchangePlugin_ImportPart::attributeChanged(const std::string& theID)
+{
+  if (theID == FILE_PATH_ID()) {
+    AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID());
+    if (aFilePathAttr->value().empty())
+      return;
+
+    AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID());
+    AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID());
+
+    // update the list of target parts
+    SessionPtr aSession = ModelAPI_Session::get();
+    DocumentPtr aDoc = document();
+    bool isPartSet = aDoc == aSession->moduleDocument();
+    if (isPartSet) {
+      std::list<std::string> anAcceptedValues;
+      anAcceptedValues.push_back(THE_NEW_PART_STR);
+
+      std::list<FeaturePtr> anImportedFeatures;
+      if (aDoc->importPart(aFilePathAttr->value().c_str(), anImportedFeatures, isPartSet))
+        anAcceptedValues.push_back(THE_PART_SET_STR);
+
+      // append names of all parts
+      std::list<FeaturePtr> aSubFeatures = aDoc->allFeatures();
+      for (std::list<FeaturePtr>::iterator aFIt = aSubFeatures.begin();
+           aFIt != aSubFeatures.end(); ++aFIt) {
+        if ((*aFIt)->getKind() == PartSetPlugin_Part::ID())
+          anAcceptedValues.push_back((*aFIt)->name());
+      }
+
+      if ((size_t)aPartsAttr->size() != anAcceptedValues.size())
+        aTargetAttr->setValue(0);
+
+      aPartsAttr->setSize((int)anAcceptedValues.size());
+      std::list<std::string>::iterator anIt = anAcceptedValues.begin();
+      for (int anInd = 0; anIt != anAcceptedValues.end(); ++anIt, ++anInd)
+        aPartsAttr->setValue(anInd, *anIt);
+    }
+    else {
+      // keep only the name of the current part
+      if (aPartsAttr->size() == 0) {
+        FeaturePtr aPartFeature = ModelAPI_Tools::findPartFeature(aSession->moduleDocument(), aDoc);
+
+        aPartsAttr->setSize(1);
+        aPartsAttr->setValue(0, aPartFeature->name());
+        aTargetAttr->setValue(0);
+      }
+    }
+  }
+}
+
 
 // ================================     Auxiliary functions     ===================================
 
+DocumentPtr findDocument(DocumentPtr thePartSetDoc, const std::string& thePartName)
+{
+  DocumentPtr aDoc;
+  if (thePartName == THE_PART_SET_STR)
+    aDoc = thePartSetDoc;
+  else {
+    FeaturePtr aPartFeature;
+    if (thePartName == THE_NEW_PART_STR) {
+      // create new part
+      aPartFeature = thePartSetDoc->addFeature(PartSetPlugin_Part::ID());
+      if (aPartFeature)
+        aPartFeature->execute();
+    }
+    else {
+      // find existing part by its name
+      std::list<FeaturePtr> aSubFeatures = thePartSetDoc->allFeatures();
+      for (std::list<FeaturePtr>::iterator aFIt = aSubFeatures.begin();
+           aFIt != aSubFeatures.end(); ++aFIt) {
+        if ((*aFIt)->getKind() == PartSetPlugin_Part::ID() && (*aFIt)->name() == thePartName) {
+          aPartFeature = *aFIt;
+          break;
+        }
+      }
+    }
+
+    if (aPartFeature) {
+      ResultPartPtr aPartResult =
+          std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->lastResult());
+      if (aPartResult)
+        aDoc = aPartResult->partDoc();
+    }
+  }
+  return aDoc;
+}
+
+typedef std::map<std::string, std::map<std::string, std::set<int> > > ObjectNameMap;
+
 bool splitName(std::string& theName, int& theIndex)
 {
   size_t aLastUndercore = theName.find_last_of('_');
@@ -93,12 +181,13 @@ bool splitName(std::string& theName, int& theIndex)
   return isOk;
 }
 
-void addIndexedName(const std::string& theName, std::map<std::string, std::set<int> >& theIndexedNames)
+void addIndexedName(const ObjectPtr& theObject, ObjectNameMap& theIndexedNames)
 {
-  std::string aName = theName;
+  std::string aName = theObject->data()->name();
+  std::string aGroup = theObject->groupName();
   int anIndex = 0;
   bool isIndexed = splitName(aName, anIndex);
-  std::set<int>& anIndices = theIndexedNames[aName];
+  std::set<int>& anIndices = theIndexedNames[aGroup][aName];
   if (isIndexed)
     anIndices.insert(anIndex);
 }
@@ -109,7 +198,7 @@ void addIndexedName(const std::string& theName, std::map<std::string, std::set<i
 // 'Point_1', 'Point_2' => {'Point', [1, 2]}.
 // Thus, the new point should have index 3 and therefore the name 'Point_3'.
 static void collectOldNames(DocumentPtr theDocument, std::list<FeaturePtr>& theAvoided,
-                            std::map<std::string, std::set<int> >& theIndexedNames)
+                            ObjectNameMap& theIndexedNames)
 {
   std::list<FeaturePtr> anAllFeatures = theDocument->allFeatures();
   std::list<FeaturePtr>::iterator aFIt = anAllFeatures.begin();
@@ -122,26 +211,36 @@ static void collectOldNames(DocumentPtr theDocument, std::list<FeaturePtr>& theA
     }
 
     // store name of feature
-    addIndexedName((*aFIt)->data()->name(), theIndexedNames);
+    addIndexedName(*aFIt, theIndexedNames);
     // store names of results
     const std::list<ResultPtr>& aResults = (*aFIt)->results();
     for (std::list<ResultPtr>::const_iterator aRIt = aResults.begin();
          aRIt != aResults.end(); ++aRIt)
-      addIndexedName((*aRIt)->data()->name(), theIndexedNames);
+      addIndexedName(*aRIt, theIndexedNames);
   }
 }
 
-static std::string uniqueName(const std::string& theName,
-                              std::map<std::string, std::set<int> >& theExistingNames)
+static std::string uniqueName(const ObjectPtr& theObject, ObjectNameMap& theExistingNames)
 {
-  std::string aName = theName;
+  std::string aName = theObject->data()->name();
+  std::string aGroup = theObject->groupName();
   int anIndex = 1;
   splitName(aName, anIndex);
 
-  std::map<std::string, std::set<int> >::iterator aFound = theExistingNames.find(aName);
-  bool isUnique = false;
-  if (aFound == theExistingNames.end())
-    isUnique = true;
+  ObjectNameMap::iterator aFoundGroup = theExistingNames.find(aGroup);
+  bool isUnique = aFoundGroup == theExistingNames.end();
+
+  std::map<std::string, std::set<int> >::iterator aFound;
+  if (!isUnique) {
+    aFound = aFoundGroup->second.find(aName);
+    isUnique = aFound == aFoundGroup->second.end();
+  }
+
+  if (isUnique) {
+    // name is unique
+    aName = theObject->data()->name();
+    addIndexedName(theObject, theExistingNames);
+  }
   else {
     // search the appropriate index
     std::set<int>::iterator aFoundIndex = aFound->second.find(anIndex);
@@ -156,29 +255,24 @@ static std::string uniqueName(const std::string& theName,
     aFound->second.insert(anIndex);
   }
 
-  if (isUnique) {
-    // name is unique
-    aName = theName;
-    addIndexedName(theName, theExistingNames);
-  }
   return aName;
 }
 
 void correntNonUniqueNames(DocumentPtr theDocument, std::list<FeaturePtr>& theImported)
 {
-  std::map<std::string, std::set<int> > aNames;
+  ObjectNameMap aNames;
   collectOldNames(theDocument, theImported, aNames);
 
   for (std::list<FeaturePtr>::iterator anIt = theImported.begin();
        anIt != theImported.end(); ++anIt) {
     // update name of feature
-    std::string aNewName = uniqueName((*anIt)->data()->name(), aNames);
+    std::string aNewName = uniqueName(*anIt, aNames);
     (*anIt)->data()->setName(aNewName);
     // update names of results
     const std::list<ResultPtr>& aResults = (*anIt)->results();
     for (std::list<ResultPtr>::const_iterator aRIt = aResults.begin();
          aRIt != aResults.end(); ++aRIt) {
-      aNewName = uniqueName((*aRIt)->data()->name(), aNames);
+      aNewName = uniqueName(*aRIt, aNames);
       (*aRIt)->data()->setName(aNewName);
     }
   }