1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 CEA
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // File : SMESHGUI_HypothesesUtils.cxx
22 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
26 #include "SMESHGUI_HypothesesUtils.h"
29 #include "SMESHGUI_Hypotheses.h"
30 #include "SMESHGUI_XmlHandler.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_GEOMGenUtils.h"
34 // SALOME GUI includes
35 #include <SUIT_Desktop.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_OverrideCursor.h>
38 #include <SUIT_ResourceMgr.h>
40 #include <SalomeApp_Study.h>
41 #include <SalomeApp_Tools.h>
43 // SALOME KERNEL includes
44 #include <utilities.h>
61 #define LibHandle HMODULE
62 #define LoadLib( name ) LoadLibrary( name )
63 #define GetProc GetProcAddress
64 #define UnLoadLib( handle ) FreeLibrary( handle );
66 #define LibHandle void*
67 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
69 #define UnLoadLib( handle ) dlclose( handle );
73 static int MYDEBUG = 0;
75 static int MYDEBUG = 0;
80 typedef QMap<std::string,HypothesisData*> THypothesisDataMap;
81 THypothesisDataMap myHypothesesMap;
82 THypothesisDataMap myAlgorithmsMap;
84 typedef QMap<std::string,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
85 THypCreatorMap myHypCreatorMap;
87 std::list<HypothesesSet*> myListOfHypothesesSets;
88 //QList<HypothesesSet*> myListOfHypothesesSets;
90 void processHypothesisStatus(const int theHypStatus,
91 SMESH::SMESH_Hypothesis_ptr theHyp,
92 const bool theIsAddition)
94 if (theHypStatus > SMESH::HYP_OK) {
97 QString aHypName ("NULL Hypothesis");
98 if (!CORBA::is_nil(theHyp)) {
99 _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
102 aHypName = Shyp->GetName().c_str();
105 aHypName = GetHypothesisData(theHyp->GetName())->Label;
109 bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
112 aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
114 aMsg = (isFatal ? "SMESH_CANT_RM_HYP" : "SMESH_RM_HYP_WRN");
116 aMsg = QObject::tr(aMsg.toLatin1().data()).arg(aHypName) +
117 QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus).toLatin1().data());
119 if ( theHypStatus == SMESH::HYP_HIDDEN_ALGO ) // PAL18501
120 aMsg = aMsg.arg( GetHypothesisData(theHyp->GetName())->Dim[0] );
122 SUIT_MessageBox::warning(SMESHGUI::desktop(),
123 QObject::tr("SMESH_WRN_WARNING"),
129 void InitAvailableHypotheses()
131 SUIT_OverrideCursor wc;
132 if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
134 SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
137 // Find name of a resource XML file ("SMESH_Meshers.xml");
139 char* cenv = getenv("SMESH_MeshersList");
141 HypsXml.sprintf("%s", cenv);
143 QStringList HypsXmlList = HypsXml.split(":", QString::SkipEmptyParts);
144 if (HypsXmlList.count() == 0)
146 SUIT_MessageBox::critical(SMESHGUI::desktop(),
147 QObject::tr("SMESH_WRN_WARNING"),
148 QObject::tr("MESHERS_FILE_NO_VARIABLE"));
152 // loop on files in HypsXml
153 QString aNoAccessFiles;
154 for (int i = 0; i < HypsXmlList.count(); i++) {
155 QString HypsXml = HypsXmlList[ i ];
157 // Find full path to the resource XML file
158 QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
159 if ( xmlFile.isEmpty() ) // try PLUGIN resources
160 xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
162 QFile file (xmlFile);
163 if (file.exists() && file.open(QIODevice::ReadOnly)) {
166 SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
169 QXmlInputSource source (&file);
170 QXmlSimpleReader reader;
171 reader.setContentHandler(aXmlHandler);
172 reader.setErrorHandler(aXmlHandler);
173 bool ok = reader.parse(source);
176 myHypothesesMap = QMap<std::string,HypothesisData*>( aXmlHandler->myHypothesesMap );
177 myAlgorithmsMap = QMap<std::string,HypothesisData*>( aXmlHandler->myAlgorithmsMap );
178 myListOfHypothesesSets.splice( myListOfHypothesesSets.begin(),
179 aXmlHandler->myListOfHypothesesSets );
182 SUIT_MessageBox::critical(SMESHGUI::desktop(),
183 QObject::tr("INF_PARSE_ERROR"),
184 QObject::tr(aXmlHandler->errorProtocol().toLatin1().data()));
188 if (aNoAccessFiles.isEmpty())
189 aNoAccessFiles = xmlFile;
191 aNoAccessFiles += ", " + xmlFile;
196 if (!aNoAccessFiles.isEmpty()) {
197 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
198 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
200 SUIT_MessageBox::warning(SMESHGUI::desktop(),
201 QObject::tr("SMESH_WRN_WARNING"),
209 QStringList GetAvailableHypotheses( const bool isAlgo,
212 const bool isNeedGeometry)
214 QStringList aHypList;
216 // Init list of available hypotheses, if needed
217 InitAvailableHypotheses();
218 bool checkGeometry = !isNeedGeometry;
219 // fill list of hypotheses/algorithms
220 THypothesisDataMap* pMap = isAlgo ? &myAlgorithmsMap : &myHypothesesMap;
221 THypothesisDataMap::iterator anIter;
222 for ( anIter = pMap->begin(); anIter != pMap->end(); anIter++ )
224 HypothesisData* aData = anIter.value();
225 if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux)
227 if (aData->IsNeedGeometry == isNeedGeometry)
228 aHypList.append(anIter.key().c_str());
231 aHypList.append(anIter.key().c_str());
237 QStringList GetHypothesesSets()
239 QStringList aSetNameList;
241 // Init list of available hypotheses, if needed
242 InitAvailableHypotheses();
244 std::list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
245 //QList<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
246 for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
248 HypothesesSet* aSet = *hypoSet;
249 if ( aSet && aSet->AlgoList.count() ) {
250 aSetNameList.append( aSet->HypoSetName );
257 HypothesesSet* GetHypothesesSet(const QString& theSetName)
259 std::list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
260 //QList<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
261 for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
263 HypothesesSet* aSet = *hypoSet;
264 if ( aSet && aSet->HypoSetName == theSetName )
270 HypothesisData* GetHypothesisData (const QString& aHypType)
272 HypothesisData* aHypData = 0;
274 // Init list of available hypotheses, if needed
275 InitAvailableHypotheses();
277 THypothesisDataMap::iterator type_data = myHypothesesMap.find(aHypType.toLatin1().data());
278 if (type_data != myHypothesesMap.end()) {
279 aHypData = type_data.value();
282 type_data = myAlgorithmsMap.find(aHypType.toLatin1().data());
283 if (type_data != myAlgorithmsMap.end())
284 aHypData = type_data.value();
289 bool IsAvailableHypothesis(const HypothesisData* algoData,
290 const QString& hypType,
296 if ( algoData->NeededHypos.contains( hypType ))
298 if ( algoData->OptionalHypos.contains( hypType)) {
305 bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
306 const HypothesisData* algo2Data)
308 if ( !algo1Data || !algo2Data )
310 const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
311 if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
312 algoIn = algo2Data; algoMain = algo1Data;
314 // look for any output type of algoIn between input types of algoMain
315 QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
316 for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
317 if ( algoMain->InputTypes.contains( *inElemType ))
322 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const QString& aHypType)
324 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType.toLatin1().data());
326 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
328 // check, if creator for this hypothesis type already exists
329 if (myHypCreatorMap.find(aHypType.toLatin1().data()) != myHypCreatorMap.end()) {
330 aCreator = myHypCreatorMap[aHypType.toLatin1().data()];
333 // 1. Init list of available hypotheses, if needed
334 InitAvailableHypotheses();
336 // 2. Get names of plugin libraries
337 HypothesisData* aHypData = GetHypothesisData(aHypType);
340 QString aClientLibName = aHypData->ClientLibName;
341 QString aServerLibName = aHypData->ServerLibName;
343 // 3. Load Client Plugin Library
345 // load plugin library
346 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
347 LibHandle libHandle = LoadLib( aClientLibName.toLatin1().data() );
349 // report any error, if occured
353 const char* anError = "Can't load client meshers plugin library";
355 const char* anError = dlerror();
361 // get method, returning hypothesis creator
362 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
363 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
365 GetHypothesisCreator procHandle =
366 (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
368 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
369 UnLoadLib(libHandle);
372 // get hypothesis creator
373 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
374 aCreator = procHandle( aHypType );
376 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
379 // map hypothesis creator to a hypothesis name
380 myHypCreatorMap[aHypType.toLatin1().data()] = aCreator;
385 catch (const SALOME::SALOME_Exception& S_ex) {
386 SalomeApp_Tools::QtCatchCorbaException(S_ex);
394 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
395 const QString& aHypName,
398 if(MYDEBUG) MESSAGE("Create " << aHypType.toLatin1().data() <<
399 " with name " << aHypName.toLatin1().data());
400 HypothesisData* aHypData = GetHypothesisData(aHypType);
401 QString aServLib = aHypData->ServerLibName;
403 SMESH::SMESH_Hypothesis_var aHypothesis;
404 aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
405 aServLib.toLatin1().data());
406 if (!aHypothesis->_is_nil()) {
407 _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
409 if (!aHypName.isEmpty())
410 SMESH::SetName(aHypSObject, aHypName);
411 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
412 return aHypothesis._retn();
415 } catch (const SALOME::SALOME_Exception & S_ex) {
416 SalomeApp_Tools::QtCatchCorbaException(S_ex);
419 return SMESH::SMESH_Hypothesis::_nil();
423 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
425 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
426 int res = SMESH::HYP_UNKNOWN_FATAL;
427 SUIT_OverrideCursor wc;
429 if (!aMesh->_is_nil()) {
430 _PTR(SObject) SM = SMESH::FindSObject(aMesh);
431 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
433 res = aMesh->AddHypothesis(aShapeObject, aHyp);
434 if (res < SMESH::HYP_UNKNOWN_FATAL) {
435 _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
437 SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
440 if (res > SMESH::HYP_OK) {
442 processHypothesisStatus(res, aHyp, true);
446 catch(const SALOME::SALOME_Exception& S_ex) {
448 SalomeApp_Tools::QtCatchCorbaException(S_ex);
449 res = SMESH::HYP_UNKNOWN_FATAL;
452 return res < SMESH::HYP_UNKNOWN_FATAL;
456 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
458 if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
459 int res = SMESH::HYP_UNKNOWN_FATAL;
460 SUIT_OverrideCursor wc;
462 if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
464 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
465 _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
466 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
467 if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
468 res = aMesh->AddHypothesis(aShapeObject, aHyp);
469 if (res < SMESH::HYP_UNKNOWN_FATAL) {
470 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
472 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
474 if (res > SMESH::HYP_OK) {
476 processHypothesisStatus(res, aHyp, true);
481 SCRUTE(aHyp->_is_nil());
482 SCRUTE(aMesh->_is_nil());
484 SCRUTE(aShapeObject->_is_nil());
487 catch(const SALOME::SALOME_Exception& S_ex) {
489 SalomeApp_Tools::QtCatchCorbaException(S_ex);
490 res = SMESH::HYP_UNKNOWN_FATAL;
494 SCRUTE(aSubMesh->_is_nil());
495 SCRUTE(aHyp->_is_nil());
497 return res < SMESH::HYP_UNKNOWN_FATAL;
500 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
502 int res = SMESH::HYP_UNKNOWN_FATAL;
503 SUIT_OverrideCursor wc;
506 _PTR(Study) aStudy = GetActiveStudyDocument();
507 _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
510 _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
511 _PTR(SObject) aRealHypo;
512 if( aHypObj->ReferencedObject( aRealHypo ) )
514 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
515 RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
519 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
520 SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
521 for( int i = 0; i < meshList.size(); i++ )
522 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
526 catch(const SALOME::SALOME_Exception& S_ex)
529 SalomeApp_Tools::QtCatchCorbaException(S_ex);
530 res = SMESH::HYP_UNKNOWN_FATAL;
532 return res < SMESH::HYP_UNKNOWN_FATAL;
535 bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
536 SMESH::SMESH_Hypothesis_ptr anHyp)
538 SALOMEDS::GenericAttribute_var anAttr;
539 SALOMEDS::AttributeIOR_var anIOR;
540 int res = SMESH::HYP_UNKNOWN_FATAL;
541 SUIT_OverrideCursor wc;
545 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
546 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
547 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
549 if (!aSubMesh->_is_nil())
550 aMesh = aSubMesh->GetFather();
552 if (!aMesh->_is_nil()) {
553 if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
554 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
555 if (res < SMESH::HYP_UNKNOWN_FATAL) {
556 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
558 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
562 else if(!aMesh->HasShapeToMesh()){
563 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
564 if (res < SMESH::HYP_UNKNOWN_FATAL) {
565 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
567 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
570 if (res > SMESH::HYP_OK) {
572 processHypothesisStatus(res, anHyp, false);
576 } catch(const SALOME::SALOME_Exception& S_ex) {
578 SalomeApp_Tools::QtCatchCorbaException(S_ex);
579 res = SMESH::HYP_UNKNOWN_FATAL;
582 return res < SMESH::HYP_UNKNOWN_FATAL;
585 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
587 SObjectList listSOmesh;
588 listSOmesh.resize(0);
590 unsigned int index = 0;
591 if (!AlgoOrHyp->_is_nil()) {
592 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
595 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
597 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
598 for (unsigned int i = 0; i < listSO.size(); i++) {
599 _PTR(SObject) SO = listSO[i];
601 _PTR(SObject) aFather = SO->GetFather();
603 _PTR(SObject) SOfatherFather = aFather->GetFather();
604 if (SOfatherFather) {
605 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
607 listSOmesh.resize(index);
608 listSOmesh[index - 1] = SOfatherFather;
615 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
619 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
620 QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
622 QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
623 for ( int i = 0; i < errors.length(); ++i ) {
624 const SMESH::AlgoStateError & error = errors[ i ];
625 const bool hasAlgo = ( strlen( error.algoName ) != 0 );
628 msg = QObject::tr( "STATE_ALGO_MISSING" );
630 switch( error.state ) {
631 CASE2MESSAGE( HYP_MISSING );
632 CASE2MESSAGE( HYP_NOTCONFORM );
633 CASE2MESSAGE( HYP_BAD_PARAMETER );
634 CASE2MESSAGE( HYP_BAD_GEOMETRY );
637 // apply args to message:
640 msg = msg.arg( error.algoName.in() );
642 msg = msg.arg( error.algoDim );
644 msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
645 // %4 - hypothesis dim == algoDim
646 msg = msg.arg( error.algoDim );
648 if ( i ) resMsg += ";\n";
653 } // end of namespace SMESH