Salome HOME
Porting Salome to OCCT 7.7.0
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ImportPart.cpp
index 14f2c19511c78055003704b03e867956885461a7..c7f198bc948c0c40c5e3467a354661563baf1ea4 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
 
 #include <ExchangePlugin_ImportPart.h>
 
+#include <Locale_Convert.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>
+#include <string>
+
+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 +49,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,45 +63,121 @@ 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(Locale::Convert::toString((*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, Locale::Convert::toString(aPartFeature->name()));
+        aTargetAttr->setValue(0);
+      }
+    }
+  }
+}
+
 
 // ================================     Auxiliary functions     ===================================
 
-typedef std::map<std::string, std::map<std::string, std::set<int> > > ObjectNameMap;
+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() &&
+          Locale::Convert::toString((*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::wstring, std::set<int> > > ObjectNameMap;
 
-bool splitName(std::string& theName, int& theIndex)
+bool splitName(std::wstring& theName, int& theIndex)
 {
   size_t aLastUndercore = theName.find_last_of('_');
-  bool isOk = aLastUndercore != std::string::npos;
+  bool isOk = aLastUndercore != std::wstring::npos;
   if (isOk) {
-    char* isNumber;
-    std::string anIndexStr = theName.substr(aLastUndercore + 1);
-    theIndex = std::strtol(anIndexStr.c_str(), &isNumber, 10);
+    size_t isNumber;
+    std::wstring anIndexStr = theName.substr(aLastUndercore + 1);
+    theIndex = std::stol(anIndexStr, &isNumber);
     isOk = isNumber != 0;
     if (isOk)
       theName.erase(aLastUndercore);
@@ -97,7 +187,7 @@ bool splitName(std::string& theName, int& theIndex)
 
 void addIndexedName(const ObjectPtr& theObject, ObjectNameMap& theIndexedNames)
 {
-  std::string aName = theObject->data()->name();
+  std::wstring aName =theObject->data()->name();
   std::string aGroup = theObject->groupName();
   int anIndex = 0;
   bool isIndexed = splitName(aName, anIndex);
@@ -134,9 +224,9 @@ static void collectOldNames(DocumentPtr theDocument, std::list<FeaturePtr>& theA
   }
 }
 
-static std::string uniqueName(const ObjectPtr& theObject, ObjectNameMap& theExistingNames)
+static std::wstring uniqueName(const ObjectPtr& theObject, ObjectNameMap& theExistingNames)
 {
-  std::string aName = theObject->data()->name();
+  std::wstring aName = theObject->data()->name();
   std::string aGroup = theObject->groupName();
   int anIndex = 1;
   splitName(aName, anIndex);
@@ -144,7 +234,7 @@ static std::string uniqueName(const ObjectPtr& theObject, ObjectNameMap& theExis
   ObjectNameMap::iterator aFoundGroup = theExistingNames.find(aGroup);
   bool isUnique = aFoundGroup == theExistingNames.end();
 
-  std::map<std::string, std::set<int> >::iterator aFound;
+  std::map<std::wstring, std::set<int> >::iterator aFound;
   if (!isUnique) {
     aFound = aFoundGroup->second.find(aName);
     isUnique = aFound == aFoundGroup->second.end();
@@ -162,7 +252,7 @@ static std::string uniqueName(const ObjectPtr& theObject, ObjectNameMap& theExis
       if (anIndex != *aFoundIndex)
         break;
     // compose the new name
-    std::ostringstream aNewName;
+    std::wostringstream aNewName;
     aNewName << aName << "_" << anIndex;
     aName = aNewName.str();
     // add new index
@@ -180,7 +270,7 @@ void correntNonUniqueNames(DocumentPtr theDocument, std::list<FeaturePtr>& theIm
   for (std::list<FeaturePtr>::iterator anIt = theImported.begin();
        anIt != theImported.end(); ++anIt) {
     // update name of feature
-    std::string aNewName = uniqueName(*anIt, aNames);
+    std::wstring aNewName = uniqueName(*anIt, aNames);
     (*anIt)->data()->setName(aNewName);
     // update names of results
     const std::list<ResultPtr>& aResults = (*anIt)->results();