From abe3e0bbe9a83c7b07818571dc949d87c2fd0f4a Mon Sep 17 00:00:00 2001 From: azakir Date: Wed, 16 Dec 2020 10:31:52 +0100 Subject: [PATCH] to remove tmp file --- src/SMESHGUI/MG_ADAPT.cxx | 94 ++++++++++++++++++++++++++++++++---- src/SMESHGUI/MG_ADAPT.hxx | 9 +++- src/SMESHGUI/MG_ADAPTGUI.cxx | 6 +++ src/SMESHGUI/MG_ADAPTGUI.hxx | 2 + 4 files changed, 100 insertions(+), 11 deletions(-) diff --git a/src/SMESHGUI/MG_ADAPT.cxx b/src/SMESHGUI/MG_ADAPT.cxx index 9e3936606..ce0bbeeab 100644 --- a/src/SMESHGUI/MG_ADAPT.cxx +++ b/src/SMESHGUI/MG_ADAPT.cxx @@ -37,6 +37,17 @@ #include #include + +static std::string removeFile(std::string fileName, int& notOk) +{ + std::string errStr; + notOk = std::remove(fileName.c_str()); + if (notOk) errStr = ToComment(" \n error while removing file : ") + << fileName; + else errStr= ToComment("\n file : ")<< fileName << " succesfully deleted! \n "; + + return errStr; +} namespace { struct GET_DEFAULT // struct used to get default value from GetOptionValue() @@ -484,7 +495,9 @@ throw (std::invalid_argument) if (op_val->second != optionValue) { - const char* ptr = optionValue.c_str(); + + std::string lowerOptionValue = toLowerStr(optionValue); + const char* ptr = lowerOptionValue.c_str(); // strip white spaces while (ptr[0] == ' ') ptr++; @@ -497,7 +510,7 @@ throw (std::invalid_argument) if (i == 0) { // empty string } else if (_charOptions.count(optionName)) { - // do not check strings + // do not check strings } else if (_doubleOptions.count(optionName)) { // check if value is double toDbl(ptr, &typeOk); @@ -581,6 +594,18 @@ throw (std::invalid_argument) return val; } //================================================================================ +/*! + * \brief Converts a string to a lower + */ +//================================================================================ +std::string MgAdapt::toLowerStr(const std::string& str) +{ + std::string s = str; + for ( size_t i = 0; i <= s.size(); ++i ) + s[i] = tolower( s[i] ); + return s; +} +//================================================================================ /*! * \brief Converts a string to a bool */ @@ -662,7 +687,7 @@ std::string MgAdapt::getCommandToRun(MgAdapt* hyp) int MgAdapt::compute(std::string& errStr) { - std::string cmd= getCommandToRun(); + std::string cmd = getCommandToRun(); int err = 0; execCmd( cmd.c_str(), err ); // run @@ -675,6 +700,7 @@ int MgAdapt::compute(std::string& errStr) { convertMeshFile(meshFormatOutputMesh, solFormatOutput); } + //~if (!err) cleanUp(); return err; } @@ -682,8 +708,19 @@ void MgAdapt::execCmd( const char* cmd, int& err) { err = 1; std::array buffer; - std:: ofstream logStream; - logStream.open(logFile); + std::streambuf* buf; + outFileStream fileStream; + if (printLogInFile) + { + fileStream.open(logFile); + buf = fileStream.rdbuf(); + } + else + { + buf = std::cout.rdbuf(); + } + std::ostream logStream(buf); + std::unique_ptr pipe(popen(cmd, "r"), pclose ); if(!pipe) { @@ -693,9 +730,34 @@ void MgAdapt::execCmd( const char* cmd, int& err) { logStream<::iterator it = tmpFilesToBeDeleted.begin(); + for (; it!=tmpFilesToBeDeleted.end(); ++it) + { + errStr=removeFile(*it, notOk); + if (notOk) + { + appendMsgToLogFile(errStr); + } + + } +} + +void MgAdapt::appendMsgToLogFile(std::string& msg) +{ + std::ofstream logStream; + logStream.open(logFile, std::ofstream::out | std::ofstream::app); + logStream<< msg; + logStream.close(); +} //================================================================================ /*! * \brief Return command to run MG-Tetra mesher excluding file prefix (-f) @@ -717,6 +779,8 @@ std::string MgAdapt::getCommandToRun() errStr = ToComment(" failed to find .mesh or .sol file from converter ")<< strerror( errno ); return errStr; } + tmpFilesToBeDeleted.push_back(meshIn); + tmpFilesToBeDeleted.push_back(solFileIn); if(useBackgroundMap && !isFileExist(sizeMapIn)) { @@ -724,8 +788,7 @@ std::string MgAdapt::getCommandToRun() return errStr; } - meshFormatOutputMesh = meshIn; - solFormatOutput.push_back(solFileIn); + cmd+= " --in "+ meshIn; meshFormatOutputMesh = getFileName()+".mesh"; @@ -735,12 +798,22 @@ std::string MgAdapt::getCommandToRun() { cmd+= " --background_mesh "+ sizeMapIn ; cmd+= " --background_sizemap "+ solFileIn; + tmpFilesToBeDeleted.push_back(sizeMapIn); } //~else //~{ //~// constant value TODO //~} - + /* sizemap file is not adapted in case of only surface adaptation see MeshGems docs */ + std::string adapOp = "adaptation"; + std::string adpOpVal = getOptionValue(adapOp); + std::string surfaceAdapt = "surface"; + if(surfaceAdapt != adpOpVal ) + { + std::string solFileOut = getFileName()+".sol"; + cmd+= " --write_sizemap "+ solFileOut; + solFormatOutput.push_back(solFileOut); + } if (verbosityLevel != defaultVerboseLevel()) { @@ -1298,7 +1371,8 @@ void MgAdapt::getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit if ( erreur < 0 ) { - //~addMessage( ToComment(" error: error while reading field last time step ") << nomcha << " in file " << aFile , /*fatal=*/true ); + //~addMessage( ToComment(" error: error while reading field ") << nomcha << "step (numdt, numit) = " <<"("<< numdt<< ", " \ + numit<< ")" <<" in file " << aFile , /*fatal=*/true ); return; } diff --git a/src/SMESHGUI/MG_ADAPT.hxx b/src/SMESHGUI/MG_ADAPT.hxx index 2ac1f7100..e11898f00 100644 --- a/src/SMESHGUI/MG_ADAPT.hxx +++ b/src/SMESHGUI/MG_ADAPT.hxx @@ -60,7 +60,10 @@ struct MgAdaptHypothesisData }; - +class outFileStream : public std::ofstream{ +public: + ~outFileStream(){close();} //to close file at dtor +}; /*! * \brief Class to generate string from any type @@ -211,6 +214,7 @@ public: static double toDbl(const std::string&, bool* isOk = 0) throw (std::invalid_argument); static bool toBool(const std::string&, bool* isOk = 0) throw (std::invalid_argument); static int toInt(const std::string&, bool* isOk = 0 ) throw (std::invalid_argument); + static std::string toLowerStr(const std::string& str); /* default values */ @@ -308,6 +312,7 @@ private : std::vector< std::string> solFormatOutput; std::vector groupVec; std::vector famVec; + std::vector< std::string> tmpFilesToBeDeleted; /* convert MED-->.mesh format */ void convertMedFile(std::string& meshIn,std::string& solFileIn, std::string& sizeMapIn) ; @@ -325,6 +330,8 @@ private : med_idt openMedFile(const std::string aFile) ; bool isFileExist(std::string& fName) const; void execCmd( const char* cmd, int& err); + void cleanUp(); + void appendMsgToLogFile(std::string& msg); }; diff --git a/src/SMESHGUI/MG_ADAPTGUI.cxx b/src/SMESHGUI/MG_ADAPTGUI.cxx index 628912e46..928b93023 100644 --- a/src/SMESHGUI/MG_ADAPTGUI.cxx +++ b/src/SMESHGUI/MG_ADAPTGUI.cxx @@ -947,6 +947,7 @@ MgAdaptAdvWidget::MgAdaptAdvWidget( QWidget* parent, std::vector * connect( myOptionTable, SIGNAL( itemChanged(QTreeWidgetItem *, int)), SLOT( itemChanged(QTreeWidgetItem *, int ))); connect( addBtn, SIGNAL( clicked() ), this, SLOT( onAddOption() ) ); + connect(workingDirectoryPushButton, SIGNAL(pressed()), this, SLOT(_onWorkingDirectoryPushButton())); } MgAdaptAdvWidget::~MgAdaptAdvWidget() @@ -1120,6 +1121,11 @@ void MgAdaptAdvWidget::setupWidget() // QMetaObject::connectSlotsByName(this); } +void MgAdaptAdvWidget::_onWorkingDirectoryPushButton() +{ + QString aDirName=QFileDialog::getExistingDirectory (); + if (!(aDirName.isEmpty()))workingDirectoryLineEdit->setText(aDirName); +} namespace { diff --git a/src/SMESHGUI/MG_ADAPTGUI.hxx b/src/SMESHGUI/MG_ADAPTGUI.hxx index db4012425..ced54d236 100644 --- a/src/SMESHGUI/MG_ADAPTGUI.hxx +++ b/src/SMESHGUI/MG_ADAPTGUI.hxx @@ -280,6 +280,8 @@ public slots: void onAddOption(); void itemChanged(QTreeWidgetItem * tblRow, int column); +private slots: + void _onWorkingDirectoryPushButton(); }; -- 2.30.2