1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ExchangePlugin_Dump.h>
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>
29 #include <ModelHighAPI_Dumper.h>
31 #include <Config_ModuleReader.h>
33 #include <GeomAlgoAPI_Tools.h>
35 #ifdef EXCHANGEPLUGIN_DUMP_NAMING
36 static const bool THE_DUMP_NAMING = true;
38 static const bool THE_DUMP_NAMING = false;
41 #ifdef EXCHANGEPLUGIN_DUMP_GEO
42 static const bool THE_DUMP_GEO = true;
44 static const bool THE_DUMP_GEO = false;
47 #ifdef EXCHANGEPLUGIN_DUMP_WEAK
48 static const bool THE_DUMP_WEAK = true;
50 static const bool THE_DUMP_WEAK = false;
54 ExchangePlugin_Dump::ExchangePlugin_Dump()
58 ExchangePlugin_Dump::~ExchangePlugin_Dump()
62 void ExchangePlugin_Dump::initAttributes()
64 data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
65 data()->addAttribute(FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
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());
71 data()->addAttribute(EXPORT_VARIABLES_ID(), ModelAPI_AttributeBoolean::typeId());
73 data()->addAttribute(DUMP_DIR_ID(), ModelAPI_AttributeString::typeId());
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);
81 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), DUMP_DIR_ID());
84 void ExchangePlugin_Dump::execute()
86 AttributeStringPtr aFilePathAttr =
87 this->string(ExchangePlugin_Dump::FILE_PATH_ID());
88 std::string aFilePath = aFilePathAttr->value();
89 if (aFilePath.empty())
95 void ExchangePlugin_Dump::dump(const std::string& theFileName)
97 // load DumpAssistant from Python side
98 Config_ModuleReader::loadScript("salome.shaper.model.dump");
100 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
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.");
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.");
123 std::list<FeaturePtr> aFeatures = aDoc->allFeatures();
124 for(std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
125 aFeatIt != aFeatures.end();
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.");
137 // process selected types of the dump
138 ModelHighAPI_Dumper* aDumper = ModelHighAPI_Dumper::getInstance();
140 setError("An error occured while dumping to " + theFileName);
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()
148 int aNbSelectedTypes = 0;
149 for (int i = 0; i < THE_TYPES_SIZE; ++i)
153 if (boolean(TOPOLOGICAL_NAMING_DUMP_ID())->value()) {
154 ModelHighAPI_Dumper::DumpStoragePtr aTopoNameStorage(new ModelHighAPI_Dumper::DumpStorage);
155 aDumper->addCustomStorage(aTopoNameStorage);
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);
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);
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();
179 aDumpDir = GeomAlgoAPI_Tools::File_Tools::path(theFileName);
180 aDumper->setDumpDir(aDumpDir);
182 if (!aDumper->process(aDoc, theFileName)) {
183 setError("An error occurred while dumping to " + theFileName);
185 if (boolean(EXPORT_VARIABLES_ID())->value()) {
186 aDumper->exportVariables();
189 // clear cashed data after export variables was performed
190 aDumper->clearCustomStorage();