Salome HOME
Issue #2683: Add selection type to Dump feature
[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     QString aNewFile = SUIT_Tools::addSlash(aNewTmpDir) + aFileName;
80     if (!QFile::copy(aCurrentFile, aNewFile))
81       isDone = false;
82   }
83   if (isDone) {
84     myTmpDirectory = aNewTmpDir;
85   }
86   else {
87     removeDirectory(aNewTmpDir);
88     myTmpDirectory = "";
89   }
90
91   SessionPtr aMgr = ModelAPI_Session::get();
92   aMgr->load(qPrintable(aNewTmpDir));
93   myModule->setIsOpened(true);
94   return true;
95 }
96
97 bool SHAPERGUI_DataModel::save(QStringList& theFiles)
98 {
99   LightApp_DataModel::save( theFiles );
100   XGUI_Workshop* aWorkShop = myModule->workshop();
101   std::list<std::string> aFileNames;
102
103   CAM_Application* anApp = myModule->application();
104   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(anApp->activeStudy());
105   SUIT_ResourceMgr* aResMgr = anApp->resourceMgr();
106
107   // it is important to check whether the file is saved in the multi-files mode in order to save
108   // files in temporary directories, which are removed in the full SALOME mode after copiying
109   // the files content in a result file.
110   bool isMultiFile = aResMgr ? aResMgr->booleanValue("Study", "multi_file", false) : false;
111
112   std::string aTmpDir = aStudy->GetTmpDir(qPrintable(myStudyPath), isMultiFile);
113   //std::string aTmpDir = aStudy->GetTmpDir("", false);//true );
114   theFiles.append(QString(aTmpDir.c_str()));
115
116   SessionPtr aMgr = ModelAPI_Session::get();
117   if (aMgr->isAutoUpdateBlocked())
118     aMgr->blockAutoUpdate(false);
119
120   aWorkShop->saveDocument(QString(aTmpDir.c_str()), aFileNames);
121   std::list<std::string>::iterator aIt;
122   for (aIt = aFileNames.begin(); aIt != aFileNames.end(); ++aIt) {
123     QString aName((*aIt).c_str());
124     aName.replace(QChar('\\'), QChar('/'));
125     int aN = aName.lastIndexOf('/');
126     theFiles.append(aName.right(aName.length() - aN - 1));
127   }
128   return true;
129 }
130
131 bool SHAPERGUI_DataModel::saveAs(const QString& thePath, CAM_Study* theStudy, QStringList& theFiles)
132 {
133   myStudyPath = thePath;
134   return save(theFiles);
135 }
136
137 bool SHAPERGUI_DataModel::close()
138 {
139   myModule->workshop()->closeDocument();
140   removeDirectory(myTmpDirectory);
141   myTmpDirectory = "";
142   return LightApp_DataModel::close();
143 }
144
145 bool SHAPERGUI_DataModel::create(CAM_Study* theStudy)
146 {
147   return true;
148 }
149
150 bool SHAPERGUI_DataModel::isModified() const
151 {
152   SessionPtr aMgr = ModelAPI_Session::get();
153   return aMgr->isModified();
154 }
155
156 bool SHAPERGUI_DataModel::isSaved() const
157 {
158   return !isModified();
159 }
160
161 void SHAPERGUI_DataModel::update(LightApp_DataObject* theObj, LightApp_Study* theStudy)
162 {
163   // Nothing to do here: we always keep the data tree in the up-to-date state
164   // The only goal of this method is to hide default behavior from LightApp_DataModel
165   return;
166 }
167
168 void SHAPERGUI_DataModel::initRootObject()
169 {
170   LightApp_Study* study = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
171   CAM_ModuleObject *aModelRoot = dynamic_cast<CAM_ModuleObject*>(root());
172   if(study && aModelRoot == NULL) {
173     aModelRoot = createModuleObject( study->root() );
174     aModelRoot->setDataModel( this );
175     setRoot(aModelRoot);
176   }
177 }
178
179 void SHAPERGUI_DataModel::removeDirectory(const QString& theDirectoryName)
180 {
181   Qtx::rmDir(theDirectoryName);
182 }
183
184 bool SHAPERGUI_DataModel::dumpPython(const QString& thePath, CAM_Study* theStudy,
185                                      bool isMultiFile,  QStringList& theListOfFiles)
186 {
187   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(theStudy);
188   if (!aStudy)
189     return false;
190
191   std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
192   ModelAPI_Session::get()->startOperation(ExchangePlugin_Dump::ID());
193   FeaturePtr aFeature = aDoc->addFeature(ExchangePlugin_Dump::ID());
194   if (aFeature.get()) {
195     std::string aTmpDir = aStudy->GetTmpDir(thePath.toStdString().c_str(), isMultiFile);
196     std::string aFileName = aTmpDir + DUMP_NAME;
197
198     if (QFile::exists(aFileName.c_str())) {
199       QFile::remove(aFileName.c_str());
200     }
201
202     AttributeStringPtr aAttr = aFeature->string(ExchangePlugin_Dump::FILE_PATH_ID());
203     if (aAttr.get())
204       aAttr->setValue(aFileName);
205
206     aAttr = aFeature->string(ExchangePlugin_Dump::FILE_FORMAT_ID());
207     if (aAttr.get())
208       aAttr->setValue(".py");
209
210     AttributeStringPtr aTypeAttr = aFeature->string(ExchangePlugin_Dump::SELECTION_TYPE_ID());
211     if (aTypeAttr.get())
212       aTypeAttr->setValue(ExchangePlugin_Dump::TOPOLOGICAL_NAMING_DUMP_ID());
213
214     ModelAPI_Session::get()->finishOperation();
215
216     if (QFile::exists(aFileName.c_str())) {
217       if (isMultiFile) {
218         QFile aInFile(aFileName.c_str());
219         if (!aInFile.open(QIODevice::ReadOnly | QIODevice::Text))
220           return false;
221         QTextStream aText(&aInFile);
222         QString aTrace(aText.readAll());
223         aInFile.close();
224
225         QStringList aBuffer;
226         aBuffer.push_back(QString("def RebuildData( theStudy ):"));
227         QStringList aList(aTrace.split("\n"));
228         foreach(QString aStr, aList) {
229           QString s = "  " + aStr;
230           aBuffer.push_back(s);
231         }
232         aTrace = aBuffer.join("\n");
233
234         QFile aOutFile(aFileName.c_str());
235         if (!aOutFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
236           return false;
237
238         QTextStream aOut(&aOutFile);
239         aOut << aTrace.toStdString().c_str() << "\n";
240         aOut.flush();
241         aOutFile.close();
242       }
243       theListOfFiles.append(aTmpDir.c_str());
244       theListOfFiles.append(DUMP_NAME);
245       return true;
246     }
247   }
248   return false;
249 }
250