1 // Copyright (C) 2014-2019 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 <ModelAPI_Document.h>
23 #include <ModelAPI_Feature.h>
24 #include <ModelHighAPI_Tools.h>
25 #include <ModelHighAPI_Dumper.h>
26 #include <ModelHighAPI_Services.h>
27 #include <ModelHighAPI_Selection.h>
28 //--------------------------------------------------------------------------------------
30 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature)
31 : ModelHighAPI_Interface(theFeature)
36 /// Constructor with values for XAO export.
37 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
38 const std::string & theFilePath,
39 const std::string & theAuthor,
40 const std::string & theGeometryName)
41 : ModelHighAPI_Interface(theFeature)
44 fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
45 fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID()));
46 fillAttribute(theAuthor, theFeature->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()));
47 fillAttribute(theGeometryName,
48 theFeature->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID()));
49 fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
51 apply(); // finish operation to make sure the export is done on the current state of the history
54 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
55 const std::string & theFilePath, const ModelHighAPI_Selection& theResult,
56 const std::string & theAuthor, const std::string & theGeometryName)
57 : ModelHighAPI_Interface(theFeature)
60 fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
61 fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID()));
62 fillAttribute(theAuthor, theFeature->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()));
63 fillAttribute(theGeometryName,
64 theFeature->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID()));
65 fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
66 std::list<ModelHighAPI_Selection> aListOfOneSel;
67 aListOfOneSel.push_back(theResult);
68 fillAttribute(aListOfOneSel,
69 theFeature->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
71 apply(); // finish operation to make sure the export is done on the current state of the history
75 /// Constructor with values for export in other formats than XAO.
76 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
77 const std::string & theFilePath,
78 const std::list<ModelHighAPI_Selection> & theSelectionList,
79 const std::string & theFileFormat)
80 : ModelHighAPI_Interface(theFeature)
83 fillAttribute("Regular", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
84 fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::FILE_PATH_ID()));
85 fillAttribute(theSelectionList,
86 theFeature->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
87 fillAttribute(theFileFormat, theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
89 apply(); // finish operation to make sure the export is done on the current state of the history
92 ExchangeAPI_Export::~ExchangeAPI_Export()
96 // this method is needed on Windows because back-slashes in python may cause error
97 static void correctSeparators(std::string& thePath) {
98 // replace single "\" or triple "\\\" or more by double "\"
99 for (std::size_t aFind = thePath.find('\\'); aFind != std::string::npos;
100 aFind = thePath.find('\\', aFind)) {
102 std::size_t aFind2 = thePath.find('\\', aFind + 1);
103 if (aFind2 == std::string::npos || aFind2 > aFind + 1) { // single, so add one more
104 thePath.replace(aFind, 1, 2, '\\');
105 } else { // if there is more than double "\", remove them
106 for (aFind2 = thePath.find('\\', aFind2 + 1);
107 aFind2 != std::string::npos && aFind2 <= aFind + 2;
108 aFind2 = thePath.find('\\', aFind2)) {
109 thePath.erase(aFind2, 1);
116 void ExchangeAPI_Export::dump(ModelHighAPI_Dumper& theDumper) const
118 FeaturePtr aBase = feature();
119 const std::string& aDocName = theDumper.name(aBase->document());
121 theDumper << aBase << " = model.";
123 std::string exportType = aBase->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID())->value();
125 if (exportType == "XAO") {
126 std::string aTmpXAOFile =
127 aBase->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value();
128 correctSeparators(aTmpXAOFile);
129 theDumper << "exportToXAO(" << aDocName << ", '" << aTmpXAOFile << "'" ;
130 std::string theAuthor = aBase->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
131 if (! theAuthor.empty())
132 theDumper << ", '" << theAuthor << "'";
133 std::string theGeometryName =
134 aBase->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
135 if (! theGeometryName.empty())
136 theDumper << ", '" << theGeometryName << "'";
137 theDumper << ")" << std::endl;
140 std::string aFilePath = aBase->string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->value();
141 correctSeparators(aFilePath);
142 theDumper << "exportToFile(" << aDocName << ", \"" << aFilePath << "\", " <<
143 aBase->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
144 std::string theFileFormat =
145 aBase->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID())->value();
146 if (!theFileFormat.empty())
147 theDumper << ", '" << theFileFormat << "'";
148 theDumper << ")" << std::endl;
152 ExportPtr exportToFile(const std::shared_ptr<ModelAPI_Document> & thePart,
153 const std::string & theFilePath,
154 const std::list<ModelHighAPI_Selection> & theSelectionList,
155 const std::string & theFileFormat)
157 apply(); // finish previous operation to make sure all previous operations are done
158 std::shared_ptr<ModelAPI_Feature> aFeature =
159 thePart->addFeature(ExchangePlugin_ExportFeature::ID());
160 return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectionList, theFileFormat));
163 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
164 const std::string & theFilePath,
165 const std::string & theAuthor,
166 const std::string & theGeometryName)
168 apply(); // finish previous operation to make sure all previous operations are done
169 std::shared_ptr<ModelAPI_Feature> aFeature =
170 thePart->addFeature(ExchangePlugin_ExportFeature::ID());
171 return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theAuthor, theGeometryName));
174 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
175 const std::string & theFilePath, const ModelHighAPI_Selection& theSelectedShape,
176 const std::string & theAuthor, const std::string & theGeometryName)
178 apply(); // finish previous operation to make sure all previous operations are done
179 std::shared_ptr<ModelAPI_Feature> aFeature =
180 thePart->addFeature(ExchangePlugin_ExportFeature::ID());
181 // special internal case when for XAO a selection list is filled
182 return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectedShape, "XAO"));
185 //--------------------------------------------------------------------------------------