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