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