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,
200 QStringList aHypList;
202 // Init list of available hypotheses, if needed
203 InitAvailableHypotheses();
205 // fill list of hypotheses/algorithms
206 THypothesisDataMap* pMap = isAlgo ? &myAlgorithmsMap : &myHypothesesMap;
207 THypothesisDataMap::iterator anIter;
208 for ( anIter = pMap->begin(); anIter != pMap->end(); anIter++ )
210 HypothesisData* aData = (*anIter).second;
211 if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux )
212 aHypList.append(((*anIter).first).c_str());
218 HypothesisData* GetHypothesisData (const char* aHypType)
220 HypothesisData* aHypData = 0;
222 // Init list of available hypotheses, if needed
223 InitAvailableHypotheses();
225 if (myHypothesesMap.find(aHypType) == myHypothesesMap.end()) {
226 if (myAlgorithmsMap.find(aHypType) != myAlgorithmsMap.end()) {
227 aHypData = myAlgorithmsMap[aHypType];
231 aHypData = myHypothesesMap[aHypType];
237 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const char* aHypType)
239 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType);
241 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
243 // check, if creator for this hypothesis type already exists
244 if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
245 aCreator = myHypCreatorMap[aHypType];
248 // 1. Init list of available hypotheses, if needed
249 InitAvailableHypotheses();
251 // 2. Get names of plugin libraries
252 HypothesisData* aHypData = GetHypothesisData(aHypType);
255 QString aClientLibName = aHypData->ClientLibName;
256 QString aServerLibName = aHypData->ServerLibName;
258 // 3. Load Client Plugin Library
260 // load plugin library
261 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
262 void* libHandle = dlopen (aClientLibName, RTLD_LAZY);
264 // report any error, if occured
265 const char* anError = dlerror();
266 if(MYDEBUG) MESSAGE(anError);
269 // get method, returning hypothesis creator
270 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
271 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
272 (QString aHypType, QString aServerLibName, SMESHGUI* aSMESHGUI);
273 GetHypothesisCreator procHandle =
274 (GetHypothesisCreator)dlsym(libHandle, "GetHypothesisCreator");
276 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
280 // get hypothesis creator
281 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType);
282 aCreator = procHandle(aHypType, aServerLibName, SMESHGUI::GetSMESHGUI());
284 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
287 // map hypothesis creator to a hypothesis name
288 myHypCreatorMap[aHypType] = aCreator;
293 catch (const SALOME::SALOME_Exception& S_ex) {
294 SalomeApp_Tools::QtCatchCorbaException(S_ex);
302 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const char* aHypType,
303 const char* aHypName,
306 if(MYDEBUG) MESSAGE("Create " << aHypType << " with name " << aHypName);
308 SMESH::SMESH_Hypothesis_var Hyp;
310 HypothesisData* aHypData = GetHypothesisData(aHypType);
311 QString aServLib = aHypData->ServerLibName;
314 Hyp = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType, aServLib);
315 if (!Hyp->_is_nil()) {
316 _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
318 //if (strcmp(aHypName,"") != 0)
319 if (strlen(aHypName) > 0)
320 SMESH::SetName(SHyp, aHypName);
321 //SalomeApp_Application* app =
322 // dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
324 // app->objectBrowser()->updateTree();
325 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
330 catch (const SALOME::SALOME_Exception & S_ex) {
331 SalomeApp_Tools::QtCatchCorbaException(S_ex);
334 return SMESH::SMESH_Hypothesis::_nil();
338 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
340 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
341 int res = SMESH::HYP_UNKNOWN_FATAL;
342 SUIT_OverrideCursor wc;
344 if (!aMesh->_is_nil()) {
345 _PTR(SObject) SM = SMESH::FindSObject(aMesh);
346 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
348 res = aMesh->AddHypothesis(aShapeObject, aHyp);
349 if (res < SMESH::HYP_UNKNOWN_FATAL) {
350 _PTR(SObject) SH = SMESH::FindSObject(aHyp);
352 SMESH::ModifiedMesh(SM, false);
355 if (res > SMESH::HYP_OK) {
357 processHypothesisStatus(res, aHyp, true);
361 catch(const SALOME::SALOME_Exception& S_ex) {
363 SalomeApp_Tools::QtCatchCorbaException(S_ex);
364 res = SMESH::HYP_UNKNOWN_FATAL;
367 return res < SMESH::HYP_UNKNOWN_FATAL;
371 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
373 if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
374 int res = SMESH::HYP_UNKNOWN_FATAL;
375 SUIT_OverrideCursor wc;
377 if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
379 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
380 _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
381 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
382 if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
383 res = aMesh->AddHypothesis(aShapeObject, aHyp);
384 if (res < SMESH::HYP_UNKNOWN_FATAL) {
385 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
387 SMESH::ModifiedMesh(meshSO, false);
389 if (res > SMESH::HYP_OK) {
391 processHypothesisStatus(res, aHyp, true);
396 SCRUTE(aHyp->_is_nil());
397 SCRUTE(aMesh->_is_nil());
399 SCRUTE(aShapeObject->_is_nil());
402 catch(const SALOME::SALOME_Exception& S_ex) {
404 SalomeApp_Tools::QtCatchCorbaException(S_ex);
405 res = SMESH::HYP_UNKNOWN_FATAL;
409 SCRUTE(aSubMesh->_is_nil());
410 SCRUTE(aHyp->_is_nil());
412 return res < SMESH::HYP_UNKNOWN_FATAL;
415 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
417 int res = SMESH::HYP_UNKNOWN_FATAL;
418 SUIT_OverrideCursor wc;
421 _PTR(Study) aStudy = GetActiveStudyDocument();
422 _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
425 _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
426 _PTR(SObject) aRealHypo;
427 if( aHypObj->ReferencedObject( aRealHypo ) )
429 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
430 RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
434 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
435 SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
436 for( int i = 0; i < meshList.size(); i++ )
437 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
441 catch(const SALOME::SALOME_Exception& S_ex)
444 SalomeApp_Tools::QtCatchCorbaException(S_ex);
445 res = SMESH::HYP_UNKNOWN_FATAL;
447 return res < SMESH::HYP_UNKNOWN_FATAL;
450 bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
451 SMESH::SMESH_Hypothesis_ptr anHyp)
453 SALOMEDS::GenericAttribute_var anAttr;
454 SALOMEDS::AttributeIOR_var anIOR;
455 int res = SMESH::HYP_UNKNOWN_FATAL;
456 SUIT_OverrideCursor wc;
460 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
461 if (!aShapeObject->_is_nil()) {
462 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
463 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
465 if (!aSubMesh->_is_nil())
466 aMesh = aSubMesh->GetFather();
468 if (!aMesh->_is_nil()) {
469 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
470 if (res < SMESH::HYP_UNKNOWN_FATAL) {
471 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
473 SMESH::ModifiedMesh(meshSO, false);
475 if (res > SMESH::HYP_OK) {
477 processHypothesisStatus(res, anHyp, false);
482 } catch(const SALOME::SALOME_Exception& S_ex) {
484 SalomeApp_Tools::QtCatchCorbaException(S_ex);
485 res = SMESH::HYP_UNKNOWN_FATAL;
488 return res < SMESH::HYP_UNKNOWN_FATAL;
491 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
493 SObjectList listSOmesh;
494 listSOmesh.resize(0);
496 unsigned int index = 0;
497 if (!AlgoOrHyp->_is_nil()) {
498 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
501 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
503 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
504 for (unsigned int i = 0; i < listSO.size(); i++) {
505 _PTR(SObject) SO = listSO[i];
507 _PTR(SObject) aFather = SO->GetFather();
509 _PTR(SObject) SOfatherFather = aFather->GetFather();
510 if (SOfatherFather) {
511 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
513 listSOmesh.resize(index);
514 listSOmesh[index - 1] = SOfatherFather;
521 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");