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