1 // Copyright (C) 2014-2019 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>
28 #include <ModelHighAPI_Dumper.h>
30 #include <Config_ModuleReader.h>
32 #ifdef EXCHANGEPLUGIN_DUMP_NAMING
33 static const bool THE_DUMP_NAMING = true;
35 static const bool THE_DUMP_NAMING = false;
38 #ifdef EXCHANGEPLUGIN_DUMP_GEO
39 static const bool THE_DUMP_GEO = true;
41 static const bool THE_DUMP_GEO = false;
44 #ifdef EXCHANGEPLUGIN_DUMP_WEAK
45 static const bool THE_DUMP_WEAK = true;
47 static const bool THE_DUMP_WEAK = false;
51 ExchangePlugin_Dump::ExchangePlugin_Dump()
55 ExchangePlugin_Dump::~ExchangePlugin_Dump()
59 void ExchangePlugin_Dump::initAttributes()
61 data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
62 data()->addAttribute(FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
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());
68 data()->addAttribute(EXPORT_VARIABLES_ID(), ModelAPI_AttributeBoolean::typeId());
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);
77 void ExchangePlugin_Dump::execute()
79 AttributeStringPtr aFilePathAttr =
80 this->string(ExchangePlugin_Dump::FILE_PATH_ID());
81 std::string aFilePath = aFilePathAttr->value();
82 if (aFilePath.empty())
88 void ExchangePlugin_Dump::dump(const std::string& theFileName)
90 // load DumpAssistant from Python side
91 Config_ModuleReader::loadScript("salome.shaper.model.dump");
93 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
95 int aFeaturesNb = aDoc->size(ModelAPI_Feature::group());
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.");
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.");
116 std::list<FeaturePtr> aFeatures = aDoc->allFeatures();
117 for(std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
118 aFeatIt != aFeatures.end();
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.");
130 // process selected types of the dump
131 ModelHighAPI_Dumper* aDumper = ModelHighAPI_Dumper::getInstance();
133 setError("An error occured while dumping to " + theFileName);
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()
141 int aNbSelectedTypes = 0;
142 for (int i = 0; i < THE_TYPES_SIZE; ++i)
146 if (boolean(TOPOLOGICAL_NAMING_DUMP_ID())->value()) {
147 ModelHighAPI_Dumper::DumpStoragePtr aTopoNameStorage(new ModelHighAPI_Dumper::DumpStorage);
148 aDumper->addCustomStorage(aTopoNameStorage);
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);
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);
165 if (!aDumper->process(aDoc, theFileName)) {
166 setError("An error occurred while dumping to " + theFileName);
168 if (boolean(EXPORT_VARIABLES_ID())->value()) {
169 aDumper->exportVariables();
172 // clear cashed data after export variables was performed
173 aDumper->clearCustomStorage();