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;
86 //typedef QMap<QString,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
87 //THypCreatorMap myHypCreatorMap;
89 QList<HypothesesSet*> myListOfHypothesesSets;
91 void processHypothesisStatus(const int theHypStatus,
92 SMESH::SMESH_Hypothesis_ptr theHyp,
93 const bool theIsAddition)
95 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) {
145 SUIT_MessageBox::critical(SMESHGUI::desktop(),
146 QObject::tr("SMESH_WRN_WARNING"),
147 QObject::tr("MESHERS_FILE_NO_VARIABLE"));
151 // loop on files in HypsXml
152 QString aNoAccessFiles;
153 for (int i = 0; i < HypsXmlList.count(); i++) {
154 QString HypsXml = HypsXmlList[ i ];
156 // Find full path to the resource XML file
157 QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
158 if ( xmlFile.isEmpty() ) // try PLUGIN resources
159 xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
161 QFile file (xmlFile);
162 if (file.exists() && file.open(QIODevice::ReadOnly)) {
165 SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
168 QXmlInputSource source (&file);
169 QXmlSimpleReader reader;
170 reader.setContentHandler(aXmlHandler);
171 reader.setErrorHandler(aXmlHandler);
172 bool ok = reader.parse(source);
175 myHypothesesMap.unite( aXmlHandler->myHypothesesMap );
176 myAlgorithmsMap.unite( aXmlHandler->myAlgorithmsMap );
177 QList<HypothesesSet*>::iterator it, pos = myListOfHypothesesSets.begin();
178 for ( it = aXmlHandler->myListOfHypothesesSets.begin();
179 it != aXmlHandler->myListOfHypothesesSets.end();
181 myListOfHypothesesSets.insert( pos, *it );
185 SUIT_MessageBox::critical(SMESHGUI::desktop(),
186 QObject::tr("INF_PARSE_ERROR"),
187 QObject::tr(aXmlHandler->errorProtocol().toLatin1().data()));
191 if (aNoAccessFiles.isEmpty())
192 aNoAccessFiles = xmlFile;
194 aNoAccessFiles += ", " + xmlFile;
199 if (!aNoAccessFiles.isEmpty()) {
200 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
201 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
203 SUIT_MessageBox::warning(SMESHGUI::desktop(),
204 QObject::tr("SMESH_WRN_WARNING"),
212 QStringList GetAvailableHypotheses( const bool isAlgo,
215 const bool isNeedGeometry)
217 QStringList aHypList;
219 // Init list of available hypotheses, if needed
220 InitAvailableHypotheses();
221 bool checkGeometry = ( !isNeedGeometry && isAlgo );
222 // fill list of hypotheses/algorithms
223 THypothesisDataMap& pMap = isAlgo ? myAlgorithmsMap : myHypothesesMap;
224 THypothesisDataMap::iterator anIter;
225 for ( anIter = pMap.begin(); anIter != pMap.end(); anIter++ ) {
226 HypothesisData* aData = anIter.value();
227 if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux) {
229 if (aData->IsNeedGeometry == isNeedGeometry)
230 aHypList.append(anIter.key());
233 aHypList.append(anIter.key());
241 QStringList GetHypothesesSets()
243 QStringList aSetNameList;
245 // Init list of available hypotheses, if needed
246 InitAvailableHypotheses();
248 QList<HypothesesSet*>::iterator hypoSet;
249 for ( hypoSet = myListOfHypothesesSets.begin();
250 hypoSet != myListOfHypothesesSets.end();
252 HypothesesSet* aSet = *hypoSet;
253 if ( aSet && aSet->AlgoList.count() ) {
254 aSetNameList.append( aSet->HypoSetName );
261 HypothesesSet* GetHypothesesSet(const QString& theSetName)
263 QList<HypothesesSet*>::iterator hypoSet;
264 for ( hypoSet = myListOfHypothesesSets.begin();
265 hypoSet != myListOfHypothesesSets.end();
267 HypothesesSet* aSet = *hypoSet;
268 if ( aSet && aSet->HypoSetName == theSetName )
274 HypothesisData* GetHypothesisData (const QString& aHypType)
276 HypothesisData* aHypData = 0;
278 // Init list of available hypotheses, if needed
279 InitAvailableHypotheses();
281 if (myHypothesesMap.find(aHypType) != myHypothesesMap.end()) {
282 aHypData = myHypothesesMap[aHypType];
284 else if (myAlgorithmsMap.find(aHypType) != myAlgorithmsMap.end()) {
285 aHypData = myAlgorithmsMap[aHypType];
290 bool IsAvailableHypothesis(const HypothesisData* algoData,
291 const QString& hypType,
297 if ( algoData->NeededHypos.contains( hypType ))
299 if ( algoData->OptionalHypos.contains( hypType)) {
306 bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
307 const HypothesisData* algo2Data)
309 if ( !algo1Data || !algo2Data )
311 const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
312 if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
313 algoIn = algo2Data; algoMain = algo1Data;
315 // look for any output type of algoIn between input types of algoMain
316 QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
317 for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
318 if ( algoMain->InputTypes.contains( *inElemType ))
323 HypothesisCreatorPtr GetHypothesisCreator(const QString& aHypType)
325 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType.toLatin1().data());
327 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
329 // check, if creator for this hypothesis type already exists
331 //if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
332 // aCreator = myHypCreatorMap[aHypType];
336 // 1. Init list of available hypotheses, if needed
337 InitAvailableHypotheses();
339 // 2. Get names of plugin libraries
340 HypothesisData* aHypData = GetHypothesisData(aHypType);
342 return HypothesisCreatorPtr(aCreator);
343 QString aClientLibName = aHypData->ClientLibName;
344 QString aServerLibName = aHypData->ServerLibName;
346 // 3. Load Client Plugin Library
348 // load plugin library
349 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
350 LibHandle libHandle = LoadLib( aClientLibName.toLatin1().data() );
352 // report any error, if occured
355 const char* anError = "Can't load client meshers plugin library";
357 const char* anError = dlerror();
363 // get method, returning hypothesis creator
364 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
365 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
367 GetHypothesisCreator procHandle =
368 (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
370 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
371 UnLoadLib(libHandle);
374 // get hypothesis creator
375 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
376 aCreator = procHandle( aHypType );
378 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
381 // map hypothesis creator to a hypothesis name
383 //myHypCreatorMap[aHypType] = aCreator;
388 catch (const SALOME::SALOME_Exception& S_ex) {
389 SalomeApp_Tools::QtCatchCorbaException(S_ex);
393 return HypothesisCreatorPtr(aCreator);
397 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
398 const QString& aHypName,
401 if(MYDEBUG) MESSAGE("Create " << aHypType.toLatin1().data() <<
402 " with name " << aHypName.toLatin1().data());
403 HypothesisData* aHypData = GetHypothesisData(aHypType);
404 QString aServLib = aHypData->ServerLibName;
406 SMESH::SMESH_Hypothesis_var aHypothesis;
407 aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
408 aServLib.toLatin1().data());
409 if (!aHypothesis->_is_nil()) {
410 _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
412 if (!aHypName.isEmpty())
413 SMESH::SetName(aHypSObject, aHypName);
414 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
415 return aHypothesis._retn();
418 } catch (const SALOME::SALOME_Exception & S_ex) {
419 SalomeApp_Tools::QtCatchCorbaException(S_ex);
422 return SMESH::SMESH_Hypothesis::_nil();
426 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
428 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
429 int res = SMESH::HYP_UNKNOWN_FATAL;
430 SUIT_OverrideCursor wc;
432 if (!aMesh->_is_nil()) {
433 _PTR(SObject) SM = SMESH::FindSObject(aMesh);
434 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
436 res = aMesh->AddHypothesis(aShapeObject, aHyp);
437 if (res < SMESH::HYP_UNKNOWN_FATAL) {
438 _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
440 SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
443 if (res > SMESH::HYP_OK) {
445 processHypothesisStatus(res, aHyp, true);
449 catch(const SALOME::SALOME_Exception& S_ex) {
451 SalomeApp_Tools::QtCatchCorbaException(S_ex);
452 res = SMESH::HYP_UNKNOWN_FATAL;
455 return res < SMESH::HYP_UNKNOWN_FATAL;
459 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
461 if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
462 int res = SMESH::HYP_UNKNOWN_FATAL;
463 SUIT_OverrideCursor wc;
465 if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
467 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
468 _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
469 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
470 if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
471 res = aMesh->AddHypothesis(aShapeObject, aHyp);
472 if (res < SMESH::HYP_UNKNOWN_FATAL) {
473 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
475 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
477 if (res > SMESH::HYP_OK) {
479 processHypothesisStatus(res, aHyp, true);
484 SCRUTE(aHyp->_is_nil());
485 SCRUTE(aMesh->_is_nil());
487 SCRUTE(aShapeObject->_is_nil());
490 catch(const SALOME::SALOME_Exception& S_ex) {
492 SalomeApp_Tools::QtCatchCorbaException(S_ex);
493 res = SMESH::HYP_UNKNOWN_FATAL;
497 SCRUTE(aSubMesh->_is_nil());
498 SCRUTE(aHyp->_is_nil());
500 return res < SMESH::HYP_UNKNOWN_FATAL;
503 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
505 int res = SMESH::HYP_UNKNOWN_FATAL;
506 SUIT_OverrideCursor wc;
509 _PTR(Study) aStudy = GetActiveStudyDocument();
510 _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
513 _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
514 _PTR(SObject) aRealHypo;
515 if( aHypObj->ReferencedObject( aRealHypo ) )
517 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
518 RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
522 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
523 SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
524 for( int i = 0; i < meshList.size(); i++ )
525 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
529 catch(const SALOME::SALOME_Exception& S_ex)
532 SalomeApp_Tools::QtCatchCorbaException(S_ex);
533 res = SMESH::HYP_UNKNOWN_FATAL;
535 return res < SMESH::HYP_UNKNOWN_FATAL;
538 bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
539 SMESH::SMESH_Hypothesis_ptr anHyp)
541 SALOMEDS::GenericAttribute_var anAttr;
542 SALOMEDS::AttributeIOR_var anIOR;
543 int res = SMESH::HYP_UNKNOWN_FATAL;
544 SUIT_OverrideCursor wc;
548 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
549 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
550 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
552 if (!aSubMesh->_is_nil())
553 aMesh = aSubMesh->GetFather();
555 if (!aMesh->_is_nil()) {
556 if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
557 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
558 if (res < SMESH::HYP_UNKNOWN_FATAL) {
559 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
561 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
565 else if(!aMesh->HasShapeToMesh()){
566 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
567 if (res < SMESH::HYP_UNKNOWN_FATAL) {
568 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
570 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
573 if (res > SMESH::HYP_OK) {
575 processHypothesisStatus(res, anHyp, false);
579 } catch(const SALOME::SALOME_Exception& S_ex) {
581 SalomeApp_Tools::QtCatchCorbaException(S_ex);
582 res = SMESH::HYP_UNKNOWN_FATAL;
585 return res < SMESH::HYP_UNKNOWN_FATAL;
588 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
590 SObjectList listSOmesh;
591 listSOmesh.resize(0);
593 unsigned int index = 0;
594 if (!AlgoOrHyp->_is_nil()) {
595 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
598 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
600 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
601 for (unsigned int i = 0; i < listSO.size(); i++) {
602 _PTR(SObject) SO = listSO[i];
604 _PTR(SObject) aFather = SO->GetFather();
606 _PTR(SObject) SOfatherFather = aFather->GetFather();
607 if (SOfatherFather) {
608 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
610 listSOmesh.resize(index);
611 listSOmesh[index - 1] = SOfatherFather;
618 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
622 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
623 QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
625 QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
626 for ( int i = 0; i < errors.length(); ++i ) {
627 const SMESH::AlgoStateError & error = errors[ i ];
628 const bool hasAlgo = ( strlen( error.algoName ) != 0 );
631 msg = QObject::tr( "STATE_ALGO_MISSING" );
633 switch( error.state ) {
634 CASE2MESSAGE( HYP_MISSING );
635 CASE2MESSAGE( HYP_NOTCONFORM );
636 CASE2MESSAGE( HYP_BAD_PARAMETER );
637 CASE2MESSAGE( HYP_BAD_GEOMETRY );
640 // apply args to message:
643 msg = msg.arg( error.algoName.in() );
645 msg = msg.arg( error.algoDim );
647 msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
648 // %4 - hypothesis dim == algoDim
649 msg = msg.arg( error.algoDim );
651 if ( i ) resMsg += ";\n";
656 } // end of namespace SMESH