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