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()));
192 if (aNoAccessFiles.isEmpty())
193 aNoAccessFiles = xmlFile;
195 aNoAccessFiles += ", " + xmlFile;
200 if (!aNoAccessFiles.isEmpty()) {
201 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
202 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
204 SUIT_MessageBox::warning(SMESHGUI::desktop(),
205 QObject::tr("SMESH_WRN_WARNING"),
213 QStringList GetAvailableHypotheses( const bool isAlgo,
216 const bool isNeedGeometry)
218 QStringList aHypList;
220 // Init list of available hypotheses, if needed
221 InitAvailableHypotheses();
222 bool checkGeometry = ( !isNeedGeometry && isAlgo );
223 // fill list of hypotheses/algorithms
224 THypothesisDataMap& pMap = isAlgo ? myAlgorithmsMap : myHypothesesMap;
225 THypothesisDataMap::iterator anIter;
226 for ( anIter = pMap.begin(); anIter != pMap.end(); anIter++ ) {
227 HypothesisData* aData = anIter.value();
228 if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux) {
230 if (aData->IsNeedGeometry == isNeedGeometry)
231 aHypList.append(anIter.key());
234 aHypList.append(anIter.key());
242 QStringList GetHypothesesSets()
244 QStringList aSetNameList;
246 // Init list of available hypotheses, if needed
247 InitAvailableHypotheses();
249 QList<HypothesesSet*>::iterator hypoSet;
250 for ( hypoSet = myListOfHypothesesSets.begin();
251 hypoSet != myListOfHypothesesSets.end();
253 HypothesesSet* aSet = *hypoSet;
254 if ( aSet && aSet->count( true ) ) {
255 aSetNameList.append( aSet->name() );
262 HypothesesSet* GetHypothesesSet(const QString& theSetName)
264 QList<HypothesesSet*>::iterator hypoSet;
265 for ( hypoSet = myListOfHypothesesSets.begin();
266 hypoSet != myListOfHypothesesSets.end();
268 HypothesesSet* aSet = *hypoSet;
269 if ( aSet && aSet->name() == theSetName )
275 HypothesisData* GetHypothesisData (const QString& aHypType)
277 HypothesisData* aHypData = 0;
279 // Init list of available hypotheses, if needed
280 InitAvailableHypotheses();
282 if (myHypothesesMap.find(aHypType) != myHypothesesMap.end()) {
283 aHypData = myHypothesesMap[aHypType];
285 else if (myAlgorithmsMap.find(aHypType) != myAlgorithmsMap.end()) {
286 aHypData = myAlgorithmsMap[aHypType];
291 bool IsAvailableHypothesis(const HypothesisData* algoData,
292 const QString& hypType,
298 if ( algoData->NeededHypos.contains( hypType ))
300 if ( algoData->OptionalHypos.contains( hypType)) {
307 bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
308 const HypothesisData* algo2Data)
310 if ( !algo1Data || !algo2Data )
312 const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
313 if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
314 algoIn = algo2Data; algoMain = algo1Data;
316 // look for any output type of algoIn between input types of algoMain
317 QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
318 for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
319 if ( algoMain->InputTypes.contains( *inElemType ))
324 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const QString& aHypType)
326 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType.toLatin1().data());
328 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
330 // check, if creator for this hypothesis type already exists
332 //if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
333 // aCreator = myHypCreatorMap[aHypType];
337 // 1. Init list of available hypotheses, if needed
338 InitAvailableHypotheses();
340 // 2. Get names of plugin libraries
341 HypothesisData* aHypData = GetHypothesisData(aHypType);
345 QString aClientLibName = aHypData->ClientLibName;
346 QString aServerLibName = aHypData->ServerLibName;
348 // 3. Load Client Plugin Library
350 // load plugin library
351 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
352 LibHandle libHandle = LoadLib( aClientLibName.toLatin1().data() );
354 // report any error, if occured
357 const char* anError = "Can't load client meshers plugin library";
359 const char* anError = dlerror();
365 // get method, returning hypothesis creator
366 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
367 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
369 GetHypothesisCreator procHandle =
370 (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
372 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
373 UnLoadLib(libHandle);
376 // get hypothesis creator
377 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType.toLatin1().data());
378 aCreator = procHandle( aHypType );
380 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
383 // map hypothesis creator to a hypothesis name
385 //myHypCreatorMap[aHypType] = aCreator;
390 catch (const SALOME::SALOME_Exception& S_ex) {
391 SalomeApp_Tools::QtCatchCorbaException(S_ex);
399 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const QString& aHypType,
400 const QString& aHypName,
403 if(MYDEBUG) MESSAGE("Create " << aHypType.toLatin1().data() <<
404 " with name " << aHypName.toLatin1().data());
405 HypothesisData* aHypData = GetHypothesisData(aHypType);
406 QString aServLib = aHypData->ServerLibName;
408 SMESH::SMESH_Hypothesis_var aHypothesis;
409 aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType.toLatin1().data(),
410 aServLib.toLatin1().data());
411 if (!aHypothesis->_is_nil()) {
412 _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
414 if (!aHypName.isEmpty())
415 SMESH::SetName(aHypSObject, aHypName);
416 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
417 return aHypothesis._retn();
420 } catch (const SALOME::SALOME_Exception & S_ex) {
421 SalomeApp_Tools::QtCatchCorbaException(S_ex);
424 return SMESH::SMESH_Hypothesis::_nil();
428 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
430 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
431 int res = SMESH::HYP_UNKNOWN_FATAL;
432 SUIT_OverrideCursor wc;
434 if (!aMesh->_is_nil()) {
435 _PTR(SObject) SM = SMESH::FindSObject(aMesh);
436 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
438 res = aMesh->AddHypothesis(aShapeObject, aHyp);
439 if (res < SMESH::HYP_UNKNOWN_FATAL) {
440 _PTR(SObject) aSH = SMESH::FindSObject(aHyp);
442 SMESH::ModifiedMesh(SM, false, aMesh->NbNodes()==0);
445 if (res > SMESH::HYP_OK) {
447 processHypothesisStatus(res, aHyp, true);
451 catch(const SALOME::SALOME_Exception& S_ex) {
453 SalomeApp_Tools::QtCatchCorbaException(S_ex);
454 res = SMESH::HYP_UNKNOWN_FATAL;
457 return res < SMESH::HYP_UNKNOWN_FATAL;
461 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
463 if(MYDEBUG) MESSAGE("SMESHGUI::AddHypothesisOnSubMesh() ");
464 int res = SMESH::HYP_UNKNOWN_FATAL;
465 SUIT_OverrideCursor wc;
467 if (!aSubMesh->_is_nil() && ! aHyp->_is_nil()) {
469 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
470 _PTR(SObject) SsubM = SMESH::FindSObject(aSubMesh);
471 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SsubM);
472 if (!aMesh->_is_nil() && SsubM && !aShapeObject->_is_nil()) {
473 res = aMesh->AddHypothesis(aShapeObject, aHyp);
474 if (res < SMESH::HYP_UNKNOWN_FATAL) {
475 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
477 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
479 if (res > SMESH::HYP_OK) {
481 processHypothesisStatus(res, aHyp, true);
486 SCRUTE(aHyp->_is_nil());
487 SCRUTE(aMesh->_is_nil());
489 SCRUTE(aShapeObject->_is_nil());
492 catch(const SALOME::SALOME_Exception& S_ex) {
494 SalomeApp_Tools::QtCatchCorbaException(S_ex);
495 res = SMESH::HYP_UNKNOWN_FATAL;
499 SCRUTE(aSubMesh->_is_nil());
500 SCRUTE(aHyp->_is_nil());
502 return res < SMESH::HYP_UNKNOWN_FATAL;
505 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
507 int res = SMESH::HYP_UNKNOWN_FATAL;
508 SUIT_OverrideCursor wc;
511 _PTR(Study) aStudy = GetActiveStudyDocument();
512 _PTR(SObject) aHypObj = aStudy->FindObjectID( IObject->getEntry() );
515 _PTR(SObject) MorSM = SMESH::GetMeshOrSubmesh( aHypObj );
516 _PTR(SObject) aRealHypo;
517 if( aHypObj->ReferencedObject( aRealHypo ) )
519 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aRealHypo ) );
520 RemoveHypothesisOrAlgorithmOnMesh( MorSM, hypo );
524 SMESH_Hypothesis_var hypo = SMESH_Hypothesis::_narrow( SObjectToObject( aHypObj ) );
525 SObjectList meshList = GetMeshesUsingAlgoOrHypothesis( hypo );
526 for( int i = 0; i < meshList.size(); i++ )
527 RemoveHypothesisOrAlgorithmOnMesh( meshList[ i ], hypo );
531 catch(const SALOME::SALOME_Exception& S_ex)
534 SalomeApp_Tools::QtCatchCorbaException(S_ex);
535 res = SMESH::HYP_UNKNOWN_FATAL;
537 return res < SMESH::HYP_UNKNOWN_FATAL;
540 bool RemoveHypothesisOrAlgorithmOnMesh (_PTR(SObject) MorSM,
541 SMESH::SMESH_Hypothesis_ptr anHyp)
543 SALOMEDS::GenericAttribute_var anAttr;
544 SALOMEDS::AttributeIOR_var anIOR;
545 int res = SMESH::HYP_UNKNOWN_FATAL;
546 SUIT_OverrideCursor wc;
550 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
551 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
552 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
554 if (!aSubMesh->_is_nil())
555 aMesh = aSubMesh->GetFather();
557 if (!aMesh->_is_nil()) {
558 if (aMesh->HasShapeToMesh() && !aShapeObject->_is_nil()) {
559 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
560 if (res < SMESH::HYP_UNKNOWN_FATAL) {
561 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
563 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
567 else if(!aMesh->HasShapeToMesh()){
568 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
569 if (res < SMESH::HYP_UNKNOWN_FATAL) {
570 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
572 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
575 if (res > SMESH::HYP_OK) {
577 processHypothesisStatus(res, anHyp, false);
581 } catch(const SALOME::SALOME_Exception& S_ex) {
583 SalomeApp_Tools::QtCatchCorbaException(S_ex);
584 res = SMESH::HYP_UNKNOWN_FATAL;
587 return res < SMESH::HYP_UNKNOWN_FATAL;
590 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
592 SObjectList listSOmesh;
593 listSOmesh.resize(0);
595 unsigned int index = 0;
596 if (!AlgoOrHyp->_is_nil()) {
597 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
600 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
602 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
603 for (unsigned int i = 0; i < listSO.size(); i++) {
604 _PTR(SObject) SO = listSO[i];
606 _PTR(SObject) aFather = SO->GetFather();
608 _PTR(SObject) SOfatherFather = aFather->GetFather();
609 if (SOfatherFather) {
610 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
612 listSOmesh.resize(index);
613 listSOmesh[index - 1] = SOfatherFather;
620 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
624 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
625 QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
627 QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
628 for ( int i = 0; i < errors.length(); ++i ) {
629 const SMESH::AlgoStateError & error = errors[ i ];
630 const bool hasAlgo = ( strlen( error.algoName ) != 0 );
633 msg = QObject::tr( "STATE_ALGO_MISSING" );
635 switch( error.state ) {
636 CASE2MESSAGE( HYP_MISSING );
637 CASE2MESSAGE( HYP_NOTCONFORM );
638 CASE2MESSAGE( HYP_BAD_PARAMETER );
639 CASE2MESSAGE( HYP_BAD_GEOMETRY );
642 // apply args to message:
645 msg = msg.arg( error.algoName.in() );
647 msg = msg.arg( error.algoDim );
649 msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
650 // %4 - hypothesis dim == algoDim
651 msg = msg.arg( error.algoDim );
653 if ( i ) resMsg += ";\n";
658 } // end of namespace SMESH