1 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
20 #include "SMESHGUI_HypothesesUtils.h"
23 #include "SMESHGUI_Hypotheses.h"
24 #include "SMESHGUI_XmlHandler.h"
25 #include "SMESHGUI_Utils.h"
26 #include "SMESHGUI_GEOMGenUtils.h"
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"
35 #include "OB_Browser.h"
37 #include "SalomeApp_Study.h"
38 #include "SalomeApp_Tools.h"
39 #include "SalomeApp_Application.h"
41 #include <SALOMEDSClient_Study.hxx>
42 #include <SALOMEDSClient_SObject.hxx>
44 #include "SALOMEconfig.h"
45 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
53 static int MYDEBUG = 0;
55 static int MYDEBUG = 0;
62 typedef map<string,HypothesisData*> THypothesisDataMap;
63 THypothesisDataMap myHypothesesMap;
64 THypothesisDataMap myAlgorithmsMap;
66 typedef map<string,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
67 THypCreatorMap myHypCreatorMap;
69 void addMap(const THypothesisDataMap& theMap,
70 THypothesisDataMap& toMap)
72 THypothesisDataMap::const_iterator it;
73 for (it = theMap.begin(); it != theMap.end(); it++)
78 void processHypothesisStatus(const int theHypStatus,
79 SMESH::SMESH_Hypothesis_ptr theHyp,
80 const bool theIsAddition)
82 if (theHypStatus > SMESH::HYP_OK) {
85 QString aHypName ("NULL Hypothesis");
86 if (!CORBA::is_nil(theHyp)) {
87 _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
90 aHypName = Shyp->GetName().c_str();
93 aHypName = GetHypothesisData(theHyp->GetName())->Label;
97 bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
100 aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
102 aMsg = (isFatal ? "SMESH_CANT_RM_HYP" : "SMESH_RM_HYP_WRN");
104 aMsg = QObject::tr(aMsg).arg(aHypName) +
105 QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus));
107 SUIT_MessageBox::warn1(SMESHGUI::desktop(),
108 QObject::tr("SMESH_WRN_WARNING"),
110 QObject::tr("SMESH_BUT_OK"));
115 void InitAvailableHypotheses()
117 SUIT_OverrideCursor wc;
118 if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
120 SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
123 // Find name of a resource XML file ("SMESH_Meshers.xml");
125 char* cenv = getenv("SMESH_MeshersList");
127 HypsXml.sprintf("%s", cenv);
129 QStringList HypsXmlList = QStringList::split(":", HypsXml, false);
130 if (HypsXmlList.count() == 0)
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"));
139 // loop on files in HypsXml
140 QString aNoAccessFiles;
141 for (int i = 0; i < HypsXmlList.count(); i++) {
142 QString HypsXml = HypsXmlList[ i ];
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");
149 QFile file (xmlFile);
150 if (file.exists() && file.open(IO_ReadOnly)) {
153 SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
156 QXmlInputSource source (file);
157 QXmlSimpleReader reader;
158 reader.setContentHandler(aXmlHandler);
159 reader.setErrorHandler(aXmlHandler);
160 bool ok = reader.parse(source);
163 addMap(aXmlHandler->myHypothesesMap, myHypothesesMap);
164 addMap(aXmlHandler->myAlgorithmsMap, myAlgorithmsMap);
167 SUIT_MessageBox::error1(SMESHGUI::desktop(),
168 QObject::tr("INF_PARSE_ERROR"),
169 QObject::tr(aXmlHandler->errorProtocol()),
170 QObject::tr("SMESH_BUT_OK"));
174 if (aNoAccessFiles.isEmpty())
175 aNoAccessFiles = xmlFile;
177 aNoAccessFiles += ", " + xmlFile;
182 if (!aNoAccessFiles.isEmpty()) {
183 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
184 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
186 SUIT_MessageBox::warn1(SMESHGUI::desktop(),
187 QObject::tr("SMESH_WRN_WARNING"),
189 QObject::tr("SMESH_BUT_OK"));
196 QStringList GetAvailableHypotheses(const bool isAlgo)
198 QStringList aHypList;
200 // Init list of available hypotheses, if needed
201 InitAvailableHypotheses();
203 // fill list of hypotheses/algorithms
204 THypothesisDataMap::iterator anIter;
206 anIter = myAlgorithmsMap.begin();
207 for (; anIter != myAlgorithmsMap.end(); anIter++) {
208 aHypList.append(((*anIter).first).c_str());
212 anIter = myHypothesesMap.begin();
213 for (; anIter != myHypothesesMap.end(); anIter++) {
214 aHypList.append(((*anIter).first).c_str());
222 HypothesisData* GetHypothesisData (const char* aHypType)
224 HypothesisData* aHypData = 0;
226 // Init list of available hypotheses, if needed
227 InitAvailableHypotheses();
229 if (myHypothesesMap.find(aHypType) == myHypothesesMap.end()) {
230 if (myAlgorithmsMap.find(aHypType) != myAlgorithmsMap.end()) {
231 aHypData = myAlgorithmsMap[aHypType];
235 aHypData = myHypothesesMap[aHypType];
241 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const char* aHypType)
243 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType);
245 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
247 // check, if creator for this hypothesis type already exists
248 if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
249 aCreator = myHypCreatorMap[aHypType];
252 // 1. Init list of available hypotheses, if needed
253 InitAvailableHypotheses();
255 // 2. Get names of plugin libraries
256 HypothesisData* aHypData = GetHypothesisData(aHypType);
260 QString aClientLibName = aHypData->ClientLibName;
261 QString aServerLibName = aHypData->ServerLibName;
263 // 3. Load Client Plugin Library
265 // load plugin library
266 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
267 void* libHandle = dlopen (aClientLibName, RTLD_LAZY);
269 // report any error, if occured
270 const char* anError = dlerror();
271 if(MYDEBUG) MESSAGE(anError);
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");
281 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
285 // get hypothesis creator
286 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType);
287 aCreator = procHandle(aHypType, aServerLibName, SMESHGUI::GetSMESHGUI());
289 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
292 // map hypothesis creator to a hypothesis name
293 myHypCreatorMap[aHypType] = aCreator;
298 catch (const SALOME::SALOME_Exception& S_ex) {
299 SalomeApp_Tools::QtCatchCorbaException(S_ex);
307 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const char* aHypType,
308 const char* aHypName,
311 if(MYDEBUG) MESSAGE("Create " << aHypType << " with name " << aHypName);
313 SMESH::SMESH_Hypothesis_var Hyp;
315 HypothesisData* aHypData = GetHypothesisData(aHypType);
316 QString aServLib = aHypData->ServerLibName;
319 Hyp = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType, aServLib);
320 if (!Hyp->_is_nil()) {
321 _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
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());
329 // app->objectBrowser()->updateTree();
330 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
335 catch (const SALOME::SALOME_Exception & S_ex) {
336 SalomeApp_Tools::QtCatchCorbaException(S_ex);
339 return SMESH::SMESH_Hypothesis::_nil();
343 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
345 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
346 int res = SMESH::HYP_UNKNOWN_FATAL;
347 SUIT_OverrideCursor wc;
349 if (!aMesh->_is_nil()) {
350 _PTR(SObject) SM = SMESH::FindSObject(aMesh);
351 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
353 res = aMesh->AddHypothesis(aShapeObject, aHyp);
354 if (res < SMESH::HYP_UNKNOWN_FATAL) {
355 _PTR(SObject) SH = SMESH::FindSObject(aHyp);
357 SMESH::ModifiedMesh(SM, false);
360 if (res > SMESH::HYP_OK) {
362 processHypothesisStatus(res, aHyp, true);
366 catch(const SALOME::SALOME_Exception& S_ex) {
368 SalomeApp_Tools::QtCatchCorbaException(S_ex);
369 res = SMESH::HYP_UNKNOWN_FATAL;
372 return res < SMESH::HYP_UNKNOWN_FATAL;
376 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
378 if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
379 int res = SMESH::HYP_UNKNOWN_FATAL;
380 SUIT_OverrideCursor wc;
382 if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
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);
392 SMESH::ModifiedMesh(meshSO, false);
394 if (res > SMESH::HYP_OK) {
396 processHypothesisStatus(res, aHyp, true);
401 SCRUTE(aHyp->_is_nil());
402 SCRUTE(aMesh->_is_nil());
404 SCRUTE(aShapeObject->_is_nil());
407 catch(const SALOME::SALOME_Exception& S_ex) {
409 SalomeApp_Tools::QtCatchCorbaException(S_ex);
410 res = SMESH::HYP_UNKNOWN_FATAL;
414 SCRUTE(aSubMesh->_is_nil());
415 SCRUTE(aHyp->_is_nil());
417 return res < SMESH::HYP_UNKNOWN_FATAL;
420 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
422 int res = SMESH::HYP_UNKNOWN_FATAL;
423 SUIT_OverrideCursor wc;
425 if (IObject->hasReference()) {
427 _PTR(Study) aStudy = GetActiveStudyDocument();
428 SMESH_Hypothesis_var anHyp = IObjectToInterface<SMESH_Hypothesis>(IObject);
429 _PTR(SObject) aHypSObj = aStudy->FindObjectID(IObject->getReference());
431 _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh(aHypSObj);
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);
440 if (!aSubMesh->_is_nil())
441 aMesh = aSubMesh->GetFather();
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);
448 SMESH::ModifiedMesh(meshSO, false);
450 if (res > SMESH::HYP_OK) {
452 processHypothesisStatus(res, anHyp, false);
460 catch(const SALOME::SALOME_Exception& S_ex) {
462 SalomeApp_Tools::QtCatchCorbaException(S_ex);
463 res = SMESH::HYP_UNKNOWN_FATAL;
466 else if (IObject->hasEntry()) {
467 if(MYDEBUG) MESSAGE("IObject entry " << IObject->getEntry());
469 return res < SMESH::HYP_UNKNOWN_FATAL;
472 bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
473 SMESH::SMESH_Hypothesis_ptr anHyp)
475 SALOMEDS::GenericAttribute_var anAttr;
476 SALOMEDS::AttributeIOR_var anIOR;
477 int res = SMESH::HYP_UNKNOWN_FATAL;
478 SUIT_OverrideCursor wc;
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);
487 if (!aSubMesh->_is_nil())
488 aMesh = aSubMesh->GetFather();
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);
495 SMESH::ModifiedMesh(meshSO, false);
497 if (res > SMESH::HYP_OK) {
499 processHypothesisStatus(res, anHyp, false);
504 } catch(const SALOME::SALOME_Exception& S_ex) {
506 SalomeApp_Tools::QtCatchCorbaException(S_ex);
507 res = SMESH::HYP_UNKNOWN_FATAL;
510 return res < SMESH::HYP_UNKNOWN_FATAL;
513 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
515 SObjectList listSOmesh;
516 listSOmesh.resize(0);
518 unsigned int index = 0;
519 if (!AlgoOrHyp->_is_nil()) {
520 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
523 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
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];
529 _PTR(SObject) aFather = SO->GetFather();
531 _PTR(SObject) SOfatherFather = aFather->GetFather();
532 if (SOfatherFather) {
533 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
535 listSOmesh.resize(index);
536 listSOmesh[index - 1] = SOfatherFather;
543 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");