Salome HOME
418ab137021da7e20a04b4559af537c1feb399db
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Dump.cpp
1 // Copyright (C) 2014-2021  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 #ifdef EXCHANGEPLUGIN_DUMP_NAMING
33 static const bool THE_DUMP_NAMING = true;
34 #else
35 static const bool THE_DUMP_NAMING = false;
36 #endif
37
38 #ifdef EXCHANGEPLUGIN_DUMP_GEO
39 static const bool THE_DUMP_GEO = true;
40 #else
41 static const bool THE_DUMP_GEO = false;
42 #endif
43
44 #ifdef EXCHANGEPLUGIN_DUMP_WEAK
45 static const bool THE_DUMP_WEAK = true;
46 #else
47 static const bool THE_DUMP_WEAK = false;
48 #endif
49
50
51 ExchangePlugin_Dump::ExchangePlugin_Dump()
52 {
53 }
54
55 ExchangePlugin_Dump::~ExchangePlugin_Dump()
56 {
57 }
58
59 void ExchangePlugin_Dump::initAttributes()
60 {
61   data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
62   data()->addAttribute(FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
63
64   data()->addAttribute(TOPOLOGICAL_NAMING_DUMP_ID(), ModelAPI_AttributeBoolean::typeId());
65   data()->addAttribute(GEOMETRIC_DUMP_ID(), ModelAPI_AttributeBoolean::typeId());
66   data()->addAttribute(WEAK_NAMING_DUMP_ID(), ModelAPI_AttributeBoolean::typeId());
67
68   data()->addAttribute(EXPORT_VARIABLES_ID(), ModelAPI_AttributeBoolean::typeId());
69
70   // default values
71   boolean(TOPOLOGICAL_NAMING_DUMP_ID())->setValue(THE_DUMP_NAMING);
72   boolean(GEOMETRIC_DUMP_ID())->setValue(THE_DUMP_GEO);
73   boolean(WEAK_NAMING_DUMP_ID())->setValue(THE_DUMP_WEAK);
74   boolean(EXPORT_VARIABLES_ID())->setValue(false);
75 }
76
77 void ExchangePlugin_Dump::execute()
78 {
79   AttributeStringPtr aFilePathAttr =
80       this->string(ExchangePlugin_Dump::FILE_PATH_ID());
81   std::string aFilePath = aFilePathAttr->value();
82   if (aFilePath.empty())
83     return;
84
85   dump(aFilePath);
86 }
87
88 void ExchangePlugin_Dump::dump(const std::string& theFileName)
89 {
90   // load DumpAssistant from Python side
91   Config_ModuleReader::loadScript("salome.shaper.model.dump");
92
93   DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
94
95   int aFeaturesNb = aDoc->size(ModelAPI_Feature::group());
96   if(aFeaturesNb > 1) {
97     FeaturePtr aLastFeature =
98         ModelAPI_Feature::feature(aDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
99     if(aDoc->currentFeature(true) != aLastFeature) {
100         setError("Dump cannot be done. Please move the history line to the end before dumping.");
101         return;
102     }
103   }
104
105   DocumentPtr anActiveDoc = ModelAPI_Session::get()->activeDocument();
106   aFeaturesNb = anActiveDoc->size(ModelAPI_Feature::group());
107   if(aFeaturesNb > 1) {
108     FeaturePtr aLastFeature =
109         ModelAPI_Feature::feature(anActiveDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
110     if(anActiveDoc->currentFeature(true) != aLastFeature) {
111       setError("Dump cannot be done. Please move the history line to the end before dumping.");
112       return;
113     }
114   }
115
116   std::list<FeaturePtr> aFeatures = aDoc->allFeatures();
117   for(std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
118       aFeatIt != aFeatures.end();
119       ++aFeatIt) {
120     ResultPartPtr aResultPart =
121       std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->firstResult());
122     if(aResultPart.get()) {
123       if(!aResultPart->isActivated()) {
124         setError("Error: Not all parts are loaded. Can not dump.");
125         return;
126       }
127     }
128   }
129
130   // process selected types of the dump
131   ModelHighAPI_Dumper* aDumper = ModelHighAPI_Dumper::getInstance();
132   if (!aDumper)
133     setError("An error occured while dumping to " + theFileName);
134
135   static const int THE_TYPES_SIZE = 3;
136   bool aTypes[THE_TYPES_SIZE] = {
137     boolean(TOPOLOGICAL_NAMING_DUMP_ID())->value(),
138     boolean(GEOMETRIC_DUMP_ID())->value(),
139     boolean(WEAK_NAMING_DUMP_ID())->value()
140   };
141   int aNbSelectedTypes = 0;
142   for (int i = 0; i < THE_TYPES_SIZE; ++i)
143     if (aTypes[i])
144       ++aNbSelectedTypes;
145
146   if (boolean(TOPOLOGICAL_NAMING_DUMP_ID())->value()) {
147     ModelHighAPI_Dumper::DumpStoragePtr aTopoNameStorage(new ModelHighAPI_Dumper::DumpStorage);
148     aDumper->addCustomStorage(aTopoNameStorage);
149   }
150   if (boolean(GEOMETRIC_DUMP_ID())->value()) {
151     ModelHighAPI_Dumper::DumpStoragePtr aGeomSelectionStorage(
152         new ModelHighAPI_Dumper::DumpStorageGeom);
153     if (aNbSelectedTypes > 1)
154       aGeomSelectionStorage->setFilenameSuffix("_geo");
155     aDumper->addCustomStorage(aGeomSelectionStorage);
156   }
157   if (boolean(WEAK_NAMING_DUMP_ID())->value()) {
158     ModelHighAPI_Dumper::DumpStoragePtr aWeakNamingStorage(
159         new ModelHighAPI_Dumper::DumpStorageWeak);
160     if (aNbSelectedTypes > 1)
161       aWeakNamingStorage->setFilenameSuffix("_weak");
162     aDumper->addCustomStorage(aWeakNamingStorage);
163   }
164
165   if (!aDumper->process(aDoc, theFileName)) {
166     setError("An error occurred while dumping to " + theFileName);
167   } else {
168     if (boolean(EXPORT_VARIABLES_ID())->value()) {
169       aDumper->exportVariables();
170     }
171   }
172   // clear cashed data after export variables was performed
173   aDumper->clearCustomStorage();
174
175 }