1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "SHAPERGUI_DataModel.h"
6 #include <XGUI_Workshop.h>
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_AttributeString.h>
10 #include <ExchangePlugin_Dump.h>
12 #include <LightApp_Study.h>
13 #include <CAM_Application.h>
14 #include <CAM_DataObject.h>
15 #include <SUIT_Tools.h>
16 #include <SUIT_ResourceMgr.h>
20 #include <QTextStream>
22 #define DUMP_NAME "shaper_dump.py"
25 SHAPERGUI_DataModel::SHAPERGUI_DataModel(SHAPERGUI* theModule)
26 : LightApp_DataModel(theModule), myStudyPath(""), myModule(theModule)
30 SHAPERGUI_DataModel::~SHAPERGUI_DataModel()
34 bool SHAPERGUI_DataModel::open(const QString& thePath, CAM_Study* theStudy, QStringList theFiles)
36 LightApp_DataModel::open( thePath, theStudy, theFiles );
37 if (theFiles.size() == 0)
40 myStudyPath = thePath;
42 // If the file is Multi(contain all module files inside), the open SALOME functionality creates
43 // these files in a temporary directory. After the open functionality is finished, it removes
44 // these files (in the full SALOME mode).
45 // The postponed loading of the files is realized in the SHAPER module. So, it is important do
46 // not remove the opened files.
47 // The following code creates a new tmp directory with a copy of files.
48 QString aTmpDir = theFiles.first();
50 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( myModule->application()->activeStudy() );
51 QString aNewTmpDir = aStudy->GetTmpDir("", false).c_str();
55 QStringList aFiles = aDir.entryList(QDir::Files);
56 QStringList::const_iterator anIt = aFiles.begin(), aLast = aFiles.end();
57 for (; anIt != aLast; anIt++) {
58 QString aFileName = *anIt;
60 QString aCurrentFile = SUIT_Tools::addSlash(aTmpDir) + aFileName;
61 QString aNewFile = SUIT_Tools::addSlash(aNewTmpDir) + aFileName;
62 if (!QFile::copy(aCurrentFile, aNewFile))
66 myTmpDirectory = aNewTmpDir;
69 removeDirectory(aNewTmpDir);
73 SessionPtr aMgr = ModelAPI_Session::get();
74 aMgr->load(qPrintable(aNewTmpDir));
75 myModule->setIsOpened(true);
79 bool SHAPERGUI_DataModel::save(QStringList& theFiles)
81 LightApp_DataModel::save( theFiles );
82 XGUI_Workshop* aWorkShop = myModule->workshop();
83 std::list<std::string> aFileNames;
85 CAM_Application* anApp = myModule->application();
86 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(anApp->activeStudy());
87 SUIT_ResourceMgr* aResMgr = anApp->resourceMgr();
89 // it is important to check whether the file is saved in the multi-files mode in order to save
90 // files in temporary directories, which are removed in the full SALOME mode after copiying
91 // the files content in a result file.
92 bool isMultiFile = aResMgr ? aResMgr->booleanValue("Study", "multi_file", false) : false;
94 std::string aTmpDir = aStudy->GetTmpDir(qPrintable(myStudyPath), isMultiFile);
95 //std::string aTmpDir = aStudy->GetTmpDir("", false);//true );
96 theFiles.append(QString(aTmpDir.c_str()));
98 aWorkShop->saveDocument(QString(aTmpDir.c_str()), aFileNames);
99 std::list<std::string>::iterator aIt;
100 for (aIt = aFileNames.begin(); aIt != aFileNames.end(); ++aIt) {
101 QString aName((*aIt).c_str());
102 aName.replace(QChar('\\'), QChar('/'));
103 int aN = aName.lastIndexOf('/');
104 theFiles.append(aName.right(aName.length() - aN - 1));
109 bool SHAPERGUI_DataModel::saveAs(const QString& thePath, CAM_Study* theStudy, QStringList& theFiles)
111 myStudyPath = thePath;
112 return save(theFiles);
115 bool SHAPERGUI_DataModel::close()
117 myModule->workshop()->closeDocument();
118 removeDirectory(myTmpDirectory);
120 return LightApp_DataModel::close();
123 bool SHAPERGUI_DataModel::create(CAM_Study* theStudy)
128 bool SHAPERGUI_DataModel::isModified() const
130 SessionPtr aMgr = ModelAPI_Session::get();
131 return aMgr->isModified();
134 bool SHAPERGUI_DataModel::isSaved() const
136 return !isModified();
139 void SHAPERGUI_DataModel::update(LightApp_DataObject* theObj, LightApp_Study* theStudy)
141 // Nothing to do here: we always keep the data tree in the up-to-date state
142 // The only goal of this method is to hide default behavior from LightApp_DataModel
146 void SHAPERGUI_DataModel::initRootObject()
148 LightApp_Study* study = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
149 CAM_ModuleObject *aModelRoot = dynamic_cast<CAM_ModuleObject*>(root());
150 if(study && aModelRoot == NULL) {
151 aModelRoot = createModuleObject( study->root() );
152 aModelRoot->setDataModel( this );
157 void SHAPERGUI_DataModel::removeDirectory(const QString& theDirectoryName)
159 Qtx::rmDir(theDirectoryName);
162 bool SHAPERGUI_DataModel::dumpPython(const QString& thePath, CAM_Study* theStudy,
163 bool isMultiFile, QStringList& theListOfFiles)
165 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(theStudy);
169 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
170 ModelAPI_Session::get()->startOperation(ExchangePlugin_Dump::ID());
171 FeaturePtr aFeature = aDoc->addFeature(ExchangePlugin_Dump::ID());
172 if (aFeature.get()) {
173 std::string aTmpDir = aStudy->GetTmpDir(thePath.toStdString().c_str(), isMultiFile);
174 std::string aFileName = aTmpDir + DUMP_NAME;
176 if (QFile::exists(aFileName.c_str())) {
177 QFile::remove(aFileName.c_str());
180 AttributeStringPtr aAttr = aFeature->string(ExchangePlugin_Dump::FILE_PATH_ID());
182 aAttr->setValue(aFileName);
184 aAttr = aFeature->string(ExchangePlugin_Dump::FILE_FORMAT_ID());
186 aAttr->setValue(".py");
187 ModelAPI_Session::get()->finishOperation();
189 if (QFile::exists(aFileName.c_str())) {
191 QFile aInFile(aFileName.c_str());
192 if (!aInFile.open(QIODevice::ReadOnly | QIODevice::Text))
194 QTextStream aText(&aInFile);
195 QString aTrace(aText.readAll());
199 aBuffer.push_back(QString("def RebuildData( theStudy ):"));
200 QStringList aList(aTrace.split("\n"));
201 foreach(QString aStr, aList) {
202 QString s = " " + aStr;
203 aBuffer.push_back(s);
205 aTrace = aBuffer.join("\n");
207 QFile aOutFile(aFileName.c_str());
208 if (!aOutFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
211 QTextStream aOut(&aOutFile);
212 aOut << aTrace.toStdString().c_str() << "\n";
216 theListOfFiles.append(aTmpDir.c_str());
217 theListOfFiles.append(DUMP_NAME);