Salome HOME
Issue #2811: Update content of Object node on creation moment
[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_AttributeBoolean.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_ResultPart.h>
28
29 #include <ModelHighAPI_Dumper.h>
30
31 #include <Config_ModuleReader.h>
32
33
34 ExchangePlugin_Dump::ExchangePlugin_Dump()
35 {
36 }
37
38 ExchangePlugin_Dump::~ExchangePlugin_Dump()
39 {
40 }
41
42 void ExchangePlugin_Dump::initAttributes()
43 {
44   data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
45   data()->addAttribute(FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
46
47   data()->addAttribute(SELECTION_TYPE_ID(), ModelAPI_AttributeString::typeId());
48   //string(SELECTION_TYPE_ID())->setValue(TOPOLOGICAL_NAMING_DUMP_ID()); // default value
49 }
50
51 void ExchangePlugin_Dump::execute()
52 {
53   AttributeStringPtr aFilePathAttr =
54       this->string(ExchangePlugin_Dump::FILE_PATH_ID());
55   std::string aFilePath = aFilePathAttr->value();
56   if (aFilePath.empty())
57     return;
58
59   dump(aFilePath);
60 }
61
62 void ExchangePlugin_Dump::dump(const std::string& theFileName)
63 {
64   // load DumpAssistant from Python side
65   Config_ModuleReader::loadScript("salome.shaper.model.dump");
66
67   ModelHighAPI_Dumper* aDumper = ModelHighAPI_Dumper::getInstance();
68   aDumper->clear();
69   aDumper->setSelectionByGeometry(string(SELECTION_TYPE_ID())->value() == GEOMETRIC_DUMP_ID());
70   aDumper->setSelectionWeakNaming(string(SELECTION_TYPE_ID())->value() == WEAK_NAMING_DUMP_ID());
71
72   DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
73
74   int aFeaturesNb = aDoc->size(ModelAPI_Feature::group());
75   if(aFeaturesNb > 1) {
76     FeaturePtr aLastFeature =
77         ModelAPI_Feature::feature(aDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
78     if(aDoc->currentFeature(true) != aLastFeature) {
79         setError("Dump cannot be done. Please move the history line to the end before dumping.");
80         return;
81     }
82   }
83
84   DocumentPtr anActiveDoc = ModelAPI_Session::get()->activeDocument();
85   aFeaturesNb = anActiveDoc->size(ModelAPI_Feature::group());
86   if(aFeaturesNb > 1) {
87     FeaturePtr aLastFeature =
88         ModelAPI_Feature::feature(anActiveDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
89     if(anActiveDoc->currentFeature(true) != aLastFeature) {
90         setError("Dump cannot be done. Please move the history line to the end before dumping.");
91         return;
92     }
93   }
94
95   std::list<FeaturePtr> aFeatures = aDoc->allFeatures();
96   for(std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
97       aFeatIt != aFeatures.end();
98       ++aFeatIt) {
99     ResultPartPtr aResultPart =
100       std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->firstResult());
101     if(aResultPart.get()) {
102       if(!aResultPart->isActivated()) {
103         setError("Error: Not all parts are loaded. Can not dump.");
104         return;
105       }
106     }
107   }
108
109   if (!aDumper || !aDumper->process(aDoc, theFileName))
110     setError("An error occured while dumping to " + theFileName);
111 }