Salome HOME
Update copyrights 2014.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_HypothesesUtils.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_HypothesesUtils.cxx
25 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_HypothesesUtils.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Hypotheses.h"
32 #include "SMESHGUI_XmlHandler.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_GEOMGenUtils.h"
35
36 // SALOME GUI includes
37 #include <SUIT_Desktop.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_OverrideCursor.h>
40 #include <SUIT_ResourceMgr.h>
41
42 #include <SalomeApp_Study.h>
43 #include <SalomeApp_Tools.h>
44
45 // SALOME KERNEL includes
46 #include <utilities.h>
47
48 // STL includes
49 #include <string>
50
51 // Qt includes
52 #include <QMap>
53 #include <QDir>
54 //#include <QList>
55
56
57 // Other includes
58 #ifdef WIN32
59 #include <windows.h>
60 #else
61 #include <dlfcn.h>
62 #endif
63
64 #ifdef WIN32
65 #define LibHandle HMODULE
66 #define LoadLib( name ) LoadLibrary( name )
67 #define GetProc GetProcAddress
68 #define UnLoadLib( handle ) FreeLibrary( handle );
69 #else
70 #define LibHandle void*
71 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
72 #define GetProc dlsym
73 #define UnLoadLib( handle ) dlclose( handle );
74 #endif
75
76 #ifdef _DEBUG_
77 static int MYDEBUG = 1;
78 #else
79 static int MYDEBUG = 0;
80 #endif
81
82 namespace SMESH
83 {
84   typedef IMap<QString,HypothesisData*> THypothesisDataMap;
85   THypothesisDataMap myHypothesesMap;
86   THypothesisDataMap myAlgorithmsMap;
87   
88   // BUG 0020378
89   //typedef QMap<QString,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
90   //THypCreatorMap myHypCreatorMap;
91
92   QList<HypothesesSet*> myListOfHypothesesSets;
93
94   void processHypothesisStatus(const int theHypStatus,
95                                SMESH::SMESH_Hypothesis_ptr theHyp,
96                                const bool theIsAddition)
97   {
98     if (theHypStatus > SMESH::HYP_OK) {
99       // get Hyp name
100       QString aHypName ("NULL Hypothesis");
101       if (!CORBA::is_nil(theHyp)) {
102         _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
103         if (Shyp)
104           // name in study
105           aHypName = Shyp->GetName().c_str();
106         else
107           // label in xml file
108           aHypName = GetHypothesisData(theHyp->GetName())->Label;
109       }
110
111       // message
112       bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
113       QString aMsg;
114       if (theIsAddition)
115         aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
116       else
117         aMsg = (isFatal ? "SMESH_CANT_RM_HYP"  : "SMESH_RM_HYP_WRN");
118
119       aMsg = QObject::tr(aMsg.toLatin1().data()).arg(aHypName) +
120         QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus).toLatin1().data());
121
122       if ( theHypStatus == SMESH::HYP_HIDDEN_ALGO ) // PAL18501
123         aMsg = aMsg.arg( GetHypothesisData(theHyp->GetName())->Dim[0] );
124
125       SUIT_MessageBox::warning(SMESHGUI::desktop(),
126                                QObject::tr("SMESH_WRN_WARNING"),
127                                aMsg);
128     }
129   }
130
131   //================================================================================
132   /*!
133    * \brief Prepends dimension and appends '[custom]' to the name of hypothesis set
134    */
135   //================================================================================
136
137   static QString mangledHypoSetName(HypothesesSet* hypSet)
138   {
139     QString name = hypSet->name();
140
141     // prepend 'xD: '
142     int dim = hypSet->maxDim();
143     if ( dim > -1 )
144       name = QString("%1D: %2").arg(dim).arg(name);
145
146     // custom
147     if ( hypSet->getIsCustom() )
148       name = QString("%1 [custom]").arg(name);
149
150     return name;
151   }
152
153   //================================================================================
154   /*!
155    * \brief Removes dimension and '[custom]' from the name of hypothesis set
156    */
157   //================================================================================
158
159   static QString demangledHypoSetName(QString name)
160   {
161     name.remove(QRegExp("[0-3]D: "));
162     name.remove(" [custom]");
163     return name;
164   }
165
166   void InitAvailableHypotheses()
167   {
168     SUIT_OverrideCursor wc;
169     if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
170       // Resource manager
171       SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
172       if (!resMgr) return;
173
174       // Find name of a resource XML file ("SMESH_Meshers.xml");
175       QString HypsXml;
176       char* cenv = getenv("SMESH_MeshersList");
177       if (cenv)
178         HypsXml.sprintf("%s", cenv);
179
180       QStringList HypsXmlList = HypsXml.split(":", QString::SkipEmptyParts);
181       if (HypsXmlList.count() == 0) {
182         SUIT_MessageBox::critical(SMESHGUI::desktop(),
183                                   QObject::tr("SMESH_WRN_WARNING"),
184                                   QObject::tr("MESHERS_FILE_NO_VARIABLE"));
185         return;
186       }
187       // get full names of xml files from HypsXmlList
188       QStringList xmlFiles;
189       xmlFiles.append( QDir::home().filePath("CustomMeshers.xml")); // may be inexistent
190       for (int i = 0; i < HypsXmlList.count(); i++) {
191         QString HypsXml = HypsXmlList[ i ];
192
193         // Find full path to the resource XML file
194         QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
195         if ( xmlFile.isEmpty() ) // try PLUGIN resources
196           xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
197         if ( !xmlFile.isEmpty() )
198           xmlFiles.append( xmlFile );
199       }
200
201       // loop on xmlFiles
202       QString aNoAccessFiles;
203       for (int i = 0; i < xmlFiles.count(); i++)
204       {
205         QString xmlFile = xmlFiles[ i ];
206         QFile file (xmlFile);
207         if (file.exists() && file.open(QIODevice::ReadOnly))
208         {
209           file.close();
210
211           SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
212           ASSERT(aXmlHandler);
213
214           QXmlInputSource source (&file);
215           QXmlSimpleReader reader;
216           reader.setContentHandler(aXmlHandler);
217           reader.setErrorHandler(aXmlHandler);
218           bool ok = reader.parse(source);
219           file.close();
220           if (ok) {
221
222             THypothesisDataMap::ConstIterator it1 = aXmlHandler->myHypothesesMap.begin();
223             
224             for( ;it1 != aXmlHandler->myHypothesesMap.end(); it1++)
225               myHypothesesMap.insert( it1.key(), it1.value() );
226             
227             it1 = aXmlHandler->myAlgorithmsMap.begin();
228             for( ;it1 != aXmlHandler->myAlgorithmsMap.end(); it1++)
229               myAlgorithmsMap.insert( it1.key(), it1.value() );
230             
231             QList<HypothesesSet*>::iterator it;
232             for ( it = aXmlHandler->myListOfHypothesesSets.begin(); 
233                   it != aXmlHandler->myListOfHypothesesSets.end();
234                   ++it )
235             {
236               (*it)->setIsCustom( i == 0 );
237               myListOfHypothesesSets.append( *it );
238             }
239           }
240           else {
241             SUIT_MessageBox::critical(SMESHGUI::desktop(),
242                                       QObject::tr("INF_PARSE_ERROR"),
243                                       QObject::tr(aXmlHandler->errorProtocol().toLatin1().data()));
244           }
245           delete aXmlHandler;
246         }
247         else if ( i > 0 ) { // 1st is ~/CustomMeshers.xml
248           if (aNoAccessFiles.isEmpty())
249             aNoAccessFiles = xmlFile;
250           else
251             aNoAccessFiles += ", " + xmlFile;
252         }
253       } // end loop on xmlFiles
254
255
256       if (!aNoAccessFiles.isEmpty()) {
257         QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
258         aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
259         wc.suspend();
260         SUIT_MessageBox::warning(SMESHGUI::desktop(),
261                                  QObject::tr("SMESH_WRN_WARNING"),
262                                  aMess);
263         wc.resume();
264       }
265     }
266   }
267
268
269   QStringList GetAvailableHypotheses( const bool isAlgo,
270                                       const int  theDim,
271                                       const bool isAux,
272                                       const bool isNeedGeometry,
273                                       const bool isSubMesh)
274   {
275     QStringList aHypList;
276
277     // Init list of available hypotheses, if needed
278     InitAvailableHypotheses();
279     bool checkGeometry = ( !isNeedGeometry && isAlgo );
280     const char* context = isSubMesh ? "LOCAL" : "GLOBAL";
281     // fill list of hypotheses/algorithms
282     THypothesisDataMap& pMap = isAlgo ? myAlgorithmsMap : myHypothesesMap;
283     THypothesisDataMap::ConstIterator anIter;
284     for ( anIter = pMap.begin(); anIter != pMap.end(); anIter++ ) {
285       HypothesisData* aData = anIter.value();
286       if(!aData || aData->Label.isEmpty()) continue;
287       if (( theDim < 0 || aData->Dim.contains( theDim )) &&
288           ( isAlgo || aData->IsAuxOrNeedHyp == isAux ) &&
289           ( aData->Context == "ANY" || aData->Context == context ))
290       {
291         if (checkGeometry) {
292           if (aData->IsNeedGeometry == isNeedGeometry)
293             aHypList.append(anIter.key());
294         }
295         else {
296           aHypList.append(anIter.key());
297         }
298       }
299     }
300     return aHypList;
301   }
302
303
304   QStringList GetHypothesesSets(int maxDim, const QString& MeshType)
305   {
306     QStringList aSetNameList;
307
308     // Init list of available hypotheses, if needed
309     InitAvailableHypotheses();
310     QList<HypothesesSet*>::iterator hypoSet;
311     for ( hypoSet  = myListOfHypothesesSets.begin();
312           hypoSet != myListOfHypothesesSets.end();
313           ++hypoSet ) {
314       HypothesesSet* aSet = *hypoSet;
315       bool isAvailable = false;
316       if ( !MeshType.isEmpty() )
317       {
318         if ( aSet->maxDim() != maxDim)
319           continue;
320         aSet->init( true );
321         while ( aSet->next(), aSet->more() )
322         {
323           if ( HypothesisData* hypData = SMESH::GetHypothesisData( aSet->current() ) )
324           {
325             QStringList::const_iterator inElemType = hypData->OutputTypes.begin();
326             for ( ; inElemType != hypData->OutputTypes.end(); inElemType++ )
327             {
328               if ( *inElemType == MeshType ){
329                 isAvailable = true;
330                 break;
331               }
332             }
333           }
334           if ( isAvailable ) break;
335         }
336       }
337       else if ( aSet && ( aSet->count( true ) || aSet->count( false )) &&
338                 aSet->maxDim() <= maxDim)
339       {
340         isAvailable = true;
341       }
342       if ( isAvailable ) aSetNameList.append( mangledHypoSetName( aSet ));
343     }
344     aSetNameList.removeDuplicates();
345     aSetNameList.sort();
346
347     //  reverse order of aSetNameList
348     QStringList reversedNames;
349     for ( int i = 0; i < aSetNameList.count(); ++i )
350       reversedNames.prepend( aSetNameList[i] );
351     
352     return reversedNames;
353   }
354
355   HypothesesSet* GetHypothesesSet(const QString& theSetName)
356   {
357     QString name = demangledHypoSetName( theSetName );
358     QList<HypothesesSet*>::iterator hypoSet;
359     for ( hypoSet  = myListOfHypothesesSets.begin(); 
360           hypoSet != myListOfHypothesesSets.end();
361           ++hypoSet ) {
362       HypothesesSet* aSet = *hypoSet;
363       if ( aSet && aSet->name() == name )
364         return aSet;
365     }
366     return 0;
367   }
368
369   HypothesisData* GetHypothesisData (const QString& aHypType)
370   {
371     HypothesisData* aHypData = 0;
372
373     // Init list of available hypotheses, if needed
374     InitAvailableHypotheses();
375
376     if (myHypothesesMap.contains(aHypType)) {
377       aHypData = myHypothesesMap[aHypType];
378     }
379     else if (myAlgorithmsMap.contains(aHypType)) {
380       aHypData = myAlgorithmsMap[aHypType];
381     }
382     return aHypData;
383   }
384
385   bool IsAvailableHypothesis(const HypothesisData* algoData,
386                              const QString&        hypType,
387                              bool&                 isAuxiliary)
388   {
389     isAuxiliary = false;
390     if ( !algoData )
391       return false;
392     if ( algoData->BasicHypos.contains( hypType ))
393       return true;
394     if ( algoData->OptionalHypos.contains( hypType)) {
395       isAuxiliary = true;
396       return true;
397     }
398     return false;
399   }
400
401   bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
402                              const HypothesisData* algo2Data)
403   {
404     if ( !algo1Data || !algo2Data )
405       return false;
406     const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
407     if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
408       algoIn = algo2Data; algoMain = algo1Data;
409     }
410     // look for any output type of algoIn between input types of algoMain
411     QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
412     for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
413       if ( algoMain->InputTypes.contains( *inElemType ))
414         return true;
415     return false;
416   }
417
418   SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const QString& aHypType)
419   {
420     if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType.toLatin1().data());
421
422     SMESHGUI_GenericHypothesisCreator* aCreator = 0;
423
424     // check, if creator for this hypothesis type already exists
425     // BUG 0020378
426     //if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
427     //  aCreator = myHypCreatorMap[aHypType];
428     //}
429     //else
430     {
431       // 1. Init list of available hypotheses, if needed
432       InitAvailableHypotheses();
433
434       // 2. Get names of plugin libraries
435       HypothesisData* aHypData = GetHypothesisData(aHypType);
436       if (!aHypData) 
437         return aCreator;
438
439       QString aClientLibName = aHypData->ClientLibName;
440       QString aServerLibName = aHypData->ServerLibName;
441
442       // 3. Load Client Plugin Library
443       try {
444         // load plugin library
445         if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
446         LibHandle libHandle = LoadLib( aClientLibName.toLatin1().data() );
447         if (!libHandle) {
448           // report any error, if occured
449           {
450 #ifdef WIN32
451             const char* anError = "Can't load client meshers plugin library";
452 #else
453             const char* anError = dlerror();      
454 #endif
455             INFOS(anError); // always display this kind of error !
456           }
457         }
458         else {
459           // get method, returning hypothesis creator
460           if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
461           typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
462             ( const QString& );
463           GetHypothesisCreator procHandle =
464             (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
465           if (!procHandle) {
466             if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
467             UnLoadLib(libHandle);
468           }
469           else {
470             // get hypothesis creator
471             if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
472             aCreator = procHandle( aHypType );
473             if (!aCreator) {
474               if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
475             }
476             else {
477               // map hypothesis creator to a hypothesis name
478               // BUG 0020378
479               //myHypCreatorMap[aHypType] = aCreator;
480
481               //rnv : This dynamic property of the QObject stores the name of the plugin.
482               //      It is used to obtain plugin root dir environment variable
483               //      in the SMESHGUI_HypothesisDlg class. Plugin root dir environment 
484               //      variable is used to display documentation.
485               aCreator->setProperty(PLUGIN_NAME,aHypData->PluginName);
486             }
487           }
488         }
489       }
490       catch (const SALOME::SALOME_Exception& S_ex) {
491         SalomeApp_Tools::QtCatchCorbaException(S_ex);
492       }
493     }
494
495     return aCreator;
496   }
497
498
499   SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
500                                                const QString& aHypName,
501                                                const bool isAlgo)
502   {
503     if(MYDEBUG) MESSAGE("Create " << aHypType.toLatin1().data() << 
504                         " with name " << aHypName.toLatin1().data());
505     HypothesisData* aHypData = GetHypothesisData(aHypType);
506     QString aServLib = aHypData->ServerLibName;
507     try {
508       SMESH::SMESH_Hypothesis_var aHypothesis;
509       aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
510                                                               aServLib.toLatin1().data());
511       if (!aHypothesis->_is_nil()) {
512         _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
513         if (aHypSObject) {
514           if (!aHypName.isEmpty())
515             SMESH::SetName(aHypSObject, aHypName);
516           SMESHGUI::Modified();
517           SMESHGUI::GetSMESHGUI()->updateObjBrowser();
518           return aHypothesis._retn();
519         }
520       }
521     } catch (const SALOME::SALOME_Exception & S_ex) {
522       SalomeApp_Tools::QtCatchCorbaException(S_ex);
523     }
524
525     return SMESH::SMESH_Hypothesis::_nil();
526   }
527
528
529   bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
530   {
531     if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
532     int res = SMESH::HYP_UNKNOWN_FATAL;
533     SUIT_OverrideCursor wc;
534
535     if (!aMesh->_is_nil()) {
536       _PTR(SObject) SM = SMESH::FindSObject(aMesh);
537       GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
538       try {
539         res = aMesh->AddHypothesis(aShapeObject, aHyp);
540         if (res < SMESH::HYP_UNKNOWN_FATAL) {
541           _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
542           if (SM && aSH) {
543             SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
544           }
545         }
546         if (res > SMESH::HYP_OK) {
547           wc.suspend();
548           processHypothesisStatus(res, aHyp, true);
549           wc.resume();
550         }
551       }
552       catch(const SALOME::SALOME_Exception& S_ex) {
553         wc.suspend();
554         SalomeApp_Tools::QtCatchCorbaException(S_ex);
555         res = SMESH::HYP_UNKNOWN_FATAL;
556       }
557     }
558     return res < SMESH::HYP_UNKNOWN_FATAL;
559   }
560
561
562   bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
563   {
564     if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
565     int res = SMESH::HYP_UNKNOWN_FATAL;
566     SUIT_OverrideCursor wc;
567
568     if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
569       try {
570         SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
571         _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
572         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
573         if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
574           res = aMesh->AddHypothesis(aShapeObject, aHyp);
575           if (res < SMESH::HYP_UNKNOWN_FATAL)  {
576             _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
577             if (meshSO)
578               SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
579           }
580           if (res > SMESH::HYP_OK) {
581             wc.suspend();
582             processHypothesisStatus(res, aHyp, true);
583             wc.resume();
584           }
585         }
586         else {
587           SCRUTE(aHyp->_is_nil());
588           SCRUTE(aMesh->_is_nil());
589           SCRUTE(!SsubM);
590           SCRUTE(aShapeObject->_is_nil());
591         }
592       }
593       catch(const SALOME::SALOME_Exception& S_ex) {
594         wc.suspend();
595         SalomeApp_Tools::QtCatchCorbaException(S_ex);
596         res = SMESH::HYP_UNKNOWN_FATAL;
597       }
598     }
599     else {
600       SCRUTE(aSubMesh->_is_nil());
601       SCRUTE(aHyp->_is_nil());
602     }
603     return res < SMESH::HYP_UNKNOWN_FATAL;
604   }
605
606   bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
607   {
608     int res = SMESH::HYP_UNKNOWN_FATAL;
609     SUIT_OverrideCursor wc;
610
611     try {
612       _PTR(Study) aStudy = GetActiveStudyDocument();
613       _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
614       if( aHypObj )
615         {
616           _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
617           _PTR(SObject) aRealHypo;
618           if( aHypObj->ReferencedObject( aRealHypo ) )
619             {
620               SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
621               RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
622             }
623           else
624             {
625               SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
626               SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
627               for( int i = 0; i < meshList.size(); i++ )
628                 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
629             }
630         }
631     }
632     catch(const SALOME::SALOME_Exception& S_ex)
633       {
634         wc.suspend();
635         SalomeApp_Tools::QtCatchCorbaException(S_ex);
636         res = SMESH::HYP_UNKNOWN_FATAL;
637       }
638     return res < SMESH::HYP_UNKNOWN_FATAL;
639   }
640
641   bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject)               MorSM,
642                                           SMESH::SMESH_Hypothesis_ptr anHyp)
643   {
644     int res = SMESH::HYP_UNKNOWN_FATAL;
645     SUIT_OverrideCursor wc;
646
647     if (MorSM) {
648       try {
649         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
650         SMESH::SMESH_Mesh_var aMesh        = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
651         SMESH::SMESH_subMesh_var aSubMesh  = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
652
653         if (!aSubMesh->_is_nil())
654           aMesh = aSubMesh->GetFather();
655
656         if (!aMesh->_is_nil()) {    
657           if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
658             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
659             if (res < SMESH::HYP_UNKNOWN_FATAL) {
660               _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
661               if (meshSO)
662                 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
663             }
664             
665           }
666           else if(!aMesh->HasShapeToMesh()){
667             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
668             if (res < SMESH::HYP_UNKNOWN_FATAL) {
669               _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
670               if (meshSO)
671                 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);              
672             }
673           }
674           if (res > SMESH::HYP_OK) {
675             wc.suspend();
676             processHypothesisStatus(res, anHyp, false);
677             wc.resume();
678           }
679         }
680       } catch(const SALOME::SALOME_Exception& S_ex) {
681         wc.suspend();
682         SalomeApp_Tools::QtCatchCorbaException(S_ex);
683         res = SMESH::HYP_UNKNOWN_FATAL;
684       }
685     }
686     return res < SMESH::HYP_UNKNOWN_FATAL;
687   }
688
689   SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
690   {
691     SObjectList listSOmesh;
692     listSOmesh.resize(0);
693
694     unsigned int index = 0;
695     if (!AlgoOrHyp->_is_nil()) {
696       _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
697       if (SO_Hypothesis) {
698         SObjectList listSO =
699           SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
700
701         if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
702         for (unsigned int i = 0; i < listSO.size(); i++) {
703           _PTR(SObject) SO = listSO[i];
704           if (SO) {
705             _PTR(SObject) aFather = SO->GetFather();
706             if (aFather) {
707               _PTR(SObject) SOfatherFather = aFather->GetFather();
708               if (SOfatherFather) {
709                 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
710                 index++;
711                 listSOmesh.resize(index);
712                 listSOmesh[index - 1] = SOfatherFather;
713               }
714             }
715           }
716         }
717       }
718     }
719     if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
720     return listSOmesh;
721   }
722
723 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
724   QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
725   {
726     QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
727     for ( int i = 0; i < errors.length(); ++i ) {
728       const SMESH::AlgoStateError & error = errors[ i ];
729       const bool hasAlgo = ( strlen( error.algoName ) != 0 );
730       QString msg;
731       if ( !hasAlgo )
732         msg = QObject::tr( "STATE_ALGO_MISSING" );
733       else 
734         switch( error.state ) {
735           CASE2MESSAGE( HYP_MISSING );
736           CASE2MESSAGE( HYP_NOTCONFORM );
737           CASE2MESSAGE( HYP_BAD_PARAMETER );
738           CASE2MESSAGE( HYP_BAD_GEOMETRY );
739         default: continue;
740         }
741       // apply args to message:
742       // %1 - algo name
743       if ( hasAlgo )
744         msg = msg.arg( error.algoName.in() );
745       // %2 - dimension
746       msg = msg.arg( error.algoDim );
747       // %3 - global/local
748       msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
749       // %4 - hypothesis dim == algoDim
750       msg = msg.arg( error.algoDim );
751
752       if ( i ) resMsg += ";\n";
753       resMsg += msg;
754     }
755     return resMsg;
756   }
757 } // end of namespace SMESH