Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Dump.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ExchangePlugin_Dump.h>
22
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_ResultPart.h>
27
28 #include <ModelHighAPI_Dumper.h>
29
30 #include <Config_ModuleReader.h>
31
32
33 ExchangePlugin_Dump::ExchangePlugin_Dump()
34 {
35 }
36
37 ExchangePlugin_Dump::~ExchangePlugin_Dump()
38 {
39 }
40
41 void ExchangePlugin_Dump::initAttributes()
42 {
43   data()->addAttribute(ExchangePlugin_Dump::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
44   data()->addAttribute(ExchangePlugin_Dump::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
45 }
46
47 void ExchangePlugin_Dump::execute()
48 {
49   AttributeStringPtr aFilePathAttr =
50       this->string(ExchangePlugin_Dump::FILE_PATH_ID());
51   std::string aFilePath = aFilePathAttr->value();
52   if (aFilePath.empty())
53     return;
54
55   dump(aFilePath);
56 }
57
58 void ExchangePlugin_Dump::dump(const std::string& theFileName)
59 {
60   // load DumpAssistant from Python side
61   Config_ModuleReader::loadScript("salome.shaper.model.dump");
62
63   ModelHighAPI_Dumper* aDumper = ModelHighAPI_Dumper::getInstance();
64   aDumper->clear();
65   DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
66
67   int aFeaturesNb = aDoc->size(ModelAPI_Feature::group());
68   if(aFeaturesNb > 1) {
69     FeaturePtr aLastFeature =
70         ModelAPI_Feature::feature(aDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
71     if(aDoc->currentFeature(true) != aLastFeature) {
72         setError("Dump cannot be done. Please move the history line to the end before dumping.");
73         return;
74     }
75   }
76
77   DocumentPtr anActiveDoc = ModelAPI_Session::get()->activeDocument();
78   aFeaturesNb = anActiveDoc->size(ModelAPI_Feature::group());
79   if(aFeaturesNb > 1) {
80     FeaturePtr aLastFeature =
81         ModelAPI_Feature::feature(anActiveDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
82     if(anActiveDoc->currentFeature(true) != aLastFeature) {
83         setError("Dump cannot be done. Please move the history line to the end before dumping.");
84         return;
85     }
86   }
87
88   std::list<FeaturePtr> aFeatures = aDoc->allFeatures();
89   for(std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
90       aFeatIt != aFeatures.end();
91       ++aFeatIt) {
92     ResultPartPtr aResultPart =
93       std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->firstResult());
94     if(aResultPart.get()) {
95       if(!aResultPart->isActivated()) {
96         setError("Error: Not all parts are loaded. Can not dump.");
97         return;
98       }
99     }
100   }
101
102   if (!aDumper || !aDumper->process(aDoc, theFileName))
103     setError("An error occured while dumping to " + theFileName);
104 }