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<QString,HypothesisData*> THypothesisDataMap;
81 THypothesisDataMap myHypothesesMap;
82 THypothesisDataMap myAlgorithmsMap;
84 typedef QMap<QString,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
85 THypCreatorMap myHypCreatorMap;
87 QList<HypothesesSet*> myListOfHypothesesSets;
89 void processHypothesisStatus(const int theHypStatus,
90 SMESH::SMESH_Hypothesis_ptr theHyp,
91 const bool theIsAddition)
93 if (theHypStatus > SMESH::HYP_OK) {
95 QString aHypName ("NULL Hypothesis");
96 if (!CORBA::is_nil(theHyp)) {
97 _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
100 aHypName = Shyp->GetName().c_str();
103 aHypName = GetHypothesisData(theHyp->GetName())->Label;
107 bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
110 aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
112 aMsg = (isFatal ? "SMESH_CANT_RM_HYP" : "SMESH_RM_HYP_WRN");
114 aMsg = QObject::tr(aMsg.toLatin1().data()).arg(aHypName) +
115 QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus).toLatin1().data());
117 if ( theHypStatus == SMESH::HYP_HIDDEN_ALGO ) // PAL18501
118 aMsg = aMsg.arg( GetHypothesisData(theHyp->GetName())->Dim[0] );
120 SUIT_MessageBox::warning(SMESHGUI::desktop(),
121 QObject::tr("SMESH_WRN_WARNING"),
127 void InitAvailableHypotheses()
129 SUIT_OverrideCursor wc;
130 if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
132 SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
135 // Find name of a resource XML file ("SMESH_Meshers.xml");
137 char* cenv = getenv("SMESH_MeshersList");
139 HypsXml.sprintf("%s", cenv);
141 QStringList HypsXmlList = HypsXml.split(":", QString::SkipEmptyParts);
142 if (HypsXmlList.count() == 0) {
143 SUIT_MessageBox::critical(SMESHGUI::desktop(),
144 QObject::tr("SMESH_WRN_WARNING"),
145 QObject::tr("MESHERS_FILE_NO_VARIABLE"));
149 // loop on files in HypsXml
150 QString aNoAccessFiles;
151 for (int i = 0; i < HypsXmlList.count(); i++) {
152 QString HypsXml = HypsXmlList[ i ];
154 // Find full path to the resource XML file
155 QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
156 if ( xmlFile.isEmpty() ) // try PLUGIN resources
157 xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
159 QFile file (xmlFile);
160 if (file.exists() && file.open(QIODevice::ReadOnly)) {
163 SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
166 QXmlInputSource source (&file);
167 QXmlSimpleReader reader;
168 reader.setContentHandler(aXmlHandler);
169 reader.setErrorHandler(aXmlHandler);
170 bool ok = reader.parse(source);
173 myHypothesesMap.unite( aXmlHandler->myHypothesesMap );
174 myAlgorithmsMap.unite( aXmlHandler->myAlgorithmsMap );
175 QList<HypothesesSet*>::iterator it, pos = myListOfHypothesesSets.begin();
176 for ( it = aXmlHandler->myListOfHypothesesSets.begin();
177 it != aXmlHandler->myListOfHypothesesSets.end();
179 myListOfHypothesesSets.insert( pos, *it );
183 SUIT_MessageBox::critical(SMESHGUI::desktop(),
184 QObject::tr("INF_PARSE_ERROR"),
185 QObject::tr(aXmlHandler->errorProtocol().toLatin1().data()));
189 if (aNoAccessFiles.isEmpty())
190 aNoAccessFiles = xmlFile;
192 aNoAccessFiles += ", " + xmlFile;
197 if (!aNoAccessFiles.isEmpty()) {
198 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
199 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
201 SUIT_MessageBox::warning(SMESHGUI::desktop(),
202 QObject::tr("SMESH_WRN_WARNING"),
210 QStringList GetAvailableHypotheses( const bool isAlgo,
213 const bool isNeedGeometry)
215 QStringList aHypList;
217 // Init list of available hypotheses, if needed
218 InitAvailableHypotheses();
219 bool checkGeometry = ( !isNeedGeometry && isAlgo );
220 // fill list of hypotheses/algorithms
221 THypothesisDataMap& pMap = isAlgo ? myAlgorithmsMap : myHypothesesMap;
222 THypothesisDataMap::iterator anIter;
223 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());
231 aHypList.append(anIter.key());
239 QStringList GetHypothesesSets()
241 QStringList aSetNameList;
243 // Init list of available hypotheses, if needed
244 InitAvailableHypotheses();
246 QList<HypothesesSet*>::iterator hypoSet;
247 for ( hypoSet = myListOfHypothesesSets.begin();
248 hypoSet != myListOfHypothesesSets.end();
250 HypothesesSet* aSet = *hypoSet;
251 if ( aSet && aSet->AlgoList.count() ) {
252 aSetNameList.append( aSet->HypoSetName );
259 HypothesesSet* GetHypothesesSet(const QString& theSetName)
261 QList<HypothesesSet*>::iterator hypoSet;
262 for ( hypoSet = myListOfHypothesesSets.begin();
263 hypoSet != myListOfHypothesesSets.end();
265 HypothesesSet* aSet = *hypoSet;
266 if ( aSet && aSet->HypoSetName == theSetName )
272 HypothesisData* GetHypothesisData (const QString& aHypType)
274 HypothesisData* aHypData = 0;
276 // Init list of available hypotheses, if needed
277 InitAvailableHypotheses();
279 if (myHypothesesMap.find(aHypType) != myHypothesesMap.end()) {
280 aHypData = myHypothesesMap[aHypType];
282 else if (myAlgorithmsMap.find(aHypType) != myAlgorithmsMap.end()) {
283 aHypData = myAlgorithmsMap[aHypType];
288 bool IsAvailableHypothesis(const HypothesisData* algoData,
289 const QString& hypType,
295 if ( algoData->NeededHypos.contains( hypType ))
297 if ( algoData->OptionalHypos.contains( hypType)) {
304 bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
305 const HypothesisData* algo2Data)
307 if ( !algo1Data || !algo2Data )
309 const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
310 if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
311 algoIn = algo2Data; algoMain = algo1Data;
313 // look for any output type of algoIn between input types of algoMain
314 QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
315 for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
316 if ( algoMain->InputTypes.contains( *inElemType ))
321 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const QString& aHypType)
323 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType.toLatin1().data());
325 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
327 // check, if creator for this hypothesis type already exists
328 if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
329 aCreator = myHypCreatorMap[aHypType];
332 // 1. Init list of available hypotheses, if needed
333 InitAvailableHypotheses();
335 // 2. Get names of plugin libraries
336 HypothesisData* aHypData = GetHypothesisData(aHypType);
339 QString aClientLibName = aHypData->ClientLibName;
340 QString aServerLibName = aHypData->ServerLibName;
342 // 3. Load Client Plugin Library
344 // load plugin library
345 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
346 LibHandle libHandle = LoadLib( aClientLibName.toLatin1().data() );
348 // report any error, if occured
351 const char* anError = "Can't load client meshers plugin library";
353 const char* anError = dlerror();
359 // get method, returning hypothesis creator
360 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
361 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
363 GetHypothesisCreator procHandle =
364 (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
366 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
367 UnLoadLib(libHandle);
370 // get hypothesis creator
371 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
372 aCreator = procHandle( aHypType );
374 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
377 // map hypothesis creator to a hypothesis name
378 myHypCreatorMap[aHypType] = aCreator;
383 catch (const SALOME::SALOME_Exception& S_ex) {
384 SalomeApp_Tools::QtCatchCorbaException(S_ex);
392 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
393 const QString& aHypName,
396 if(MYDEBUG) MESSAGE("Create " << aHypType.toLatin1().data() <<
397 " with name " << aHypName.toLatin1().data());
398 HypothesisData* aHypData = GetHypothesisData(aHypType);
399 QString aServLib = aHypData->ServerLibName;
401 SMESH::SMESH_Hypothesis_var aHypothesis;
402 aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
403 aServLib.toLatin1().data());
404 if (!aHypothesis->_is_nil()) {
405 _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
407 if (!aHypName.isEmpty())
408 SMESH::SetName(aHypSObject, aHypName);
409 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
410 return aHypothesis._retn();
413 } catch (const SALOME::SALOME_Exception & S_ex) {
414 SalomeApp_Tools::QtCatchCorbaException(S_ex);
417 return SMESH::SMESH_Hypothesis::_nil();
421 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
423 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
424 int res = SMESH::HYP_UNKNOWN_FATAL;
425 SUIT_OverrideCursor wc;
427 if (!aMesh->_is_nil()) {
428 _PTR(SObject) SM = SMESH::FindSObject(aMesh);
429 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
431 res = aMesh->AddHypothesis(aShapeObject, aHyp);
432 if (res < SMESH::HYP_UNKNOWN_FATAL) {
433 _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
435 SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
438 if (res > SMESH::HYP_OK) {
440 processHypothesisStatus(res, aHyp, true);
444 catch(const SALOME::SALOME_Exception& S_ex) {
446 SalomeApp_Tools::QtCatchCorbaException(S_ex);
447 res = SMESH::HYP_UNKNOWN_FATAL;
450 return res < SMESH::HYP_UNKNOWN_FATAL;
454 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
456 if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
457 int res = SMESH::HYP_UNKNOWN_FATAL;
458 SUIT_OverrideCursor wc;
460 if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
462 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
463 _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
464 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
465 if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
466 res = aMesh->AddHypothesis(aShapeObject, aHyp);
467 if (res < SMESH::HYP_UNKNOWN_FATAL) {
468 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
470 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
472 if (res > SMESH::HYP_OK) {
474 processHypothesisStatus(res, aHyp, true);
479 SCRUTE(aHyp->_is_nil());
480 SCRUTE(aMesh->_is_nil());
482 SCRUTE(aShapeObject->_is_nil());
485 catch(const SALOME::SALOME_Exception& S_ex) {
487 SalomeApp_Tools::QtCatchCorbaException(S_ex);
488 res = SMESH::HYP_UNKNOWN_FATAL;
492 SCRUTE(aSubMesh->_is_nil());
493 SCRUTE(aHyp->_is_nil());
495 return res < SMESH::HYP_UNKNOWN_FATAL;
498 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
500 int res = SMESH::HYP_UNKNOWN_FATAL;
501 SUIT_OverrideCursor wc;
504 _PTR(Study) aStudy = GetActiveStudyDocument();
505 _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
508 _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
509 _PTR(SObject) aRealHypo;
510 if( aHypObj->ReferencedObject( aRealHypo ) )
512 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
513 RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
517 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
518 SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
519 for( int i = 0; i < meshList.size(); i++ )
520 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
524 catch(const SALOME::SALOME_Exception& S_ex)
527 SalomeApp_Tools::QtCatchCorbaException(S_ex);
528 res = SMESH::HYP_UNKNOWN_FATAL;
530 return res < SMESH::HYP_UNKNOWN_FATAL;
533 bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
534 SMESH::SMESH_Hypothesis_ptr anHyp)
536 SALOMEDS::GenericAttribute_var anAttr;
537 SALOMEDS::AttributeIOR_var anIOR;
538 int res = SMESH::HYP_UNKNOWN_FATAL;
539 SUIT_OverrideCursor wc;
543 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
544 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
545 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
547 if (!aSubMesh->_is_nil())
548 aMesh = aSubMesh->GetFather();
550 if (!aMesh->_is_nil()) {
551 if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
552 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
553 if (res < SMESH::HYP_UNKNOWN_FATAL) {
554 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
556 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
560 else if(!aMesh->HasShapeToMesh()){
561 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
562 if (res < SMESH::HYP_UNKNOWN_FATAL) {
563 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
565 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
568 if (res > SMESH::HYP_OK) {
570 processHypothesisStatus(res, anHyp, false);
574 } catch(const SALOME::SALOME_Exception& S_ex) {
576 SalomeApp_Tools::QtCatchCorbaException(S_ex);
577 res = SMESH::HYP_UNKNOWN_FATAL;
580 return res < SMESH::HYP_UNKNOWN_FATAL;
583 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
585 SObjectList listSOmesh;
586 listSOmesh.resize(0);
588 unsigned int index = 0;
589 if (!AlgoOrHyp->_is_nil()) {
590 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
593 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
595 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
596 for (unsigned int i = 0; i < listSO.size(); i++) {
597 _PTR(SObject) SO = listSO[i];
599 _PTR(SObject) aFather = SO->GetFather();
601 _PTR(SObject) SOfatherFather = aFather->GetFather();
602 if (SOfatherFather) {
603 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
605 listSOmesh.resize(index);
606 listSOmesh[index - 1] = SOfatherFather;
613 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
617 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
618 QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
620 QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
621 for ( int i = 0; i < errors.length(); ++i ) {
622 const SMESH::AlgoStateError & error = errors[ i ];
623 const bool hasAlgo = ( strlen( error.algoName ) != 0 );
626 msg = QObject::tr( "STATE_ALGO_MISSING" );
628 switch( error.state ) {
629 CASE2MESSAGE( HYP_MISSING );
630 CASE2MESSAGE( HYP_NOTCONFORM );
631 CASE2MESSAGE( HYP_BAD_PARAMETER );
632 CASE2MESSAGE( HYP_BAD_GEOMETRY );
635 // apply args to message:
638 msg = msg.arg( error.algoName.in() );
640 msg = msg.arg( error.algoDim );
642 msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
643 // %4 - hypothesis dim == algoDim
644 msg = msg.arg( error.algoDim );
646 if ( i ) resMsg += ";\n";
651 } // end of namespace SMESH