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