Salome HOME
Update in accordance with changes in QtxComboBox.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_HypothesesUtils.cxx
1 // Copyright (C) 2007-2016  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 = 0;
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(SMESH::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
515   bool IsApplicable(const QString&        aHypType,
516                     GEOM::GEOM_Object_ptr theGeomObject,
517                     const bool            toCheckAll)
518   {
519     if ( getenv("NO_LIMIT_ALGO_BY_SHAPE")) // allow a workaround for a case if
520       return true;                         // IsApplicable() returns false due to a bug
521
522     HypothesisData* aHypData = GetHypothesisData(aHypType);
523     QString aServLib = aHypData->ServerLibName;
524     return SMESHGUI::GetSMESHGen()->IsApplicable( aHypType.toLatin1().data(),
525                                                   aServLib.toLatin1().data(),
526                                                   theGeomObject,
527                                                   toCheckAll);
528   }
529
530
531   bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
532   {
533     if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
534     int res = SMESH::HYP_UNKNOWN_FATAL;
535     SUIT_OverrideCursor wc;
536
537     if (!aMesh->_is_nil()) {
538       _PTR(SObject) SM = SMESH::FindSObject(aMesh);
539       GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
540       try {
541         CORBA::String_var error;
542         res = aMesh->AddHypothesis(aShapeObject, aHyp, error.out());
543         if (res < SMESH::HYP_UNKNOWN_FATAL) {
544           _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
545           if (SM && aSH) {
546             SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
547           }
548         }
549         if (res > SMESH::HYP_OK) {
550           wc.suspend();
551           processHypothesisStatus(res, aHyp, true, error.in() );
552           wc.resume();
553         }
554       }
555       catch(const SALOME::SALOME_Exception& S_ex) {
556         wc.suspend();
557         SalomeApp_Tools::QtCatchCorbaException(S_ex);
558         res = SMESH::HYP_UNKNOWN_FATAL;
559       }
560     }
561     return res < SMESH::HYP_UNKNOWN_FATAL;
562   }
563
564
565   bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
566   {
567     if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
568     int res = SMESH::HYP_UNKNOWN_FATAL;
569     SUIT_OverrideCursor wc;
570
571     if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
572       try {
573         SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
574         _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
575         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
576         if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil())
577         {
578           CORBA::String_var error;
579           res = aMesh->AddHypothesis( aShapeObject, aHyp, error.out() );
580           if (res < SMESH::HYP_UNKNOWN_FATAL)  {
581             _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
582             if (meshSO)
583               SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
584           }
585           if (res > SMESH::HYP_OK) {
586             wc.suspend();
587             processHypothesisStatus( res, aHyp, true, error.in() );
588             wc.resume();
589           }
590         }
591         else {
592           SCRUTE(aHyp->_is_nil());
593           SCRUTE(aMesh->_is_nil());
594           SCRUTE(!SsubM);
595           SCRUTE(aShapeObject->_is_nil());
596         }
597       }
598       catch(const SALOME::SALOME_Exception& S_ex) {
599         wc.suspend();
600         SalomeApp_Tools::QtCatchCorbaException(S_ex);
601         res = SMESH::HYP_UNKNOWN_FATAL;
602       }
603     }
604     else {
605       SCRUTE(aSubMesh->_is_nil());
606       SCRUTE(aHyp->_is_nil());
607     }
608     return res < SMESH::HYP_UNKNOWN_FATAL;
609   }
610
611   bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
612   {
613     int res = SMESH::HYP_UNKNOWN_FATAL;
614     SUIT_OverrideCursor wc;
615
616     try {
617       _PTR(Study) aStudy = GetActiveStudyDocument();
618       _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
619       if( aHypObj )
620         {
621           _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
622           _PTR(SObject) aRealHypo;
623           if( aHypObj->ReferencedObject( aRealHypo ) )
624             {
625               SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
626               RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
627             }
628           else
629             {
630               SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
631               SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
632               for( size_t i = 0; i < meshList.size(); i++ )
633                 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
634             }
635         }
636     }
637     catch(const SALOME::SALOME_Exception& S_ex)
638       {
639         wc.suspend();
640         SalomeApp_Tools::QtCatchCorbaException(S_ex);
641         res = SMESH::HYP_UNKNOWN_FATAL;
642       }
643     return res < SMESH::HYP_UNKNOWN_FATAL;
644   }
645
646   bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject)               MorSM,
647                                           SMESH::SMESH_Hypothesis_ptr anHyp)
648   {
649     int res = SMESH::HYP_UNKNOWN_FATAL;
650     SUIT_OverrideCursor wc;
651
652     if (MorSM) {
653       try {
654         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
655         SMESH::SMESH_Mesh_var aMesh        = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
656         SMESH::SMESH_subMesh_var aSubMesh  = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
657
658         if (!aSubMesh->_is_nil())
659           aMesh = aSubMesh->GetFather();
660
661         if (!aMesh->_is_nil()) {
662           if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
663             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
664             if (res < SMESH::HYP_UNKNOWN_FATAL) {
665               _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
666               if (meshSO)
667                 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
668             }
669
670           }
671           else if(!aMesh->HasShapeToMesh()){
672             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
673             if (res < SMESH::HYP_UNKNOWN_FATAL) {
674               _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
675               if (meshSO)
676                 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
677             }
678           }
679           if (res > SMESH::HYP_OK) {
680             wc.suspend();
681             processHypothesisStatus(res, anHyp, false);
682             wc.resume();
683           }
684         }
685       } catch(const SALOME::SALOME_Exception& S_ex) {
686         wc.suspend();
687         SalomeApp_Tools::QtCatchCorbaException(S_ex);
688         res = SMESH::HYP_UNKNOWN_FATAL;
689       }
690     }
691     return res < SMESH::HYP_UNKNOWN_FATAL;
692   }
693
694   SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
695   {
696     SObjectList listSOmesh;
697     listSOmesh.resize(0);
698
699     unsigned int index = 0;
700     if (!AlgoOrHyp->_is_nil()) {
701       _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
702       if (SO_Hypothesis) {
703         SObjectList listSO =
704           SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
705
706         if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
707         for (unsigned int i = 0; i < listSO.size(); i++) {
708           _PTR(SObject) SO = listSO[i];
709           if (SO) {
710             _PTR(SObject) aFather = SO->GetFather();
711             if (aFather) {
712               _PTR(SObject) SOfatherFather = aFather->GetFather();
713               if (SOfatherFather) {
714                 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
715                 index++;
716                 listSOmesh.resize(index);
717                 listSOmesh[index - 1] = SOfatherFather;
718               }
719             }
720           }
721         }
722       }
723     }
724     if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
725     return listSOmesh;
726   }
727
728 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
729   QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
730   {
731     QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
732     for ( size_t i = 0; i < errors.length(); ++i ) {
733       const SMESH::AlgoStateError & error = errors[ i ];
734       const bool hasAlgo = ( strlen( error.algoName ) != 0 );
735       QString msg;
736       if ( !hasAlgo )
737         msg = QObject::tr( "STATE_ALGO_MISSING" );
738       else
739         switch( error.state ) {
740           CASE2MESSAGE( HYP_MISSING );
741           CASE2MESSAGE( HYP_NOTCONFORM );
742           CASE2MESSAGE( HYP_BAD_PARAMETER );
743           CASE2MESSAGE( HYP_BAD_GEOMETRY );
744         default: continue;
745         }
746       // apply args to message:
747       // %1 - algo name
748       if ( hasAlgo )
749         msg = msg.arg( error.algoName.in() );
750       // %2 - dimension
751       msg = msg.arg( error.algoDim );
752       // %3 - global/local
753       msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
754       // %4 - hypothesis dim == algoDim
755       msg = msg.arg( error.algoDim );
756
757       if ( i ) resMsg += ";\n";
758       resMsg += msg;
759     }
760     return resMsg;
761   }
762 } // end of namespace SMESH