Salome HOME
Make the SHAPERSTUDY python dump works on SHAPER objects stable, even after modificat...
[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(TOPOLOGICAL_NAMING_DUMP_ID(), ModelAPI_AttributeBoolean::typeId());
47   data()->addAttribute(GEOMETRIC_DUMP_ID(), ModelAPI_AttributeBoolean::typeId());
48   data()->addAttribute(WEAK_NAMING_DUMP_ID(), ModelAPI_AttributeBoolean::typeId());
49
50   data()->addAttribute(EXPORT_VARIABLES_ID(), ModelAPI_AttributeBoolean::typeId());
51
52   // default values
53   boolean(TOPOLOGICAL_NAMING_DUMP_ID())->setValue(true);
54   boolean(GEOMETRIC_DUMP_ID())->setValue(true);
55   boolean(WEAK_NAMING_DUMP_ID())->setValue(false);
56   boolean(EXPORT_VARIABLES_ID())->setValue(false);
57 }
58
59 void ExchangePlugin_Dump::execute()
60 {
61   AttributeStringPtr aFilePathAttr =
62       this->string(ExchangePlugin_Dump::FILE_PATH_ID());
63   std::string aFilePath = aFilePathAttr->value();
64   if (aFilePath.empty())
65     return;
66
67   dump(aFilePath);
68 }
69
70 void ExchangePlugin_Dump::dump(const std::string& theFileName)
71 {
72   // load DumpAssistant from Python side
73   Config_ModuleReader::loadScript("salome.shaper.model.dump");
74
75   DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
76
77   int aFeaturesNb = aDoc->size(ModelAPI_Feature::group());
78   if(aFeaturesNb > 1) {
79     FeaturePtr aLastFeature =
80         ModelAPI_Feature::feature(aDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
81     if(aDoc->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   DocumentPtr anActiveDoc = ModelAPI_Session::get()->activeDocument();
88   aFeaturesNb = anActiveDoc->size(ModelAPI_Feature::group());
89   if(aFeaturesNb > 1) {
90     FeaturePtr aLastFeature =
91         ModelAPI_Feature::feature(anActiveDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
92     if(anActiveDoc->currentFeature(true) != aLastFeature) {
93       setError("Dump cannot be done. Please move the history line to the end before dumping.");
94       return;
95     }
96   }
97
98   std::list<FeaturePtr> aFeatures = aDoc->allFeatures();
99   for(std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
100       aFeatIt != aFeatures.end();
101       ++aFeatIt) {
102     ResultPartPtr aResultPart =
103       std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->firstResult());
104     if(aResultPart.get()) {
105       if(!aResultPart->isActivated()) {
106         setError("Error: Not all parts are loaded. Can not dump.");
107         return;
108       }
109     }
110   }
111
112   // process selected types of the dump
113   ModelHighAPI_Dumper* aDumper = ModelHighAPI_Dumper::getInstance();
114   if (!aDumper)
115     setError("An error occured while dumping to " + theFileName);
116
117   static const int THE_TYPES_SIZE = 3;
118   bool aTypes[THE_TYPES_SIZE] = {
119     boolean(TOPOLOGICAL_NAMING_DUMP_ID())->value(),
120     boolean(GEOMETRIC_DUMP_ID())->value(),
121     boolean(WEAK_NAMING_DUMP_ID())->value()
122   };
123   int aNbSelectedTypes = 0;
124   for (int i = 0; i < THE_TYPES_SIZE; ++i)
125     if (aTypes[i])
126       ++aNbSelectedTypes;
127
128   if (boolean(TOPOLOGICAL_NAMING_DUMP_ID())->value()) {
129     ModelHighAPI_Dumper::DumpStoragePtr aTopoNameStorage(new ModelHighAPI_Dumper::DumpStorage);
130     aDumper->addCustomStorage(aTopoNameStorage);
131   }
132   if (boolean(GEOMETRIC_DUMP_ID())->value()) {
133     ModelHighAPI_Dumper::DumpStoragePtr aGeomSelectionStorage(
134         new ModelHighAPI_Dumper::DumpStorageGeom);
135     if (aNbSelectedTypes > 1)
136       aGeomSelectionStorage->setFilenameSuffix("_geo");
137     aDumper->addCustomStorage(aGeomSelectionStorage);
138   }
139   if (boolean(WEAK_NAMING_DUMP_ID())->value()) {
140     ModelHighAPI_Dumper::DumpStoragePtr aWeakNamingStorage(
141         new ModelHighAPI_Dumper::DumpStorageWeak);
142     if (aNbSelectedTypes > 1)
143       aWeakNamingStorage->setFilenameSuffix("_weak");
144     aDumper->addCustomStorage(aWeakNamingStorage);
145   }
146
147   if (!aDumper->process(aDoc, theFileName)) {
148     setError("An error occurred while dumping to " + theFileName);
149   } else {
150     if (boolean(EXPORT_VARIABLES_ID())->value()) {
151       aDumper->exportVariables();
152     }
153   }
154   // clear cashed data after export variables was performed
155   aDumper->clearCustomStorage();
156
157 }