1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ExchangePlugin_ImportPart.h>
22 #include <ModelAPI_AttributeString.h>
23 #include <ModelAPI_ResultPart.h>
24 #include <ModelAPI_Session.h>
26 #include <PartSetPlugin_Part.h>
28 ExchangePlugin_ImportPart::ExchangePlugin_ImportPart()
32 void ExchangePlugin_ImportPart::initAttributes()
34 data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
37 void ExchangePlugin_ImportPart::execute()
39 AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID());
40 std::string aFilename = aFilePathAttr->value();
41 if (aFilename.empty()) {
42 setError("File path is empty.");
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;
57 aPartFeature->execute();
58 aPartResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->lastResult());
60 isOk = aPartResult && aPartResult->partDoc()->import(aFilename.c_str());
63 setError("Cannot import the document.");