Salome HOME
Merge branch 'V9_11_BR'
[modules/shaper.git] / src / ExchangeAPI / ExchangeAPI_Export.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 <ExchangePlugin_ExportPart.h>
23 //--------------------------------------------------------------------------------------
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Feature.h>
26 #include <ModelHighAPI_Tools.h>
27 #include <ModelHighAPI_Dumper.h>
28 #include <ModelHighAPI_Services.h>
29 #include <ModelHighAPI_Selection.h>
30 //--------------------------------------------------------------------------------------
31
32 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature)
33 : ModelHighAPI_Interface(theFeature)
34 {
35   initialize();
36 }
37
38 /// Constructor with values for XAO export.
39 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
40                               const std::string & theFilePath,
41                               const std::string & theAuthor,
42                               const std::string & theGeometryName)
43 : ModelHighAPI_Interface(theFeature)
44 {
45   initialize();
46   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
47   fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID()));
48   fillAttribute(theAuthor, theFeature->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()));
49   fillAttribute(theGeometryName,
50                 theFeature->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID()));
51   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
52   execute();
53   apply(); // finish operation to make sure the export is done on the current state of the history
54 }
55
56 // Constructor with values for STL of selected result export.
57 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
58                                        const std::string & theFilePath,
59                                        const ModelHighAPI_Selection& theSelectedShape,
60                                        const ModelHighAPI_Double& theDeflectionRelative,
61                                        const ModelHighAPI_Double& theDeflectionAbsolute,
62                                        const bool theIsRelative,
63                                        const bool theIsASCII)
64   : ModelHighAPI_Interface(theFeature)
65 {
66   initialize();
67   fillAttribute("STL", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
68   fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID()));
69
70   if (theIsRelative) {
71     fillAttribute(ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE_RELATIVE(),
72       theFeature->string(ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE()) );
73     fillAttribute(theDeflectionRelative,
74       theFeature->real(ExchangePlugin_ExportFeature::STL_RELATIVE()) );
75   }
76   else {
77     fillAttribute(ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE_ABSOLUTE(),
78       theFeature->string(ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE()) );
79     fillAttribute(theDeflectionAbsolute,
80       theFeature->real(ExchangePlugin_ExportFeature::STL_ABSOLUTE()) );
81   }
82
83   if(theIsASCII){
84     fillAttribute(ExchangePlugin_ExportFeature::STL_FILE_TYPE_ASCII(),
85       theFeature->string(ExchangePlugin_ExportFeature::STL_FILE_TYPE()));
86   }
87   else
88   {
89     fillAttribute(ExchangePlugin_ExportFeature::STL_FILE_TYPE_BINARY(),
90       theFeature->string(ExchangePlugin_ExportFeature::STL_FILE_TYPE()));
91   }
92
93   fillAttribute(theSelectedShape,
94                 theFeature->selection(ExchangePlugin_ExportFeature::STL_OBJECT_SELECTED()));
95   fillAttribute("STL", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
96   execute();
97   apply(); // finish operation to make sure the export is done on the current state of the history
98 }
99
100
101
102 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
103   const std::string & theFilePath, const ModelHighAPI_Selection& theResult,
104   const std::string & theAuthor, const std::string & theGeometryName)
105   : ModelHighAPI_Interface(theFeature)
106 {
107   initialize();
108   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
109   fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID()));
110   fillAttribute(theAuthor, theFeature->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()));
111   fillAttribute(theGeometryName,
112     theFeature->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID()));
113   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
114   std::list<ModelHighAPI_Selection> aListOfOneSel;
115   aListOfOneSel.push_back(theResult);
116   fillAttribute(aListOfOneSel,
117     theFeature->selectionList(ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID()));
118   execute();
119   apply(); // finish operation to make sure the export is done on the current state of the history
120 }
121
122
123
124 /// Constructor with values for export in other formats than XAO.
125 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
126                               const std::string & theFilePath,
127                               const std::list<ModelHighAPI_Selection> & theSelectionList,
128                               const std::string & theFileFormat)
129 : ModelHighAPI_Interface(theFeature)
130 {
131   initialize();
132   fillAttribute("Regular", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
133   fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::FILE_PATH_ID()));
134   fillAttribute(theSelectionList,
135                 theFeature->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
136   fillAttribute(theFileFormat, theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
137   execute();
138   apply(); // finish operation to make sure the export is done on the current state of the history
139 }
140
141 ExchangeAPI_Export::~ExchangeAPI_Export()
142 {
143 }
144
145 // this method is needed on Windows because back-slashes in python may cause error
146 static void correctSeparators(std::string& thePath) {
147   // replace single "\" or triple "\\\" or more by double "\"
148   for (std::size_t aFind = thePath.find('\\'); aFind != std::string::npos;
149     aFind = thePath.find('\\', aFind)) {
150     // search the next
151     std::size_t aFind2 = thePath.find('\\', aFind + 1);
152     if (aFind2 == std::string::npos || aFind2 > aFind + 1) { // single, so add one more
153       thePath.replace(aFind, 1, 2, '\\');
154     } else { // if there is more than double "\", remove them
155       for (aFind2 = thePath.find('\\', aFind2 + 1);
156            aFind2 != std::string::npos && aFind2 <= aFind + 2;
157            aFind2 = thePath.find('\\', aFind2)) {
158         thePath.erase(aFind2, 1);
159       }
160     }
161     aFind += 2;
162   }
163 }
164
165 void ExchangeAPI_Export::dump(ModelHighAPI_Dumper& theDumper) const
166 {
167   FeaturePtr aBase = feature();
168   const std::string& aDocName = theDumper.name(aBase->document());
169
170   theDumper << aBase << " = model.";
171
172   std::string exportType = aBase->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID())->value();
173
174   if (exportType == "XAO") {
175     std::string aTmpXAOFile =
176                 aBase->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value();
177     correctSeparators(aTmpXAOFile);
178     theDumper << "exportToXAO(" << aDocName << ", '" << aTmpXAOFile << "'" ;
179     AttributeSelectionListPtr aShapeSelected =
180       aBase->selectionList(ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID());
181     if (aShapeSelected->isInitialized() && aShapeSelected->size() == 1) {
182       theDumper<<", "<<aShapeSelected->value(0);
183     }
184
185     std::string theAuthor = aBase->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
186     if (! theAuthor.empty())
187       theDumper << ", '" << theAuthor << "'";
188     std::string theGeometryName =
189                 aBase->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
190     if (! theGeometryName.empty())
191       theDumper << ", '" << theGeometryName << "'";
192     theDumper << ")" << std::endl;
193   }
194   else if (exportType == "STL") {
195     std::string aTmpSTLFile =
196                 aBase->string(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID())->value();
197     correctSeparators(aTmpSTLFile);
198     theDumper << "exportToSTL(" << aDocName << ", '" << aTmpSTLFile << "'" ;
199     AttributeSelectionPtr aShapeSelected =
200       aBase->selection(ExchangePlugin_ExportFeature::STL_OBJECT_SELECTED());
201
202     theDumper<<","<< aShapeSelected;
203
204     theDumper <<","<<  stlabsolute() <<","<< stlrelative();
205
206     if (stldeflectionType()->value()
207          == ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE_RELATIVE()){
208       theDumper <<","<< "True";
209     }
210     else {
211       theDumper <<","<< "False";
212     }
213
214     if (stlfileType()->value() == ExchangePlugin_ExportFeature::STL_FILE_TYPE_BINARY()) {
215       theDumper << "False";
216     }
217     else {
218       theDumper <<  "True";
219     }
220     theDumper << ")" << std::endl;
221   }
222   else {
223     std::string aFilePath = aBase->string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->value();
224     correctSeparators(aFilePath);
225       theDumper << "exportToFile(" << aDocName << ", \"" << aFilePath << "\", " <<
226       aBase->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
227     std::string theFileFormat =
228       aBase->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID())->value();
229     if (!theFileFormat.empty())
230       theDumper << ", '" << theFileFormat << "'";
231     theDumper << ")" << std::endl;
232   }
233 }
234
235 ExportPtr exportToFile(const std::shared_ptr<ModelAPI_Document> & thePart,
236                   const std::string & theFilePath,
237                   const std::list<ModelHighAPI_Selection> & theSelectionList,
238                   const std::string & theFileFormat)
239 {
240   apply(); // finish previous operation to make sure all previous operations are done
241   std::shared_ptr<ModelAPI_Feature> aFeature =
242     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
243   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectionList, theFileFormat));
244 }
245
246 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
247                  const std::string & theFilePath,
248                  const std::string & theAuthor,
249                  const std::string & theGeometryName)
250 {
251   apply(); // finish previous operation to make sure all previous operations are done
252   std::shared_ptr<ModelAPI_Feature> aFeature =
253     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
254   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theAuthor, theGeometryName));
255 }
256
257 ExportPtr exportToSTL(const std::shared_ptr<ModelAPI_Document> & thePart,
258       const std::string & theFilePath,
259       const ModelHighAPI_Selection& theSelectedShape,
260       const ModelHighAPI_Double&  theDeflectionRelative,
261       const ModelHighAPI_Double&  theDeflectionAbsolute,
262       const bool theIsRelative,
263       const bool theIsASCII)
264 {
265   apply(); // finish previous operation to make sure all previous operations are done
266   std::shared_ptr<ModelAPI_Feature> aFeature =
267     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
268
269   return ExportPtr(new ExchangeAPI_Export(aFeature,
270                                           theFilePath,
271                                           theSelectedShape,
272                                           theDeflectionRelative,
273                                           theDeflectionAbsolute,
274                                           theIsRelative,
275                                           theIsASCII));
276 }
277
278 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
279   const std::string & theFilePath, const ModelHighAPI_Selection& theSelectedShape,
280   const std::string & /*theAuthor*/, const std::string & /*theGeometryName*/)
281 {
282   apply(); // finish previous operation to make sure all previous operations are done
283   std::shared_ptr<ModelAPI_Feature> aFeature =
284     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
285   // special internal case when for XAO a selection list is filled
286   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectedShape, "XAO"));
287 }
288
289 void exportPart(const std::shared_ptr<ModelAPI_Document> & thePart,
290                 const std::string & theFilePath,
291                 const std::list<ModelHighAPI_Selection> & theSelected)
292 {
293   FeaturePtr aFeature = thePart->addFeature(ExchangePlugin_ExportPart::ID());
294   aFeature->string(ExchangePlugin_ExportPart::FILE_PATH_ID())->setValue(theFilePath);
295   if (!theSelected.empty()) {
296     fillAttribute(theSelected,
297         aFeature->selectionList(ExchangePlugin_ExportPart::SELECTION_LIST_ID()));
298   }
299   // restart transaction to execute and delete the macro-feature
300   apply();
301 }
302 //--------------------------------------------------------------------------------------