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