Salome HOME
Task 5.1.7: To be able to export a part to a file and import it into an existing...
[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 <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 //--------------------------------------------------------------------------------------
31
32 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature)
33 : ModelHighAPI_Interface(theFeature)
34 {
35   initialize();
36 }
37
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)
44 {
45   initialize();
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()));
52   execute();
53   apply(); // finish operation to make sure the export is done on the current state of the history
54 }
55
56 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
57   const std::string & theFilePath, const ModelHighAPI_Selection& theResult,
58   const std::string & theAuthor, const std::string & theGeometryName)
59   : ModelHighAPI_Interface(theFeature)
60 {
61   initialize();
62   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
63   fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID()));
64   fillAttribute(theAuthor, theFeature->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()));
65   fillAttribute(theGeometryName,
66     theFeature->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID()));
67   fillAttribute("XAO", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
68   std::list<ModelHighAPI_Selection> aListOfOneSel;
69   aListOfOneSel.push_back(theResult);
70   fillAttribute(aListOfOneSel,
71     theFeature->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
72   execute();
73   apply(); // finish operation to make sure the export is done on the current state of the history
74 }
75
76
77 /// Constructor with values for export in other formats than XAO.
78 ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr<ModelAPI_Feature>& theFeature,
79                               const std::string & theFilePath,
80                               const std::list<ModelHighAPI_Selection> & theSelectionList,
81                               const std::string & theFileFormat)
82 : ModelHighAPI_Interface(theFeature)
83 {
84   initialize();
85   fillAttribute("Regular", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID()));
86   fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::FILE_PATH_ID()));
87   fillAttribute(theSelectionList,
88                 theFeature->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
89   fillAttribute(theFileFormat, theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()));
90   execute();
91   apply(); // finish operation to make sure the export is done on the current state of the history
92 }
93
94 ExchangeAPI_Export::~ExchangeAPI_Export()
95 {
96 }
97
98 // this method is needed on Windows because back-slashes in python may cause error
99 static void correctSeparators(std::string& thePath) {
100   // replace single "\" or triple "\\\" or more by double "\"
101   for (std::size_t aFind = thePath.find('\\'); aFind != std::string::npos;
102     aFind = thePath.find('\\', aFind)) {
103     // search the next
104     std::size_t aFind2 = thePath.find('\\', aFind + 1);
105     if (aFind2 == std::string::npos || aFind2 > aFind + 1) { // single, so add one more
106       thePath.replace(aFind, 1, 2, '\\');
107     } else { // if there is more than double "\", remove them
108       for (aFind2 = thePath.find('\\', aFind2 + 1);
109            aFind2 != std::string::npos && aFind2 <= aFind + 2;
110            aFind2 = thePath.find('\\', aFind2)) {
111         thePath.erase(aFind2, 1);
112       }
113     }
114     aFind += 2;
115   }
116 }
117
118 void ExchangeAPI_Export::dump(ModelHighAPI_Dumper& theDumper) const
119 {
120   FeaturePtr aBase = feature();
121   const std::string& aDocName = theDumper.name(aBase->document());
122
123   theDumper << aBase << " = model.";
124
125   std::string exportType = aBase->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID())->value();
126
127   if (exportType == "XAO") {
128     std::string aTmpXAOFile =
129                 aBase->string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value();
130     correctSeparators(aTmpXAOFile);
131     theDumper << "exportToXAO(" << aDocName << ", '" << aTmpXAOFile << "'" ;
132     AttributeSelectionListPtr aShapeSelected =
133       aBase->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
134     if (aShapeSelected->isInitialized() && aShapeSelected->size() == 1) {
135       theDumper<<", "<<aShapeSelected->value(0);
136     }
137
138     std::string theAuthor = aBase->string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
139     if (! theAuthor.empty())
140       theDumper << ", '" << theAuthor << "'";
141     std::string theGeometryName =
142                 aBase->string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
143     if (! theGeometryName.empty())
144       theDumper << ", '" << theGeometryName << "'";
145     theDumper << ")" << std::endl;
146   }
147   else {
148     std::string aFilePath = aBase->string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->value();
149     correctSeparators(aFilePath);
150       theDumper << "exportToFile(" << aDocName << ", \"" << aFilePath << "\", " <<
151       aBase->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
152     std::string theFileFormat =
153       aBase->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID())->value();
154     if (!theFileFormat.empty())
155       theDumper << ", '" << theFileFormat << "'";
156     theDumper << ")" << std::endl;
157   }
158 }
159
160 ExportPtr exportToFile(const std::shared_ptr<ModelAPI_Document> & thePart,
161                   const std::string & theFilePath,
162                   const std::list<ModelHighAPI_Selection> & theSelectionList,
163                   const std::string & theFileFormat)
164 {
165   apply(); // finish previous operation to make sure all previous operations are done
166   std::shared_ptr<ModelAPI_Feature> aFeature =
167     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
168   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectionList, theFileFormat));
169 }
170
171 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
172                  const std::string & theFilePath,
173                  const std::string & theAuthor,
174                  const std::string & theGeometryName)
175 {
176   apply(); // finish previous operation to make sure all previous operations are done
177   std::shared_ptr<ModelAPI_Feature> aFeature =
178     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
179   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theAuthor, theGeometryName));
180 }
181
182 ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
183   const std::string & theFilePath, const ModelHighAPI_Selection& theSelectedShape,
184   const std::string & theAuthor, const std::string & theGeometryName)
185 {
186   apply(); // finish previous operation to make sure all previous operations are done
187   std::shared_ptr<ModelAPI_Feature> aFeature =
188     thePart->addFeature(ExchangePlugin_ExportFeature::ID());
189   // special internal case when for XAO a selection list is filled
190   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectedShape, "XAO"));
191 }
192
193 void exportPart(const std::shared_ptr<ModelAPI_Document> & thePart,
194                 const std::string & theFilePath,
195                 const std::list<ModelHighAPI_Selection> & theSelected)
196 {
197   FeaturePtr aFeature = thePart->addFeature(ExchangePlugin_ExportPart::ID());
198   aFeature->string(ExchangePlugin_ExportPart::FILE_PATH_ID())->setValue(theFilePath);
199   if (!theSelected.empty()) {
200     fillAttribute(theSelected,
201         aFeature->selectionList(ExchangePlugin_ExportPart::SELECTION_LIST_ID()));
202   }
203   // restart transaction to execute and delete the marcro-feature
204   apply();
205 }
206 //--------------------------------------------------------------------------------------