Salome HOME
Merge multi-study removal branch.
[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
71 #define LibHandle void*
72 #define LoadLib( name ) dlopen( name, RTLD_LAZY | RTLD_GLOBAL )
73 #define GetProc dlsym
74 #define UnLoadLib( handle ) dlclose( handle );
75 #endif
76
77 #ifdef _DEBUG_
78 static int MYDEBUG = 0;
79 #else
80 static int MYDEBUG = 0;
81 #endif
82
83 namespace SMESH
84 {
85   typedef IMap<QString,HypothesisData*> THypothesisDataMap;
86   THypothesisDataMap myHypothesesMap;
87   THypothesisDataMap myAlgorithmsMap;
88
89   // BUG 0020378
90   //typedef QMap<QString,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
91   //THypCreatorMap myHypCreatorMap;
92
93   QList<HypothesesSet*> myListOfHypothesesSets;
94
95   void processHypothesisStatus(const int                   theHypStatus,
96                                SMESH::SMESH_Hypothesis_ptr theHyp,
97                                const bool                  theIsAddition,
98                                const char*                 theError = 0)
99   {
100     if (theHypStatus > SMESH::HYP_OK) {
101       // get Hyp name
102       QString aHypName ("NULL Hypothesis");
103       if (!CORBA::is_nil(theHyp)) {
104         _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
105         if (Shyp) {
106           // name in study
107           aHypName = Shyp->GetName().c_str();
108         }
109         else {
110           // label in xml file
111           CORBA::String_var hypType = theHyp->GetName();
112           aHypName = GetHypothesisData( hypType.in() )->Label;
113         }
114       }
115
116       // message
117       bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
118       QString aMsg;
119       if (theIsAddition)
120         aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
121       else
122         aMsg = (isFatal ? "SMESH_CANT_RM_HYP"  : "SMESH_RM_HYP_WRN");
123
124       aMsg = QObject::tr(aMsg.toLatin1().data()).arg(aHypName);
125
126       if ( theError && theError[0] )
127       {
128         aMsg += theError;
129       }
130       else
131       {
132         aMsg += QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus).toLatin1().data());
133
134         if ( theHypStatus == SMESH::HYP_HIDDEN_ALGO ) { // PAL18501
135           CORBA::String_var hypType = theHyp->GetName();
136           if ( HypothesisData* hd = GetHypothesisData( hypType.in() ))
137             aMsg = aMsg.arg( hd->Dim[0] );
138         }
139       }
140       SUIT_MessageBox::warning(SMESHGUI::desktop(),
141                                QObject::tr("SMESH_WRN_WARNING"),
142                                aMsg);
143     }
144   }
145
146   //================================================================================
147   /*!
148    * \brief Prepends dimension and appends '[custom]' to the name of hypothesis set
149    */
150   //================================================================================
151
152   static QString mangledHypoSetName(HypothesesSet* hypSet)
153   {
154     QString name = hypSet->name();
155
156     // prepend 'xD: '
157     int dim = hypSet->maxDim();
158     if ( dim > -1 )
159       name = QString("%1D: %2").arg(dim).arg(name);
160
161     // custom
162     if ( hypSet->getIsCustom() )
163       name = QString("%1 [custom]").arg(name);
164
165     return name;
166   }
167
168   //================================================================================
169   /*!
170    * \brief Removes dimension and '[custom]' from the name of hypothesis set
171    */
172   //================================================================================
173
174   static QString demangledHypoSetName(QString name)
175   {
176     name.remove(QRegExp("[0-3]D: "));
177     name.remove(" [custom]");
178     return name;
179   }
180
181   void InitAvailableHypotheses()
182   {
183     SUIT_OverrideCursor wc;
184     if ( myHypothesesMap.empty() && myAlgorithmsMap.empty() )
185     {
186       // Resource manager
187       SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
188       if (!resMgr) return;
189
190       // Find name of a resource XML file ("SMESH_Meshers.xml");
191       QString HypsXml;
192       char* cenv = getenv("SMESH_MeshersList");
193       if (cenv)
194         HypsXml.sprintf("%s", cenv);
195
196       QStringList HypsXmlList = HypsXml.split(":", QString::SkipEmptyParts);
197       if (HypsXmlList.count() == 0) {
198         SUIT_MessageBox::critical(SMESHGUI::desktop(),
199                                   QObject::tr("SMESH_WRN_WARNING"),
200                                   QObject::tr("MESHERS_FILE_NO_VARIABLE"));
201         return;
202       }
203       // get full names of xml files from HypsXmlList
204       QStringList xmlFiles;
205       xmlFiles.append( QDir::home().filePath("CustomMeshers.xml")); // may be inexistent
206       for (int i = 0; i < HypsXmlList.count(); i++) {
207         QString HypsXml = HypsXmlList[ i ];
208
209         // Find full path to the resource XML file
210         QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
211         if ( xmlFile.isEmpty() ) // try PLUGIN resources
212           xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
213         if ( !xmlFile.isEmpty() )
214           xmlFiles.append( xmlFile );
215       }
216
217       // loop on xmlFiles
218       QString aNoAccessFiles;
219       for (int i = 0; i < xmlFiles.count(); i++)
220       {
221         QString xmlFile = xmlFiles[ i ];
222         QFile file (xmlFile);
223         if (file.exists() && file.open(QIODevice::ReadOnly))
224         {
225           file.close();
226
227           SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
228           ASSERT(aXmlHandler);
229
230           QXmlInputSource source (&file);
231           QXmlSimpleReader reader;
232           reader.setContentHandler(aXmlHandler);
233           reader.setErrorHandler(aXmlHandler);
234           bool ok = reader.parse(source);
235           file.close();
236           if (ok) {
237
238             THypothesisDataMap::ConstIterator it1 = aXmlHandler->myHypothesesMap.begin();
239
240             for( ;it1 != aXmlHandler->myHypothesesMap.end(); it1++)
241               myHypothesesMap.insert( it1.key(), it1.value() );
242
243             it1 = aXmlHandler->myAlgorithmsMap.begin();
244             for( ;it1 != aXmlHandler->myAlgorithmsMap.end(); it1++)
245               myAlgorithmsMap.insert( it1.key(), it1.value() );
246
247             QList<HypothesesSet*>::iterator it;
248             for ( it = aXmlHandler->myListOfHypothesesSets.begin();
249                   it != aXmlHandler->myListOfHypothesesSets.end();
250                   ++it )
251             {
252               (*it)->setIsCustom( i == 0 );
253               myListOfHypothesesSets.append( *it );
254             }
255           }
256           else {
257             SUIT_MessageBox::critical(SMESHGUI::desktop(),
258                                       QObject::tr("INF_PARSE_ERROR"),
259                                       QObject::tr(aXmlHandler->errorProtocol().toLatin1().data()));
260           }
261           delete aXmlHandler;
262         }
263         else if ( i > 0 ) { // 1st is ~/CustomMeshers.xml
264           if (aNoAccessFiles.isEmpty())
265             aNoAccessFiles = xmlFile;
266           else
267             aNoAccessFiles += ", " + xmlFile;
268         }
269       } // end loop on xmlFiles
270
271
272       if (!aNoAccessFiles.isEmpty()) {
273         QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
274         aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
275         wc.suspend();
276         SUIT_MessageBox::warning(SMESHGUI::desktop(),
277                                  QObject::tr("SMESH_WRN_WARNING"),
278                                  aMess);
279         wc.resume();
280       }
281     }
282   }
283
284
285   QStringList GetAvailableHypotheses( const bool isAlgo,
286                                       const int  theDim,
287                                       const bool isAux,
288                                       const bool hasGeometry,
289                                       const bool isSubMesh)
290   {
291     QStringList aHypList;
292
293     // Init list of available hypotheses, if needed
294     InitAvailableHypotheses();
295     bool checkGeometry = ( isAlgo );
296     const char* context = isSubMesh ? "LOCAL" : "GLOBAL";
297     // fill list of hypotheses/algorithms
298     THypothesisDataMap& pMap = isAlgo ? myAlgorithmsMap : myHypothesesMap;
299     THypothesisDataMap::ConstIterator anIter;
300     for ( anIter = pMap.begin(); anIter != pMap.end(); anIter++ )
301     {
302       HypothesisData* aData = anIter.value();
303       if (( aData && !aData->Label.isEmpty() ) &&
304           ( theDim < 0              || aData->Dim.contains( theDim )) &&
305           ( isAlgo                  || aData->IsAuxOrNeedHyp == isAux ) &&
306           ( aData->Context == "ANY" || aData->Context == context ) &&
307           ( !checkGeometry          || (!aData->IsNeedGeometry  ||
308                                         ( aData->IsNeedGeometry > 0 ) == hasGeometry)))
309       {
310         aHypList.append(anIter.key());
311       }
312     }
313     return aHypList;
314   }
315
316
317   QStringList GetHypothesesSets(int maxDim)
318   {
319     QStringList aSetNameList;
320
321     // Init list of available hypotheses, if needed
322     InitAvailableHypotheses();
323     QList<HypothesesSet*>::iterator hypoSet;
324     for ( hypoSet  = myListOfHypothesesSets.begin();
325         hypoSet != myListOfHypothesesSets.end();
326         ++hypoSet ) {
327       HypothesesSet* aSet = *hypoSet;
328       if ( aSet && ( aSet->count( true ) || aSet->count( false )) &&
329           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     if(MYDEBUG) 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         if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
520         LibHandle libHandle = LoadLib( aClientLibName.toLatin1().data() );
521         if (!libHandle) {
522           // report any error, if occurred
523           {
524 #ifdef WIN32
525             const char* anError = "Can't load client meshers plugin library";
526 #else
527             const char* anError = dlerror();
528 #endif
529             INFOS(anError); // always display this kind of error !
530           }
531         }
532         else {
533           // get method, returning hypothesis creator
534           if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
535           typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
536             ( const QString& );
537           GetHypothesisCreator procHandle =
538             (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
539           if (!procHandle) {
540             if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
541             UnLoadLib(libHandle);
542           }
543           else {
544             // get hypothesis creator
545             if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
546             aCreator = procHandle( aHypType );
547             if (!aCreator) {
548               if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
549             }
550             else {
551               // map hypothesis creator to a hypothesis name
552               // BUG 0020378
553               //myHypCreatorMap[aHypType] = aCreator;
554
555               //rnv : This dynamic property of the QObject stores the name of the plugin.
556               //      It is used to obtain plugin root dir environment variable
557               //      in the SMESHGUI_HypothesisDlg class. Plugin root dir environment
558               //      variable is used to display documentation.
559               aCreator->setProperty(SMESH::Plugin_Name(),aHypData->PluginName);
560             }
561           }
562         }
563       }
564       catch (const SALOME::SALOME_Exception& S_ex) {
565         SalomeApp_Tools::QtCatchCorbaException(S_ex);
566       }
567     }
568
569     return aCreator;
570   }
571
572
573   SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
574                                                const QString& aHypName,
575                                                const bool isAlgo)
576   {
577     if(MYDEBUG) MESSAGE("Create " << aHypType.toLatin1().data() <<
578                         " with name " << aHypName.toLatin1().data());
579     HypothesisData* aHypData = GetHypothesisData(aHypType);
580     QString aServLib = aHypData->ServerLibName;
581     try {
582       SMESH::SMESH_Hypothesis_var aHypothesis;
583       aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
584                                                               aServLib.toLatin1().data());
585       if (!aHypothesis->_is_nil()) {
586         _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
587         if (aHypSObject) {
588           if (!aHypName.isEmpty())
589             SMESH::SetName(aHypSObject, aHypName);
590           SMESHGUI::Modified();
591           SMESHGUI::GetSMESHGUI()->updateObjBrowser();
592           return aHypothesis._retn();
593         }
594       }
595     } catch (const SALOME::SALOME_Exception & S_ex) {
596       SalomeApp_Tools::QtCatchCorbaException(S_ex);
597     }
598
599     return SMESH::SMESH_Hypothesis::_nil();
600   }
601
602   bool IsApplicable(const QString&        aHypType,
603                     GEOM::GEOM_Object_ptr theGeomObject,
604                     const bool            toCheckAll)
605   {
606     if ( getenv("NO_LIMIT_ALGO_BY_SHAPE")) // allow a workaround for a case if
607       return true;                         // IsApplicable() returns false due to a bug
608
609     HypothesisData* aHypData = GetHypothesisData(aHypType);
610     QString aServLib = aHypData->ServerLibName;
611     return SMESHGUI::GetSMESHGen()->IsApplicable( aHypType.toLatin1().data(),
612                                                   aServLib.toLatin1().data(),
613                                                   theGeomObject,
614                                                   toCheckAll);
615   }
616
617
618   bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
619   {
620     if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
621     int res = SMESH::HYP_UNKNOWN_FATAL;
622     SUIT_OverrideCursor wc;
623
624     if (!aMesh->_is_nil()) {
625       _PTR(SObject) SM = SMESH::FindSObject(aMesh);
626       GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
627       try {
628         CORBA::String_var error;
629         res = aMesh->AddHypothesis(aShapeObject, aHyp, error.out());
630         if (res < SMESH::HYP_UNKNOWN_FATAL) {
631           _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
632           if (SM && aSH) {
633             SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
634           }
635         }
636         if (res > SMESH::HYP_OK) {
637           wc.suspend();
638           processHypothesisStatus(res, aHyp, true, error.in() );
639           wc.resume();
640         }
641       }
642       catch(const SALOME::SALOME_Exception& S_ex) {
643         wc.suspend();
644         SalomeApp_Tools::QtCatchCorbaException(S_ex);
645         res = SMESH::HYP_UNKNOWN_FATAL;
646       }
647     }
648     return res < SMESH::HYP_UNKNOWN_FATAL;
649   }
650
651
652   bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
653   {
654     if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
655     int res = SMESH::HYP_UNKNOWN_FATAL;
656     SUIT_OverrideCursor wc;
657
658     if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
659       try {
660         SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
661         _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
662         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
663         if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil())
664         {
665           CORBA::String_var error;
666           res = aMesh->AddHypothesis( aShapeObject, aHyp, error.out() );
667           if (res < SMESH::HYP_UNKNOWN_FATAL)  {
668             _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
669             if (meshSO)
670               SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
671           }
672           if (res > SMESH::HYP_OK) {
673             wc.suspend();
674             processHypothesisStatus( res, aHyp, true, error.in() );
675             wc.resume();
676           }
677         }
678         else {
679           SCRUTE(aHyp->_is_nil());
680           SCRUTE(aMesh->_is_nil());
681           SCRUTE(!SsubM);
682           SCRUTE(aShapeObject->_is_nil());
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     else {
692       SCRUTE(aSubMesh->_is_nil());
693       SCRUTE(aHyp->_is_nil());
694     }
695     return res < SMESH::HYP_UNKNOWN_FATAL;
696   }
697
698   bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
699   {
700     int res = SMESH::HYP_UNKNOWN_FATAL;
701     SUIT_OverrideCursor wc;
702
703     try {
704       _PTR(Study) aStudy = getStudy();
705       _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
706       if( aHypObj )
707       {
708         _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
709         _PTR(SObject) aRealHypo;
710         if( aHypObj->ReferencedObject( aRealHypo ) )
711         {
712           SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
713           RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
714         }
715         else
716         {
717           SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
718           SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
719           for( size_t i = 0; i < meshList.size(); i++ )
720             RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
721         }
722       }
723     }
724     catch(const SALOME::SALOME_Exception& S_ex)
725     {
726       wc.suspend();
727       SalomeApp_Tools::QtCatchCorbaException(S_ex);
728       res = SMESH::HYP_UNKNOWN_FATAL;
729     }
730     return res < SMESH::HYP_UNKNOWN_FATAL;
731   }
732
733   bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject)               MorSM,
734                                           SMESH::SMESH_Hypothesis_ptr anHyp)
735   {
736     int res = SMESH::HYP_UNKNOWN_FATAL;
737     SUIT_OverrideCursor wc;
738
739     if (MorSM) {
740       try {
741         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
742         SMESH::SMESH_Mesh_var aMesh        = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
743         SMESH::SMESH_subMesh_var aSubMesh  = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
744
745         if (!aSubMesh->_is_nil())
746           aMesh = aSubMesh->GetFather();
747
748         if (!aMesh->_is_nil()) {
749           if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
750             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
751           }
752           else if(!aMesh->HasShapeToMesh()){
753             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
754           }
755           if (res > SMESH::HYP_OK) {
756             wc.suspend();
757             processHypothesisStatus(res, anHyp, false);
758             wc.resume();
759           }
760           if ( _PTR(SObject) meshSO = SMESH::FindSObject(aMesh) )
761           {
762             if ( res < SMESH::HYP_UNKNOWN_FATAL )
763               SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
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