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