Salome HOME
Updated copyright comment
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_HypothesesUtils.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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_GEOMGenUtils.h"
32 #include "SMESHGUI_Hypotheses.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35 #include "SMESHGUI_XmlHandler.h"
36
37 #include <SMESH_Actor.h>
38 #include <SMESH_TryCatch.hxx>
39
40 // SALOME GUI includes
41 #include <SUIT_Desktop.h>
42 #include <SUIT_MessageBox.h>
43 #include <SUIT_OverrideCursor.h>
44 #include <SUIT_ResourceMgr.h>
45
46 #include <SalomeApp_Study.h>
47 #include <SalomeApp_Tools.h>
48
49 // SALOME KERNEL includes
50 #include <utilities.h>
51
52 // STL includes
53 #include <string>
54
55 // Qt includes
56 #include <QDir>
57
58
59 // Other includes
60 #ifdef WIN32
61 #include <windows.h>
62 #else
63 #include <dlfcn.h>
64 #endif
65
66 #ifdef WIN32
67  #define LibHandle HMODULE
68  #define LoadLib( name ) LoadLibrary( name )
69  #define GetProc GetProcAddress
70  #define UnLoadLib( handle ) FreeLibrary( handle );
71  #define env_sep ";"
72 #else // WIN32
73  #define LibHandle void*
74  #ifdef DYNLOAD_LOCAL
75   #define LoadLib( name ) dlopen( name, RTLD_LAZY | RTLD_LOCAL )
76  #else // DYNLOAD_LOCAL
77   #define LoadLib( name ) dlopen( name, RTLD_LAZY | RTLD_GLOBAL )
78  #endif // DYNLOAD_LOCAL
79  #define GetProc dlsym
80  #define UnLoadLib( handle ) dlclose( handle );
81  #define env_sep ":"
82 #endif // WIN32
83
84 namespace SMESH
85 {
86   typedef IMap<QString,HypothesisData*> THypothesisDataMap;
87   THypothesisDataMap myHypothesesMap;
88   THypothesisDataMap myAlgorithmsMap;
89
90   // BUG 0020378
91   //typedef QMap<QString,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
92   //THypCreatorMap myHypCreatorMap;
93
94   QList<HypothesesSet*> myListOfHypothesesSets;
95
96   void processHypothesisStatus(const int                   theHypStatus,
97                                SMESH::SMESH_Hypothesis_ptr theHyp,
98                                const bool                  theIsAddition,
99                                const char*                 theError = 0)
100   {
101     if (theHypStatus > SMESH::HYP_OK) {
102       // get Hyp name
103       QString aHypName ("NULL Hypothesis");
104       if (!CORBA::is_nil(theHyp)) {
105         _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
106         if (Shyp) {
107           // name in study
108           aHypName = Shyp->GetName().c_str();
109         }
110         else {
111           // label in xml file
112           CORBA::String_var hypType = theHyp->GetName();
113           aHypName = GetHypothesisData( hypType.in() )->Label;
114         }
115       }
116
117       // message
118       bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
119       QString aMsg;
120       if (theIsAddition)
121         aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
122       else
123         aMsg = (isFatal ? "SMESH_CANT_RM_HYP"  : "SMESH_RM_HYP_WRN");
124
125       aMsg = QObject::tr(aMsg.toLatin1().data()).arg(aHypName);
126
127       if ( theError && theError[0] )
128       {
129         aMsg += theError;
130       }
131       else
132       {
133         aMsg += QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus).toLatin1().data());
134
135         if ( theHypStatus == SMESH::HYP_HIDDEN_ALGO ) { // PAL18501
136           CORBA::String_var hypType = theHyp->GetName();
137           if ( HypothesisData* hd = GetHypothesisData( hypType.in() ))
138             aMsg = aMsg.arg( hd->Dim[0] );
139         }
140       }
141       SUIT_MessageBox::warning(SMESHGUI::desktop(),
142                                QObject::tr("SMESH_WRN_WARNING"),
143                                aMsg);
144     }
145   }
146
147   //================================================================================
148   /*!
149    * \brief Prepends dimension and appends '[custom]' to the name of hypothesis set
150    */
151   //================================================================================
152
153   static QString mangledHypoSetName(HypothesesSet* hypSet)
154   {
155     QString name = hypSet->name();
156
157     // prepend 'xD: '
158     int dim = hypSet->maxDim();
159     if ( dim > -1 )
160       name = QString("%1D: %2").arg(dim).arg(name);
161
162     // custom
163     if ( hypSet->getIsCustom() )
164       name = QString("%1 [custom]").arg(name);
165
166     return name;
167   }
168
169   //================================================================================
170   /*!
171    * \brief Removes dimension and '[custom]' from the name of hypothesis set
172    */
173   //================================================================================
174
175   static QString demangledHypoSetName(QString name)
176   {
177     name.remove(QRegExp("[0-3]D: "));
178     name.remove(" [custom]");
179     return name;
180   }
181
182   void InitAvailableHypotheses()
183   {
184     SUIT_OverrideCursor wc;
185     if ( myHypothesesMap.empty() && myAlgorithmsMap.empty() )
186     {
187       // Resource manager
188       SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
189       if (!resMgr) return;
190
191       // Find name of a resource XML file ("SMESH_Meshers.xml");
192       QString HypsXml;
193       char* cenv = getenv("SMESH_MeshersList");
194       if (cenv)
195         HypsXml.sprintf("%s", cenv);
196
197       QStringList HypsXmlList = HypsXml.split(env_sep, QString::SkipEmptyParts);
198       if (HypsXmlList.count() == 0) {
199         SUIT_MessageBox::critical(SMESHGUI::desktop(),
200                                   QObject::tr("SMESH_WRN_WARNING"),
201                                   QObject::tr("MESHERS_FILE_NO_VARIABLE"));
202         return;
203       }
204       // get full names of xml files from HypsXmlList
205       QStringList xmlFiles;
206       xmlFiles.append( QDir::home().filePath("CustomMeshers.xml")); // may be inexistent
207       for (int i = 0; i < HypsXmlList.count(); i++) {
208         QString HypsXml = HypsXmlList[ i ];
209
210         // Find full path to the resource XML file
211         QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
212         if ( xmlFile.isEmpty() ) // try PLUGIN resources
213           xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
214         if ( !xmlFile.isEmpty() )
215           xmlFiles.append( xmlFile );
216       }
217
218       // loop on xmlFiles
219       QString aNoAccessFiles;
220       for (int i = 0; i < xmlFiles.count(); i++)
221       {
222         QString xmlFile = xmlFiles[ i ];
223         QFile file (xmlFile);
224         if (file.exists() && file.open(QIODevice::ReadOnly))
225         {
226           file.close();
227
228           SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
229           ASSERT(aXmlHandler);
230
231           QXmlInputSource source (&file);
232           QXmlSimpleReader reader;
233           reader.setContentHandler(aXmlHandler);
234           reader.setErrorHandler(aXmlHandler);
235           bool ok = reader.parse(source);
236           file.close();
237           if (ok) {
238
239             THypothesisDataMap::ConstIterator it1 = aXmlHandler->myHypothesesMap.begin();
240
241             for( ;it1 != aXmlHandler->myHypothesesMap.end(); it1++)
242               myHypothesesMap.insert( it1.key(), it1.value() );
243
244             it1 = aXmlHandler->myAlgorithmsMap.begin();
245             for( ;it1 != aXmlHandler->myAlgorithmsMap.end(); it1++)
246               myAlgorithmsMap.insert( it1.key(), it1.value() );
247
248             QList<HypothesesSet*>::iterator it;
249             for ( it = aXmlHandler->myListOfHypothesesSets.begin();
250                   it != aXmlHandler->myListOfHypothesesSets.end();
251                   ++it )
252             {
253               (*it)->setIsCustom( i == 0 );
254               myListOfHypothesesSets.append( *it );
255             }
256           }
257           else {
258             SUIT_MessageBox::critical(SMESHGUI::desktop(),
259                                       QObject::tr("INF_PARSE_ERROR"),
260                                       QObject::tr(aXmlHandler->errorProtocol().toLatin1().data()));
261           }
262           delete aXmlHandler;
263         }
264         else if ( i > 0 ) { // 1st is ~/CustomMeshers.xml
265           if (aNoAccessFiles.isEmpty())
266             aNoAccessFiles = xmlFile;
267           else
268             aNoAccessFiles += ", " + xmlFile;
269         }
270       } // end loop on xmlFiles
271
272
273       if (!aNoAccessFiles.isEmpty()) {
274         QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
275         aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
276         wc.suspend();
277         SUIT_MessageBox::warning(SMESHGUI::desktop(),
278                                  QObject::tr("SMESH_WRN_WARNING"),
279                                  aMess);
280         wc.resume();
281       }
282     }
283   }
284
285
286   QStringList GetAvailableHypotheses( const bool isAlgo,
287                                       const int  theDim,
288                                       const bool isAux,
289                                       const bool hasGeometry,
290                                       const bool isSubMesh)
291   {
292     QStringList aHypList;
293
294     // Init list of available hypotheses, if needed
295     InitAvailableHypotheses();
296     bool checkGeometry = ( isAlgo );
297     const char* context = isSubMesh ? "LOCAL" : "GLOBAL";
298     // fill list of hypotheses/algorithms
299     THypothesisDataMap& pMap = isAlgo ? myAlgorithmsMap : myHypothesesMap;
300     THypothesisDataMap::ConstIterator anIter;
301     for ( anIter = pMap.begin(); anIter != pMap.end(); anIter++ )
302     {
303       HypothesisData* aData = anIter.value();
304       if (( aData && !aData->Label.isEmpty() ) &&
305           ( theDim < 0              || aData->Dim.contains( theDim )) &&
306           ( isAlgo                  || aData->IsAuxOrNeedHyp == isAux ) &&
307           ( aData->Context == "ANY" || aData->Context == context ) &&
308           ( !checkGeometry          || (!aData->IsNeedGeometry  ||
309                                         ( aData->IsNeedGeometry > 0 ) == hasGeometry)))
310       {
311         aHypList.append(anIter.key());
312       }
313     }
314     return aHypList;
315   }
316
317
318   QStringList GetHypothesesSets(int maxDim)
319   {
320     QStringList aSetNameList;
321
322     // Init list of available hypotheses, if needed
323     InitAvailableHypotheses();
324     QList<HypothesesSet*>::iterator hypoSet;
325     for ( hypoSet  = myListOfHypothesesSets.begin();
326         hypoSet != myListOfHypothesesSets.end();
327         ++hypoSet ) {
328       HypothesesSet* aSet = *hypoSet;
329       if ( aSet && 0 <= aSet->maxDim() && aSet->maxDim() <= maxDim )
330       {
331         aSetNameList.append( mangledHypoSetName( aSet ));
332       }
333     }
334     aSetNameList.removeDuplicates();
335     aSetNameList.sort();
336
337     //  reverse order of aSetNameList
338     QStringList reversedNames;
339     for ( int i = 0; i < aSetNameList.count(); ++i )
340       reversedNames.prepend( aSetNameList[i] );
341
342     return reversedNames;
343   }
344
345   HypothesesSet* GetHypothesesSet(const QString& theSetName)
346   {
347     QString name = demangledHypoSetName( theSetName );
348     QList<HypothesesSet*>::iterator hypoSet;
349     for ( hypoSet  = myListOfHypothesesSets.begin();
350           hypoSet != myListOfHypothesesSets.end();
351           ++hypoSet ) {
352       HypothesesSet* aSet = *hypoSet;
353       if ( aSet && aSet->name() == name )
354         return aSet;
355     }
356     return 0;
357   }
358
359   HypothesisData* GetHypothesisData (const QString& aHypType)
360   {
361     HypothesisData* aHypData = 0;
362
363     // Init list of available hypotheses, if needed
364     InitAvailableHypotheses();
365
366     if (myHypothesesMap.contains(aHypType)) {
367       aHypData = myHypothesesMap[aHypType];
368     }
369     else if (myAlgorithmsMap.contains(aHypType)) {
370       aHypData = myAlgorithmsMap[aHypType];
371     }
372     return aHypData;
373   }
374
375   //================================================================================
376   /*!
377    * \brief Return the HypothesisData holding a name of a group of hypotheses
378    *        a given hypothesis belongs to
379    */
380   //================================================================================
381
382   HypothesisData* GetGroupTitle( const HypothesisData* hyp, const bool isAlgo )
383   {
384     static std::vector< std::vector< HypothesisData > > theGroups;
385     if ( theGroups.empty() )
386     {
387       theGroups.resize(14); // 14: isAlgo * 10 + dim
388
389       QString dummyS("GROUP");
390       QList<int> dummyIL; dummyIL << 1;
391       QStringList dummySL;
392       HypothesisData group( dummyS,dummyS,dummyS,dummyS,dummyS,dummyS,dummyS,-1,-1,
393                             dummyIL, 0, dummySL,dummySL,dummySL,dummySL,0,0 );
394       // no group
395       int key = 0;
396       theGroups[ key ].push_back( group );
397
398       // 1D algo
399       key = 11;
400       //        0: Basic
401       group.Label = "GROUP:" + QObject::tr( "SMESH_1D_ALGO_GROUP_BASIC" );
402       theGroups[ key ].push_back( group );
403       //        1: Advanced
404       group.Label = "GROUP:" + QObject::tr( "SMESH_1D_ALGO_GROUP_ADVANCED" );
405       theGroups[ key ].push_back( group );
406
407       // 1D hypotheses
408       key = 01;
409       //        0: Basic
410       group.Label = "GROUP:" + QObject::tr( "SMESH_1D_HYP_GROUP_BASIC" );
411       theGroups[ key ].push_back( group );
412       //        1: Progression
413       group.Label = "GROUP:" + QObject::tr( "SMESH_1D_HYP_GROUP_PROGRESSION" );
414       theGroups[ key ].push_back( group );
415       //        2: Advanced
416       group.Label = "GROUP:" + QObject::tr( "SMESH_1D_HYP_GROUP_ADVANCED" );
417       theGroups[ key ].push_back( group );
418
419       // 2D algo
420       key = 12;
421       //        0: Regular
422       group.Label = "GROUP:" + QObject::tr( "SMESH_2D_ALGO_GROUP_REGULAR" );
423       theGroups[ key ].push_back( group );
424       //        1: Free
425       group.Label = "GROUP:" + QObject::tr( "SMESH_2D_ALGO_GROUP_FREE" );
426       theGroups[ key ].push_back( group );
427       //        2: Advanced
428       group.Label = "GROUP:" + QObject::tr( "SMESH_2D_ALGO_GROUP_ADVANCED" );
429       theGroups[ key ].push_back( group );
430
431       // 3D algo
432       key = 13;
433       //        0: Regular
434       group.Label = "GROUP:" + QObject::tr( "SMESH_3D_ALGO_GROUP_REGULAR" );
435       theGroups[ key ].push_back( group );
436       //        1: Free
437       group.Label = "GROUP:" + QObject::tr( "SMESH_3D_ALGO_GROUP_FREE" );
438       theGroups[ key ].push_back( group );
439       //        2: Advanced
440       group.Label = "GROUP:" + QObject::tr( "SMESH_3D_ALGO_GROUP_ADVANCED" );
441       theGroups[ key ].push_back( group );
442     }
443
444     size_t key = 0, groupID = 0;
445     if ( hyp && !hyp->Dim.isEmpty() )
446     {
447       key     = hyp->Dim[0] + isAlgo * 10;
448       groupID = hyp->GroupID;
449     }
450
451     if ( key < theGroups.size() && !theGroups[ key ].empty() )
452     {
453       std::vector< HypothesisData > & group = theGroups[ key ];
454       return & ( groupID < group.size() ? group[ groupID ] : group.back() );
455     }
456     return & theGroups[ 0 ][ 0 ];
457   }
458
459   bool IsAvailableHypothesis(const HypothesisData* algoData,
460                              const QString&        hypType,
461                              bool&                 isAuxiliary)
462   {
463     isAuxiliary = false;
464     if ( !algoData )
465       return false;
466     if ( algoData->BasicHypos.contains( hypType ))
467       return true;
468     if ( algoData->OptionalHypos.contains( hypType )) {
469       isAuxiliary = true;
470       return true;
471     }
472     return false;
473   }
474
475   bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
476                              const HypothesisData* algo2Data)
477   {
478     if ( !algo1Data || !algo2Data )
479       return false;
480     const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
481     if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
482       algoIn = algo2Data; algoMain = algo1Data;
483     }
484     // look for any output type of algoIn between input types of algoMain
485     QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
486     for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
487       if ( algoMain->InputTypes.contains( *inElemType ))
488         return true;
489     return false;
490   }
491
492   SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const QString& aHypType)
493   {
494     MESSAGE("Get HypothesisCreator for " << aHypType.toLatin1().data());
495
496     SMESHGUI_GenericHypothesisCreator* aCreator = 0;
497
498     // check, if creator for this hypothesis type already exists
499     // BUG 0020378
500     //if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
501     //  aCreator = myHypCreatorMap[aHypType];
502     //}
503     //else
504     {
505       // 1. Init list of available hypotheses, if needed
506       InitAvailableHypotheses();
507
508       // 2. Get names of plugin libraries
509       HypothesisData* aHypData = GetHypothesisData(aHypType);
510       if (!aHypData)
511         return aCreator;
512
513       QString aClientLibName = aHypData->ClientLibName;
514       QString aServerLibName = aHypData->ServerLibName;
515
516       // 3. Load Client Plugin Library
517       try {
518         // load plugin library
519         MESSAGE("Loading client meshers plugin library ...");
520 #ifdef WIN32
521 #ifdef UNICODE
522         LPTSTR path = new TCHAR[aClientLibName.length() + 1];
523         path[aClientLibName.toWCharArray(path)] = '\0';
524 #else
525         QByteArray baPath = aClientLibName.toUtf8();
526         const char* path = baPath.data();
527 #endif
528 #else
529         QByteArray baPath = aClientLibName.toUtf8();
530         char* path = baPath.data();
531 #endif
532         LibHandle libHandle = LoadLib( path );
533 #if defined(WIN32) && defined(UNICODE)
534       delete path;
535 #endif
536         if (!libHandle) {
537           // report any error, if occurred
538           {
539 #ifdef WIN32
540             const char* anError = "Can't load client meshers plugin library";
541 #else
542             const char* anError = dlerror();
543 #endif
544             INFOS(anError); // always display this kind of error !
545           }
546         }
547         else {
548           // get method, returning hypothesis creator
549           MESSAGE("Find GetHypothesisCreator() method ...");
550           typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
551             ( const QString& );
552           GetHypothesisCreator procHandle =
553             (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
554           if (!procHandle) {
555             MESSAGE("bad hypothesis client plugin library");
556             UnLoadLib(libHandle);
557           }
558           else {
559             // get hypothesis creator
560             MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
561             aCreator = procHandle( aHypType );
562             if (!aCreator) {
563               MESSAGE("no such a hypothesis in this plugin");
564             }
565             else {
566               // map hypothesis creator to a hypothesis name
567               // BUG 0020378
568               //myHypCreatorMap[aHypType] = aCreator;
569
570               //rnv : This dynamic property of the QObject stores the name of the plugin.
571               //      It is used to obtain plugin root dir environment variable
572               //      in the SMESHGUI_HypothesisDlg class. Plugin root dir environment
573               //      variable is used to display documentation.
574               aCreator->setProperty(SMESH::Plugin_Name(),aHypData->PluginName);
575             }
576           }
577         }
578       }
579       catch (const SALOME::SALOME_Exception& S_ex) {
580         SalomeApp_Tools::QtCatchCorbaException(S_ex);
581       }
582     }
583
584     return aCreator;
585   }
586
587
588   SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
589                                                const QString& aHypName,
590                                                const bool /*isAlgo*/)
591   {
592     MESSAGE("Create " << aHypType.toLatin1().data() <<
593                         " with name " << aHypName.toLatin1().data());
594     HypothesisData* aHypData = GetHypothesisData(aHypType);
595     QString aServLib = aHypData->ServerLibName;
596     try {
597       SMESH::SMESH_Hypothesis_var aHypothesis;
598       aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
599                                                               aServLib.toUtf8().data());
600       if (!aHypothesis->_is_nil()) {
601         _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
602         if (aHypSObject) {
603           if (!aHypName.isEmpty())
604             SMESH::SetName(aHypSObject, aHypName);
605           SMESHGUI::Modified();
606           SMESHGUI::GetSMESHGUI()->updateObjBrowser();
607           return aHypothesis._retn();
608         }
609       }
610     } catch (const SALOME::SALOME_Exception & S_ex) {
611       SalomeApp_Tools::QtCatchCorbaException(S_ex);
612     }
613
614     return SMESH::SMESH_Hypothesis::_nil();
615   }
616
617   bool IsApplicable(const QString&        aHypType,
618                     GEOM::GEOM_Object_ptr theGeomObject,
619                     const bool            toCheckAll)
620   {
621     if ( getenv("NO_LIMIT_ALGO_BY_SHAPE")) // allow a workaround for a case if
622       return true;                         // IsApplicable() returns false due to a bug
623
624     HypothesisData* aHypData = GetHypothesisData(aHypType);
625     QString aServLib = aHypData->ServerLibName;
626     SMESH_TRY;
627     return SMESHGUI::GetSMESHGen()->IsApplicable( aHypType.toLatin1().data(),
628                                                   aServLib.toUtf8().data(),
629                                                   theGeomObject,
630                                                   toCheckAll);
631     SMESH_CATCH( SMESH::printErrorInDebugMode );
632     return false;
633   }
634
635
636   bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
637   {
638     MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
639     int res = SMESH::HYP_UNKNOWN_FATAL;
640     SUIT_OverrideCursor wc;
641
642     if (!aMesh->_is_nil()) {
643       _PTR(SObject) SM = SMESH::FindSObject(aMesh);
644       GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
645       try {
646         CORBA::String_var error;
647         res = aMesh->AddHypothesis(aShapeObject, aHyp, error.out());
648         UpdateViewer(aMesh);
649         if (res > SMESH::HYP_OK) {
650           wc.suspend();
651           processHypothesisStatus(res, aHyp, true, error.in() );
652           wc.resume();
653         }
654       }
655       catch(const SALOME::SALOME_Exception& S_ex) {
656         wc.suspend();
657         SalomeApp_Tools::QtCatchCorbaException(S_ex);
658         res = SMESH::HYP_UNKNOWN_FATAL;
659       }
660     }
661     return res < SMESH::HYP_UNKNOWN_FATAL;
662   }
663
664
665   bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
666   {
667     MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
668     int res = SMESH::HYP_UNKNOWN_FATAL;
669     SUIT_OverrideCursor wc;
670
671     if ( !aSubMesh->_is_nil() && !aHyp->_is_nil() ) {
672       try {
673         SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
674         _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
675         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
676         if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil())
677         {
678           CORBA::String_var error;
679           res = aMesh->AddHypothesis( aShapeObject, aHyp, error.out() );
680           UpdateViewer(aMesh);
681           if (res > SMESH::HYP_OK) {
682             wc.suspend();
683             processHypothesisStatus( res, aHyp, true, error.in() );
684             wc.resume();
685           }
686         }
687         else {
688           SCRUTE(aHyp->_is_nil());
689           SCRUTE(aMesh->_is_nil());
690           SCRUTE(!SsubM);
691           SCRUTE(aShapeObject->_is_nil());
692         }
693       }
694       catch(const SALOME::SALOME_Exception& S_ex) {
695         wc.suspend();
696         SalomeApp_Tools::QtCatchCorbaException(S_ex);
697         res = SMESH::HYP_UNKNOWN_FATAL;
698       }
699     }
700     else {
701       SCRUTE(aSubMesh->_is_nil());
702       SCRUTE(aHyp->_is_nil());
703     }
704     return res < SMESH::HYP_UNKNOWN_FATAL;
705   }
706
707   bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
708   {
709     int res = SMESH::HYP_UNKNOWN_FATAL;
710     SUIT_OverrideCursor wc;
711
712     try {
713       _PTR(Study) aStudy = getStudy();
714       _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
715       if( aHypObj )
716       {
717         _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
718         _PTR(SObject) aRealHypo;
719         if( aHypObj->ReferencedObject( aRealHypo ) )
720         {
721           SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
722           RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
723         }
724         else
725         {
726           SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
727           SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
728           for( size_t i = 0; i < meshList.size(); i++ )
729             RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
730         }
731       }
732     }
733     catch(const SALOME::SALOME_Exception& S_ex)
734     {
735       wc.suspend();
736       SalomeApp_Tools::QtCatchCorbaException(S_ex);
737       res = SMESH::HYP_UNKNOWN_FATAL;
738     }
739     return res < SMESH::HYP_UNKNOWN_FATAL;
740   }
741
742   bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject)               MorSM,
743                                           SMESH::SMESH_Hypothesis_ptr anHyp)
744   {
745     int res = SMESH::HYP_UNKNOWN_FATAL;
746     SUIT_OverrideCursor wc;
747
748     if (MorSM) {
749       try {
750         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
751         SMESH::SMESH_Mesh_var aMesh        = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
752         SMESH::SMESH_subMesh_var aSubMesh  = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
753
754         if (!aSubMesh->_is_nil())
755           aMesh = aSubMesh->GetFather();
756
757         if (!aMesh->_is_nil()) {
758           if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
759             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
760           }
761           else if(!aMesh->HasShapeToMesh()){
762             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
763           }
764           if (res > SMESH::HYP_OK) {
765             wc.suspend();
766             processHypothesisStatus(res, anHyp, false);
767             wc.resume();
768           }
769           UpdateViewer(aMesh);
770         }
771       } catch(const SALOME::SALOME_Exception& S_ex) {
772         wc.suspend();
773         SalomeApp_Tools::QtCatchCorbaException(S_ex);
774         res = SMESH::HYP_UNKNOWN_FATAL;
775       }
776     }
777     return res < SMESH::HYP_UNKNOWN_FATAL;
778   } 
779
780   void UpdateViewer(SMESH::SMESH_Mesh_ptr theMesh)
781   {
782     if (_PTR(SObject) meshSO = SMESH::FindSObject(theMesh))
783     {
784       if (SMESH_Actor* actor = SMESH::FindActorByEntry(meshSO->GetID().c_str()))
785         if (actor->GetVisibility())
786           actor->Update();
787     }
788   }
789
790   SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
791   {
792     SObjectList listSOmesh;
793     listSOmesh.resize(0);
794
795     unsigned int index = 0;
796     if (!AlgoOrHyp->_is_nil()) {
797       _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
798       if (SO_Hypothesis) {
799         SObjectList listSO = SMESH::getStudy()->FindDependances(SO_Hypothesis);
800
801         MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
802         for (unsigned int i = 0; i < listSO.size(); i++) {
803           _PTR(SObject) SO = listSO[i];
804           if (SO) {
805             _PTR(SObject) aFather = SO->GetFather();
806             if (aFather) {
807               _PTR(SObject) SOfatherFather = aFather->GetFather();
808               if (SOfatherFather) {
809                 MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
810                 index++;
811                 listSOmesh.resize(index);
812                 listSOmesh[index - 1] = SOfatherFather;
813               }
814             }
815           }
816         }
817       }
818     }
819     MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
820     return listSOmesh;
821   }
822
823 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
824   QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
825   {
826     QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
827     for ( size_t i = 0; i < errors.length(); ++i ) {
828       const SMESH::AlgoStateError & error = errors[ i ];
829       const bool hasAlgo = ( strlen( error.algoName ) != 0 );
830       QString msg;
831       if ( !hasAlgo )
832         msg = QObject::tr( "STATE_ALGO_MISSING" );
833       else
834         switch( error.state ) {
835           CASE2MESSAGE( HYP_MISSING );
836           CASE2MESSAGE( HYP_NOTCONFORM );
837           CASE2MESSAGE( HYP_BAD_PARAMETER );
838           CASE2MESSAGE( HYP_BAD_GEOMETRY );
839         default: continue;
840         }
841       // apply args to message:
842       // %1 - algo name
843       if ( hasAlgo )
844         msg = msg.arg( error.algoName.in() );
845       // %2 - dimension
846       msg = msg.arg( error.algoDim );
847       // %3 - global/local
848       msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
849       // %4 - hypothesis dim == algoDim
850       msg = msg.arg( error.algoDim );
851
852       if ( i ) resMsg += ";\n";
853       resMsg += msg;
854     }
855     return resMsg;
856   }
857 } // end of namespace SMESH