]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ImportPart.cpp
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
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ExchangePlugin_ImportPart.h>
21
22 #include <ModelAPI_AttributeString.h>
23 #include <ModelAPI_ResultPart.h>
24 #include <ModelAPI_Session.h>
25
26 #include <PartSetPlugin_Part.h>
27
28 ExchangePlugin_ImportPart::ExchangePlugin_ImportPart()
29 {
30 }
31
32 void ExchangePlugin_ImportPart::initAttributes()
33 {
34   data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
35 }
36
37 void ExchangePlugin_ImportPart::execute()
38 {
39   AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID());
40   std::string aFilename = aFilePathAttr->value();
41   if (aFilename.empty()) {
42     setError("File path is empty.");
43     return;
44   }
45
46   // load the file into the active document
47   SessionPtr aSession = ModelAPI_Session::get();
48   DocumentPtr aDoc = aSession->activeDocument();
49   bool isPartSet = aDoc == aSession->moduleDocument();
50   bool isOk = aDoc->import(aFilename.c_str(), isPartSet);
51   if (!isOk && isPartSet) {
52     // there are features not appropriate for PartSet,
53     // create new part and load there
54     FeaturePtr aPartFeature = aDoc->addFeature(PartSetPlugin_Part::ID());
55     ResultPartPtr aPartResult;
56     if (aPartFeature) {
57       aPartFeature->execute();
58       aPartResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->lastResult());
59     }
60     isOk = aPartResult && aPartResult->partDoc()->import(aFilename.c_str());
61   }
62   if (!isOk)
63     setError("Cannot import the document.");
64 }