Salome HOME
PAL10237. Add GetHypothesesSets()
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_HypothesesUtils.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 //  This library is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU Lesser General Public
6 //  License as published by the Free Software Foundation; either
7 //  version 2.1 of the License.
8 //
9 //  This library is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 //  Lesser General Public License for more details.
13 //
14 //  You should have received a copy of the GNU Lesser General Public
15 //  License along with this library; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
19
20 #include "SMESHGUI_HypothesesUtils.h"
21
22 #include "SMESHGUI.h"
23 #include "SMESHGUI_Hypotheses.h"
24 #include "SMESHGUI_XmlHandler.h"
25 #include "SMESHGUI_Utils.h"
26 #include "SMESHGUI_GEOMGenUtils.h"
27
28 #include "SUIT_Tools.h"
29 #include "SUIT_Desktop.h"
30 #include "SUIT_MessageBox.h"
31 #include "SUIT_OverrideCursor.h"
32 #include "SUIT_ResourceMgr.h"
33 #include "SUIT_Session.h"
34
35 #include "OB_Browser.h"
36
37 #include "SalomeApp_Study.h"
38 #include "SalomeApp_Tools.h"
39 #include "SalomeApp_Application.h"
40
41 #include <SALOMEDSClient_Study.hxx>
42 #include <SALOMEDSClient_SObject.hxx>
43
44 #include "SALOMEconfig.h"
45 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
46
47 #include <map>
48 #include <string>
49
50 #include <dlfcn.h>
51
52 #ifdef _DEBUG_
53 static int MYDEBUG = 0;
54 #else
55 static int MYDEBUG = 0;
56 #endif
57
58 namespace SMESH{
59
60   using namespace std;
61
62   typedef map<string,HypothesisData*> THypothesisDataMap;
63   THypothesisDataMap myHypothesesMap;
64   THypothesisDataMap myAlgorithmsMap;
65
66   typedef map<string,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
67   THypCreatorMap myHypCreatorMap;
68
69   list<HypothesesSet*> myListOfHypothesesSets;
70
71   void processHypothesisStatus(const int theHypStatus,
72                                SMESH::SMESH_Hypothesis_ptr theHyp,
73                                const bool theIsAddition)
74   {
75     if (theHypStatus > SMESH::HYP_OK) {
76
77       // get Hyp name
78       QString aHypName ("NULL Hypothesis");
79       if (!CORBA::is_nil(theHyp)) {
80         _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
81         if (Shyp)
82           // name in study
83           aHypName = Shyp->GetName().c_str();
84         else
85           // label in xml file
86           aHypName = GetHypothesisData(theHyp->GetName())->Label;
87       }
88
89       // message
90       bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
91       QString aMsg;
92       if (theIsAddition)
93         aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
94       else
95         aMsg = (isFatal ? "SMESH_CANT_RM_HYP"  : "SMESH_RM_HYP_WRN");
96
97       aMsg = QObject::tr(aMsg).arg(aHypName) +
98         QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus));
99
100       SUIT_MessageBox::warn1(SMESHGUI::desktop(),
101                             QObject::tr("SMESH_WRN_WARNING"),
102                             aMsg,
103                             QObject::tr("SMESH_BUT_OK"));
104     }
105   }
106
107
108   void InitAvailableHypotheses()
109   {
110     SUIT_OverrideCursor wc;
111     if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
112       // Resource manager
113       SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
114       if (!resMgr) return;
115
116       // Find name of a resource XML file ("SMESH_Meshers.xml");
117       QString HypsXml;
118       char* cenv = getenv("SMESH_MeshersList");
119       if (cenv)
120         HypsXml.sprintf("%s", cenv);
121
122       QStringList HypsXmlList = QStringList::split(":", HypsXml, false);
123       if (HypsXmlList.count() == 0)
124         {
125           SUIT_MessageBox::error1(SMESHGUI::desktop(),
126                                  QObject::tr("SMESH_WRN_WARNING"),
127                                  QObject::tr("MESHERS_FILE_NO_VARIABLE"),
128                                  QObject::tr("SMESH_BUT_OK"));
129           return;
130         }
131
132       // loop on files in HypsXml
133       QString aNoAccessFiles;
134       for (int i = 0; i < HypsXmlList.count(); i++) {
135         QString HypsXml = HypsXmlList[ i ];
136
137         // Find full path to the resource XML file
138         QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
139         if ( xmlFile.isEmpty() ) // try PLUGIN resources
140           xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
141         
142         QFile file (xmlFile);
143         if (file.exists() && file.open(IO_ReadOnly)) {
144           file.close();
145
146           SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
147           ASSERT(aXmlHandler);
148
149           QXmlInputSource source (file);
150           QXmlSimpleReader reader;
151           reader.setContentHandler(aXmlHandler);
152           reader.setErrorHandler(aXmlHandler);
153           bool ok = reader.parse(source);
154           file.close();
155           if (ok) {
156             myHypothesesMap.insert( aXmlHandler->myHypothesesMap.begin(),
157                                     aXmlHandler->myHypothesesMap.end() );
158             myAlgorithmsMap.insert( aXmlHandler->myAlgorithmsMap.begin(),
159                                     aXmlHandler->myAlgorithmsMap.end() );
160             myListOfHypothesesSets.splice( myListOfHypothesesSets.begin(),
161                                            aXmlHandler->myListOfHypothesesSets );
162           }
163           else {
164             SUIT_MessageBox::error1(SMESHGUI::desktop(),
165                                    QObject::tr("INF_PARSE_ERROR"),
166                                    QObject::tr(aXmlHandler->errorProtocol()),
167                                    QObject::tr("SMESH_BUT_OK"));
168           }
169         }
170         else {
171           if (aNoAccessFiles.isEmpty())
172             aNoAccessFiles = xmlFile;
173           else
174             aNoAccessFiles += ", " + xmlFile;
175         }
176       } // end loop
177
178
179       if (!aNoAccessFiles.isEmpty()) {
180         QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
181         aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
182         wc.suspend();
183         SUIT_MessageBox::warn1(SMESHGUI::desktop(),
184                               QObject::tr("SMESH_WRN_WARNING"),
185                               aMess,
186                               QObject::tr("SMESH_BUT_OK"));
187         wc.resume();
188       }
189     }
190   }
191
192
193   QStringList GetAvailableHypotheses( const bool isAlgo, 
194                                       const int theDim,                          
195                                       const bool isAux )
196   {
197     QStringList aHypList;
198
199     // Init list of available hypotheses, if needed
200     InitAvailableHypotheses();
201
202     // fill list of hypotheses/algorithms
203     THypothesisDataMap* pMap = isAlgo ? &myAlgorithmsMap : &myHypothesesMap;
204     THypothesisDataMap::iterator anIter;
205     for ( anIter = pMap->begin(); anIter != pMap->end(); anIter++ )
206     {
207       HypothesisData* aData = (*anIter).second;
208       if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux )
209         aHypList.append(((*anIter).first).c_str());
210     }
211     return aHypList;
212   }
213
214
215   QStringList GetHypothesesSets()
216   {
217     QStringList aSetNameList;
218
219     // Init list of available hypotheses, if needed
220     InitAvailableHypotheses();
221
222     list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
223     for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
224     {
225       HypothesesSet* aSet = *hypoSet;
226       if ( aSet && aSet->AlgoList.count() ) {
227         aSetNameList.append( aSet->HypoSetName );
228       }
229     }
230
231     return aSetNameList;
232   }
233
234   HypothesesSet* GetHypothesesSet(const QString theSetName)
235   {
236     list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
237     for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
238     {
239       HypothesesSet* aSet = *hypoSet;
240       if ( aSet && aSet->HypoSetName == theSetName )
241         return aSet;
242     }
243     return 0;
244   }
245
246   HypothesisData* GetHypothesisData (const char* aHypType)
247   {
248     HypothesisData* aHypData = 0;
249
250     // Init list of available hypotheses, if needed
251     InitAvailableHypotheses();
252
253     if (myHypothesesMap.find(aHypType) == myHypothesesMap.end()) {
254       if (myAlgorithmsMap.find(aHypType) != myAlgorithmsMap.end()) {
255         aHypData = myAlgorithmsMap[aHypType];
256       }
257     }
258     else {
259       aHypData = myHypothesesMap[aHypType];
260     }
261     return aHypData;
262   }
263
264
265   SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const char* aHypType)
266   {
267     if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType);
268
269     SMESHGUI_GenericHypothesisCreator* aCreator = 0;
270
271     // check, if creator for this hypothesis type already exists
272     if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
273       aCreator = myHypCreatorMap[aHypType];
274     }
275     else {
276       // 1. Init list of available hypotheses, if needed
277       InitAvailableHypotheses();
278
279       // 2. Get names of plugin libraries
280       HypothesisData* aHypData = GetHypothesisData(aHypType);
281       if (!aHypData) 
282         return aCreator;
283       QString aClientLibName = aHypData->ClientLibName;
284       QString aServerLibName = aHypData->ServerLibName;
285
286       // 3. Load Client Plugin Library
287       try {
288         // load plugin library
289         if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
290         void* libHandle = dlopen (aClientLibName, RTLD_LAZY);
291         if (!libHandle) {
292           // report any error, if occured
293           const char* anError = dlerror();
294           if(MYDEBUG) MESSAGE(anError);
295         }
296         else {
297           // get method, returning hypothesis creator
298           if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
299           typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
300             (QString aHypType, QString aServerLibName, SMESHGUI* aSMESHGUI);
301           GetHypothesisCreator procHandle =
302             (GetHypothesisCreator)dlsym(libHandle, "GetHypothesisCreator");
303           if (!procHandle) {
304             if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
305             dlclose(libHandle);
306           }
307           else {
308             // get hypothesis creator
309             if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType);
310             aCreator = procHandle(aHypType, aServerLibName, SMESHGUI::GetSMESHGUI());
311             if (!aCreator) {
312               if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
313             }
314             else {
315               // map hypothesis creator to a hypothesis name
316               myHypCreatorMap[aHypType] = aCreator;
317             }
318           }
319         }
320       }
321       catch (const SALOME::SALOME_Exception& S_ex) {
322         SalomeApp_Tools::QtCatchCorbaException(S_ex);
323       }
324     }
325
326     return aCreator;
327   }
328
329
330   SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const char* aHypType,
331                                                const char* aHypName,
332                                                const bool isAlgo)
333   {
334     if(MYDEBUG) MESSAGE("Create " << aHypType << " with name " << aHypName);
335
336     SMESH::SMESH_Hypothesis_var Hyp;
337
338     HypothesisData* aHypData = GetHypothesisData(aHypType);
339     QString aServLib = aHypData->ServerLibName;
340
341     try {
342       Hyp = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType, aServLib);
343       if (!Hyp->_is_nil()) {
344         _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
345         if (SHyp) {
346           //if (strcmp(aHypName,"") != 0)
347           if (strlen(aHypName) > 0)
348             SMESH::SetName(SHyp, aHypName);
349           //SalomeApp_Application* app =
350           //  dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
351           //if (app)
352           //  app->objectBrowser()->updateTree();
353           SMESHGUI::GetSMESHGUI()->updateObjBrowser();
354           return Hyp._retn();
355         }
356       }
357     }
358     catch (const SALOME::SALOME_Exception & S_ex) {
359       SalomeApp_Tools::QtCatchCorbaException(S_ex);
360     }
361
362     return SMESH::SMESH_Hypothesis::_nil();
363   }
364
365
366   bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
367   {
368     if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
369     int res = SMESH::HYP_UNKNOWN_FATAL;
370     SUIT_OverrideCursor wc;
371
372     if (!aMesh->_is_nil()) {
373       _PTR(SObject) SM = SMESH::FindSObject(aMesh);
374       GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
375       try {
376         res = aMesh->AddHypothesis(aShapeObject, aHyp);
377         if (res < SMESH::HYP_UNKNOWN_FATAL) {
378           _PTR(SObject) SH = SMESH::FindSObject(aHyp);
379           if (SM && SH) {
380             SMESH::ModifiedMesh(SM, false);
381           }
382         }
383         if (res > SMESH::HYP_OK) {
384           wc.suspend();
385           processHypothesisStatus(res, aHyp, true);
386           wc.resume();
387         }
388       }
389       catch(const SALOME::SALOME_Exception& S_ex) {
390         wc.suspend();
391         SalomeApp_Tools::QtCatchCorbaException(S_ex);
392         res = SMESH::HYP_UNKNOWN_FATAL;
393       }
394     }
395     return res < SMESH::HYP_UNKNOWN_FATAL;
396   }
397
398
399   bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
400   {
401     if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
402     int res = SMESH::HYP_UNKNOWN_FATAL;
403     SUIT_OverrideCursor wc;
404
405     if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
406       try {
407         SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
408         _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
409         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
410         if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
411           res = aMesh->AddHypothesis(aShapeObject, aHyp);
412           if (res < SMESH::HYP_UNKNOWN_FATAL)  {
413             _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
414             if (meshSO)
415               SMESH::ModifiedMesh(meshSO, false);
416           }
417           if (res > SMESH::HYP_OK) {
418             wc.suspend();
419             processHypothesisStatus(res, aHyp, true);
420             wc.resume();
421           }
422         }
423         else {
424           SCRUTE(aHyp->_is_nil());
425           SCRUTE(aMesh->_is_nil());
426           SCRUTE(!SsubM);
427           SCRUTE(aShapeObject->_is_nil());
428         }
429       }
430       catch(const SALOME::SALOME_Exception& S_ex) {
431         wc.suspend();
432         SalomeApp_Tools::QtCatchCorbaException(S_ex);
433         res = SMESH::HYP_UNKNOWN_FATAL;
434       }
435     }
436     else {
437       SCRUTE(aSubMesh->_is_nil());
438       SCRUTE(aHyp->_is_nil());
439     }
440     return res < SMESH::HYP_UNKNOWN_FATAL;
441   }
442
443   bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
444   {
445     int res = SMESH::HYP_UNKNOWN_FATAL;
446     SUIT_OverrideCursor wc;
447
448     try {
449       _PTR(Study) aStudy = GetActiveStudyDocument();
450       _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
451       if( aHypObj )
452       {
453         _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
454         _PTR(SObject) aRealHypo;
455         if( aHypObj->ReferencedObject( aRealHypo ) )
456         {
457           SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
458           RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
459         }
460         else
461         {
462           SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
463           SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
464           for( int i = 0; i < meshList.size(); i++ )
465             RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
466         }
467       }
468     }
469     catch(const SALOME::SALOME_Exception& S_ex)
470     {
471       wc.suspend();
472       SalomeApp_Tools::QtCatchCorbaException(S_ex);
473       res = SMESH::HYP_UNKNOWN_FATAL;
474     }
475     return res < SMESH::HYP_UNKNOWN_FATAL;
476   }
477
478   bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
479                                           SMESH::SMESH_Hypothesis_ptr anHyp)
480   {
481     SALOMEDS::GenericAttribute_var anAttr;
482     SALOMEDS::AttributeIOR_var anIOR;
483     int res = SMESH::HYP_UNKNOWN_FATAL;
484     SUIT_OverrideCursor wc;
485
486     if (MorSM) {
487       try {
488         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
489         if (!aShapeObject->_is_nil()) {
490           SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
491           SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
492
493           if (!aSubMesh->_is_nil())
494             aMesh = aSubMesh->GetFather();
495
496           if (!aMesh->_is_nil()) {
497             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
498             if (res < SMESH::HYP_UNKNOWN_FATAL) {
499               _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
500               if (meshSO)
501                 SMESH::ModifiedMesh(meshSO, false);
502             }
503             if (res > SMESH::HYP_OK) {
504               wc.suspend();
505               processHypothesisStatus(res, anHyp, false);
506               wc.resume();
507             }
508           }
509         }
510       } catch(const SALOME::SALOME_Exception& S_ex) {
511         wc.suspend();
512         SalomeApp_Tools::QtCatchCorbaException(S_ex);
513         res = SMESH::HYP_UNKNOWN_FATAL;
514       }
515     }
516     return res < SMESH::HYP_UNKNOWN_FATAL;
517   }
518
519   SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
520   {
521     SObjectList listSOmesh;
522     listSOmesh.resize(0);
523
524     unsigned int index = 0;
525     if (!AlgoOrHyp->_is_nil()) {
526       _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
527       if (SO_Hypothesis) {
528         SObjectList listSO =
529           SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
530
531         if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
532         for (unsigned int i = 0; i < listSO.size(); i++) {
533           _PTR(SObject) SO = listSO[i];
534           if (SO) {
535             _PTR(SObject) aFather = SO->GetFather();
536             if (aFather) {
537               _PTR(SObject) SOfatherFather = aFather->GetFather();
538               if (SOfatherFather) {
539                 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
540                 index++;
541                 listSOmesh.resize(index);
542                 listSOmesh[index - 1] = SOfatherFather;
543               }
544             }
545           }
546         }
547       }
548     }
549     if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
550     return listSOmesh;
551   }
552 }