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 AttributeSelectionListPtr aShapeSelected =
131 aBase->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
132 if (aShapeSelected->isInitialized() && aShapeSelected->size() == 1) {
133 theDumper<<", "<<aShapeSelected->value(0);
136 std::string theAuthor = aBase->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
137 if (! theAuthor.empty())
138 theDumper << ", '" << theAuthor << "'";
139 std::string theGeometryName =
140 aBase->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
141 if (! theGeometryName.empty())
142 theDumper << ", '" << theGeometryName << "'";
143 theDumper << ")" << std::endl;
146 std::string aFilePath = aBase->string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->value();
147 correctSeparators(aFilePath);
148 theDumper << "exportToFile(" << aDocName << ", \"" << aFilePath << "\", " <<
149 aBase->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
150 std::string theFileFormat =
151 aBase->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID())->value();
152 if (!theFileFormat.empty())
153 theDumper << ", '" << theFileFormat << "'";
154 theDumper << ")" << std::endl;
158 ExportPtr exportToFile(const std::shared_ptr<ModelAPI_Document> & thePart,
159 const std::string & theFilePath,
160 const std::list<ModelHighAPI_Selection> & theSelectionList,
161 const std::string & theFileFormat)
163 apply(); // finish previous operation to make sure all previous operations are done
164 std::shared_ptr<ModelAPI_Feature> aFeature =
165 thePart->addFeature(ExchangePlugin_ExportFeature::ID());
166 return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectionList, theFileFormat));
169 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
170 const std::string & theFilePath,
171 const std::string & theAuthor,
172 const std::string & theGeometryName)
174 apply(); // finish previous operation to make sure all previous operations are done
175 std::shared_ptr<ModelAPI_Feature> aFeature =
176 thePart->addFeature(ExchangePlugin_ExportFeature::ID());
177 return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theAuthor, theGeometryName));
180 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
181 const std::string & theFilePath, const ModelHighAPI_Selection& theSelectedShape,
182 const std::string & theAuthor, const std::string & theGeometryName)
184 apply(); // finish previous operation to make sure all previous operations are done
185 std::shared_ptr<ModelAPI_Feature> aFeature =
186 thePart->addFeature(ExchangePlugin_ExportFeature::ID());
187 // special internal case when for XAO a selection list is filled
188 return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectedShape, "XAO"));
191 //--------------------------------------------------------------------------------------