Salome HOME
Update copyrights
[modules/shaper.git] / src / ExchangeAPI / ExchangeAPI_Export.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
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 //--------------------------------------------------------------------------------------
28
29 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature)
30 : ModelHighAPI_Interface(theFeature)
31 {
32   initialize();
33 }
34
35 /// Constructor with values for XAO export.
36 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
37                               const std::string & theFilePath,
38                               const std::string & theAuthor,
39                               const std::string & theGeometryName)
40 : ModelHighAPI_Interface(theFeature)
41 {
42   initialize();
43   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
44   fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID()));
45   fillAttribute(theAuthor, theFeature->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()));
46   fillAttribute(theGeometryName,
47                 theFeature->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID()));
48   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
49   execute();
50   apply(); // finish operation to make sure the export is done on the current state of the history
51 }
52
53 /// Constructor with values for export in other formats than XAO.
54 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
55                               const std::string & theFilePath,
56                               const std::list<ModelHighAPI_Selection> & theSelectionList,
57                               const std::string & theFileFormat)
58 : ModelHighAPI_Interface(theFeature)
59 {
60   initialize();
61   fillAttribute("Regular", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
62   fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::FILE_PATH_ID()));
63   fillAttribute(theSelectionList,
64                 theFeature->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
65   fillAttribute(theFileFormat, theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
66   execute();
67   apply(); // finish operation to make sure the export is done on the current state of the history
68 }
69
70 ExchangeAPI_Export::~ExchangeAPI_Export()
71 {
72 }
73
74 // this method is needed on Windows because back-slashes in python may cause error
75 static void correctSeparators(std::string& thePath) {
76   // replace single "\" or triple "\\\" or more by double "\"
77   for (std::size_t aFind = thePath.find('\\'); aFind != std::string::npos;
78     aFind = thePath.find('\\', aFind)) {
79     // search the next
80     std::size_t aFind2 = thePath.find('\\', aFind + 1);
81     if (aFind2 == std::string::npos || aFind2 > aFind + 1) { // single, so add one more
82       thePath.replace(aFind, 1, 2, '\\');
83     } else { // if there is more than double "\", remove them
84       for (aFind2 = thePath.find('\\', aFind2 + 1);
85            aFind2 != std::string::npos && aFind2 <= aFind + 2;
86            aFind2 = thePath.find('\\', aFind2)) {
87         thePath.erase(aFind2, 1);
88       }
89     }
90     aFind += 2;
91   }
92 }
93
94 void ExchangeAPI_Export::dump(ModelHighAPI_Dumper& theDumper) const
95 {
96   FeaturePtr aBase = feature();
97   const std::string& aDocName = theDumper.name(aBase->document());
98
99   theDumper << aBase << " = model.";
100
101   std::string exportType = aBase->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID())->value();
102
103   if (exportType == "XAO") {
104     std::string aTmpXAOFile =
105                 aBase->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value();
106     correctSeparators(aTmpXAOFile);
107     theDumper << "exportToXAO(" << aDocName << ", '" << aTmpXAOFile << "'" ;
108     std::string theAuthor = aBase->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
109     if (! theAuthor.empty())
110       theDumper << ", '" << theAuthor << "'";
111     std::string theGeometryName =
112                 aBase->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
113     if (! theGeometryName.empty())
114       theDumper << ", '" << theGeometryName << "'";
115     theDumper << ")" << std::endl;
116   }
117   else {
118     std::string aFilePath = aBase->string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->value();
119     correctSeparators(aFilePath);
120       theDumper << "exportToFile(" << aDocName << ", \"" << aFilePath << "\", " <<
121       aBase->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
122     std::string theFileFormat =
123       aBase->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID())->value();
124     if (!theFileFormat.empty())
125       theDumper << ", '" << theFileFormat << "'";
126     theDumper << ")" << std::endl;
127   }
128 }
129
130 ExportPtr exportToFile(const std::shared_ptr<ModelAPI_Document> & thePart,
131                   const std::string & theFilePath,
132                   const std::list<ModelHighAPI_Selection> & theSelectionList,
133                   const std::string & theFileFormat)
134 {
135   apply(); // finish previous operation to make sure all previous operations are done
136   std::shared_ptr<ModelAPI_Feature> aFeature =
137     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
138   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectionList, theFileFormat));
139 }
140
141 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
142                  const std::string & theFilePath,
143                  const std::string & theAuthor,
144                  const std::string & theGeometryName)
145 {
146   apply(); // finish previous operation to make sure all previous operations are done
147   std::shared_ptr<ModelAPI_Feature> aFeature =
148     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
149   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theAuthor, theGeometryName));
150 }
151
152 //--------------------------------------------------------------------------------------