1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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 <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 //--------------------------------------------------------------------------------------
32 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature)
33 : ModelHighAPI_Interface(theFeature)
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)
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()));
53 apply(); // finish operation to make sure the export is done on the current state of the history
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,
64 : ModelHighAPI_Interface(theFeature)
67 fillAttribute("STL", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
68 fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID()));
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()) );
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()) );
84 fillAttribute(ExchangePlugin_ExportFeature::STL_FILE_TYPE_ASCII(),
85 theFeature->string(ExchangePlugin_ExportFeature::STL_FILE_TYPE()));
89 fillAttribute(ExchangePlugin_ExportFeature::STL_FILE_TYPE_BINARY(),
90 theFeature->string(ExchangePlugin_ExportFeature::STL_FILE_TYPE()));
93 fillAttribute(theSelectedShape,theFeature->selection(ExchangePlugin_ExportFeature::STL_OBJECT_SELECTED()));
94 fillAttribute("STL", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
96 apply(); // finish operation to make sure the export is done on the current state of the history
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)
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()));
118 apply(); // finish operation to make sure the export is done on the current state of the history
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)
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()));
137 apply(); // finish operation to make sure the export is done on the current state of the history
140 ExchangeAPI_Export::~ExchangeAPI_Export()
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)) {
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);
164 void ExchangeAPI_Export::dump(ModelHighAPI_Dumper& theDumper) const
166 FeaturePtr aBase = feature();
167 const std::string& aDocName = theDumper.name(aBase->document());
169 theDumper << aBase << " = model.";
171 std::string exportType = aBase->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID())->value();
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);
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;
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());
201 theDumper<<","<< aShapeSelected;
203 theDumper <<","<< stlabsolute() <<","<< stlrelative();
205 if (stldeflectionType()->value() == ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE_RELATIVE()) {
206 theDumper <<","<< "True";
209 theDumper <<","<< "False";
212 if (stlfileType()->value() == ExchangePlugin_ExportFeature::STL_FILE_TYPE_BINARY()) {
213 theDumper << "False";
218 theDumper << ")" << std::endl;
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;
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)
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));
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)
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));
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)
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());
267 return ExportPtr(new ExchangeAPI_Export(aFeature,
270 theDeflectionRelative,
271 theDeflectionAbsolute,
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*/)
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"));
287 void exportPart(const std::shared_ptr<ModelAPI_Document> & thePart,
288 const std::string & theFilePath,
289 const std::list<ModelHighAPI_Selection> & theSelected)
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()));
297 // restart transaction to execute and delete the macro-feature
300 //--------------------------------------------------------------------------------------