1 // Copyright (C) 2014-2023 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 const ModelHighAPI_Double& theDeflectionRelative,
61 const ModelHighAPI_Double& theDeflectionAbsolute,
62 const bool theIsRelative,
63 const bool theIsASCII)
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(theDeflectionRelative,
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(theDeflectionAbsolute,
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,
94 theFeature->selection(ExchangePlugin_ExportFeature::STL_OBJECT_SELECTED()));
95 fillAttribute("STL", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
97 apply(); // finish operation to make sure the export is done on the current state of the history
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)
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()));
119 apply(); // finish operation to make sure the export is done on the current state of the history
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)
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()));
138 apply(); // finish operation to make sure the export is done on the current state of the history
141 ExchangeAPI_Export::~ExchangeAPI_Export()
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)) {
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);
165 void ExchangeAPI_Export::dump(ModelHighAPI_Dumper& theDumper) const
167 FeaturePtr aBase = feature();
168 const std::string& aDocName = theDumper.name(aBase->document());
170 theDumper << aBase << " = model.";
172 std::string exportType = aBase->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID())->value();
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);
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;
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());
202 theDumper<<","<< aShapeSelected;
204 theDumper <<","<< stlabsolute() <<","<< stlrelative();
206 if (stldeflectionType()->value()
207 == ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE_RELATIVE()){
208 theDumper <<","<< "True";
211 theDumper <<","<< "False";
214 if (stlfileType()->value() == ExchangePlugin_ExportFeature::STL_FILE_TYPE_BINARY()) {
215 theDumper << "False";
220 theDumper << ")" << std::endl;
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;
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)
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));
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)
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));
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)
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());
269 return ExportPtr(new ExchangeAPI_Export(aFeature,
272 theDeflectionRelative,
273 theDeflectionAbsolute,
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*/)
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"));
289 void exportPart(const std::shared_ptr<ModelAPI_Document> & thePart,
290 const std::string & theFilePath,
291 const std::list<ModelHighAPI_Selection> & theSelected)
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()));
299 // restart transaction to execute and delete the macro-feature
302 //--------------------------------------------------------------------------------------