Salome HOME
Task 5.1.7: To be able to export a part to a file and import it into an existing...
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ImportPart.cpp
index ed0c456b75c903f7ee08c9e9f4de0ca8936366f6..11eae6c0d742c8420780de2e9bf1297a797bf0c2 100644 (file)
 #include <ExchangePlugin_ImportPart.h>
 
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_ResultPart.h>
 #include <ModelAPI_Session.h>
 
+#include <PartSetPlugin_Part.h>
+
 ExchangePlugin_ImportPart::ExchangePlugin_ImportPart()
 {
 }
@@ -42,6 +45,20 @@ void ExchangePlugin_ImportPart::execute()
 
   // load the file into the active document
   SessionPtr aSession = ModelAPI_Session::get();
-  if (!aSession->activeDocument()->import(aFilename.c_str()))
+  DocumentPtr aDoc = aSession->activeDocument();
+  bool isPartSet = aDoc == aSession->moduleDocument();
+  bool isOk = aDoc->import(aFilename.c_str(), 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());
+    }
+    isOk = aPartResult && aPartResult->partDoc()->import(aFilename.c_str());
+  }
+  if (!isOk)
     setError("Cannot import the document.");
 }