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