Salome HOME
bug 9264. enable reading plugins xml files
[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   void addMap(const THypothesisDataMap& theMap,
70                THypothesisDataMap& toMap)
71   {
72     THypothesisDataMap::const_iterator it;
73     for (it = theMap.begin(); it != theMap.end(); it++)
74       toMap.insert(*it);
75   }
76
77
78   void processHypothesisStatus(const int theHypStatus,
79                                SMESH::SMESH_Hypothesis_ptr theHyp,
80                                const bool theIsAddition)
81   {
82     if (theHypStatus > SMESH::HYP_OK) {
83
84       // get Hyp name
85       QString aHypName ("NULL Hypothesis");
86       if (!CORBA::is_nil(theHyp)) {
87         _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
88         if (Shyp)
89           // name in study
90           aHypName = Shyp->GetName().c_str();
91         else
92           // label in xml file
93           aHypName = GetHypothesisData(theHyp->GetName())->Label;
94       }
95
96       // message
97       bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
98       QString aMsg;
99       if (theIsAddition)
100         aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
101       else
102         aMsg = (isFatal ? "SMESH_CANT_RM_HYP"  : "SMESH_RM_HYP_WRN");
103
104       aMsg = QObject::tr(aMsg).arg(aHypName) +
105         QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus));
106
107       SUIT_MessageBox::warn1(SMESHGUI::desktop(),
108                             QObject::tr("SMESH_WRN_WARNING"),
109                             aMsg,
110                             QObject::tr("SMESH_BUT_OK"));
111     }
112   }
113
114
115   void InitAvailableHypotheses()
116   {
117     SUIT_OverrideCursor wc;
118     if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
119       // Resource manager
120       SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
121       if (!resMgr) return;
122
123       // Find name of a resource XML file ("SMESH_Meshers.xml");
124       QString HypsXml;
125       char* cenv = getenv("SMESH_MeshersList");
126       if (cenv)
127         HypsXml.sprintf("%s", cenv);
128
129       QStringList HypsXmlList = QStringList::split(":", HypsXml, false);
130       if (HypsXmlList.count() == 0)
131         {
132           SUIT_MessageBox::error1(SMESHGUI::desktop(),
133                                  QObject::tr("SMESH_WRN_WARNING"),
134                                  QObject::tr("MESHERS_FILE_NO_VARIABLE"),
135                                  QObject::tr("SMESH_BUT_OK"));
136           return;
137         }
138
139       // loop on files in HypsXml
140       QString aNoAccessFiles;
141       for (int i = 0; i < HypsXmlList.count(); i++) {
142         QString HypsXml = HypsXmlList[ i ];
143
144         // Find full path to the resource XML file
145         QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
146         if ( xmlFile.isEmpty() ) // try PLUGIN resources
147           xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
148         
149         QFile file (xmlFile);
150         if (file.exists() && file.open(IO_ReadOnly)) {
151           file.close();
152
153           SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
154           ASSERT(aXmlHandler);
155
156           QXmlInputSource source (file);
157           QXmlSimpleReader reader;
158           reader.setContentHandler(aXmlHandler);
159           reader.setErrorHandler(aXmlHandler);
160           bool ok = reader.parse(source);
161           file.close();
162           if (ok) {
163             addMap(aXmlHandler->myHypothesesMap, myHypothesesMap);
164             addMap(aXmlHandler->myAlgorithmsMap, myAlgorithmsMap);
165           }
166           else {
167             SUIT_MessageBox::error1(SMESHGUI::desktop(),
168                                    QObject::tr("INF_PARSE_ERROR"),
169                                    QObject::tr(aXmlHandler->errorProtocol()),
170                                    QObject::tr("SMESH_BUT_OK"));
171           }
172         }
173         else {
174           if (aNoAccessFiles.isEmpty())
175             aNoAccessFiles = xmlFile;
176           else
177             aNoAccessFiles += ", " + xmlFile;
178         }
179       } // end loop
180
181
182       if (!aNoAccessFiles.isEmpty()) {
183         QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
184         aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
185         wc.suspend();
186         SUIT_MessageBox::warn1(SMESHGUI::desktop(),
187                               QObject::tr("SMESH_WRN_WARNING"),
188                               aMess,
189                               QObject::tr("SMESH_BUT_OK"));
190         wc.resume();
191       }
192     }
193   }
194
195
196   QStringList GetAvailableHypotheses(const bool isAlgo)
197   {
198     QStringList aHypList;
199
200     // Init list of available hypotheses, if needed
201     InitAvailableHypotheses();
202
203     // fill list of hypotheses/algorithms
204     THypothesisDataMap::iterator anIter;
205     if (isAlgo) {
206       anIter = myAlgorithmsMap.begin();
207       for (; anIter != myAlgorithmsMap.end(); anIter++) {
208         aHypList.append(((*anIter).first).c_str());
209       }
210     }
211     else {
212       anIter = myHypothesesMap.begin();
213       for (; anIter != myHypothesesMap.end(); anIter++) {
214         aHypList.append(((*anIter).first).c_str());
215       }
216     }
217
218     return aHypList;
219   }
220
221
222   HypothesisData* GetHypothesisData (const char* aHypType)
223   {
224     HypothesisData* aHypData = 0;
225
226     // Init list of available hypotheses, if needed
227     InitAvailableHypotheses();
228
229     if (myHypothesesMap.find(aHypType) == myHypothesesMap.end()) {
230       if (myAlgorithmsMap.find(aHypType) != myAlgorithmsMap.end()) {
231         aHypData = myAlgorithmsMap[aHypType];
232       }
233     }
234     else {
235       aHypData = myHypothesesMap[aHypType];
236     }
237     return aHypData;
238   }
239
240
241   SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const char* aHypType)
242   {
243     if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType);
244
245     SMESHGUI_GenericHypothesisCreator* aCreator = 0;
246
247     // check, if creator for this hypothesis type already exists
248     if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
249       aCreator = myHypCreatorMap[aHypType];
250     }
251     else {
252       // 1. Init list of available hypotheses, if needed
253       InitAvailableHypotheses();
254
255       // 2. Get names of plugin libraries
256       HypothesisData* aHypData = GetHypothesisData(aHypType);
257       if (!aHypData) {
258         return aCreator;
259       }
260       QString aClientLibName = aHypData->ClientLibName;
261       QString aServerLibName = aHypData->ServerLibName;
262
263       // 3. Load Client Plugin Library
264       try {
265         // load plugin library
266         if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
267         void* libHandle = dlopen (aClientLibName, RTLD_LAZY);
268         if (!libHandle) {
269           // report any error, if occured
270           const char* anError = dlerror();
271           if(MYDEBUG) MESSAGE(anError);
272         }
273         else {
274           // get method, returning hypothesis creator
275           if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
276           typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
277             (QString aHypType, QString aServerLibName, SMESHGUI* aSMESHGUI);
278           GetHypothesisCreator procHandle =
279             (GetHypothesisCreator)dlsym(libHandle, "GetHypothesisCreator");
280           if (!procHandle) {
281             if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
282             dlclose(libHandle);
283           }
284           else {
285             // get hypothesis creator
286             if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType);
287             aCreator = procHandle(aHypType, aServerLibName, SMESHGUI::GetSMESHGUI());
288             if (!aCreator) {
289               if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
290             }
291             else {
292               // map hypothesis creator to a hypothesis name
293               myHypCreatorMap[aHypType] = aCreator;
294             }
295           }
296         }
297       }
298       catch (const SALOME::SALOME_Exception& S_ex) {
299         SalomeApp_Tools::QtCatchCorbaException(S_ex);
300       }
301     }
302
303     return aCreator;
304   }
305
306
307   SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const char* aHypType,
308                                                const char* aHypName,
309                                                const bool isAlgo)
310   {
311     if(MYDEBUG) MESSAGE("Create " << aHypType << " with name " << aHypName);
312
313     SMESH::SMESH_Hypothesis_var Hyp;
314
315     HypothesisData* aHypData = GetHypothesisData(aHypType);
316     QString aServLib = aHypData->ServerLibName;
317
318     try {
319       Hyp = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType, aServLib);
320       if (!Hyp->_is_nil()) {
321         _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
322         if (SHyp) {
323           //if (strcmp(aHypName,"") != 0)
324           if (strlen(aHypName) > 0)
325             SMESH::SetName(SHyp, aHypName);
326           //SalomeApp_Application* app =
327           //  dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
328           //if (app)
329           //  app->objectBrowser()->updateTree();
330           SMESHGUI::GetSMESHGUI()->updateObjBrowser();
331           return Hyp._retn();
332         }
333       }
334     }
335     catch (const SALOME::SALOME_Exception & S_ex) {
336       SalomeApp_Tools::QtCatchCorbaException(S_ex);
337     }
338
339     return SMESH::SMESH_Hypothesis::_nil();
340   }
341
342
343   bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
344   {
345     if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
346     int res = SMESH::HYP_UNKNOWN_FATAL;
347     SUIT_OverrideCursor wc;
348
349     if (!aMesh->_is_nil()) {
350       _PTR(SObject) SM = SMESH::FindSObject(aMesh);
351       GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
352       try {
353         res = aMesh->AddHypothesis(aShapeObject, aHyp);
354         if (res < SMESH::HYP_UNKNOWN_FATAL) {
355           _PTR(SObject) SH = SMESH::FindSObject(aHyp);
356           if (SM && SH) {
357             SMESH::ModifiedMesh(SM, false);
358           }
359         }
360         if (res > SMESH::HYP_OK) {
361           wc.suspend();
362           processHypothesisStatus(res, aHyp, true);
363           wc.resume();
364         }
365       }
366       catch(const SALOME::SALOME_Exception& S_ex) {
367         wc.suspend();
368         SalomeApp_Tools::QtCatchCorbaException(S_ex);
369         res = SMESH::HYP_UNKNOWN_FATAL;
370       }
371     }
372     return res < SMESH::HYP_UNKNOWN_FATAL;
373   }
374
375
376   bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
377   {
378     if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
379     int res = SMESH::HYP_UNKNOWN_FATAL;
380     SUIT_OverrideCursor wc;
381
382     if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
383       try {
384         SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
385         _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
386         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
387         if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
388           res = aMesh->AddHypothesis(aShapeObject, aHyp);
389           if (res < SMESH::HYP_UNKNOWN_FATAL)  {
390             _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
391             if (meshSO)
392               SMESH::ModifiedMesh(meshSO, false);
393           }
394           if (res > SMESH::HYP_OK) {
395             wc.suspend();
396             processHypothesisStatus(res, aHyp, true);
397             wc.resume();
398           }
399         }
400         else {
401           SCRUTE(aHyp->_is_nil());
402           SCRUTE(aMesh->_is_nil());
403           SCRUTE(!SsubM);
404           SCRUTE(aShapeObject->_is_nil());
405         }
406       }
407       catch(const SALOME::SALOME_Exception& S_ex) {
408         wc.suspend();
409         SalomeApp_Tools::QtCatchCorbaException(S_ex);
410         res = SMESH::HYP_UNKNOWN_FATAL;
411       }
412     }
413     else {
414       SCRUTE(aSubMesh->_is_nil());
415       SCRUTE(aHyp->_is_nil());
416     }
417     return res < SMESH::HYP_UNKNOWN_FATAL;
418   }
419
420   bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
421   {
422     int res = SMESH::HYP_UNKNOWN_FATAL;
423     SUIT_OverrideCursor wc;
424
425     if (IObject->hasReference()) {
426       try {
427         _PTR(Study) aStudy = GetActiveStudyDocument();
428         SMESH_Hypothesis_var anHyp = IObjectToInterface<SMESH_Hypothesis>(IObject);
429         _PTR(SObject) aHypSObj = aStudy->FindObjectID(IObject->getReference());
430         if (aHypSObj) {
431           _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh(aHypSObj);
432           if (MorSM) {
433             GEOM::GEOM_Object_var aShape = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
434             if (!aShape->_is_nil()){
435               SMESH::SMESH_Mesh_var aMesh =
436                 SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
437               SMESH::SMESH_subMesh_var aSubMesh =
438                 SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
439
440               if (!aSubMesh->_is_nil())
441                 aMesh = aSubMesh->GetFather();
442
443               if (!aMesh->_is_nil()) {
444                 res = aMesh->RemoveHypothesis(aShape, anHyp);
445                 if (res < SMESH::HYP_UNKNOWN_FATAL) {
446                   _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
447                   if (meshSO)
448                     SMESH::ModifiedMesh(meshSO, false);
449                 }
450                 if (res > SMESH::HYP_OK) {
451                   wc.suspend();
452                   processHypothesisStatus(res, anHyp, false);
453                   wc.resume();
454                 }
455               }
456             }
457           }
458         }
459       }
460       catch(const SALOME::SALOME_Exception& S_ex) {
461         wc.suspend();
462         SalomeApp_Tools::QtCatchCorbaException(S_ex);
463         res = SMESH::HYP_UNKNOWN_FATAL;
464       }
465     }
466     else if (IObject->hasEntry()) {
467       if(MYDEBUG) MESSAGE("IObject entry " << IObject->getEntry());
468     }
469     return res < SMESH::HYP_UNKNOWN_FATAL;
470   }
471
472   bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
473                                           SMESH::SMESH_Hypothesis_ptr anHyp)
474   {
475     SALOMEDS::GenericAttribute_var anAttr;
476     SALOMEDS::AttributeIOR_var anIOR;
477     int res = SMESH::HYP_UNKNOWN_FATAL;
478     SUIT_OverrideCursor wc;
479
480     if (MorSM) {
481       try {
482         GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
483         if (!aShapeObject->_is_nil()) {
484           SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
485           SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
486
487           if (!aSubMesh->_is_nil())
488             aMesh = aSubMesh->GetFather();
489
490           if (!aMesh->_is_nil()) {
491             res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
492             if (res < SMESH::HYP_UNKNOWN_FATAL) {
493               _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
494               if (meshSO)
495                 SMESH::ModifiedMesh(meshSO, false);
496             }
497             if (res > SMESH::HYP_OK) {
498               wc.suspend();
499               processHypothesisStatus(res, anHyp, false);
500               wc.resume();
501             }
502           }
503         }
504       } catch(const SALOME::SALOME_Exception& S_ex) {
505         wc.suspend();
506         SalomeApp_Tools::QtCatchCorbaException(S_ex);
507         res = SMESH::HYP_UNKNOWN_FATAL;
508       }
509     }
510     return res < SMESH::HYP_UNKNOWN_FATAL;
511   }
512
513   SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
514   {
515     SObjectList listSOmesh;
516     listSOmesh.resize(0);
517
518     unsigned int index = 0;
519     if (!AlgoOrHyp->_is_nil()) {
520       _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
521       if (SO_Hypothesis) {
522         SObjectList listSO =
523           SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
524
525         if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
526         for (unsigned int i = 0; i < listSO.size(); i++) {
527           _PTR(SObject) SO = listSO[i];
528           if (!SO) {
529             _PTR(SObject) aFather = SO->GetFather();
530             if (aFather) {
531               _PTR(SObject) SOfatherFather = aFather->GetFather();
532               if (SOfatherFather) {
533                 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
534                 index++;
535                 listSOmesh.resize(index);
536                 listSOmesh[index - 1] = SOfatherFather;
537               }
538             }
539           }
540         }
541       }
542     }
543     if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
544     return listSOmesh;
545   }
546 }