1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SMESH SMESHGUI : GUI for SMESH component
23 // File : SMESHGUI_HypothesesUtils.cxx
24 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
27 #include "SMESHGUI_HypothesesUtils.h"
30 #include "SMESHGUI_Hypotheses.h"
31 #include "SMESHGUI_XmlHandler.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_GEOMGenUtils.h"
35 // SALOME GUI includes
36 #include <SUIT_Desktop.h>
37 #include <SUIT_MessageBox.h>
38 #include <SUIT_OverrideCursor.h>
39 #include <SUIT_ResourceMgr.h>
41 #include <SalomeApp_Study.h>
42 #include <SalomeApp_Tools.h>
44 // SALOME KERNEL includes
45 #include <utilities.h>
62 #define LibHandle HMODULE
63 #define LoadLib( name ) LoadLibrary( name )
64 #define GetProc GetProcAddress
65 #define UnLoadLib( handle ) FreeLibrary( handle );
67 #define LibHandle void*
68 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
70 #define UnLoadLib( handle ) dlclose( handle );
74 static int MYDEBUG = 0;
76 static int MYDEBUG = 0;
81 typedef QMap<QString,HypothesisData*> THypothesisDataMap;
82 THypothesisDataMap myHypothesesMap;
83 THypothesisDataMap myAlgorithmsMap;
85 typedef QMap<QString,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
86 THypCreatorMap myHypCreatorMap;
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) {
96 QString aHypName ("NULL Hypothesis");
97 if (!CORBA::is_nil(theHyp)) {
98 _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
101 aHypName = Shyp->GetName().c_str();
104 aHypName = GetHypothesisData(theHyp->GetName())->Label;
108 bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
111 aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
113 aMsg = (isFatal ? "SMESH_CANT_RM_HYP" : "SMESH_RM_HYP_WRN");
115 aMsg = QObject::tr(aMsg.toLatin1().data()).arg(aHypName) +
116 QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus).toLatin1().data());
118 if ( theHypStatus == SMESH::HYP_HIDDEN_ALGO ) // PAL18501
119 aMsg = aMsg.arg( GetHypothesisData(theHyp->GetName())->Dim[0] );
121 SUIT_MessageBox::warning(SMESHGUI::desktop(),
122 QObject::tr("SMESH_WRN_WARNING"),
128 void InitAvailableHypotheses()
130 SUIT_OverrideCursor wc;
131 if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
133 SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
136 // Find name of a resource XML file ("SMESH_Meshers.xml");
138 char* cenv = getenv("SMESH_MeshersList");
140 HypsXml.sprintf("%s", cenv);
142 QStringList HypsXmlList = HypsXml.split(":", QString::SkipEmptyParts);
143 if (HypsXmlList.count() == 0) {
144 SUIT_MessageBox::critical(SMESHGUI::desktop(),
145 QObject::tr("SMESH_WRN_WARNING"),
146 QObject::tr("MESHERS_FILE_NO_VARIABLE"));
150 // loop on files in HypsXml
151 QString aNoAccessFiles;
152 for (int i = 0; i < HypsXmlList.count(); i++) {
153 QString HypsXml = HypsXmlList[ i ];
155 // Find full path to the resource XML file
156 QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
157 if ( xmlFile.isEmpty() ) // try PLUGIN resources
158 xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
160 QFile file (xmlFile);
161 if (file.exists() && file.open(QIODevice::ReadOnly)) {
164 SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
167 QXmlInputSource source (&file);
168 QXmlSimpleReader reader;
169 reader.setContentHandler(aXmlHandler);
170 reader.setErrorHandler(aXmlHandler);
171 bool ok = reader.parse(source);
174 myHypothesesMap.unite( aXmlHandler->myHypothesesMap );
175 myAlgorithmsMap.unite( aXmlHandler->myAlgorithmsMap );
176 QList<HypothesesSet*>::iterator it, pos = myListOfHypothesesSets.begin();
177 for ( it = aXmlHandler->myListOfHypothesesSets.begin();
178 it != aXmlHandler->myListOfHypothesesSets.end();
180 myListOfHypothesesSets.insert( pos, *it );
184 SUIT_MessageBox::critical(SMESHGUI::desktop(),
185 QObject::tr("INF_PARSE_ERROR"),
186 QObject::tr(aXmlHandler->errorProtocol().toLatin1().data()));
190 if (aNoAccessFiles.isEmpty())
191 aNoAccessFiles = xmlFile;
193 aNoAccessFiles += ", " + xmlFile;
198 if (!aNoAccessFiles.isEmpty()) {
199 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
200 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
202 SUIT_MessageBox::warning(SMESHGUI::desktop(),
203 QObject::tr("SMESH_WRN_WARNING"),
211 QStringList GetAvailableHypotheses( const bool isAlgo,
214 const bool isNeedGeometry)
216 QStringList aHypList;
218 // Init list of available hypotheses, if needed
219 InitAvailableHypotheses();
220 bool checkGeometry = ( !isNeedGeometry && isAlgo );
221 // fill list of hypotheses/algorithms
222 THypothesisDataMap& pMap = isAlgo ? myAlgorithmsMap : myHypothesesMap;
223 THypothesisDataMap::iterator anIter;
224 for ( anIter = pMap.begin(); anIter != pMap.end(); anIter++ ) {
225 HypothesisData* aData = anIter.value();
226 if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux) {
228 if (aData->IsNeedGeometry == isNeedGeometry)
229 aHypList.append(anIter.key());
232 aHypList.append(anIter.key());
240 QStringList GetHypothesesSets()
242 QStringList aSetNameList;
244 // Init list of available hypotheses, if needed
245 InitAvailableHypotheses();
247 QList<HypothesesSet*>::iterator hypoSet;
248 for ( hypoSet = myListOfHypothesesSets.begin();
249 hypoSet != myListOfHypothesesSets.end();
251 HypothesesSet* aSet = *hypoSet;
252 if ( aSet && aSet->AlgoList.count() ) {
253 aSetNameList.append( aSet->HypoSetName );
260 HypothesesSet* GetHypothesesSet(const QString& theSetName)
262 QList<HypothesesSet*>::iterator hypoSet;
263 for ( hypoSet = myListOfHypothesesSets.begin();
264 hypoSet != myListOfHypothesesSets.end();
266 HypothesesSet* aSet = *hypoSet;
267 if ( aSet && aSet->HypoSetName == theSetName )
273 HypothesisData* GetHypothesisData (const QString& aHypType)
275 HypothesisData* aHypData = 0;
277 // Init list of available hypotheses, if needed
278 InitAvailableHypotheses();
280 if (myHypothesesMap.find(aHypType) != myHypothesesMap.end()) {
281 aHypData = myHypothesesMap[aHypType];
283 else if (myAlgorithmsMap.find(aHypType) != myAlgorithmsMap.end()) {
284 aHypData = myAlgorithmsMap[aHypType];
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) != myHypCreatorMap.end()) {
330 aCreator = myHypCreatorMap[aHypType];
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
352 const char* anError = "Can't load client meshers plugin library";
354 const char* anError = dlerror();
360 // get method, returning hypothesis creator
361 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
362 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
364 GetHypothesisCreator procHandle =
365 (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
367 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
368 UnLoadLib(libHandle);
371 // get hypothesis creator
372 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
373 aCreator = procHandle( aHypType );
375 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
378 // map hypothesis creator to a hypothesis name
379 myHypCreatorMap[aHypType] = aCreator;
384 catch (const SALOME::SALOME_Exception& S_ex) {
385 SalomeApp_Tools::QtCatchCorbaException(S_ex);
393 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
394 const QString& aHypName,
397 if(MYDEBUG) MESSAGE("Create " << aHypType.toLatin1().data() <<
398 " with name " << aHypName.toLatin1().data());
399 HypothesisData* aHypData = GetHypothesisData(aHypType);
400 QString aServLib = aHypData->ServerLibName;
402 SMESH::SMESH_Hypothesis_var aHypothesis;
403 aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
404 aServLib.toLatin1().data());
405 if (!aHypothesis->_is_nil()) {
406 _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
408 if (!aHypName.isEmpty())
409 SMESH::SetName(aHypSObject, aHypName);
410 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
411 return aHypothesis._retn();
414 } catch (const SALOME::SALOME_Exception & S_ex) {
415 SalomeApp_Tools::QtCatchCorbaException(S_ex);
418 return SMESH::SMESH_Hypothesis::_nil();
422 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
424 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
425 int res = SMESH::HYP_UNKNOWN_FATAL;
426 SUIT_OverrideCursor wc;
428 if (!aMesh->_is_nil()) {
429 _PTR(SObject) SM = SMESH::FindSObject(aMesh);
430 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
432 res = aMesh->AddHypothesis(aShapeObject, aHyp);
433 if (res < SMESH::HYP_UNKNOWN_FATAL) {
434 _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
436 SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
439 if (res > SMESH::HYP_OK) {
441 processHypothesisStatus(res, aHyp, true);
445 catch(const SALOME::SALOME_Exception& S_ex) {
447 SalomeApp_Tools::QtCatchCorbaException(S_ex);
448 res = SMESH::HYP_UNKNOWN_FATAL;
451 return res < SMESH::HYP_UNKNOWN_FATAL;
455 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
457 if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
458 int res = SMESH::HYP_UNKNOWN_FATAL;
459 SUIT_OverrideCursor wc;
461 if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
463 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
464 _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
465 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
466 if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
467 res = aMesh->AddHypothesis(aShapeObject, aHyp);
468 if (res < SMESH::HYP_UNKNOWN_FATAL) {
469 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
471 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
473 if (res > SMESH::HYP_OK) {
475 processHypothesisStatus(res, aHyp, true);
480 SCRUTE(aHyp->_is_nil());
481 SCRUTE(aMesh->_is_nil());
483 SCRUTE(aShapeObject->_is_nil());
486 catch(const SALOME::SALOME_Exception& S_ex) {
488 SalomeApp_Tools::QtCatchCorbaException(S_ex);
489 res = SMESH::HYP_UNKNOWN_FATAL;
493 SCRUTE(aSubMesh->_is_nil());
494 SCRUTE(aHyp->_is_nil());
496 return res < SMESH::HYP_UNKNOWN_FATAL;
499 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
501 int res = SMESH::HYP_UNKNOWN_FATAL;
502 SUIT_OverrideCursor wc;
505 _PTR(Study) aStudy = GetActiveStudyDocument();
506 _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
509 _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
510 _PTR(SObject) aRealHypo;
511 if( aHypObj->ReferencedObject( aRealHypo ) )
513 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
514 RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
518 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
519 SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
520 for( int i = 0; i < meshList.size(); i++ )
521 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
525 catch(const SALOME::SALOME_Exception& S_ex)
528 SalomeApp_Tools::QtCatchCorbaException(S_ex);
529 res = SMESH::HYP_UNKNOWN_FATAL;
531 return res < SMESH::HYP_UNKNOWN_FATAL;
534 bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
535 SMESH::SMESH_Hypothesis_ptr anHyp)
537 SALOMEDS::GenericAttribute_var anAttr;
538 SALOMEDS::AttributeIOR_var anIOR;
539 int res = SMESH::HYP_UNKNOWN_FATAL;
540 SUIT_OverrideCursor wc;
544 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
545 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
546 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
548 if (!aSubMesh->_is_nil())
549 aMesh = aSubMesh->GetFather();
551 if (!aMesh->_is_nil()) {
552 if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
553 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
554 if (res < SMESH::HYP_UNKNOWN_FATAL) {
555 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
557 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
561 else if(!aMesh->HasShapeToMesh()){
562 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
563 if (res < SMESH::HYP_UNKNOWN_FATAL) {
564 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
566 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
569 if (res > SMESH::HYP_OK) {
571 processHypothesisStatus(res, anHyp, false);
575 } catch(const SALOME::SALOME_Exception& S_ex) {
577 SalomeApp_Tools::QtCatchCorbaException(S_ex);
578 res = SMESH::HYP_UNKNOWN_FATAL;
581 return res < SMESH::HYP_UNKNOWN_FATAL;
584 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
586 SObjectList listSOmesh;
587 listSOmesh.resize(0);
589 unsigned int index = 0;
590 if (!AlgoOrHyp->_is_nil()) {
591 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
594 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
596 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
597 for (unsigned int i = 0; i < listSO.size(); i++) {
598 _PTR(SObject) SO = listSO[i];
600 _PTR(SObject) aFather = SO->GetFather();
602 _PTR(SObject) SOfatherFather = aFather->GetFather();
603 if (SOfatherFather) {
604 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
606 listSOmesh.resize(index);
607 listSOmesh[index - 1] = SOfatherFather;
614 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
618 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
619 QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
621 QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
622 for ( int i = 0; i < errors.length(); ++i ) {
623 const SMESH::AlgoStateError & error = errors[ i ];
624 const bool hasAlgo = ( strlen( error.algoName ) != 0 );
627 msg = QObject::tr( "STATE_ALGO_MISSING" );
629 switch( error.state ) {
630 CASE2MESSAGE( HYP_MISSING );
631 CASE2MESSAGE( HYP_NOTCONFORM );
632 CASE2MESSAGE( HYP_BAD_PARAMETER );
633 CASE2MESSAGE( HYP_BAD_GEOMETRY );
636 // apply args to message:
639 msg = msg.arg( error.algoName.in() );
641 msg = msg.arg( error.algoDim );
643 msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
644 // %4 - hypothesis dim == algoDim
645 msg = msg.arg( error.algoDim );
647 if ( i ) resMsg += ";\n";
652 } // end of namespace SMESH