Salome HOME
Use compressing for saving SHAPER files
[modules/shaper.git] / src / SHAPERGUI / SHAPERGUI_DataModel.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 "SHAPERGUI_DataModel.h"
22 #include "SHAPERGUI.h"
23
24 #include <XGUI_Workshop.h>
25
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ExchangePlugin_Dump.h>
29
30 #include <LightApp_Study.h>
31 #include <CAM_Application.h>
32 #include <CAM_DataObject.h>
33 #include <SUIT_Tools.h>
34 #include <SUIT_ResourceMgr.h>
35
36 #include <QFile>
37 #include <QDir>
38 #include <QTextStream>
39
40 #define DUMP_NAME "shaper_dump.py"
41
42
43 SHAPERGUI_DataModel::SHAPERGUI_DataModel(SHAPERGUI* theModule)
44     : LightApp_DataModel(theModule), myStudyPath(""), myModule(theModule)
45 {
46 }
47
48 SHAPERGUI_DataModel::~SHAPERGUI_DataModel()
49 {
50 }
51
52 bool SHAPERGUI_DataModel::open(const QString& thePath, CAM_Study* theStudy, QStringList theFiles)
53 {
54   LightApp_DataModel::open( thePath, theStudy, theFiles );
55   if (theFiles.size() == 0)
56     return false;
57
58   myStudyPath = thePath;
59
60   // If the file is Multi(contain all module files inside), the open SALOME functionality creates
61   // these files in a temporary directory. After the open functionality is finished, it removes
62   // these files (in the full SALOME mode).
63   // The postponed loading of the files is realized in the SHAPER module. So, it is important do
64   // not remove the opened files.
65   // The following code creates a new tmp directory with a copy of files.
66   QString aTmpDir = theFiles.first();
67
68   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( myModule->application()->activeStudy() );
69   QString aNewTmpDir = aStudy->GetTmpDir("", false).c_str();
70
71   bool isDone = true;
72   QDir aDir(aTmpDir);
73   QStringList aFiles = aDir.entryList(QDir::Files);
74   QStringList::const_iterator anIt = aFiles.begin(), aLast = aFiles.end();
75   for (; anIt != aLast; anIt++) {
76     QString aFileName = *anIt;
77
78     QString aCurrentFile = SUIT_Tools::addSlash(aTmpDir) + aFileName;
79     XGUI_Workshop* aWorkShop = myModule->workshop();
80     aWorkShop->openFile(aCurrentFile);
81     //QString aNewFile = SUIT_Tools::addSlash(aNewTmpDir) + aFileName;
82     //if (!QFile::copy(aCurrentFile, aNewFile))
83     //  isDone = false;
84   }
85   //if (isDone) {
86   //  myTmpDirectory = aNewTmpDir;
87   //}
88   //else {
89   //  removeDirectory(aNewTmpDir);
90   //  myTmpDirectory = "";
91   //}
92
93   //SessionPtr aMgr = ModelAPI_Session::get();
94   //aMgr->load(qPrintable(aNewTmpDir));
95
96   myModule->setIsOpened(true);
97   return true;
98 }
99
100 bool SHAPERGUI_DataModel::save(QStringList& theFiles)
101 {
102   LightApp_DataModel::save( theFiles );
103   XGUI_Workshop* aWorkShop = myModule->workshop();
104   std::list<std::string> aFileNames;
105
106   CAM_Application* anApp = myModule->application();
107   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(anApp->activeStudy());
108   SUIT_ResourceMgr* aResMgr = anApp->resourceMgr();
109
110   // it is important to check whether the file is saved in the multi-files mode in order to save
111   // files in temporary directories, which are removed in the full SALOME mode after copiying
112   // the files content in a result file.
113   bool isMultiFile = aResMgr ? aResMgr->booleanValue("Study", "multi_file", false) : false;
114
115   std::string aTmpDir = aStudy->GetTmpDir(qPrintable(myStudyPath), isMultiFile);
116   //std::string aTmpDir = aStudy->GetTmpDir("", false);//true );
117   QString aTmp = QString(aTmpDir.c_str());
118   theFiles.append(aTmp);
119
120   SessionPtr aMgr = ModelAPI_Session::get();
121   if (aMgr->isAutoUpdateBlocked())
122     aMgr->blockAutoUpdate(false);
123
124   //aWorkShop->saveDocument(QString(aTmpDir.c_str()), aFileNames);
125   aWorkShop->setCurrentDataFile(aTmp + "shaper.opp");
126   aWorkShop->onSave();
127   QString aName = aWorkShop->currentDataFile();
128   std::string aa = aName.toStdString();
129   aName.replace(QChar('\\'), QChar('/'));
130   int aN = aName.lastIndexOf('/');
131   theFiles.append(aName.right(aName.length() - aN - 1));
132
133   //std::list<std::string>::iterator aIt;
134   //for (aIt = aFileNames.begin(); aIt != aFileNames.end(); ++aIt) {
135   //  QString aName((*aIt).c_str());
136   //  aName.replace(QChar('\\'), QChar('/'));
137   //  int aN = aName.lastIndexOf('/');
138   //  theFiles.append(aName.right(aName.length() - aN - 1));
139   //}
140   return true;
141 }
142
143 bool SHAPERGUI_DataModel::saveAs(const QString& thePath, CAM_Study* theStudy, QStringList& theFiles)
144 {
145   myStudyPath = thePath;
146   return save(theFiles);
147 }
148
149 bool SHAPERGUI_DataModel::close()
150 {
151   myModule->workshop()->closeDocument();
152   //removeDirectory(myTmpDirectory);
153   //myTmpDirectory = "";
154   return LightApp_DataModel::close();
155 }
156
157 bool SHAPERGUI_DataModel::create(CAM_Study* theStudy)
158 {
159   return true;
160 }
161
162 bool SHAPERGUI_DataModel::isModified() const
163 {
164   SessionPtr aMgr = ModelAPI_Session::get();
165   return aMgr->isModified();
166 }
167
168 bool SHAPERGUI_DataModel::isSaved() const
169 {
170   return !isModified();
171 }
172
173 void SHAPERGUI_DataModel::update(LightApp_DataObject* theObj, LightApp_Study* theStudy)
174 {
175   // Nothing to do here: we always keep the data tree in the up-to-date state
176   // The only goal of this method is to hide default behavior from LightApp_DataModel
177   return;
178 }
179
180 void SHAPERGUI_DataModel::initRootObject()
181 {
182   LightApp_Study* study = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
183   CAM_ModuleObject *aModelRoot = dynamic_cast<CAM_ModuleObject*>(root());
184   if(study && aModelRoot == NULL) {
185     aModelRoot = createModuleObject( study->root() );
186     aModelRoot->setDataModel( this );
187     setRoot(aModelRoot);
188   }
189 }
190
191 void SHAPERGUI_DataModel::removeDirectory(const QString& theDirectoryName)
192 {
193   Qtx::rmDir(theDirectoryName);
194 }
195
196 bool SHAPERGUI_DataModel::dumpPython(const QString& thePath, CAM_Study* theStudy,
197                                      bool isMultiFile,  QStringList& theListOfFiles)
198 {
199   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(theStudy);
200   if (!aStudy)
201     return false;
202
203   std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
204   ModelAPI_Session::get()->startOperation(ExchangePlugin_Dump::ID());
205   FeaturePtr aFeature = aDoc->addFeature(ExchangePlugin_Dump::ID());
206   if (aFeature.get()) {
207     std::string aTmpDir = aStudy->GetTmpDir(thePath.toStdString().c_str(), isMultiFile);
208     std::string aFileName = aTmpDir + DUMP_NAME;
209
210     if (QFile::exists(aFileName.c_str())) {
211       QFile::remove(aFileName.c_str());
212     }
213
214     AttributeStringPtr aAttr = aFeature->string(ExchangePlugin_Dump::FILE_PATH_ID());
215     if (aAttr.get())
216       aAttr->setValue(aFileName);
217
218     aAttr = aFeature->string(ExchangePlugin_Dump::FILE_FORMAT_ID());
219     if (aAttr.get())
220       aAttr->setValue(".py");
221
222     AttributeStringPtr aTypeAttr = aFeature->string(ExchangePlugin_Dump::SELECTION_TYPE_ID());
223     if (aTypeAttr.get())
224       aTypeAttr->setValue(ExchangePlugin_Dump::TOPOLOGICAL_NAMING_DUMP_ID());
225
226     ModelAPI_Session::get()->finishOperation();
227
228     if (QFile::exists(aFileName.c_str())) {
229       if (isMultiFile) {
230         QFile aInFile(aFileName.c_str());
231         if (!aInFile.open(QIODevice::ReadOnly | QIODevice::Text))
232           return false;
233         QTextStream aText(&aInFile);
234         QString aTrace(aText.readAll());
235         aInFile.close();
236
237         QStringList aBuffer;
238         aBuffer.push_back(QString("def RebuildData( theStudy ):"));
239         QStringList aList(aTrace.split("\n"));
240         foreach(QString aStr, aList) {
241           QString s = "  " + aStr;
242           aBuffer.push_back(s);
243         }
244         aTrace = aBuffer.join("\n");
245
246         QFile aOutFile(aFileName.c_str());
247         if (!aOutFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
248           return false;
249
250         QTextStream aOut(&aOutFile);
251         aOut << aTrace.toStdString().c_str() << "\n";
252         aOut.flush();
253         aOutFile.close();
254       }
255       theListOfFiles.append(aTmpDir.c_str());
256       theListOfFiles.append(DUMP_NAME);
257       return true;
258     }
259   }
260   return false;
261 }
262