From 43e6429c58b73894906230c2288d20bfc1e02d2b Mon Sep 17 00:00:00 2001 From: azakir Date: Thu, 14 Jan 2021 17:00:52 +0100 Subject: [PATCH] TUI for mg-adapt --- src/SMESH/MG_ADAPT.cxx | 90 ++++++++++++++++++++---------- src/SMESH/MG_ADAPT.hxx | 15 +++-- src/SMESH_I/MG_ADAPT_i.cxx | 107 ++++++++++++++++++++++++++++++++++-- src/SMESH_I/MG_ADAPT_i.hxx | 37 ++++++++++--- src/SMESH_I/SMESH_Gen_i.hxx | 2 + 5 files changed, 202 insertions(+), 49 deletions(-) diff --git a/src/SMESH/MG_ADAPT.cxx b/src/SMESH/MG_ADAPT.cxx index 954ea0d97..688c5da2c 100644 --- a/src/SMESH/MG_ADAPT.cxx +++ b/src/SMESH/MG_ADAPT.cxx @@ -49,6 +49,11 @@ static std::string removeFile(std::string fileName, int& notOk) return errStr; } +std::string remove_extension(const std::string& filename) { + size_t lastdot = filename.find_last_of("."); + if (lastdot == std::string::npos) return filename; + return filename.substr(0, lastdot); +} namespace { struct GET_DEFAULT // struct used to get default value from GetOptionValue() @@ -221,7 +226,9 @@ MgAdaptHypothesisData* MgAdapt::getData() const } void MgAdapt::setMedFileIn(std::string fileName) { - medFileIn = fileName; + medFileIn = fileName; + if (medFileOut == "") // default MED file Out + medFileOut = remove_extension( fileName )+ ".adapt.med"; } std::string MgAdapt::getMedFileIn() @@ -283,16 +290,31 @@ int MgAdapt::getRank() } void MgAdapt::setTimeStepRankLast() { - med_int aRank, tmst; - std::string fieldFile = useBackgroundMap ? sizeMapFile : medFileIn; - getTimeStepInfos(fieldFile, tmst, aRank); - setRankTimeStep((int) tmst, (int) aRank); + myUseLastTimeStep = true; + myUseChosenTimeStep = false; + myUseNoTimeStep = false; + //~med_int aRank, tmst; + //~std::string fieldFile = useBackgroundMap ? sizeMapFile : medFileIn; + //~getTimeStepInfos(fieldFile, tmst, aRank); + //~setRankTimeStep((int) tmst, (int) aRank); } void MgAdapt::setNoTimeStep() -{ - int aRank = (int)MED_NO_IT; - int tmst = (int)MED_NO_DT ; - setRankTimeStep(tmst, aRank); +{ + myUseLastTimeStep = false; + myUseChosenTimeStep = false; + myUseNoTimeStep = true; + //~int aRank = (int)MED_NO_IT; + //~int tmst = (int)MED_NO_DT ; + //~setRankTimeStep(tmst, aRank); +} +void MgAdapt::setChosenTimeStepRank() +{ + myUseLastTimeStep = false; + myUseChosenTimeStep = true; + myUseNoTimeStep = false; + //~int aRank = (int)MED_NO_IT; + //~int tmst = (int)MED_NO_DT ; + //~setRankTimeStep(tmst, aRank); } void MgAdapt::setUseLocalMap(bool myLocal) { @@ -454,25 +476,15 @@ bool MgAdapt::setAll() } setSizeMapFile(mapfile); - med_int rank; - med_int tmst; if (data->myUseNoTimeStep) - { - rank = MED_NO_IT; - tmst = MED_NO_DT ; - } + setNoTimeStep(); else if (data->myUseLastTimeStep) - { - std::string fieldFile = useBackgroundMap ? sizeMapFile : medFileIn; - getTimeStepInfos(fieldFile, tmst, rank); - } + setTimeStepRankLast(); else - { - rank = data->myRank; - tmst = data->myTimeStep; - } - setRankTimeStep((int)tmst, (int)rank); - + { + setChosenTimeStepRank(); + setRankTimeStep(data->myTimeStep, data->myRank); + } /* Advanced options */ setWorkingDir(data->myWorkingDir); checkDirPath(data->myWorkingDir); @@ -714,7 +726,7 @@ int MgAdapt::compute(std::string& errStr) { convertMeshFile(meshFormatOutputMesh, solFormatOutput); } - //~if (!err) cleanUp(); + if (!err) cleanUp(); return err; } @@ -787,6 +799,7 @@ std::string MgAdapt::getCommandToRun() std::string errStr; std::string cmd = getExeName(); std::string meshIn(""), sizeMapIn(""), solFileIn(""); + updateTimeStepRank(); convertMedFile(meshIn, solFileIn, sizeMapIn); if (!isFileExist(meshIn) || !isFileExist(solFileIn)) { @@ -833,7 +846,7 @@ std::string MgAdapt::getCommandToRun() cmd+= " --verbose "+ ToComment(verbosityLevel); } - + std::string option, value; bool isDefault; const TOptionValues* options[] = { &_option2value, &_customOption2value }; @@ -876,7 +889,7 @@ std::string MgAdapt::getCommandToRun() } -bool MgAdapt::isFileExist(std::string& fName) const +bool MgAdapt::isFileExist(const std::string& fName) { if ( fName.empty() ) @@ -1123,6 +1136,8 @@ void MgAdapt::convertMedFile(std::string& meshFormatMeshFileName, std::string& s MEDCoupling::MCAuto mfd = MEDCoupling::MEDFileData::New(medFileIn); MEDCoupling::MEDFileMeshes* meshes = mfd->getMeshes(); MEDCoupling::MEDFileMesh* fileMesh = meshes->getMeshAtPos(0); // ok only one mesh in file! + if (meshNameOut =="") + meshNameOut = fileMesh->getName(); storeGroupsAndFams(fileMesh); MEDCoupling::MCAuto fields = MEDCoupling::MEDFileFields::New(); @@ -1408,5 +1423,20 @@ void MgAdapt::getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit } - - +void MgAdapt::updateTimeStepRank() +{ + + med_int arank = rank; + med_int tmst = timeStep; + if (myUseNoTimeStep) + { + rank = MED_NO_IT; + tmst = MED_NO_DT ; + } + else if (myUseLastTimeStep) + { + std::string fieldFile = useBackgroundMap ? sizeMapFile : medFileIn; + getTimeStepInfos(fieldFile, tmst, arank); + } + setRankTimeStep((int)tmst, (int)arank); +} diff --git a/src/SMESH/MG_ADAPT.hxx b/src/SMESH/MG_ADAPT.hxx index dc55e8419..c97ad15d3 100644 --- a/src/SMESH/MG_ADAPT.hxx +++ b/src/SMESH/MG_ADAPT.hxx @@ -35,7 +35,7 @@ // SMESH includes //~#include - +std::string remove_extension(const std::string& filename); namespace MG_ADAPT{ class MgAdapt; @@ -54,7 +54,7 @@ struct MgAdaptHypothesisData bool myUseNoTimeStep, myUseLastTimeStep, myUseChosenTimeStep; std::string myWorkingDir, myLogFile; bool myPrintLogInFile, myKeepFiles, myRemoveLogOnSuccess; - int myVerboseLevel; + int myVerboseLevel; }; @@ -149,6 +149,8 @@ public: void setTimeStepRankLast(); void setNoTimeStep(); + void setChosenTimeStepRank(); + void updateTimeStepRank(); void setLogFile(std::string); std::string getLogFile(); @@ -219,9 +221,9 @@ public: /* default values */ static std::string defaultWorkingDirectory(); static std::string defaultLogFile(); - static bool defaultKeepFiles(); - static bool defaultRemoveLogOnSuccess(); - static int defaultVerboseLevel(); + static bool defaultKeepFiles(); + static bool defaultRemoveLogOnSuccess(); + static int defaultVerboseLevel(); static bool defaultPrintLogInFile(); static bool defaultFromMedFile(); static bool defaultMeshOutMed(); @@ -233,6 +235,7 @@ public: static bool defaultUseLastTimeStep(); static bool defaultUseChosenTimeStep(); static double defaultMaximumMemory(); + static bool isFileExist(const std::string& fName); @@ -281,6 +284,7 @@ private : std::string meshNameOut; bool publish, meshOutMed; bool useLocalMap, useBackgroundMap, useConstantValue; + bool myUseLastTimeStep, myUseNoTimeStep, myUseChosenTimeStep; std::string sizeMapFile; std::string fieldName; double constantValue; @@ -327,7 +331,6 @@ private : void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit); Status addMessage(const std::string& msg, const bool isFatal = false); 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/SMESH_I/MG_ADAPT_i.cxx b/src/SMESH_I/MG_ADAPT_i.cxx index f0b510ccd..71c3ef21e 100644 --- a/src/SMESH_I/MG_ADAPT_i.cxx +++ b/src/SMESH_I/MG_ADAPT_i.cxx @@ -111,6 +111,23 @@ SMESH::MG_ADAPT_ptr SMESH_Gen_i::CreateMG_ADAPT() SMESH::MG_ADAPT_i* aMGadapt = new SMESH::MG_ADAPT_i(); SMESH::MG_ADAPT_var anObj = aMGadapt->_this(); return anObj._retn(); +} +SMESH::MG_ADAPT_ptr SMESH_Gen_i::CreateAdaptationHypothesis() +{ + SMESH::MG_ADAPT_i* aMGadapt = new SMESH::MG_ADAPT_i(); + SMESH::MG_ADAPT_var anObj = aMGadapt->_this(); + return anObj._retn(); +} +SMESH::MG_ADAPT_OBJECT_ptr SMESH_Gen_i::Adaptation( const char* adaptationType) +{ + + if (!strcmp(adaptationType, "MG_Adapt")){ + SMESH::MG_ADAPT_OBJECT_i* mg_adapt_object = new SMESH::MG_ADAPT_OBJECT_i(); + SMESH::MG_ADAPT_OBJECT_var anObj = mg_adapt_object->_this(); + return anObj._retn(); + } + + } //~SMESH::MG_ADAPT_ptr MG_ADAPT_i::CreateMG_ADAPT() //~{ @@ -211,11 +228,11 @@ bool MG_ADAPT_i::getPublish() { return myMgAdapt->getPublish(); } -void MG_ADAPT_i::setFieldName(const char* str) +void MG_ADAPT_i::setSizeMapFieldName(const char* str) { myMgAdapt->setFieldName(str); } -char* MG_ADAPT_i::getFieldName() +char* MG_ADAPT_i::getSizeMapFieldName() { return CORBA::string_dup(myMgAdapt->getFieldName().c_str()); } @@ -227,7 +244,7 @@ CORBA::Long MG_ADAPT_i::getTimeStep() { return myMgAdapt->getTimeStep(); } -void MG_ADAPT_i::setRankTimeStep(CORBA::Long t, CORBA::Long r) +void MG_ADAPT_i::setTimeStepRank(CORBA::Long t, CORBA::Long r) { myMgAdapt->setRankTimeStep(t, r); } @@ -300,11 +317,11 @@ bool MG_ADAPT_i::getUseConstantValue() return myMgAdapt->getUseConstantValue(); } -void MG_ADAPT_i::setConstantValue(double value) +void MG_ADAPT_i::setConstantSize(double value) { myMgAdapt->setConstantValue(value); } -double MG_ADAPT_i::getConstantValue() +double MG_ADAPT_i::getConstantSize() { return myMgAdapt->getConstantValue(); } @@ -337,6 +354,19 @@ bool MG_ADAPT_i::getKeepWorkingFiles() //~void MG_ADAPT_i::setPrCORBA::LongLogInFile(bool); //~bool MG_ADAPT_i::getPrCORBA::LongLogInFile(); +void MG_ADAPT_i::setSizeMapType(const char* type) +{ + setUseLocalMap(false); + setUseBackgroundMap(false); + setUseConstantValue(false); + + if (!strcmp("Local", type)) + setUseLocalMap(true); + else if (!strcmp("Background", type)) + setUseBackgroundMap(true); + else + setUseConstantValue(true); +} void MG_ADAPT_i::setWorkingDir(const char* dir) { myMgAdapt->setWorkingDir(dir); @@ -365,7 +395,7 @@ CORBA::Long MG_ADAPT_i::compute() { errStr = ""; CORBA::Long ret; - try + try { ret = myMgAdapt->compute(errStr); } @@ -445,3 +475,68 @@ bool MG_ADAPT_i::getPrintLogInFile() } //~TOptionValues MG_ADAPT_i::getOptionValues() const; //~const TOptionValues& MG_ADAPT_i::getCustomOptionValues() const ; + +MG_ADAPT_OBJECT_i::MG_ADAPT_OBJECT_i(): SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() ) +{ + medFileIn=""; + medFileOut=""; + medFileBackground=""; + publish = false; + //~myMesh = CORBA::nil; +} + +void MG_ADAPT_OBJECT_i::setMeshIn(SMESH::SMESH_Mesh_ptr theMesh ) +{ + myMesh = SMESH::SMESH_Mesh::_duplicate(theMesh); +} +void MG_ADAPT_OBJECT_i::setMEDFileIn(const char* f) +{ + medFileIn = f; +} +void MG_ADAPT_OBJECT_i::setMEDFileOut(const char* f) +{ + medFileOut = f; +} +void MG_ADAPT_OBJECT_i::setMEDFileBackground(const char* f) +{ + medFileBackground = f; +} +void MG_ADAPT_OBJECT_i::AddHypothesis(SMESH::MG_ADAPT_ptr mg) +{ + + mg->setMedFileIn(medFileIn.c_str()); + mg->setMedFileOut(medFileOut.c_str()); + mg->setSizeMapFile(medFileBackground.c_str()); + hypothesis = SMESH::MG_ADAPT::_duplicate(mg); +} +CORBA::Long MG_ADAPT_OBJECT_i::Compute(bool publish) +{ + if(!checkMeshFileIn()){ + std::cerr<< "\n Error : Please check the MED file input or mesh input. \n"; + return -1; + } + hypothesis->setPublish(publish); + return hypothesis->compute(); +} + +bool MG_ADAPT_OBJECT_i::checkMeshFileIn() +{ + bool ret = false; // 1 ok , 0 nook + if(!::MG_ADAPT::MgAdapt::isFileExist(medFileIn)) + { + if(!myMesh->_is_nil()) + { + bool toOverwrite = true; + bool toFindOutDim = true; + medFileIn = hypothesis->getFileName(); + medFileIn+= ".med"; + myMesh->ExportMED(medFileIn.c_str(), false, -1, toOverwrite, toFindOutDim); + hypothesis->setMedFileIn(medFileIn.c_str()); + ret = true; + } + } + else + ret = true; + + return ret; +} diff --git a/src/SMESH_I/MG_ADAPT_i.hxx b/src/SMESH_I/MG_ADAPT_i.hxx index 07fc78b8f..daedce449 100644 --- a/src/SMESH_I/MG_ADAPT_i.hxx +++ b/src/SMESH_I/MG_ADAPT_i.hxx @@ -11,7 +11,9 @@ #include "SALOME_Component_i.hxx" #include "SALOME_NamingService.hxx" #include "Utils_CorbaException.hxx" - +#include +#include +#include #include //~struct MgAdaptHypothesisData; @@ -20,6 +22,7 @@ //~class MgAdapt; namespace SMESH { + class SMESH_I_EXPORT MG_ADAPT_i : public virtual SALOME::GenericObj_i, public virtual POA_SMESH::MG_ADAPT @@ -51,13 +54,13 @@ public: void setPublish(bool mybool); bool getPublish(); - void setFieldName(const char* str); - char* getFieldName(); + void setSizeMapFieldName(const char* str); + char* getSizeMapFieldName(); void setTimeStep(CORBA::Long t); CORBA::Long getTimeStep() ; - void setRankTimeStep(CORBA::Long t, CORBA::Long r ); + void setTimeStepRank(CORBA::Long t, CORBA::Long r ); CORBA::Long getRank(); void setTimeStepRankLast(); @@ -73,7 +76,7 @@ public: bool getRemoveOnSuccess(); SMESH::MgAdaptHypothesisData* getData() ; - + void setSizeMapType(const char* type); void setUseLocalMap(bool mybool); bool getUseLocalMap(); @@ -83,8 +86,8 @@ public: void setUseConstantValue(bool mybool); bool getUseConstantValue(); - void setConstantValue(double value); - double getConstantValue(); + void setConstantSize(double value); + double getConstantSize(); void setSizeMapFile(const char* str); char* getSizeMapFile(); @@ -136,5 +139,25 @@ private: //~ADAPT::ADAPT_Gen_var _gen_i; }; + +class SMESH_I_EXPORT MG_ADAPT_OBJECT_i: + public virtual SALOME::GenericObj_i, + public virtual POA_SMESH::MG_ADAPT_OBJECT { +public : + MG_ADAPT_OBJECT_i(); + void setMeshIn( SMESH::SMESH_Mesh_ptr theMesh ); + void setMEDFileIn(const char* f); + void setMEDFileOut(const char* f); + void setMEDFileBackground(const char* f); + void AddHypothesis(SMESH::MG_ADAPT_ptr); + CORBA::Long Compute(bool Publish); +private: +std::string medFileIn, medFileOut, medFileBackground; +bool checkMeshFileIn(); +bool publish; +SMESH::SMESH_Mesh_ptr myMesh; +SMESH::MG_ADAPT_ptr hypothesis; +}; + } #endif // MG_ADAPT_I_HXX diff --git a/src/SMESH_I/SMESH_Gen_i.hxx b/src/SMESH_I/SMESH_Gen_i.hxx index fa533f853..08cfb22f7 100644 --- a/src/SMESH_I/SMESH_Gen_i.hxx +++ b/src/SMESH_I/SMESH_Gen_i.hxx @@ -470,6 +470,8 @@ public: static int CountInPyDump(const TCollection_AsciiString& text); SMESH::MG_ADAPT_ptr CreateMG_ADAPT(); + SMESH::MG_ADAPT_ptr CreateAdaptationHypothesis(); + SMESH::MG_ADAPT_OBJECT_ptr Adaptation( const char* adaptationType); // ***************************************** // Internal methods