Back to index

salome-smesh  6.5.0
SMESHGUI_HypothesesUtils.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // SMESH SMESHGUI : GUI for SMESH component
00024 // File   : SMESHGUI_HypothesesUtils.cxx
00025 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
00026 // SMESH includes
00027 //
00028 #include "SMESHGUI_HypothesesUtils.h"
00029 
00030 #include "SMESHGUI.h"
00031 #include "SMESHGUI_Hypotheses.h"
00032 #include "SMESHGUI_XmlHandler.h"
00033 #include "SMESHGUI_Utils.h"
00034 #include "SMESHGUI_GEOMGenUtils.h"
00035 
00036 // SALOME GUI includes
00037 #include <SUIT_Desktop.h>
00038 #include <SUIT_MessageBox.h>
00039 #include <SUIT_OverrideCursor.h>
00040 #include <SUIT_ResourceMgr.h>
00041 
00042 #include <SalomeApp_Study.h>
00043 #include <SalomeApp_Tools.h>
00044 
00045 // SALOME KERNEL includes
00046 #include <utilities.h>
00047 
00048 // STL includes
00049 #include <string>
00050 
00051 // Qt includes
00052 #include <QMap>
00053 #include <QDir>
00054 //#include <QList>
00055 
00056 
00057 // Other includes
00058 #ifdef WNT
00059 #include <windows.h>
00060 #else
00061 #include <dlfcn.h>
00062 #endif
00063 
00064 #ifdef WNT
00065 #define LibHandle HMODULE
00066 #define LoadLib( name ) LoadLibrary( name )
00067 #define GetProc GetProcAddress
00068 #define UnLoadLib( handle ) FreeLibrary( handle );
00069 #else
00070 #define LibHandle void*
00071 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
00072 #define GetProc dlsym
00073 #define UnLoadLib( handle ) dlclose( handle );
00074 #endif
00075 
00076 #ifdef _DEBUG_
00077 static int MYDEBUG = 0;
00078 #else
00079 static int MYDEBUG = 0;
00080 #endif
00081 
00082 namespace SMESH
00083 {
00084   typedef IMap<QString,HypothesisData*> THypothesisDataMap;
00085   THypothesisDataMap myHypothesesMap;
00086   THypothesisDataMap myAlgorithmsMap;
00087   
00088   // BUG 0020378
00089   //typedef QMap<QString,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
00090   //THypCreatorMap myHypCreatorMap;
00091 
00092   QList<HypothesesSet*> myListOfHypothesesSets;
00093 
00094   void processHypothesisStatus(const int theHypStatus,
00095                                SMESH::SMESH_Hypothesis_ptr theHyp,
00096                                const bool theIsAddition)
00097   {
00098     if (theHypStatus > SMESH::HYP_OK) {
00099       // get Hyp name
00100       QString aHypName ("NULL Hypothesis");
00101       if (!CORBA::is_nil(theHyp)) {
00102         _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
00103         if (Shyp)
00104           // name in study
00105           aHypName = Shyp->GetName().c_str();
00106         else
00107           // label in xml file
00108           aHypName = GetHypothesisData(theHyp->GetName())->Label;
00109       }
00110 
00111       // message
00112       bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
00113       QString aMsg;
00114       if (theIsAddition)
00115         aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
00116       else
00117         aMsg = (isFatal ? "SMESH_CANT_RM_HYP"  : "SMESH_RM_HYP_WRN");
00118 
00119       aMsg = QObject::tr(aMsg.toLatin1().data()).arg(aHypName) +
00120         QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus).toLatin1().data());
00121 
00122       if ( theHypStatus == SMESH::HYP_HIDDEN_ALGO ) // PAL18501
00123         aMsg = aMsg.arg( GetHypothesisData(theHyp->GetName())->Dim[0] );
00124 
00125       SUIT_MessageBox::warning(SMESHGUI::desktop(),
00126                                QObject::tr("SMESH_WRN_WARNING"),
00127                                aMsg);
00128     }
00129   }
00130 
00131   //================================================================================
00135   //================================================================================
00136 
00137   static QString mangledHypoSetName(HypothesesSet* hypSet)
00138   {
00139     QString name = hypSet->name();
00140 
00141     // prepend 'xD: '
00142     int dim = hypSet->maxDim();
00143     if ( dim > -1 )
00144       name = QString("%1D: %2").arg(dim).arg(name);
00145 
00146     // custom
00147     if ( hypSet->getIsCustom() )
00148       name = QString("%1 [custom]").arg(name);
00149 
00150     return name;
00151   }
00152 
00153   //================================================================================
00157   //================================================================================
00158 
00159   static QString demangledHypoSetName(QString name)
00160   {
00161     name.remove(QRegExp("[0-3]D: "));
00162     name.remove(" [custom]");
00163     return name;
00164   }
00165 
00166   void InitAvailableHypotheses()
00167   {
00168     SUIT_OverrideCursor wc;
00169     if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
00170       // Resource manager
00171       SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
00172       if (!resMgr) return;
00173 
00174       // Find name of a resource XML file ("SMESH_Meshers.xml");
00175       QString HypsXml;
00176       char* cenv = getenv("SMESH_MeshersList");
00177       if (cenv)
00178         HypsXml.sprintf("%s", cenv);
00179 
00180       QStringList HypsXmlList = HypsXml.split(":", QString::SkipEmptyParts);
00181       if (HypsXmlList.count() == 0) {
00182         SUIT_MessageBox::critical(SMESHGUI::desktop(),
00183                                   QObject::tr("SMESH_WRN_WARNING"),
00184                                   QObject::tr("MESHERS_FILE_NO_VARIABLE"));
00185         return;
00186       }
00187       // get full names of xml files from HypsXmlList
00188       QStringList xmlFiles;
00189       xmlFiles.append( QDir::home().filePath("CustomMeshers.xml")); // may be inexistent
00190       for (int i = 0; i < HypsXmlList.count(); i++) {
00191         QString HypsXml = HypsXmlList[ i ];
00192 
00193         // Find full path to the resource XML file
00194         QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
00195         if ( xmlFile.isEmpty() ) // try PLUGIN resources
00196           xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
00197         if ( !xmlFile.isEmpty() )
00198           xmlFiles.append( xmlFile );
00199       }
00200 
00201       // loop on xmlFiles
00202       QString aNoAccessFiles;
00203       for (int i = 0; i < xmlFiles.count(); i++)
00204       {
00205         QString xmlFile = xmlFiles[ i ];
00206         QFile file (xmlFile);
00207         if (file.exists() && file.open(QIODevice::ReadOnly))
00208         {
00209           file.close();
00210 
00211           SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
00212           ASSERT(aXmlHandler);
00213 
00214           QXmlInputSource source (&file);
00215           QXmlSimpleReader reader;
00216           reader.setContentHandler(aXmlHandler);
00217           reader.setErrorHandler(aXmlHandler);
00218           bool ok = reader.parse(source);
00219           file.close();
00220           if (ok) {
00221 
00222             THypothesisDataMap::ConstIterator it1 = aXmlHandler->myHypothesesMap.begin();
00223             
00224             for( ;it1 != aXmlHandler->myHypothesesMap.end(); it1++)
00225               myHypothesesMap.insert( it1.key(), it1.value() );
00226             
00227             
00228             it1 = aXmlHandler->myAlgorithmsMap.begin();
00229             for( ;it1 != aXmlHandler->myAlgorithmsMap.end(); it1++)
00230               myAlgorithmsMap.insert( it1.key(), it1.value() );
00231             
00232             QList<HypothesesSet*>::iterator it, pos = myListOfHypothesesSets.begin();
00233             for ( it = aXmlHandler->myListOfHypothesesSets.begin(); 
00234                   it != aXmlHandler->myListOfHypothesesSets.end();
00235                   ++it )
00236             {
00237               (*it)->setIsCustom( i == 0 );
00238               myListOfHypothesesSets.insert( pos, *it );
00239             }
00240           }
00241           else {
00242             SUIT_MessageBox::critical(SMESHGUI::desktop(),
00243                                       QObject::tr("INF_PARSE_ERROR"),
00244                                       QObject::tr(aXmlHandler->errorProtocol().toLatin1().data()));
00245           }
00246           delete aXmlHandler;
00247         }
00248         else if ( i > 0 ) { // 1st is ~/CustomMeshers.xml
00249           if (aNoAccessFiles.isEmpty())
00250             aNoAccessFiles = xmlFile;
00251           else
00252             aNoAccessFiles += ", " + xmlFile;
00253         }
00254       } // end loop on xmlFiles
00255 
00256 
00257       if (!aNoAccessFiles.isEmpty()) {
00258         QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
00259         aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
00260         wc.suspend();
00261         SUIT_MessageBox::warning(SMESHGUI::desktop(),
00262                                  QObject::tr("SMESH_WRN_WARNING"),
00263                                  aMess);
00264         wc.resume();
00265       }
00266     }
00267   }
00268 
00269 
00270   QStringList GetAvailableHypotheses( const bool isAlgo, 
00271                                       const int theDim,                          
00272                                       const bool isAux,
00273                                       const bool isNeedGeometry)
00274   {
00275     QStringList aHypList;
00276 
00277     // Init list of available hypotheses, if needed
00278     InitAvailableHypotheses();
00279     bool checkGeometry = ( !isNeedGeometry && isAlgo );
00280     // fill list of hypotheses/algorithms
00281     THypothesisDataMap& pMap = isAlgo ? myAlgorithmsMap : myHypothesesMap;
00282     THypothesisDataMap::ConstIterator anIter;
00283     for ( anIter = pMap.begin(); anIter != pMap.end(); anIter++ ) {
00284       HypothesisData* aData = anIter.value();
00285       if(!aData || aData->Label.isEmpty()) continue;
00286       if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux) {
00287         if (checkGeometry) {
00288           if (aData->IsNeedGeometry == isNeedGeometry)
00289             aHypList.append(anIter.key());
00290         }
00291         else {
00292           aHypList.append(anIter.key());
00293         }
00294       }
00295     }
00296     return aHypList;
00297   }
00298 
00299 
00300   QStringList GetHypothesesSets(int maxDim)
00301   {
00302     QStringList aSetNameList;
00303 
00304     // Init list of available hypotheses, if needed
00305     InitAvailableHypotheses();
00306 
00307     QList<HypothesesSet*>::iterator hypoSet;
00308     for ( hypoSet  = myListOfHypothesesSets.begin(); 
00309           hypoSet != myListOfHypothesesSets.end();
00310           ++hypoSet ) {
00311       HypothesesSet* aSet = *hypoSet;
00312       if ( aSet &&
00313            ( aSet->count( true ) || aSet->count( false )) &&
00314            aSet->maxDim() <= maxDim)
00315       {
00316         aSetNameList.append( mangledHypoSetName( aSet ));
00317       }
00318     }
00319     aSetNameList.sort();
00320 
00321     //  reverse order of aSetNameList
00322     QStringList reversedNames;
00323     for ( int i = 0; i < aSetNameList.count(); ++i )
00324       reversedNames.prepend( aSetNameList[i] );
00325     
00326     return reversedNames;
00327   }
00328 
00329   HypothesesSet* GetHypothesesSet(const QString& theSetName)
00330   {
00331     QString name = demangledHypoSetName( theSetName );
00332     QList<HypothesesSet*>::iterator hypoSet;
00333     for ( hypoSet  = myListOfHypothesesSets.begin(); 
00334           hypoSet != myListOfHypothesesSets.end();
00335           ++hypoSet ) {
00336       HypothesesSet* aSet = *hypoSet;
00337       if ( aSet && aSet->name() == name )
00338         return aSet;
00339     }
00340     return 0;
00341   }
00342 
00343   HypothesisData* GetHypothesisData (const QString& aHypType)
00344   {
00345     HypothesisData* aHypData = 0;
00346 
00347     // Init list of available hypotheses, if needed
00348     InitAvailableHypotheses();
00349 
00350     if (myHypothesesMap.contains(aHypType)) {
00351       aHypData = myHypothesesMap[aHypType];
00352     }
00353     else if (myAlgorithmsMap.contains(aHypType)) {
00354       aHypData = myAlgorithmsMap[aHypType];
00355     }
00356     return aHypData;
00357   }
00358 
00359   bool IsAvailableHypothesis(const HypothesisData* algoData,
00360                              const QString&        hypType,
00361                              bool&                 isAuxiliary)
00362   {
00363     isAuxiliary = false;
00364     if ( !algoData )
00365       return false;
00366     if ( algoData->NeededHypos.contains( hypType ))
00367       return true;
00368     if ( algoData->OptionalHypos.contains( hypType)) {
00369       isAuxiliary = true;
00370       return true;
00371     }
00372     return false;
00373   }
00374 
00375   bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
00376                              const HypothesisData* algo2Data)
00377   {
00378     if ( !algo1Data || !algo2Data )
00379       return false;
00380     const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
00381     if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
00382       algoIn = algo2Data; algoMain = algo1Data;
00383     }
00384     // look for any output type of algoIn between input types of algoMain
00385     QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
00386     for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
00387       if ( algoMain->InputTypes.contains( *inElemType ))
00388         return true;
00389     return false;
00390   }
00391 
00392   SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const QString& aHypType)
00393   {
00394     if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType.toLatin1().data());
00395 
00396     SMESHGUI_GenericHypothesisCreator* aCreator = 0;
00397 
00398     // check, if creator for this hypothesis type already exists
00399     // BUG 0020378
00400     //if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
00401     //  aCreator = myHypCreatorMap[aHypType];
00402     //}
00403     //else
00404     {
00405       // 1. Init list of available hypotheses, if needed
00406       InitAvailableHypotheses();
00407 
00408       // 2. Get names of plugin libraries
00409       HypothesisData* aHypData = GetHypothesisData(aHypType);
00410       if (!aHypData) 
00411         return aCreator;
00412 
00413       QString aClientLibName = aHypData->ClientLibName;
00414       QString aServerLibName = aHypData->ServerLibName;
00415 
00416       // 3. Load Client Plugin Library
00417       try {
00418         // load plugin library
00419         if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
00420         LibHandle libHandle = LoadLib( aClientLibName.toLatin1().data() );
00421         if (!libHandle) {
00422           // report any error, if occured
00423           if ( MYDEBUG ) {
00424 #ifdef WIN32
00425             const char* anError = "Can't load client meshers plugin library";
00426 #else
00427             const char* anError = dlerror();      
00428 #endif
00429             MESSAGE(anError);
00430           }
00431         }
00432         else {
00433           // get method, returning hypothesis creator
00434           if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
00435           typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
00436             ( const QString& );
00437           GetHypothesisCreator procHandle =
00438             (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
00439           if (!procHandle) {
00440             if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
00441             UnLoadLib(libHandle);
00442           }
00443           else {
00444             // get hypothesis creator
00445             if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
00446             aCreator = procHandle( aHypType );
00447             if (!aCreator) {
00448               if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
00449             }
00450             else {
00451               // map hypothesis creator to a hypothesis name
00452               // BUG 0020378
00453               //myHypCreatorMap[aHypType] = aCreator;
00454 
00455              //rnv : This dynamic property of the QObject stores the name of the plugin.
00456              //      It is used to obtain plugin root dir environment variable
00457               //      in the SMESHGUI_HypothesisDlg class. Plugin root dir environment 
00458              //      variable is used to display documentation.
00459              aCreator->setProperty(PLUGIN_NAME,aHypData->PluginName);
00460             }
00461           }
00462         }
00463       }
00464       catch (const SALOME::SALOME_Exception& S_ex) {
00465         SalomeApp_Tools::QtCatchCorbaException(S_ex);
00466       }
00467     }
00468 
00469     return aCreator;
00470   }
00471 
00472 
00473   SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
00474                                                const QString& aHypName,
00475                                                const bool isAlgo)
00476   {
00477     if(MYDEBUG) MESSAGE("Create " << aHypType.toLatin1().data() << 
00478                         " with name " << aHypName.toLatin1().data());
00479     HypothesisData* aHypData = GetHypothesisData(aHypType);
00480     QString aServLib = aHypData->ServerLibName;
00481     try {
00482       SMESH::SMESH_Hypothesis_var aHypothesis;
00483       aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
00484                                                               aServLib.toLatin1().data());
00485       if (!aHypothesis->_is_nil()) {
00486         _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
00487         if (aHypSObject) {
00488           if (!aHypName.isEmpty())
00489             SMESH::SetName(aHypSObject, aHypName);
00490           SMESHGUI::Modified();
00491           SMESHGUI::GetSMESHGUI()->updateObjBrowser();
00492           return aHypothesis._retn();
00493         }
00494       }
00495     } catch (const SALOME::SALOME_Exception & S_ex) {
00496       SalomeApp_Tools::QtCatchCorbaException(S_ex);
00497     }
00498 
00499     return SMESH::SMESH_Hypothesis::_nil();
00500   }
00501 
00502 
00503   bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
00504   {
00505     if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
00506     int res = SMESH::HYP_UNKNOWN_FATAL;
00507     SUIT_OverrideCursor wc;
00508 
00509     if (!aMesh->_is_nil()) {
00510       _PTR(SObject) SM = SMESH::FindSObject(aMesh);
00511       GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
00512       try {
00513         res = aMesh->AddHypothesis(aShapeObject, aHyp);
00514         if (res < SMESH::HYP_UNKNOWN_FATAL) {
00515           _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
00516           if (SM && aSH) {
00517             SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
00518           }
00519         }
00520         if (res > SMESH::HYP_OK) {
00521           wc.suspend();
00522           processHypothesisStatus(res, aHyp, true);
00523           wc.resume();
00524         }
00525       }
00526       catch(const SALOME::SALOME_Exception& S_ex) {
00527         wc.suspend();
00528         SalomeApp_Tools::QtCatchCorbaException(S_ex);
00529         res = SMESH::HYP_UNKNOWN_FATAL;
00530       }
00531     }
00532     return res < SMESH::HYP_UNKNOWN_FATAL;
00533   }
00534 
00535 
00536   bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
00537   {
00538     if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
00539     int res = SMESH::HYP_UNKNOWN_FATAL;
00540     SUIT_OverrideCursor wc;
00541 
00542     if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
00543       try {
00544         SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
00545         _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
00546         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
00547         if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
00548           res = aMesh->AddHypothesis(aShapeObject, aHyp);
00549           if (res < SMESH::HYP_UNKNOWN_FATAL)  {
00550             _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
00551             if (meshSO)
00552               SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
00553           }
00554           if (res > SMESH::HYP_OK) {
00555             wc.suspend();
00556             processHypothesisStatus(res, aHyp, true);
00557             wc.resume();
00558           }
00559         }
00560         else {
00561           SCRUTE(aHyp->_is_nil());
00562           SCRUTE(aMesh->_is_nil());
00563           SCRUTE(!SsubM);
00564           SCRUTE(aShapeObject->_is_nil());
00565         }
00566       }
00567       catch(const SALOME::SALOME_Exception& S_ex) {
00568         wc.suspend();
00569         SalomeApp_Tools::QtCatchCorbaException(S_ex);
00570         res = SMESH::HYP_UNKNOWN_FATAL;
00571       }
00572     }
00573     else {
00574       SCRUTE(aSubMesh->_is_nil());
00575       SCRUTE(aHyp->_is_nil());
00576     }
00577     return res < SMESH::HYP_UNKNOWN_FATAL;
00578   }
00579 
00580   bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
00581   {
00582     int res = SMESH::HYP_UNKNOWN_FATAL;
00583     SUIT_OverrideCursor wc;
00584 
00585     try {
00586       _PTR(Study) aStudy = GetActiveStudyDocument();
00587       _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
00588       if( aHypObj )
00589         {
00590           _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
00591           _PTR(SObject) aRealHypo;
00592           if( aHypObj->ReferencedObject( aRealHypo ) )
00593             {
00594               SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
00595               RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
00596             }
00597           else
00598             {
00599               SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
00600               SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
00601               for( int i = 0; i < meshList.size(); i++ )
00602                 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
00603             }
00604         }
00605     }
00606     catch(const SALOME::SALOME_Exception& S_ex)
00607       {
00608         wc.suspend();
00609         SalomeApp_Tools::QtCatchCorbaException(S_ex);
00610         res = SMESH::HYP_UNKNOWN_FATAL;
00611       }
00612     return res < SMESH::HYP_UNKNOWN_FATAL;
00613   }
00614 
00615   bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
00616                                           SMESH::SMESH_Hypothesis_ptr anHyp)
00617   {
00618     SALOMEDS::GenericAttribute_var anAttr;
00619     SALOMEDS::AttributeIOR_var anIOR;
00620     int res = SMESH::HYP_UNKNOWN_FATAL;
00621     SUIT_OverrideCursor wc;
00622 
00623     if (MorSM) {
00624       try {
00625         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
00626         SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
00627         SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
00628         
00629         if (!aSubMesh->_is_nil())
00630           aMesh = aSubMesh->GetFather();
00631         
00632         if (!aMesh->_is_nil()) {    
00633           if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
00634             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
00635             if (res < SMESH::HYP_UNKNOWN_FATAL) {
00636               _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
00637               if (meshSO)
00638                 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
00639             }
00640             
00641           }
00642           else if(!aMesh->HasShapeToMesh()){
00643             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
00644             if (res < SMESH::HYP_UNKNOWN_FATAL) {
00645               _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
00646               if (meshSO)
00647                 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);              
00648             }
00649           }
00650           if (res > SMESH::HYP_OK) {
00651             wc.suspend();
00652             processHypothesisStatus(res, anHyp, false);
00653             wc.resume();
00654           }
00655         }
00656       } catch(const SALOME::SALOME_Exception& S_ex) {
00657         wc.suspend();
00658         SalomeApp_Tools::QtCatchCorbaException(S_ex);
00659         res = SMESH::HYP_UNKNOWN_FATAL;
00660       }
00661     }
00662     return res < SMESH::HYP_UNKNOWN_FATAL;
00663   }
00664 
00665   SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
00666   {
00667     SObjectList listSOmesh;
00668     listSOmesh.resize(0);
00669 
00670     unsigned int index = 0;
00671     if (!AlgoOrHyp->_is_nil()) {
00672       _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
00673       if (SO_Hypothesis) {
00674         SObjectList listSO =
00675           SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
00676 
00677         if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
00678         for (unsigned int i = 0; i < listSO.size(); i++) {
00679           _PTR(SObject) SO = listSO[i];
00680           if (SO) {
00681             _PTR(SObject) aFather = SO->GetFather();
00682             if (aFather) {
00683               _PTR(SObject) SOfatherFather = aFather->GetFather();
00684               if (SOfatherFather) {
00685                 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
00686                 index++;
00687                 listSOmesh.resize(index);
00688                 listSOmesh[index - 1] = SOfatherFather;
00689               }
00690             }
00691           }
00692         }
00693       }
00694     }
00695     if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
00696     return listSOmesh;
00697   }
00698 
00699 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
00700   QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
00701   {
00702     QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
00703     for ( int i = 0; i < errors.length(); ++i ) {
00704       const SMESH::AlgoStateError & error = errors[ i ];
00705       const bool hasAlgo = ( strlen( error.algoName ) != 0 );
00706       QString msg;
00707       if ( !hasAlgo )
00708         msg = QObject::tr( "STATE_ALGO_MISSING" );
00709       else 
00710         switch( error.state ) {
00711           CASE2MESSAGE( HYP_MISSING );
00712           CASE2MESSAGE( HYP_NOTCONFORM );
00713           CASE2MESSAGE( HYP_BAD_PARAMETER );
00714           CASE2MESSAGE( HYP_BAD_GEOMETRY );
00715         default: continue;
00716         }
00717       // apply args to message:
00718       // %1 - algo name
00719       if ( hasAlgo )
00720         msg = msg.arg( error.algoName.in() );
00721       // %2 - dimension
00722       msg = msg.arg( error.algoDim );
00723       // %3 - global/local
00724       msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
00725       // %4 - hypothesis dim == algoDim
00726       msg = msg.arg( error.algoDim );
00727 
00728       if ( i ) resMsg += ";\n";
00729       resMsg += msg;
00730     }
00731     return resMsg;
00732   }
00733 } // end of namespace SMESH