1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SHAPERGUI_DataModel.h"
22 #include "SHAPERGUI.h"
24 #include <XGUI_Workshop.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ExchangePlugin_Dump.h>
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>
38 #include <QTextStream>
40 #define DUMP_NAME "shaper_dump.py"
43 SHAPERGUI_DataModel::SHAPERGUI_DataModel(SHAPERGUI* theModule)
44 : LightApp_DataModel(theModule), myStudyPath(""), myModule(theModule)
48 SHAPERGUI_DataModel::~SHAPERGUI_DataModel()
52 bool SHAPERGUI_DataModel::open(const QString& thePath, CAM_Study* theStudy, QStringList theFiles)
54 LightApp_DataModel::open( thePath, theStudy, theFiles );
55 if (theFiles.size() == 0)
58 myStudyPath = thePath;
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();
68 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( myModule->application()->activeStudy() );
69 QString aNewTmpDir = aStudy->GetTmpDir("", false).c_str();
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;
78 QString aCurrentFile = SUIT_Tools::addSlash(aTmpDir) + aFileName;
79 XGUI_Workshop* aWorkShop = myModule->workshop();
80 aWorkShop->openFile(aCurrentFile);
83 myModule->setIsOpened(true);
87 bool SHAPERGUI_DataModel::save(QStringList& theFiles)
89 LightApp_DataModel::save( theFiles );
90 XGUI_Workshop* aWorkShop = myModule->workshop();
91 std::list<std::string> aFileNames;
93 CAM_Application* anApp = myModule->application();
94 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(anApp->activeStudy());
95 SUIT_ResourceMgr* aResMgr = anApp->resourceMgr();
97 // it is important to check whether the file is saved in the multi-files mode in order to save
98 // files in temporary directories, which are removed in the full SALOME mode after copiying
99 // the files content in a result file.
100 bool isMultiFile = aResMgr ? aResMgr->booleanValue("Study", "multi_file", false) : false;
102 std::string aTmpDir = aStudy->GetTmpDir(qPrintable(myStudyPath), isMultiFile);
103 QString aTmp = QString(aTmpDir.c_str());
104 theFiles.append(aTmp);
106 SessionPtr aMgr = ModelAPI_Session::get();
107 if (aMgr->isAutoUpdateBlocked())
108 aMgr->blockAutoUpdate(false);
110 //aWorkShop->saveDocument(QString(aTmpDir.c_str()), aFileNames);
111 aWorkShop->setCurrentDataFile(aTmp + "shaper.shaper");
113 QString aName = aWorkShop->currentDataFile();
114 aName.replace(QChar('\\'), QChar('/'));
115 int aN = aName.lastIndexOf('/');
116 theFiles.append(aName.right(aName.length() - aN - 1));
121 bool SHAPERGUI_DataModel::saveAs(const QString& thePath, CAM_Study* theStudy, QStringList& theFiles)
123 myStudyPath = thePath;
124 return save(theFiles);
127 bool SHAPERGUI_DataModel::close()
129 myModule->workshop()->closeDocument();
130 return LightApp_DataModel::close();
133 bool SHAPERGUI_DataModel::create(CAM_Study* theStudy)
138 bool SHAPERGUI_DataModel::isModified() const
140 SessionPtr aMgr = ModelAPI_Session::get();
141 return aMgr->isModified();
144 bool SHAPERGUI_DataModel::isSaved() const
146 return !isModified();
149 void SHAPERGUI_DataModel::update(LightApp_DataObject* theObj, LightApp_Study* theStudy)
151 // Nothing to do here: we always keep the data tree in the up-to-date state
152 // The only goal of this method is to hide default behavior from LightApp_DataModel
156 void SHAPERGUI_DataModel::initRootObject()
158 LightApp_Study* study = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
159 CAM_ModuleObject *aModelRoot = dynamic_cast<CAM_ModuleObject*>(root());
160 if(study && aModelRoot == NULL) {
161 aModelRoot = createModuleObject( study->root() );
162 aModelRoot->setDataModel( this );
167 void SHAPERGUI_DataModel::removeDirectory(const QString& theDirectoryName)
169 Qtx::rmDir(theDirectoryName);
172 bool SHAPERGUI_DataModel::dumpPython(const QString& thePath, CAM_Study* theStudy,
173 bool isMultiFile, QStringList& theListOfFiles)
175 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(theStudy);
179 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
180 ModelAPI_Session::get()->startOperation(ExchangePlugin_Dump::ID());
181 FeaturePtr aFeature = aDoc->addFeature(ExchangePlugin_Dump::ID());
182 if (aFeature.get()) {
183 std::string aTmpDir = aStudy->GetTmpDir(thePath.toStdString().c_str(), isMultiFile);
184 std::string aFileName = aTmpDir + DUMP_NAME;
186 if (QFile::exists(aFileName.c_str())) {
187 QFile::remove(aFileName.c_str());
190 AttributeStringPtr aAttr = aFeature->string(ExchangePlugin_Dump::FILE_PATH_ID());
192 aAttr->setValue(aFileName);
194 aAttr = aFeature->string(ExchangePlugin_Dump::FILE_FORMAT_ID());
196 aAttr->setValue(".py");
198 AttributeStringPtr aTypeAttr = aFeature->string(ExchangePlugin_Dump::SELECTION_TYPE_ID());
200 aTypeAttr->setValue(ExchangePlugin_Dump::TOPOLOGICAL_NAMING_DUMP_ID());
202 ModelAPI_Session::get()->finishOperation();
204 if (QFile::exists(aFileName.c_str())) {
206 QFile aInFile(aFileName.c_str());
207 if (!aInFile.open(QIODevice::ReadOnly | QIODevice::Text))
209 QTextStream aText(&aInFile);
210 QString aTrace(aText.readAll());
214 aBuffer.push_back(QString("def RebuildData( theStudy ):"));
215 QStringList aList(aTrace.split("\n"));
216 foreach(QString aStr, aList) {
217 QString s = " " + aStr;
218 aBuffer.push_back(s);
220 aTrace = aBuffer.join("\n");
222 QFile aOutFile(aFileName.c_str());
223 if (!aOutFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
226 QTextStream aOut(&aOutFile);
227 aOut << aTrace.toStdString().c_str() << "\n";
231 theListOfFiles.append(aTmpDir.c_str());
232 theListOfFiles.append(DUMP_NAME);