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 list<HypothesesSet*> myListOfHypothesesSets;
71 void processHypothesisStatus(const int theHypStatus,
72 SMESH::SMESH_Hypothesis_ptr theHyp,
73 const bool theIsAddition)
75 if (theHypStatus > SMESH::HYP_OK) {
78 QString aHypName ("NULL Hypothesis");
79 if (!CORBA::is_nil(theHyp)) {
80 _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
83 aHypName = Shyp->GetName().c_str();
86 aHypName = GetHypothesisData(theHyp->GetName())->Label;
90 bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
93 aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
95 aMsg = (isFatal ? "SMESH_CANT_RM_HYP" : "SMESH_RM_HYP_WRN");
97 aMsg = QObject::tr(aMsg).arg(aHypName) +
98 QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus));
100 SUIT_MessageBox::warn1(SMESHGUI::desktop(),
101 QObject::tr("SMESH_WRN_WARNING"),
103 QObject::tr("SMESH_BUT_OK"));
108 void InitAvailableHypotheses()
110 SUIT_OverrideCursor wc;
111 if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
113 SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
116 // Find name of a resource XML file ("SMESH_Meshers.xml");
118 char* cenv = getenv("SMESH_MeshersList");
120 HypsXml.sprintf("%s", cenv);
122 QStringList HypsXmlList = QStringList::split(":", HypsXml, false);
123 if (HypsXmlList.count() == 0)
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"));
132 // loop on files in HypsXml
133 QString aNoAccessFiles;
134 for (int i = 0; i < HypsXmlList.count(); i++) {
135 QString HypsXml = HypsXmlList[ i ];
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");
142 QFile file (xmlFile);
143 if (file.exists() && file.open(IO_ReadOnly)) {
146 SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
149 QXmlInputSource source (file);
150 QXmlSimpleReader reader;
151 reader.setContentHandler(aXmlHandler);
152 reader.setErrorHandler(aXmlHandler);
153 bool ok = reader.parse(source);
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 );
164 SUIT_MessageBox::error1(SMESHGUI::desktop(),
165 QObject::tr("INF_PARSE_ERROR"),
166 QObject::tr(aXmlHandler->errorProtocol()),
167 QObject::tr("SMESH_BUT_OK"));
171 if (aNoAccessFiles.isEmpty())
172 aNoAccessFiles = xmlFile;
174 aNoAccessFiles += ", " + xmlFile;
179 if (!aNoAccessFiles.isEmpty()) {
180 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
181 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
183 SUIT_MessageBox::warn1(SMESHGUI::desktop(),
184 QObject::tr("SMESH_WRN_WARNING"),
186 QObject::tr("SMESH_BUT_OK"));
193 QStringList GetAvailableHypotheses( const bool isAlgo,
197 QStringList aHypList;
199 // Init list of available hypotheses, if needed
200 InitAvailableHypotheses();
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++ )
207 HypothesisData* aData = (*anIter).second;
208 if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux )
209 aHypList.append(((*anIter).first).c_str());
215 QStringList GetHypothesesSets()
217 QStringList aSetNameList;
219 // Init list of available hypotheses, if needed
220 InitAvailableHypotheses();
222 list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
223 for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
225 HypothesesSet* aSet = *hypoSet;
226 if ( aSet && aSet->AlgoList.count() ) {
227 aSetNameList.append( aSet->HypoSetName );
234 HypothesesSet* GetHypothesesSet(const QString theSetName)
236 list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
237 for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
239 HypothesesSet* aSet = *hypoSet;
240 if ( aSet && aSet->HypoSetName == theSetName )
246 HypothesisData* GetHypothesisData (const char* aHypType)
248 HypothesisData* aHypData = 0;
250 // Init list of available hypotheses, if needed
251 InitAvailableHypotheses();
253 THypothesisDataMap::iterator type_data = myHypothesesMap.find(aHypType);
254 if (type_data != myHypothesesMap.end()) {
255 aHypData = type_data->second;
258 type_data = myAlgorithmsMap.find(aHypType);
259 if (type_data != myAlgorithmsMap.end())
260 aHypData = type_data->second;
265 bool IsAvailableHypothesis(const HypothesisData* algoData,
266 const QString& hypType,
272 if ( algoData->NeededHypos.contains( hypType ))
274 if ( algoData->OptionalHypos.contains( hypType)) {
281 bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
282 const HypothesisData* algo2Data)
284 if ( !algo1Data || !algo2Data )
286 const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
287 if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
288 algoIn = algo2Data; algoMain = algo1Data;
290 // look for any output type of algoIn between input types of algoMain
291 QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
292 for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
293 if ( algoMain->InputTypes.contains( *inElemType ))
298 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const char* aHypType)
300 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType);
302 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
304 // check, if creator for this hypothesis type already exists
305 if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
306 aCreator = myHypCreatorMap[aHypType];
309 // 1. Init list of available hypotheses, if needed
310 InitAvailableHypotheses();
312 // 2. Get names of plugin libraries
313 HypothesisData* aHypData = GetHypothesisData(aHypType);
316 QString aClientLibName = aHypData->ClientLibName;
317 QString aServerLibName = aHypData->ServerLibName;
319 // 3. Load Client Plugin Library
321 // load plugin library
322 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
323 void* libHandle = dlopen (aClientLibName, RTLD_LAZY);
325 // report any error, if occured
326 const char* anError = dlerror();
327 if(MYDEBUG) MESSAGE(anError);
330 // get method, returning hypothesis creator
331 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
332 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
334 GetHypothesisCreator procHandle =
335 (GetHypothesisCreator)dlsym(libHandle, "GetHypothesisCreator");
337 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
341 // get hypothesis creator
342 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType);
343 aCreator = procHandle( aHypType );
345 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
348 // map hypothesis creator to a hypothesis name
349 myHypCreatorMap[aHypType] = aCreator;
354 catch (const SALOME::SALOME_Exception& S_ex) {
355 SalomeApp_Tools::QtCatchCorbaException(S_ex);
363 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const char* aHypType,
364 const char* aHypName,
367 if(MYDEBUG) MESSAGE("Create " << aHypType << " with name " << aHypName);
369 SMESH::SMESH_Hypothesis_var Hyp;
371 HypothesisData* aHypData = GetHypothesisData(aHypType);
372 QString aServLib = aHypData->ServerLibName;
375 Hyp = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType, aServLib);
376 if (!Hyp->_is_nil()) {
377 _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
379 //if (strcmp(aHypName,"") != 0)
380 if (strlen(aHypName) > 0)
381 SMESH::SetName(SHyp, aHypName);
382 //SalomeApp_Application* app =
383 // dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
385 // app->objectBrowser()->updateTree();
386 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
391 catch (const SALOME::SALOME_Exception & S_ex) {
392 SalomeApp_Tools::QtCatchCorbaException(S_ex);
395 return SMESH::SMESH_Hypothesis::_nil();
399 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
401 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
402 int res = SMESH::HYP_UNKNOWN_FATAL;
403 SUIT_OverrideCursor wc;
405 if (!aMesh->_is_nil()) {
406 _PTR(SObject) SM = SMESH::FindSObject(aMesh);
407 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
409 res = aMesh->AddHypothesis(aShapeObject, aHyp);
410 if (res < SMESH::HYP_UNKNOWN_FATAL) {
411 _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
413 SMESH::ModifiedMesh(SM, false);
416 if (res > SMESH::HYP_OK) {
418 processHypothesisStatus(res, aHyp, true);
422 catch(const SALOME::SALOME_Exception& S_ex) {
424 SalomeApp_Tools::QtCatchCorbaException(S_ex);
425 res = SMESH::HYP_UNKNOWN_FATAL;
428 return res < SMESH::HYP_UNKNOWN_FATAL;
432 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
434 if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
435 int res = SMESH::HYP_UNKNOWN_FATAL;
436 SUIT_OverrideCursor wc;
438 if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
440 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
441 _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
442 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
443 if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
444 res = aMesh->AddHypothesis(aShapeObject, aHyp);
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, aHyp, true);
457 SCRUTE(aHyp->_is_nil());
458 SCRUTE(aMesh->_is_nil());
460 SCRUTE(aShapeObject->_is_nil());
463 catch(const SALOME::SALOME_Exception& S_ex) {
465 SalomeApp_Tools::QtCatchCorbaException(S_ex);
466 res = SMESH::HYP_UNKNOWN_FATAL;
470 SCRUTE(aSubMesh->_is_nil());
471 SCRUTE(aHyp->_is_nil());
473 return res < SMESH::HYP_UNKNOWN_FATAL;
476 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
478 int res = SMESH::HYP_UNKNOWN_FATAL;
479 SUIT_OverrideCursor wc;
482 _PTR(Study) aStudy = GetActiveStudyDocument();
483 _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
486 _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
487 _PTR(SObject) aRealHypo;
488 if( aHypObj->ReferencedObject( aRealHypo ) )
490 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
491 RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
495 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
496 SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
497 for( int i = 0; i < meshList.size(); i++ )
498 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
502 catch(const SALOME::SALOME_Exception& S_ex)
505 SalomeApp_Tools::QtCatchCorbaException(S_ex);
506 res = SMESH::HYP_UNKNOWN_FATAL;
508 return res < SMESH::HYP_UNKNOWN_FATAL;
511 bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
512 SMESH::SMESH_Hypothesis_ptr anHyp)
514 SALOMEDS::GenericAttribute_var anAttr;
515 SALOMEDS::AttributeIOR_var anIOR;
516 int res = SMESH::HYP_UNKNOWN_FATAL;
517 SUIT_OverrideCursor wc;
521 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
522 if (!aShapeObject->_is_nil()) {
523 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
524 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
526 if (!aSubMesh->_is_nil())
527 aMesh = aSubMesh->GetFather();
529 if (!aMesh->_is_nil()) {
530 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
531 if (res < SMESH::HYP_UNKNOWN_FATAL) {
532 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
534 SMESH::ModifiedMesh(meshSO, false);
536 if (res > SMESH::HYP_OK) {
538 processHypothesisStatus(res, anHyp, false);
543 } catch(const SALOME::SALOME_Exception& S_ex) {
545 SalomeApp_Tools::QtCatchCorbaException(S_ex);
546 res = SMESH::HYP_UNKNOWN_FATAL;
549 return res < SMESH::HYP_UNKNOWN_FATAL;
552 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
554 SObjectList listSOmesh;
555 listSOmesh.resize(0);
557 unsigned int index = 0;
558 if (!AlgoOrHyp->_is_nil()) {
559 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
562 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
564 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
565 for (unsigned int i = 0; i < listSO.size(); i++) {
566 _PTR(SObject) SO = listSO[i];
568 _PTR(SObject) aFather = SO->GetFather();
570 _PTR(SObject) SOfatherFather = aFather->GetFather();
571 if (SOfatherFather) {
572 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
574 listSOmesh.resize(index);
575 listSOmesh[index - 1] = SOfatherFather;
582 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
586 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( #enum ); break;
587 QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
589 QString resMsg = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
590 for ( int i = 0; i < errors.length(); ++i ) {
591 const SMESH::AlgoStateError & error = errors[ i ];
593 switch( error.name ) {
594 CASE2MESSAGE( MISSING_ALGO );
595 CASE2MESSAGE( MISSING_HYPO );
596 CASE2MESSAGE( NOT_CONFORM_MESH );
599 // apply args to message:
601 if ( error.algoName.in() != 0 )
602 msg = msg.arg( error.algoName.in() );
604 msg = msg.arg( error.algoDim );
606 msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
607 // %4 - hypothesis dim == algoDim
608 msg = msg.arg( error.algoDim );
610 if ( i ) resMsg += ";\n";