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