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