1 // Copyright (C) 2007-2011 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>
65 #define LibHandle HMODULE
66 #define LoadLib( name ) LoadLibrary( name )
67 #define GetProc GetProcAddress
68 #define UnLoadLib( handle ) FreeLibrary( handle );
70 #define LibHandle void*
71 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
73 #define UnLoadLib( handle ) dlclose( handle );
77 static int MYDEBUG = 0;
79 static int MYDEBUG = 0;
84 typedef IMap<QString,HypothesisData*> THypothesisDataMap;
85 THypothesisDataMap myHypothesesMap;
86 THypothesisDataMap myAlgorithmsMap;
89 //typedef QMap<QString,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
90 //THypCreatorMap myHypCreatorMap;
92 QList<HypothesesSet*> myListOfHypothesesSets;
94 void processHypothesisStatus(const int theHypStatus,
95 SMESH::SMESH_Hypothesis_ptr theHyp,
96 const bool theIsAddition)
98 if (theHypStatus > SMESH::HYP_OK) {
100 QString aHypName ("NULL Hypothesis");
101 if (!CORBA::is_nil(theHyp)) {
102 _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
105 aHypName = Shyp->GetName().c_str();
108 aHypName = GetHypothesisData(theHyp->GetName())->Label;
112 bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
115 aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
117 aMsg = (isFatal ? "SMESH_CANT_RM_HYP" : "SMESH_RM_HYP_WRN");
119 aMsg = QObject::tr(aMsg.toLatin1().data()).arg(aHypName) +
120 QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus).toLatin1().data());
122 if ( theHypStatus == SMESH::HYP_HIDDEN_ALGO ) // PAL18501
123 aMsg = aMsg.arg( GetHypothesisData(theHyp->GetName())->Dim[0] );
125 SUIT_MessageBox::warning(SMESHGUI::desktop(),
126 QObject::tr("SMESH_WRN_WARNING"),
131 //================================================================================
133 * \brief Prepends dimension and appends '[custom]' to the name of hypothesis set
135 //================================================================================
137 static QString mangledHypoSetName(HypothesesSet* hypSet)
139 QString name = hypSet->name();
142 int dim = hypSet->maxDim();
144 name = QString("%1D: %2").arg(dim).arg(name);
147 if ( hypSet->getIsCustom() )
148 name = QString("%1 [custom]").arg(name);
153 //================================================================================
155 * \brief Removes dimension and '[custom]' from the name of hypothesis set
157 //================================================================================
159 static QString demangledHypoSetName(QString name)
161 name.remove(QRegExp("[0-3]D: "));
162 name.remove(" [custom]");
166 void InitAvailableHypotheses()
168 SUIT_OverrideCursor wc;
169 if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
171 SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
174 // Find name of a resource XML file ("SMESH_Meshers.xml");
176 char* cenv = getenv("SMESH_MeshersList");
178 HypsXml.sprintf("%s", cenv);
180 QStringList HypsXmlList = HypsXml.split(":", QString::SkipEmptyParts);
181 if (HypsXmlList.count() == 0) {
182 SUIT_MessageBox::critical(SMESHGUI::desktop(),
183 QObject::tr("SMESH_WRN_WARNING"),
184 QObject::tr("MESHERS_FILE_NO_VARIABLE"));
187 // get full names of xml files from HypsXmlList
188 QStringList xmlFiles;
189 xmlFiles.append( QDir::home().filePath("CustomMeshers.xml")); // may be inexistent
190 for (int i = 0; i < HypsXmlList.count(); i++) {
191 QString HypsXml = HypsXmlList[ i ];
193 // Find full path to the resource XML file
194 QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
195 if ( xmlFile.isEmpty() ) // try PLUGIN resources
196 xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
197 if ( !xmlFile.isEmpty() )
198 xmlFiles.append( xmlFile );
202 QString aNoAccessFiles;
203 for (int i = 0; i < xmlFiles.count(); i++)
205 QString xmlFile = xmlFiles[ i ];
206 QFile file (xmlFile);
207 if (file.exists() && file.open(QIODevice::ReadOnly))
211 SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
214 QXmlInputSource source (&file);
215 QXmlSimpleReader reader;
216 reader.setContentHandler(aXmlHandler);
217 reader.setErrorHandler(aXmlHandler);
218 bool ok = reader.parse(source);
222 THypothesisDataMap::ConstIterator it1 = aXmlHandler->myHypothesesMap.begin();
224 for( ;it1 != aXmlHandler->myHypothesesMap.end(); it1++)
225 myHypothesesMap.insert( it1.key(), it1.value() );
228 it1 = aXmlHandler->myAlgorithmsMap.begin();
229 for( ;it1 != aXmlHandler->myAlgorithmsMap.end(); it1++)
230 myAlgorithmsMap.insert( it1.key(), it1.value() );
232 QList<HypothesesSet*>::iterator it, pos = myListOfHypothesesSets.begin();
233 for ( it = aXmlHandler->myListOfHypothesesSets.begin();
234 it != aXmlHandler->myListOfHypothesesSets.end();
237 (*it)->setIsCustom( i == 0 );
238 myListOfHypothesesSets.insert( pos, *it );
242 SUIT_MessageBox::critical(SMESHGUI::desktop(),
243 QObject::tr("INF_PARSE_ERROR"),
244 QObject::tr(aXmlHandler->errorProtocol().toLatin1().data()));
248 else if ( i > 0 ) { // 1st is ~/CustomMeshers.xml
249 if (aNoAccessFiles.isEmpty())
250 aNoAccessFiles = xmlFile;
252 aNoAccessFiles += ", " + xmlFile;
254 } // end loop on xmlFiles
257 if (!aNoAccessFiles.isEmpty()) {
258 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
259 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
261 SUIT_MessageBox::warning(SMESHGUI::desktop(),
262 QObject::tr("SMESH_WRN_WARNING"),
270 QStringList GetAvailableHypotheses( const bool isAlgo,
273 const bool isNeedGeometry)
275 QStringList aHypList;
277 // Init list of available hypotheses, if needed
278 InitAvailableHypotheses();
279 bool checkGeometry = ( !isNeedGeometry && isAlgo );
280 // fill list of hypotheses/algorithms
281 THypothesisDataMap& pMap = isAlgo ? myAlgorithmsMap : myHypothesesMap;
282 THypothesisDataMap::ConstIterator anIter;
283 for ( anIter = pMap.begin(); anIter != pMap.end(); anIter++ ) {
284 HypothesisData* aData = anIter.value();
285 if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux) {
287 if (aData->IsNeedGeometry == isNeedGeometry)
288 aHypList.append(anIter.key());
291 aHypList.append(anIter.key());
299 QStringList GetHypothesesSets(int maxDim)
301 QStringList aSetNameList;
303 // Init list of available hypotheses, if needed
304 InitAvailableHypotheses();
306 QList<HypothesesSet*>::iterator hypoSet;
307 for ( hypoSet = myListOfHypothesesSets.begin();
308 hypoSet != myListOfHypothesesSets.end();
310 HypothesesSet* aSet = *hypoSet;
312 ( aSet->count( true ) || aSet->count( false )) &&
313 aSet->maxDim() <= maxDim)
315 aSetNameList.append( mangledHypoSetName( aSet ));
320 // reverse order of aSetNameList
321 QStringList reversedNames;
322 for ( int i = 0; i < aSetNameList.count(); ++i )
323 reversedNames.prepend( aSetNameList[i] );
325 return reversedNames;
328 HypothesesSet* GetHypothesesSet(const QString& theSetName)
330 QString name = demangledHypoSetName( theSetName );
331 QList<HypothesesSet*>::iterator hypoSet;
332 for ( hypoSet = myListOfHypothesesSets.begin();
333 hypoSet != myListOfHypothesesSets.end();
335 HypothesesSet* aSet = *hypoSet;
336 if ( aSet && aSet->name() == name )
342 HypothesisData* GetHypothesisData (const QString& aHypType)
344 HypothesisData* aHypData = 0;
346 // Init list of available hypotheses, if needed
347 InitAvailableHypotheses();
349 if (myHypothesesMap.contains(aHypType)) {
350 aHypData = myHypothesesMap[aHypType];
352 else if (myAlgorithmsMap.contains(aHypType)) {
353 aHypData = myAlgorithmsMap[aHypType];
358 bool IsAvailableHypothesis(const HypothesisData* algoData,
359 const QString& hypType,
365 if ( algoData->NeededHypos.contains( hypType ))
367 if ( algoData->OptionalHypos.contains( hypType)) {
374 bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
375 const HypothesisData* algo2Data)
377 if ( !algo1Data || !algo2Data )
379 const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
380 if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
381 algoIn = algo2Data; algoMain = algo1Data;
383 // look for any output type of algoIn between input types of algoMain
384 QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
385 for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
386 if ( algoMain->InputTypes.contains( *inElemType ))
391 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const QString& aHypType)
393 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType.toLatin1().data());
395 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
397 // check, if creator for this hypothesis type already exists
399 //if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
400 // aCreator = myHypCreatorMap[aHypType];
404 // 1. Init list of available hypotheses, if needed
405 InitAvailableHypotheses();
407 // 2. Get names of plugin libraries
408 HypothesisData* aHypData = GetHypothesisData(aHypType);
412 QString aClientLibName = aHypData->ClientLibName;
413 QString aServerLibName = aHypData->ServerLibName;
415 // 3. Load Client Plugin Library
417 // load plugin library
418 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
419 LibHandle libHandle = LoadLib( aClientLibName.toLatin1().data() );
421 // report any error, if occured
424 const char* anError = "Can't load client meshers plugin library";
426 const char* anError = dlerror();
432 // get method, returning hypothesis creator
433 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
434 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
436 GetHypothesisCreator procHandle =
437 (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
439 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
440 UnLoadLib(libHandle);
443 // get hypothesis creator
444 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
445 aCreator = procHandle( aHypType );
447 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
450 // map hypothesis creator to a hypothesis name
452 //myHypCreatorMap[aHypType] = aCreator;
457 catch (const SALOME::SALOME_Exception& S_ex) {
458 SalomeApp_Tools::QtCatchCorbaException(S_ex);
466 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
467 const QString& aHypName,
470 if(MYDEBUG) MESSAGE("Create " << aHypType.toLatin1().data() <<
471 " with name " << aHypName.toLatin1().data());
472 HypothesisData* aHypData = GetHypothesisData(aHypType);
473 QString aServLib = aHypData->ServerLibName;
475 SMESH::SMESH_Hypothesis_var aHypothesis;
476 aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
477 aServLib.toLatin1().data());
478 if (!aHypothesis->_is_nil()) {
479 _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
481 if (!aHypName.isEmpty())
482 SMESH::SetName(aHypSObject, aHypName);
483 SMESHGUI::Modified();
484 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
485 return aHypothesis._retn();
488 } catch (const SALOME::SALOME_Exception & S_ex) {
489 SalomeApp_Tools::QtCatchCorbaException(S_ex);
492 return SMESH::SMESH_Hypothesis::_nil();
496 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
498 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
499 int res = SMESH::HYP_UNKNOWN_FATAL;
500 SUIT_OverrideCursor wc;
502 if (!aMesh->_is_nil()) {
503 _PTR(SObject) SM = SMESH::FindSObject(aMesh);
504 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
506 res = aMesh->AddHypothesis(aShapeObject, aHyp);
507 if (res < SMESH::HYP_UNKNOWN_FATAL) {
508 _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
510 SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
513 if (res > SMESH::HYP_OK) {
515 processHypothesisStatus(res, aHyp, true);
519 catch(const SALOME::SALOME_Exception& S_ex) {
521 SalomeApp_Tools::QtCatchCorbaException(S_ex);
522 res = SMESH::HYP_UNKNOWN_FATAL;
525 return res < SMESH::HYP_UNKNOWN_FATAL;
529 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
531 if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
532 int res = SMESH::HYP_UNKNOWN_FATAL;
533 SUIT_OverrideCursor wc;
535 if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
537 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
538 _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
539 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
540 if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
541 res = aMesh->AddHypothesis(aShapeObject, aHyp);
542 if (res < SMESH::HYP_UNKNOWN_FATAL) {
543 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
545 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
547 if (res > SMESH::HYP_OK) {
549 processHypothesisStatus(res, aHyp, true);
554 SCRUTE(aHyp->_is_nil());
555 SCRUTE(aMesh->_is_nil());
557 SCRUTE(aShapeObject->_is_nil());
560 catch(const SALOME::SALOME_Exception& S_ex) {
562 SalomeApp_Tools::QtCatchCorbaException(S_ex);
563 res = SMESH::HYP_UNKNOWN_FATAL;
567 SCRUTE(aSubMesh->_is_nil());
568 SCRUTE(aHyp->_is_nil());
570 return res < SMESH::HYP_UNKNOWN_FATAL;
573 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
575 int res = SMESH::HYP_UNKNOWN_FATAL;
576 SUIT_OverrideCursor wc;
579 _PTR(Study) aStudy = GetActiveStudyDocument();
580 _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
583 _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
584 _PTR(SObject) aRealHypo;
585 if( aHypObj->ReferencedObject( aRealHypo ) )
587 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
588 RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
592 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
593 SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
594 for( int i = 0; i < meshList.size(); i++ )
595 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
599 catch(const SALOME::SALOME_Exception& S_ex)
602 SalomeApp_Tools::QtCatchCorbaException(S_ex);
603 res = SMESH::HYP_UNKNOWN_FATAL;
605 return res < SMESH::HYP_UNKNOWN_FATAL;
608 bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
609 SMESH::SMESH_Hypothesis_ptr anHyp)
611 SALOMEDS::GenericAttribute_var anAttr;
612 SALOMEDS::AttributeIOR_var anIOR;
613 int res = SMESH::HYP_UNKNOWN_FATAL;
614 SUIT_OverrideCursor wc;
618 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
619 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
620 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
622 if (!aSubMesh->_is_nil())
623 aMesh = aSubMesh->GetFather();
625 if (!aMesh->_is_nil()) {
626 if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
627 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
628 if (res < SMESH::HYP_UNKNOWN_FATAL) {
629 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
631 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
635 else if(!aMesh->HasShapeToMesh()){
636 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
637 if (res < SMESH::HYP_UNKNOWN_FATAL) {
638 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
640 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
643 if (res > SMESH::HYP_OK) {
645 processHypothesisStatus(res, anHyp, false);
649 } catch(const SALOME::SALOME_Exception& S_ex) {
651 SalomeApp_Tools::QtCatchCorbaException(S_ex);
652 res = SMESH::HYP_UNKNOWN_FATAL;
655 return res < SMESH::HYP_UNKNOWN_FATAL;
658 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
660 SObjectList listSOmesh;
661 listSOmesh.resize(0);
663 unsigned int index = 0;
664 if (!AlgoOrHyp->_is_nil()) {
665 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
668 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
670 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
671 for (unsigned int i = 0; i < listSO.size(); i++) {
672 _PTR(SObject) SO = listSO[i];
674 _PTR(SObject) aFather = SO->GetFather();
676 _PTR(SObject) SOfatherFather = aFather->GetFather();
677 if (SOfatherFather) {
678 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
680 listSOmesh.resize(index);
681 listSOmesh[index - 1] = SOfatherFather;
688 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
692 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
693 QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
695 QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
696 for ( int i = 0; i < errors.length(); ++i ) {
697 const SMESH::AlgoStateError & error = errors[ i ];
698 const bool hasAlgo = ( strlen( error.algoName ) != 0 );
701 msg = QObject::tr( "STATE_ALGO_MISSING" );
703 switch( error.state ) {
704 CASE2MESSAGE( HYP_MISSING );
705 CASE2MESSAGE( HYP_NOTCONFORM );
706 CASE2MESSAGE( HYP_BAD_PARAMETER );
707 CASE2MESSAGE( HYP_BAD_GEOMETRY );
710 // apply args to message:
713 msg = msg.arg( error.algoName.in() );
715 msg = msg.arg( error.algoDim );
717 msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
718 // %4 - hypothesis dim == algoDim
719 msg = msg.arg( error.algoDim );
721 if ( i ) resMsg += ";\n";
726 } // end of namespace SMESH