1 // Copyright (C) 2014-2023 CEA, EDF
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 "ExchangeAPI_Export.h"
21 //--------------------------------------------------------------------------------------
22 #include <ExchangePlugin_ExportPart.h>
23 //--------------------------------------------------------------------------------------
24 #include <Locale_Convert.h>
25 //--------------------------------------------------------------------------------------
26 #include <ModelAPI_Document.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelHighAPI_Tools.h>
29 #include <ModelHighAPI_Dumper.h>
30 #include <ModelHighAPI_Services.h>
31 #include <ModelHighAPI_Selection.h>
32 //--------------------------------------------------------------------------------------
34 //--------------------------------------------------------------------------------------
36 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature)
37 : ModelHighAPI_Interface(theFeature)
42 /// Constructor with values for XAO export.
43 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
44 const std::string & theFilePath,
45 const std::string & theAuthor,
46 const std::string & theGeometryName)
47 : ModelHighAPI_Interface(theFeature)
50 fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
51 fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID()));
52 fillAttribute(theAuthor, theFeature->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()));
53 fillAttribute(theGeometryName,
54 theFeature->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID()));
55 fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
57 apply(); // finish operation to make sure the export is done on the current state of the history
60 // Constructor with values for STL of selected result export.
61 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
62 const std::string & theFilePath,
63 const ModelHighAPI_Selection& theSelectedShape,
64 const ModelHighAPI_Double& theDeflectionRelative,
65 const ModelHighAPI_Double& theDeflectionAbsolute,
66 const bool theIsRelative,
67 const bool theIsASCII)
68 : ModelHighAPI_Interface(theFeature)
71 fillAttribute("STL", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
72 fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID()));
75 fillAttribute(ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE_RELATIVE(),
76 theFeature->string(ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE()) );
77 fillAttribute(theDeflectionRelative,
78 theFeature->real(ExchangePlugin_ExportFeature::STL_RELATIVE()) );
81 fillAttribute(ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE_ABSOLUTE(),
82 theFeature->string(ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE()) );
83 fillAttribute(theDeflectionAbsolute,
84 theFeature->real(ExchangePlugin_ExportFeature::STL_ABSOLUTE()) );
88 fillAttribute(ExchangePlugin_ExportFeature::STL_FILE_TYPE_ASCII(),
89 theFeature->string(ExchangePlugin_ExportFeature::STL_FILE_TYPE()));
93 fillAttribute(ExchangePlugin_ExportFeature::STL_FILE_TYPE_BINARY(),
94 theFeature->string(ExchangePlugin_ExportFeature::STL_FILE_TYPE()));
97 fillAttribute(theSelectedShape,
98 theFeature->selection(ExchangePlugin_ExportFeature::STL_OBJECT_SELECTED()));
99 fillAttribute("STL", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
101 apply(); // finish operation to make sure the export is done on the current state of the history
104 /// Constructor with values for XAO of selected result export.
105 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
106 const std::string & theFilePath,
107 const ModelHighAPI_Selection& theResult,
108 const std::string & theAuthor,
109 const std::string & theGeometryName)
110 : ModelHighAPI_Interface(theFeature)
113 fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
114 fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID()));
115 fillAttribute(theAuthor, theFeature->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()));
116 fillAttribute(theGeometryName,
117 theFeature->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID()));
118 fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
119 std::list<ModelHighAPI_Selection> aListOfOneSel;
120 aListOfOneSel.push_back(theResult);
121 fillAttribute(aListOfOneSel,
122 theFeature->selectionList(ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID()));
124 apply(); // finish operation to make sure the export is done on the current state of the history
127 /// Constructor with values for XAO of selected result export to memory buffer.
128 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
129 const ModelHighAPI_Selection& theResult,
130 const std::string & theAuthor,
131 const std::string & theGeometryName)
132 : ModelHighAPI_Interface(theFeature)
135 fillAttribute("XAOMem", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
136 fillAttribute(theAuthor, theFeature->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()));
137 fillAttribute(theGeometryName,
138 theFeature->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID()));
139 fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
140 std::list<ModelHighAPI_Selection> aListOfOneSel;
141 aListOfOneSel.push_back(theResult);
142 fillAttribute(aListOfOneSel,
143 theFeature->selectionList(ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID()));
145 apply(); // finish operation to make sure the export is done on the current state of the history
148 /// Constructor with values for export in other formats than XAO.
149 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
150 const std::string & theFilePath,
151 const std::list<ModelHighAPI_Selection> & theSelectionList,
152 const std::string & theFileFormat)
153 : ModelHighAPI_Interface(theFeature)
156 fillAttribute("Regular", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
157 fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::FILE_PATH_ID()));
158 fillAttribute(theSelectionList,
159 theFeature->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
160 fillAttribute(theFileFormat, theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
162 apply(); // finish operation to make sure the export is done on the current state of the history
165 ExchangeAPI_Export::~ExchangeAPI_Export()
169 // this method is needed on Windows because back-slashes in python may cause error
170 static void correctSeparators(std::string& thePath) {
171 // replace single "\" or triple "\\\" or more by double "\"
172 for (std::size_t aFind = thePath.find('\\'); aFind != std::string::npos;
173 aFind = thePath.find('\\', aFind)) {
175 std::size_t aFind2 = thePath.find('\\', aFind + 1);
176 if (aFind2 == std::string::npos || aFind2 > aFind + 1) { // single, so add one more
177 thePath.replace(aFind, 1, 2, '\\');
178 } else { // if there is more than double "\", remove them
179 for (aFind2 = thePath.find('\\', aFind2 + 1);
180 aFind2 != std::string::npos && aFind2 <= aFind + 2;
181 aFind2 = thePath.find('\\', aFind2)) {
182 thePath.erase(aFind2, 1);
189 void ExchangeAPI_Export::dump(ModelHighAPI_Dumper& theDumper) const
191 FeaturePtr aBase = feature();
192 const std::string& aDocName = theDumper.name(aBase->document());
194 std::string exportType = aBase->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID())->value();
196 if (exportType == "XAOMem") {
197 std::string aGeometryName =
198 aBase->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
200 theDumper << "aXAOBuff";
201 std::string aGeometryNamePy;
202 if (! aGeometryName.empty()) {
203 aGeometryNamePy = aGeometryName;
206 aGeometryNamePy = Locale::Convert::toString(aBase->data()->name());
208 if (! aGeometryNamePy.empty()) {
210 std::replace(aGeometryNamePy.begin(), aGeometryNamePy.end(), ' ', '_');
211 theDumper << "_" << aGeometryNamePy;
213 theDumper << " = model.exportToXAOMem(" << aDocName;
214 AttributeSelectionListPtr aShapeSelected =
215 aBase->selectionList(ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID());
216 if (aShapeSelected->isInitialized() && aShapeSelected->size() == 1) {
217 theDumper << ", " << aShapeSelected->value(0);
220 std::string theAuthor = aBase->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
221 if (! theAuthor.empty())
222 theDumper << ", '" << theAuthor << "'";
223 if (! aGeometryName.empty())
224 theDumper << ", '" << aGeometryName << "'";
225 theDumper << ")" << std::endl;
229 theDumper << aBase << " = model.";
231 if (exportType == "XAO") {
232 std::string aTmpXAOFile =
233 aBase->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value();
234 correctSeparators(aTmpXAOFile);
235 theDumper << "exportToXAO(" << aDocName << ", '" << aTmpXAOFile << "'" ;
236 AttributeSelectionListPtr aShapeSelected =
237 aBase->selectionList(ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID());
238 if (aShapeSelected->isInitialized() && aShapeSelected->size() == 1) {
239 theDumper<<", "<<aShapeSelected->value(0);
242 std::string theAuthor = aBase->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
243 if (! theAuthor.empty())
244 theDumper << ", '" << theAuthor << "'";
245 std::string theGeometryName =
246 aBase->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
247 if (! theGeometryName.empty())
248 theDumper << ", '" << theGeometryName << "'";
249 theDumper << ")" << std::endl;
251 else if (exportType == "STL") {
252 std::string aTmpSTLFile =
253 aBase->string(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID())->value();
254 correctSeparators(aTmpSTLFile);
255 theDumper << "exportToSTL(" << aDocName << ", '" << aTmpSTLFile << "'" ;
256 AttributeSelectionPtr aShapeSelected =
257 aBase->selection(ExchangePlugin_ExportFeature::STL_OBJECT_SELECTED());
259 theDumper<<","<< aShapeSelected;
261 theDumper <<","<< stlabsolute() <<","<< stlrelative();
263 if (stldeflectionType()->value()
264 == ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE_RELATIVE()){
265 theDumper <<","<< "True";
268 theDumper <<","<< "False";
271 if (stlfileType()->value() == ExchangePlugin_ExportFeature::STL_FILE_TYPE_BINARY()) {
272 theDumper << "False";
277 theDumper << ")" << std::endl;
280 std::string aFilePath = aBase->string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->value();
281 correctSeparators(aFilePath);
282 theDumper << "exportToFile(" << aDocName << ", \"" << aFilePath << "\", " <<
283 aBase->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
284 std::string theFileFormat =
285 aBase->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID())->value();
286 if (!theFileFormat.empty())
287 theDumper << ", '" << theFileFormat << "'";
288 theDumper << ")" << std::endl;
292 ExportPtr exportToFile(const std::shared_ptr<ModelAPI_Document> & thePart,
293 const std::string & theFilePath,
294 const std::list<ModelHighAPI_Selection> & theSelectionList,
295 const std::string & theFileFormat)
297 apply(); // finish previous operation to make sure all previous operations are done
298 std::shared_ptr<ModelAPI_Feature> aFeature =
299 thePart->addFeature(ExchangePlugin_ExportFeature::ID());
300 return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectionList, theFileFormat));
303 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
304 const std::string & theFilePath,
305 const std::string & theAuthor,
306 const std::string & theGeometryName)
308 apply(); // finish previous operation to make sure all previous operations are done
309 std::shared_ptr<ModelAPI_Feature> aFeature =
310 thePart->addFeature(ExchangePlugin_ExportFeature::ID());
311 return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theAuthor, theGeometryName));
314 ExportPtr exportToSTL(const std::shared_ptr<ModelAPI_Document> & thePart,
315 const std::string & theFilePath,
316 const ModelHighAPI_Selection& theSelectedShape,
317 const ModelHighAPI_Double& theDeflectionRelative,
318 const ModelHighAPI_Double& theDeflectionAbsolute,
319 const bool theIsRelative,
320 const bool theIsASCII)
322 apply(); // finish previous operation to make sure all previous operations are done
323 std::shared_ptr<ModelAPI_Feature> aFeature =
324 thePart->addFeature(ExchangePlugin_ExportFeature::ID());
326 return ExportPtr(new ExchangeAPI_Export(aFeature,
329 theDeflectionRelative,
330 theDeflectionAbsolute,
335 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
336 const std::string & theFilePath,
337 const ModelHighAPI_Selection& theSelectedShape,
338 const std::string & /*theAuthor*/,
339 const std::string & /*theGeometryName*/)
341 apply(); // finish previous operation to make sure all previous operations are done
342 std::shared_ptr<ModelAPI_Feature> aFeature =
343 thePart->addFeature(ExchangePlugin_ExportFeature::ID());
344 // special internal case when for XAO a selection list is filled
345 return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectedShape, "XAO"));
348 PyObject* exportToXAOMem(const std::shared_ptr<ModelAPI_Document> & thePart,
349 const ModelHighAPI_Selection& theSelectedShape,
350 const std::string & theAuthor,
351 const std::string & theGeometryName)
353 apply(); // finish previous operation to make sure all previous operations are done
354 std::shared_ptr<ModelAPI_Feature> aFeature =
355 thePart->addFeature(ExchangePlugin_ExportFeature::ID());
356 ExportPtr aXAOExportAPI (new ExchangeAPI_Export
357 (aFeature, theSelectedShape, theAuthor, theGeometryName));
358 std::string aBuff = aFeature->string(ExchangePlugin_ExportFeature::MEMORY_BUFFER_ID())->value();
359 return PyBytes_FromStringAndSize(aBuff.c_str(), aBuff.length());
362 void exportPart(const std::shared_ptr<ModelAPI_Document> & thePart,
363 const std::string & theFilePath,
364 const std::list<ModelHighAPI_Selection> & theSelected)
366 FeaturePtr aFeature = thePart->addFeature(ExchangePlugin_ExportPart::ID());
367 aFeature->string(ExchangePlugin_ExportPart::FILE_PATH_ID())->setValue(theFilePath);
368 if (!theSelected.empty()) {
369 fillAttribute(theSelected,
370 aFeature->selectionList(ExchangePlugin_ExportPart::SELECTION_LIST_ID()));
372 // restart transaction to execute and delete the macro-feature
375 //--------------------------------------------------------------------------------------