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>
58 #define LibHandle HMODULE
59 #define LoadLib( name ) LoadLibrary( name )
60 #define GetProc GetProcAddress
61 #define UnLoadLib( handle ) FreeLibrary( handle );
63 #define LibHandle void*
64 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
66 #define UnLoadLib( handle ) dlclose( handle );
70 static int MYDEBUG = 0;
72 static int MYDEBUG = 0;
77 typedef std::map<std::string,HypothesisData*> THypothesisDataMap;
78 THypothesisDataMap myHypothesesMap;
79 THypothesisDataMap myAlgorithmsMap;
81 typedef std::map<std::string,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
82 THypCreatorMap myHypCreatorMap;
84 std::list<HypothesesSet*> myListOfHypothesesSets;
86 void processHypothesisStatus(const int theHypStatus,
87 SMESH::SMESH_Hypothesis_ptr theHyp,
88 const bool theIsAddition)
90 if (theHypStatus > SMESH::HYP_OK) {
93 QString aHypName ("NULL Hypothesis");
94 if (!CORBA::is_nil(theHyp)) {
95 _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
98 aHypName = Shyp->GetName().c_str();
101 aHypName = GetHypothesisData(theHyp->GetName())->Label;
105 bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
108 aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
110 aMsg = (isFatal ? "SMESH_CANT_RM_HYP" : "SMESH_RM_HYP_WRN");
112 aMsg = QObject::tr(aMsg.toLatin1().data()).arg(aHypName) +
113 QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus).toLatin1().data());
115 if ( theHypStatus == SMESH::HYP_HIDDEN_ALGO ) // PAL18501
116 aMsg = aMsg.arg( GetHypothesisData(theHyp->GetName())->Dim[0] );
118 SUIT_MessageBox::warning(SMESHGUI::desktop(),
119 QObject::tr("SMESH_WRN_WARNING"),
125 void InitAvailableHypotheses()
127 SUIT_OverrideCursor wc;
128 if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
130 SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
133 // Find name of a resource XML file ("SMESH_Meshers.xml");
135 char* cenv = getenv("SMESH_MeshersList");
137 HypsXml.sprintf("%s", cenv);
139 QStringList HypsXmlList = HypsXml.split(":", QString::SkipEmptyParts);
140 if (HypsXmlList.count() == 0)
142 SUIT_MessageBox::critical(SMESHGUI::desktop(),
143 QObject::tr("SMESH_WRN_WARNING"),
144 QObject::tr("MESHERS_FILE_NO_VARIABLE"));
148 // loop on files in HypsXml
149 QString aNoAccessFiles;
150 for (int i = 0; i < HypsXmlList.count(); i++) {
151 QString HypsXml = HypsXmlList[ i ];
153 // Find full path to the resource XML file
154 QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
155 if ( xmlFile.isEmpty() ) // try PLUGIN resources
156 xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
158 QFile file (xmlFile);
159 if (file.exists() && file.open(QIODevice::ReadOnly)) {
162 SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
165 QXmlInputSource source (&file);
166 QXmlSimpleReader reader;
167 reader.setContentHandler(aXmlHandler);
168 reader.setErrorHandler(aXmlHandler);
169 bool ok = reader.parse(source);
172 myHypothesesMap.insert( aXmlHandler->myHypothesesMap.begin(),
173 aXmlHandler->myHypothesesMap.end() );
174 myAlgorithmsMap.insert( aXmlHandler->myAlgorithmsMap.begin(),
175 aXmlHandler->myAlgorithmsMap.end() );
176 myListOfHypothesesSets.splice( myListOfHypothesesSets.begin(),
177 aXmlHandler->myListOfHypothesesSets );
180 SUIT_MessageBox::critical(SMESHGUI::desktop(),
181 QObject::tr("INF_PARSE_ERROR"),
182 QObject::tr(aXmlHandler->errorProtocol().toLatin1().data()));
186 if (aNoAccessFiles.isEmpty())
187 aNoAccessFiles = xmlFile;
189 aNoAccessFiles += ", " + xmlFile;
194 if (!aNoAccessFiles.isEmpty()) {
195 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
196 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
198 SUIT_MessageBox::warning(SMESHGUI::desktop(),
199 QObject::tr("SMESH_WRN_WARNING"),
207 QStringList GetAvailableHypotheses( const bool isAlgo,
210 const bool isNeedGeometry)
212 QStringList aHypList;
214 // Init list of available hypotheses, if needed
215 InitAvailableHypotheses();
216 bool checkGeometry = !isNeedGeometry;
217 // fill list of hypotheses/algorithms
218 THypothesisDataMap* pMap = isAlgo ? &myAlgorithmsMap : &myHypothesesMap;
219 THypothesisDataMap::iterator anIter;
220 for ( anIter = pMap->begin(); anIter != pMap->end(); anIter++ )
222 HypothesisData* aData = (*anIter).second;
223 if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux)
225 if (aData->IsNeedGeometry == isNeedGeometry)
226 aHypList.append(((*anIter).first).c_str());
229 aHypList.append(((*anIter).first).c_str());
235 QStringList GetHypothesesSets()
237 QStringList aSetNameList;
239 // Init list of available hypotheses, if needed
240 InitAvailableHypotheses();
242 std::list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
243 for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
245 HypothesesSet* aSet = *hypoSet;
246 if ( aSet && aSet->AlgoList.count() ) {
247 aSetNameList.append( aSet->HypoSetName );
254 HypothesesSet* GetHypothesesSet(const QString& theSetName)
256 std::list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
257 for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
259 HypothesesSet* aSet = *hypoSet;
260 if ( aSet && aSet->HypoSetName == theSetName )
266 HypothesisData* GetHypothesisData (const QString& aHypType)
268 HypothesisData* aHypData = 0;
270 // Init list of available hypotheses, if needed
271 InitAvailableHypotheses();
273 THypothesisDataMap::iterator type_data = myHypothesesMap.find(aHypType.toLatin1().data());
274 if (type_data != myHypothesesMap.end()) {
275 aHypData = type_data->second;
278 type_data = myAlgorithmsMap.find(aHypType.toLatin1().data());
279 if (type_data != myAlgorithmsMap.end())
280 aHypData = type_data->second;
285 bool IsAvailableHypothesis(const HypothesisData* algoData,
286 const QString& hypType,
292 if ( algoData->NeededHypos.contains( hypType ))
294 if ( algoData->OptionalHypos.contains( hypType)) {
301 bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
302 const HypothesisData* algo2Data)
304 if ( !algo1Data || !algo2Data )
306 const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
307 if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
308 algoIn = algo2Data; algoMain = algo1Data;
310 // look for any output type of algoIn between input types of algoMain
311 QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
312 for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
313 if ( algoMain->InputTypes.contains( *inElemType ))
318 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const QString& aHypType)
320 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType.toLatin1().data());
322 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
324 // check, if creator for this hypothesis type already exists
325 if (myHypCreatorMap.find(aHypType.toLatin1().data()) != myHypCreatorMap.end()) {
326 aCreator = myHypCreatorMap[aHypType.toLatin1().data()];
329 // 1. Init list of available hypotheses, if needed
330 InitAvailableHypotheses();
332 // 2. Get names of plugin libraries
333 HypothesisData* aHypData = GetHypothesisData(aHypType);
336 QString aClientLibName = aHypData->ClientLibName;
337 QString aServerLibName = aHypData->ServerLibName;
339 // 3. Load Client Plugin Library
341 // load plugin library
342 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
343 LibHandle libHandle = LoadLib( aClientLibName.toLatin1().data() );
345 // report any error, if occured
349 const char* anError = "Can't load client meshers plugin library";
351 const char* anError = dlerror();
357 // get method, returning hypothesis creator
358 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
359 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
361 GetHypothesisCreator procHandle =
362 (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
364 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
365 UnLoadLib(libHandle);
368 // get hypothesis creator
369 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
370 aCreator = procHandle( aHypType );
372 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
375 // map hypothesis creator to a hypothesis name
376 myHypCreatorMap[aHypType.toLatin1().data()] = aCreator;
381 catch (const SALOME::SALOME_Exception& S_ex) {
382 SalomeApp_Tools::QtCatchCorbaException(S_ex);
390 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
391 const QString& aHypName,
394 if(MYDEBUG) MESSAGE("Create " << aHypType.toLatin1().data() <<
395 " with name " << aHypName.toLatin1().data());
396 HypothesisData* aHypData = GetHypothesisData(aHypType);
397 QString aServLib = aHypData->ServerLibName;
399 SMESH::SMESH_Hypothesis_var aHypothesis;
400 aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
401 aServLib.toLatin1().data());
402 if (!aHypothesis->_is_nil()) {
403 _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
405 if (!aHypName.isEmpty())
406 SMESH::SetName(aHypSObject, aHypName);
407 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
408 return aHypothesis._retn();
411 } catch (const SALOME::SALOME_Exception & S_ex) {
412 SalomeApp_Tools::QtCatchCorbaException(S_ex);
415 return SMESH::SMESH_Hypothesis::_nil();
419 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
421 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
422 int res = SMESH::HYP_UNKNOWN_FATAL;
423 SUIT_OverrideCursor wc;
425 if (!aMesh->_is_nil()) {
426 _PTR(SObject) SM = SMESH::FindSObject(aMesh);
427 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
429 res = aMesh->AddHypothesis(aShapeObject, aHyp);
430 if (res < SMESH::HYP_UNKNOWN_FATAL) {
431 _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
433 SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
436 if (res > SMESH::HYP_OK) {
438 processHypothesisStatus(res, aHyp, true);
442 catch(const SALOME::SALOME_Exception& S_ex) {
444 SalomeApp_Tools::QtCatchCorbaException(S_ex);
445 res = SMESH::HYP_UNKNOWN_FATAL;
448 return res < SMESH::HYP_UNKNOWN_FATAL;
452 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
454 if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
455 int res = SMESH::HYP_UNKNOWN_FATAL;
456 SUIT_OverrideCursor wc;
458 if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
460 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
461 _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
462 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
463 if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
464 res = aMesh->AddHypothesis(aShapeObject, aHyp);
465 if (res < SMESH::HYP_UNKNOWN_FATAL) {
466 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
468 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
470 if (res > SMESH::HYP_OK) {
472 processHypothesisStatus(res, aHyp, true);
477 SCRUTE(aHyp->_is_nil());
478 SCRUTE(aMesh->_is_nil());
480 SCRUTE(aShapeObject->_is_nil());
483 catch(const SALOME::SALOME_Exception& S_ex) {
485 SalomeApp_Tools::QtCatchCorbaException(S_ex);
486 res = SMESH::HYP_UNKNOWN_FATAL;
490 SCRUTE(aSubMesh->_is_nil());
491 SCRUTE(aHyp->_is_nil());
493 return res < SMESH::HYP_UNKNOWN_FATAL;
496 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
498 int res = SMESH::HYP_UNKNOWN_FATAL;
499 SUIT_OverrideCursor wc;
502 _PTR(Study) aStudy = GetActiveStudyDocument();
503 _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
506 _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
507 _PTR(SObject) aRealHypo;
508 if( aHypObj->ReferencedObject( aRealHypo ) )
510 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
511 RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
515 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
516 SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
517 for( int i = 0; i < meshList.size(); i++ )
518 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
522 catch(const SALOME::SALOME_Exception& S_ex)
525 SalomeApp_Tools::QtCatchCorbaException(S_ex);
526 res = SMESH::HYP_UNKNOWN_FATAL;
528 return res < SMESH::HYP_UNKNOWN_FATAL;
531 bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
532 SMESH::SMESH_Hypothesis_ptr anHyp)
534 SALOMEDS::GenericAttribute_var anAttr;
535 SALOMEDS::AttributeIOR_var anIOR;
536 int res = SMESH::HYP_UNKNOWN_FATAL;
537 SUIT_OverrideCursor wc;
541 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
542 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
543 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
545 if (!aSubMesh->_is_nil())
546 aMesh = aSubMesh->GetFather();
548 if (!aMesh->_is_nil()) {
549 if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
550 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
551 if (res < SMESH::HYP_UNKNOWN_FATAL) {
552 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
554 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
558 else if(!aMesh->HasShapeToMesh()){
559 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
560 if (res < SMESH::HYP_UNKNOWN_FATAL) {
561 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
563 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
566 if (res > SMESH::HYP_OK) {
568 processHypothesisStatus(res, anHyp, false);
572 } catch(const SALOME::SALOME_Exception& S_ex) {
574 SalomeApp_Tools::QtCatchCorbaException(S_ex);
575 res = SMESH::HYP_UNKNOWN_FATAL;
578 return res < SMESH::HYP_UNKNOWN_FATAL;
581 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
583 SObjectList listSOmesh;
584 listSOmesh.resize(0);
586 unsigned int index = 0;
587 if (!AlgoOrHyp->_is_nil()) {
588 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
591 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
593 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
594 for (unsigned int i = 0; i < listSO.size(); i++) {
595 _PTR(SObject) SO = listSO[i];
597 _PTR(SObject) aFather = SO->GetFather();
599 _PTR(SObject) SOfatherFather = aFather->GetFather();
600 if (SOfatherFather) {
601 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
603 listSOmesh.resize(index);
604 listSOmesh[index - 1] = SOfatherFather;
611 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
615 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
616 QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
618 QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
619 for ( int i = 0; i < errors.length(); ++i ) {
620 const SMESH::AlgoStateError & error = errors[ i ];
621 const bool hasAlgo = ( strlen( error.algoName ) != 0 );
624 msg = QObject::tr( "STATE_ALGO_MISSING" );
626 switch( error.state ) {
627 CASE2MESSAGE( HYP_MISSING );
628 CASE2MESSAGE( HYP_NOTCONFORM );
629 CASE2MESSAGE( HYP_BAD_PARAMETER );
630 CASE2MESSAGE( HYP_BAD_GEOMETRY );
633 // apply args to message:
636 msg = msg.arg( error.algoName.in() );
638 msg = msg.arg( error.algoDim );
640 msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
641 // %4 - hypothesis dim == algoDim
642 msg = msg.arg( error.algoDim );
644 if ( i ) resMsg += ";\n";
649 } // end of namespace SMESH