]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_Dump.cpp
Salome HOME
Flexible behavior of Dump feature configured in CMake
[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 #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   // default values
69   boolean(TOPOLOGICAL_NAMING_DUMP_ID())->setValue(THE_DUMP_NAMING);
70   boolean(GEOMETRIC_DUMP_ID())->setValue(THE_DUMP_GEO);
71   boolean(WEAK_NAMING_DUMP_ID())->setValue(THE_DUMP_WEAK);
72 }
73
74 void ExchangePlugin_Dump::execute()
75 {
76   AttributeStringPtr aFilePathAttr =
77       this->string(ExchangePlugin_Dump::FILE_PATH_ID());
78   std::string aFilePath = aFilePathAttr->value();
79   if (aFilePath.empty())
80     return;
81
82   dump(aFilePath);
83 }
84
85 void ExchangePlugin_Dump::dump(const std::string& theFileName)
86 {
87   // load DumpAssistant from Python side
88   Config_ModuleReader::loadScript("salome.shaper.model.dump");
89
90   DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
91
92   int aFeaturesNb = aDoc->size(ModelAPI_Feature::group());
93   if(aFeaturesNb > 1) {
94     FeaturePtr aLastFeature =
95         ModelAPI_Feature::feature(aDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
96     if(aDoc->currentFeature(true) != aLastFeature) {
97         setError("Dump cannot be done. Please move the history line to the end before dumping.");
98         return;
99     }
100   }
101
102   DocumentPtr anActiveDoc = ModelAPI_Session::get()->activeDocument();
103   aFeaturesNb = anActiveDoc->size(ModelAPI_Feature::group());
104   if(aFeaturesNb > 1) {
105     FeaturePtr aLastFeature =
106         ModelAPI_Feature::feature(anActiveDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
107     if(anActiveDoc->currentFeature(true) != aLastFeature) {
108       setError("Dump cannot be done. Please move the history line to the end before dumping.");
109       return;
110     }
111   }
112
113   std::list<FeaturePtr> aFeatures = aDoc->allFeatures();
114   for(std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
115       aFeatIt != aFeatures.end();
116       ++aFeatIt) {
117     ResultPartPtr aResultPart =
118       std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->firstResult());
119     if(aResultPart.get()) {
120       if(!aResultPart->isActivated()) {
121         setError("Error: Not all parts are loaded. Can not dump.");
122         return;
123       }
124     }
125   }
126
127   // process selected types of the dump
128   ModelHighAPI_Dumper* aDumper = ModelHighAPI_Dumper::getInstance();
129   if (!aDumper)
130     setError("An error occured while dumping to " + theFileName);
131
132   static const int THE_TYPES_SIZE = 3;
133   bool aTypes[THE_TYPES_SIZE] = {
134     boolean(TOPOLOGICAL_NAMING_DUMP_ID())->value(),
135     boolean(GEOMETRIC_DUMP_ID())->value(),
136     boolean(WEAK_NAMING_DUMP_ID())->value()
137   };
138   int aNbSelectedTypes = 0;
139   for (int i = 0; i < THE_TYPES_SIZE; ++i)
140     if (aTypes[i])
141       ++aNbSelectedTypes;
142
143   if (boolean(TOPOLOGICAL_NAMING_DUMP_ID())->value()) {
144     ModelHighAPI_Dumper::DumpStoragePtr aTopoNameStorage(new ModelHighAPI_Dumper::DumpStorage);
145     aDumper->addCustomStorage(aTopoNameStorage);
146   }
147   if (boolean(GEOMETRIC_DUMP_ID())->value()) {
148     ModelHighAPI_Dumper::DumpStoragePtr aGeomSelectionStorage(
149         new ModelHighAPI_Dumper::DumpStorageGeom);
150     if (aNbSelectedTypes > 1)
151       aGeomSelectionStorage->setFilenameSuffix("_geo");
152     aDumper->addCustomStorage(aGeomSelectionStorage);
153   }
154   if (boolean(WEAK_NAMING_DUMP_ID())->value()) {
155     ModelHighAPI_Dumper::DumpStoragePtr aWeakNamingStorage(
156         new ModelHighAPI_Dumper::DumpStorageWeak);
157     if (aNbSelectedTypes > 1)
158       aWeakNamingStorage->setFilenameSuffix("_weak");
159     aDumper->addCustomStorage(aWeakNamingStorage);
160   }
161
162   if (!aDumper->process(aDoc, theFileName))
163     setError("An error occured while dumping to " + theFileName);
164 }