1 // Copyright (C) 2007-2010 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
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File : SMESHGUI_HypothesesUtils.cxx
25 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
28 #include "SMESHGUI_HypothesesUtils.h"
31 #include "SMESHGUI_Hypotheses.h"
32 #include "SMESHGUI_XmlHandler.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_GEOMGenUtils.h"
36 // SALOME GUI includes
37 #include <SUIT_Desktop.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_OverrideCursor.h>
40 #include <SUIT_ResourceMgr.h>
42 #include <SalomeApp_Study.h>
43 #include <SalomeApp_Tools.h>
45 // SALOME KERNEL includes
46 #include <utilities.h>
64 #define LibHandle HMODULE
65 #define LoadLib( name ) LoadLibrary( name )
66 #define GetProc GetProcAddress
67 #define UnLoadLib( handle ) FreeLibrary( handle );
69 #define LibHandle void*
70 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
72 #define UnLoadLib( handle ) dlclose( handle );
76 static int MYDEBUG = 0;
78 static int MYDEBUG = 0;
83 typedef QMap<QString,HypothesisData*> THypothesisDataMap;
84 THypothesisDataMap myHypothesesMap;
85 THypothesisDataMap myAlgorithmsMap;
88 //typedef QMap<QString,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
89 //THypCreatorMap myHypCreatorMap;
91 QList<HypothesesSet*> myListOfHypothesesSets;
93 void processHypothesisStatus(const int theHypStatus,
94 SMESH::SMESH_Hypothesis_ptr theHyp,
95 const bool theIsAddition)
97 if (theHypStatus > SMESH::HYP_OK) {
99 QString aHypName ("NULL Hypothesis");
100 if (!CORBA::is_nil(theHyp)) {
101 _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
104 aHypName = Shyp->GetName().c_str();
107 aHypName = GetHypothesisData(theHyp->GetName())->Label;
111 bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
114 aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
116 aMsg = (isFatal ? "SMESH_CANT_RM_HYP" : "SMESH_RM_HYP_WRN");
118 aMsg = QObject::tr(aMsg.toLatin1().data()).arg(aHypName) +
119 QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus).toLatin1().data());
121 if ( theHypStatus == SMESH::HYP_HIDDEN_ALGO ) // PAL18501
122 aMsg = aMsg.arg( GetHypothesisData(theHyp->GetName())->Dim[0] );
124 SUIT_MessageBox::warning(SMESHGUI::desktop(),
125 QObject::tr("SMESH_WRN_WARNING"),
130 //================================================================================
132 * \brief Prepends dimension and appends '[custom]' to the name of hypothesis set
134 //================================================================================
136 static QString mangledHypoSetName(HypothesesSet* hypSet)
138 QString name = hypSet->name();
141 int dim = hypSet->maxDim();
143 name = QString("%1D: %2").arg(dim).arg(name);
146 if ( hypSet->getIsCustom() )
147 name = QString("%1 [custom]").arg(name);
152 //================================================================================
154 * \brief Removes dimension and '[custom]' from the name of hypothesis set
156 //================================================================================
158 static QString demangledHypoSetName(QString name)
160 name.remove(QRegExp("[0-3]D: "));
161 name.remove(" [custom]");
165 void InitAvailableHypotheses()
167 SUIT_OverrideCursor wc;
168 if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
170 SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
173 // Find name of a resource XML file ("SMESH_Meshers.xml");
175 char* cenv = getenv("SMESH_MeshersList");
177 HypsXml.sprintf("%s", cenv);
179 QStringList HypsXmlList = HypsXml.split(":", QString::SkipEmptyParts);
180 if (HypsXmlList.count() == 0) {
181 SUIT_MessageBox::critical(SMESHGUI::desktop(),
182 QObject::tr("SMESH_WRN_WARNING"),
183 QObject::tr("MESHERS_FILE_NO_VARIABLE"));
186 // get full names of xml files from HypsXmlList
187 QStringList xmlFiles;
188 xmlFiles.append( QDir::home().filePath("CustomMeshers.xml")); // may be inexistent
189 for (int i = 0; i < HypsXmlList.count(); i++) {
190 QString HypsXml = HypsXmlList[ i ];
192 // Find full path to the resource XML file
193 QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
194 if ( xmlFile.isEmpty() ) // try PLUGIN resources
195 xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
196 if ( !xmlFile.isEmpty() )
197 xmlFiles.append( xmlFile );
201 QString aNoAccessFiles;
202 for (int i = 0; i < xmlFiles.count(); i++)
204 QString xmlFile = xmlFiles[ i ];
205 QFile file (xmlFile);
206 if (file.exists() && file.open(QIODevice::ReadOnly))
210 SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
213 QXmlInputSource source (&file);
214 QXmlSimpleReader reader;
215 reader.setContentHandler(aXmlHandler);
216 reader.setErrorHandler(aXmlHandler);
217 bool ok = reader.parse(source);
220 myHypothesesMap.unite( aXmlHandler->myHypothesesMap );
221 myAlgorithmsMap.unite( aXmlHandler->myAlgorithmsMap );
222 QList<HypothesesSet*>::iterator it, pos = myListOfHypothesesSets.begin();
223 for ( it = aXmlHandler->myListOfHypothesesSets.begin();
224 it != aXmlHandler->myListOfHypothesesSets.end();
227 (*it)->setIsCustom( i == 0 );
228 myListOfHypothesesSets.insert( pos, *it );
232 SUIT_MessageBox::critical(SMESHGUI::desktop(),
233 QObject::tr("INF_PARSE_ERROR"),
234 QObject::tr(aXmlHandler->errorProtocol().toLatin1().data()));
238 else if ( i > 0 ) { // 1st is ~/CustomMeshers.xml
239 if (aNoAccessFiles.isEmpty())
240 aNoAccessFiles = xmlFile;
242 aNoAccessFiles += ", " + xmlFile;
244 } // end loop on xmlFiles
247 if (!aNoAccessFiles.isEmpty()) {
248 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
249 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
251 SUIT_MessageBox::warning(SMESHGUI::desktop(),
252 QObject::tr("SMESH_WRN_WARNING"),
260 QStringList GetAvailableHypotheses( const bool isAlgo,
263 const bool isNeedGeometry)
265 QStringList aHypList;
267 // Init list of available hypotheses, if needed
268 InitAvailableHypotheses();
269 bool checkGeometry = ( !isNeedGeometry && isAlgo );
270 // fill list of hypotheses/algorithms
271 THypothesisDataMap& pMap = isAlgo ? myAlgorithmsMap : myHypothesesMap;
272 THypothesisDataMap::iterator anIter;
273 for ( anIter = pMap.begin(); anIter != pMap.end(); anIter++ ) {
274 HypothesisData* aData = anIter.value();
275 if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux) {
277 if (aData->IsNeedGeometry == isNeedGeometry)
278 aHypList.append(anIter.key());
281 aHypList.append(anIter.key());
289 QStringList GetHypothesesSets(int maxDim)
291 QStringList aSetNameList;
293 // Init list of available hypotheses, if needed
294 InitAvailableHypotheses();
296 QList<HypothesesSet*>::iterator hypoSet;
297 for ( hypoSet = myListOfHypothesesSets.begin();
298 hypoSet != myListOfHypothesesSets.end();
300 HypothesesSet* aSet = *hypoSet;
302 ( aSet->count( true ) || aSet->count( false )) &&
303 aSet->maxDim() <= maxDim)
305 aSetNameList.append( mangledHypoSetName( aSet ));
310 // reverse order of aSetNameList
311 QStringList reversedNames;
312 for ( int i = 0; i < aSetNameList.count(); ++i )
313 reversedNames.prepend( aSetNameList[i] );
315 return reversedNames;
318 HypothesesSet* GetHypothesesSet(const QString& theSetName)
320 QString name = demangledHypoSetName( theSetName );
321 QList<HypothesesSet*>::iterator hypoSet;
322 for ( hypoSet = myListOfHypothesesSets.begin();
323 hypoSet != myListOfHypothesesSets.end();
325 HypothesesSet* aSet = *hypoSet;
326 if ( aSet && aSet->name() == name )
332 HypothesisData* GetHypothesisData (const QString& aHypType)
334 HypothesisData* aHypData = 0;
336 // Init list of available hypotheses, if needed
337 InitAvailableHypotheses();
339 if (myHypothesesMap.find(aHypType) != myHypothesesMap.end()) {
340 aHypData = myHypothesesMap[aHypType];
342 else if (myAlgorithmsMap.find(aHypType) != myAlgorithmsMap.end()) {
343 aHypData = myAlgorithmsMap[aHypType];
348 bool IsAvailableHypothesis(const HypothesisData* algoData,
349 const QString& hypType,
355 if ( algoData->NeededHypos.contains( hypType ))
357 if ( algoData->OptionalHypos.contains( hypType)) {
364 bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
365 const HypothesisData* algo2Data)
367 if ( !algo1Data || !algo2Data )
369 const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
370 if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
371 algoIn = algo2Data; algoMain = algo1Data;
373 // look for any output type of algoIn between input types of algoMain
374 QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
375 for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
376 if ( algoMain->InputTypes.contains( *inElemType ))
381 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const QString& aHypType)
383 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType.toLatin1().data());
385 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
387 // check, if creator for this hypothesis type already exists
389 //if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
390 // aCreator = myHypCreatorMap[aHypType];
394 // 1. Init list of available hypotheses, if needed
395 InitAvailableHypotheses();
397 // 2. Get names of plugin libraries
398 HypothesisData* aHypData = GetHypothesisData(aHypType);
402 QString aClientLibName = aHypData->ClientLibName;
403 QString aServerLibName = aHypData->ServerLibName;
405 // 3. Load Client Plugin Library
407 // load plugin library
408 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
409 LibHandle libHandle = LoadLib( aClientLibName.toLatin1().data() );
411 // report any error, if occured
414 const char* anError = "Can't load client meshers plugin library";
416 const char* anError = dlerror();
422 // get method, returning hypothesis creator
423 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
424 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
426 GetHypothesisCreator procHandle =
427 (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
429 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
430 UnLoadLib(libHandle);
433 // get hypothesis creator
434 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
435 aCreator = procHandle( aHypType );
437 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
440 // map hypothesis creator to a hypothesis name
442 //myHypCreatorMap[aHypType] = aCreator;
447 catch (const SALOME::SALOME_Exception& S_ex) {
448 SalomeApp_Tools::QtCatchCorbaException(S_ex);
456 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
457 const QString& aHypName,
460 if(MYDEBUG) MESSAGE("Create " << aHypType.toLatin1().data() <<
461 " with name " << aHypName.toLatin1().data());
462 HypothesisData* aHypData = GetHypothesisData(aHypType);
463 QString aServLib = aHypData->ServerLibName;
465 SMESH::SMESH_Hypothesis_var aHypothesis;
466 aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
467 aServLib.toLatin1().data());
468 if (!aHypothesis->_is_nil()) {
469 _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
471 if (!aHypName.isEmpty())
472 SMESH::SetName(aHypSObject, aHypName);
473 SMESHGUI::Modified();
474 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
475 return aHypothesis._retn();
478 } catch (const SALOME::SALOME_Exception & S_ex) {
479 SalomeApp_Tools::QtCatchCorbaException(S_ex);
482 return SMESH::SMESH_Hypothesis::_nil();
486 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
488 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
489 int res = SMESH::HYP_UNKNOWN_FATAL;
490 SUIT_OverrideCursor wc;
492 if (!aMesh->_is_nil()) {
493 _PTR(SObject) SM = SMESH::FindSObject(aMesh);
494 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
496 res = aMesh->AddHypothesis(aShapeObject, aHyp);
497 if (res < SMESH::HYP_UNKNOWN_FATAL) {
498 _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
500 SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
503 if (res > SMESH::HYP_OK) {
505 processHypothesisStatus(res, aHyp, true);
509 catch(const SALOME::SALOME_Exception& S_ex) {
511 SalomeApp_Tools::QtCatchCorbaException(S_ex);
512 res = SMESH::HYP_UNKNOWN_FATAL;
515 return res < SMESH::HYP_UNKNOWN_FATAL;
519 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
521 if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
522 int res = SMESH::HYP_UNKNOWN_FATAL;
523 SUIT_OverrideCursor wc;
525 if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
527 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
528 _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
529 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
530 if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
531 res = aMesh->AddHypothesis(aShapeObject, aHyp);
532 if (res < SMESH::HYP_UNKNOWN_FATAL) {
533 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
535 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
537 if (res > SMESH::HYP_OK) {
539 processHypothesisStatus(res, aHyp, true);
544 SCRUTE(aHyp->_is_nil());
545 SCRUTE(aMesh->_is_nil());
547 SCRUTE(aShapeObject->_is_nil());
550 catch(const SALOME::SALOME_Exception& S_ex) {
552 SalomeApp_Tools::QtCatchCorbaException(S_ex);
553 res = SMESH::HYP_UNKNOWN_FATAL;
557 SCRUTE(aSubMesh->_is_nil());
558 SCRUTE(aHyp->_is_nil());
560 return res < SMESH::HYP_UNKNOWN_FATAL;
563 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
565 int res = SMESH::HYP_UNKNOWN_FATAL;
566 SUIT_OverrideCursor wc;
569 _PTR(Study) aStudy = GetActiveStudyDocument();
570 _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
573 _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
574 _PTR(SObject) aRealHypo;
575 if( aHypObj->ReferencedObject( aRealHypo ) )
577 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
578 RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
582 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
583 SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
584 for( int i = 0; i < meshList.size(); i++ )
585 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
589 catch(const SALOME::SALOME_Exception& S_ex)
592 SalomeApp_Tools::QtCatchCorbaException(S_ex);
593 res = SMESH::HYP_UNKNOWN_FATAL;
595 return res < SMESH::HYP_UNKNOWN_FATAL;
598 bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
599 SMESH::SMESH_Hypothesis_ptr anHyp)
601 SALOMEDS::GenericAttribute_var anAttr;
602 SALOMEDS::AttributeIOR_var anIOR;
603 int res = SMESH::HYP_UNKNOWN_FATAL;
604 SUIT_OverrideCursor wc;
608 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
609 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
610 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
612 if (!aSubMesh->_is_nil())
613 aMesh = aSubMesh->GetFather();
615 if (!aMesh->_is_nil()) {
616 if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
617 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
618 if (res < SMESH::HYP_UNKNOWN_FATAL) {
619 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
621 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
625 else if(!aMesh->HasShapeToMesh()){
626 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
627 if (res < SMESH::HYP_UNKNOWN_FATAL) {
628 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
630 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
633 if (res > SMESH::HYP_OK) {
635 processHypothesisStatus(res, anHyp, false);
639 } catch(const SALOME::SALOME_Exception& S_ex) {
641 SalomeApp_Tools::QtCatchCorbaException(S_ex);
642 res = SMESH::HYP_UNKNOWN_FATAL;
645 return res < SMESH::HYP_UNKNOWN_FATAL;
648 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
650 SObjectList listSOmesh;
651 listSOmesh.resize(0);
653 unsigned int index = 0;
654 if (!AlgoOrHyp->_is_nil()) {
655 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
658 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
660 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
661 for (unsigned int i = 0; i < listSO.size(); i++) {
662 _PTR(SObject) SO = listSO[i];
664 _PTR(SObject) aFather = SO->GetFather();
666 _PTR(SObject) SOfatherFather = aFather->GetFather();
667 if (SOfatherFather) {
668 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
670 listSOmesh.resize(index);
671 listSOmesh[index - 1] = SOfatherFather;
678 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
682 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
683 QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
685 QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
686 for ( int i = 0; i < errors.length(); ++i ) {
687 const SMESH::AlgoStateError & error = errors[ i ];
688 const bool hasAlgo = ( strlen( error.algoName ) != 0 );
691 msg = QObject::tr( "STATE_ALGO_MISSING" );
693 switch( error.state ) {
694 CASE2MESSAGE( HYP_MISSING );
695 CASE2MESSAGE( HYP_NOTCONFORM );
696 CASE2MESSAGE( HYP_BAD_PARAMETER );
697 CASE2MESSAGE( HYP_BAD_GEOMETRY );
700 // apply args to message:
703 msg = msg.arg( error.algoName.in() );
705 msg = msg.arg( error.algoDim );
707 msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
708 // %4 - hypothesis dim == algoDim
709 msg = msg.arg( error.algoDim );
711 if ( i ) resMsg += ";\n";
716 } // end of namespace SMESH