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