Salome HOME
Make correct python dump for exportToGeom with separated results
[modules/shaper.git] / src / ExchangeAPI / ExchangeAPI_Export.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 "ExchangeAPI_Export.h"
21 //--------------------------------------------------------------------------------------
22 #include <ModelAPI_Document.h>
23 #include <ModelAPI_Feature.h>
24 #include <ModelHighAPI_Tools.h>
25 #include <ModelHighAPI_Dumper.h>
26 #include <ModelHighAPI_Services.h>
27 #include <ModelHighAPI_Selection.h>
28 //--------------------------------------------------------------------------------------
29
30 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature)
31 : ModelHighAPI_Interface(theFeature)
32 {
33   initialize();
34 }
35
36 /// Constructor with values for XAO export.
37 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
38                               const std::string & theFilePath,
39                               const std::string & theAuthor,
40                               const std::string & theGeometryName)
41 : ModelHighAPI_Interface(theFeature)
42 {
43   initialize();
44   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
45   fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID()));
46   fillAttribute(theAuthor, theFeature->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()));
47   fillAttribute(theGeometryName,
48                 theFeature->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID()));
49   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
50   execute();
51   apply(); // finish operation to make sure the export is done on the current state of the history
52 }
53
54 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
55   const std::string & theFilePath, const ModelHighAPI_Selection& theResult,
56   const std::string & theAuthor, const std::string & theGeometryName)
57   : ModelHighAPI_Interface(theFeature)
58 {
59   initialize();
60   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
61   fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID()));
62   fillAttribute(theAuthor, theFeature->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()));
63   fillAttribute(theGeometryName,
64     theFeature->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID()));
65   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
66   std::list<ModelHighAPI_Selection> aListOfOneSel;
67   aListOfOneSel.push_back(theResult);
68   fillAttribute(aListOfOneSel,
69     theFeature->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
70   execute();
71   apply(); // finish operation to make sure the export is done on the current state of the history
72 }
73
74
75 /// Constructor with values for export in other formats than XAO.
76 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
77                               const std::string & theFilePath,
78                               const std::list<ModelHighAPI_Selection> & theSelectionList,
79                               const std::string & theFileFormat)
80 : ModelHighAPI_Interface(theFeature)
81 {
82   initialize();
83   fillAttribute("Regular", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
84   fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::FILE_PATH_ID()));
85   fillAttribute(theSelectionList,
86                 theFeature->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
87   fillAttribute(theFileFormat, theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
88   execute();
89   apply(); // finish operation to make sure the export is done on the current state of the history
90 }
91
92 ExchangeAPI_Export::~ExchangeAPI_Export()
93 {
94 }
95
96 // this method is needed on Windows because back-slashes in python may cause error
97 static void correctSeparators(std::string& thePath) {
98   // replace single "\" or triple "\\\" or more by double "\"
99   for (std::size_t aFind = thePath.find('\\'); aFind != std::string::npos;
100     aFind = thePath.find('\\', aFind)) {
101     // search the next
102     std::size_t aFind2 = thePath.find('\\', aFind + 1);
103     if (aFind2 == std::string::npos || aFind2 > aFind + 1) { // single, so add one more
104       thePath.replace(aFind, 1, 2, '\\');
105     } else { // if there is more than double "\", remove them
106       for (aFind2 = thePath.find('\\', aFind2 + 1);
107            aFind2 != std::string::npos && aFind2 <= aFind + 2;
108            aFind2 = thePath.find('\\', aFind2)) {
109         thePath.erase(aFind2, 1);
110       }
111     }
112     aFind += 2;
113   }
114 }
115
116 void ExchangeAPI_Export::dump(ModelHighAPI_Dumper& theDumper) const
117 {
118   FeaturePtr aBase = feature();
119   const std::string& aDocName = theDumper.name(aBase->document());
120
121   theDumper << aBase << " = model.";
122
123   std::string exportType = aBase->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID())->value();
124
125   if (exportType == "XAO") {
126     std::string aTmpXAOFile =
127                 aBase->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value();
128     correctSeparators(aTmpXAOFile);
129     theDumper << "exportToXAO(" << aDocName << ", '" << aTmpXAOFile << "'" ;
130     AttributeSelectionListPtr aShapeSelected =
131       aBase->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
132     if (aShapeSelected->isInitialized() && aShapeSelected->size() == 1) {
133       theDumper<<", "<<aShapeSelected->value(0);
134     }
135
136     std::string theAuthor = aBase->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
137     if (! theAuthor.empty())
138       theDumper << ", '" << theAuthor << "'";
139     std::string theGeometryName =
140                 aBase->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
141     if (! theGeometryName.empty())
142       theDumper << ", '" << theGeometryName << "'";
143     theDumper << ")" << std::endl;
144   }
145   else {
146     std::string aFilePath = aBase->string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->value();
147     correctSeparators(aFilePath);
148       theDumper << "exportToFile(" << aDocName << ", \"" << aFilePath << "\", " <<
149       aBase->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
150     std::string theFileFormat =
151       aBase->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID())->value();
152     if (!theFileFormat.empty())
153       theDumper << ", '" << theFileFormat << "'";
154     theDumper << ")" << std::endl;
155   }
156 }
157
158 ExportPtr exportToFile(const std::shared_ptr<ModelAPI_Document> & thePart,
159                   const std::string & theFilePath,
160                   const std::list<ModelHighAPI_Selection> & theSelectionList,
161                   const std::string & theFileFormat)
162 {
163   apply(); // finish previous operation to make sure all previous operations are done
164   std::shared_ptr<ModelAPI_Feature> aFeature =
165     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
166   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectionList, theFileFormat));
167 }
168
169 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
170                  const std::string & theFilePath,
171                  const std::string & theAuthor,
172                  const std::string & theGeometryName)
173 {
174   apply(); // finish previous operation to make sure all previous operations are done
175   std::shared_ptr<ModelAPI_Feature> aFeature =
176     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
177   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theAuthor, theGeometryName));
178 }
179
180 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
181   const std::string & theFilePath, const ModelHighAPI_Selection& theSelectedShape,
182   const std::string & theAuthor, const std::string & theGeometryName)
183 {
184   apply(); // finish previous operation to make sure all previous operations are done
185   std::shared_ptr<ModelAPI_Feature> aFeature =
186     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
187   // special internal case when for XAO a selection list is filled
188   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectedShape, "XAO"));
189 }
190
191 //--------------------------------------------------------------------------------------