1 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SMESHGUI_HypothesesUtils.h"
23 #include "SMESHGUI_Hypotheses.h"
24 #include "SMESHGUI_XmlHandler.h"
25 #include "SMESHGUI_Utils.h"
26 #include "SMESHGUI_GEOMGenUtils.h"
28 #include "SUIT_Tools.h"
29 #include "SUIT_Desktop.h"
30 #include "SUIT_MessageBox.h"
31 #include "SUIT_OverrideCursor.h"
32 #include "SUIT_ResourceMgr.h"
33 #include "SUIT_Session.h"
35 #include "OB_Browser.h"
37 #include "SalomeApp_Study.h"
38 #include "SalomeApp_Tools.h"
39 #include "SalomeApp_Application.h"
41 #include <SALOMEDSClient_Study.hxx>
42 #include <SALOMEDSClient_SObject.hxx>
44 #include "SALOMEconfig.h"
45 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
57 #define LibHandle HMODULE
58 #define LoadLib( name ) LoadLibrary( name )
59 #define GetProc GetProcAddress
60 #define UnLoadLib( handle ) FreeLibrary( handle );
62 #define LibHandle void*
63 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
65 #define UnLoadLib( handle ) dlclose( handle );
69 static int MYDEBUG = 0;
71 static int MYDEBUG = 0;
78 typedef map<string,HypothesisData*> THypothesisDataMap;
79 THypothesisDataMap myHypothesesMap;
80 THypothesisDataMap myAlgorithmsMap;
82 typedef map<string,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
83 THypCreatorMap myHypCreatorMap;
85 list<HypothesesSet*> myListOfHypothesesSets;
87 void processHypothesisStatus(const int theHypStatus,
88 SMESH::SMESH_Hypothesis_ptr theHyp,
89 const bool theIsAddition)
91 if (theHypStatus > SMESH::HYP_OK) {
94 QString aHypName ("NULL Hypothesis");
95 if (!CORBA::is_nil(theHyp)) {
96 _PTR(SObject) Shyp = SMESH::FindSObject(theHyp);
99 aHypName = Shyp->GetName().c_str();
102 aHypName = GetHypothesisData(theHyp->GetName())->Label;
106 bool isFatal = (theHypStatus >= SMESH::HYP_UNKNOWN_FATAL);
109 aMsg = (isFatal ? "SMESH_CANT_ADD_HYP" : "SMESH_ADD_HYP_WRN");
111 aMsg = (isFatal ? "SMESH_CANT_RM_HYP" : "SMESH_RM_HYP_WRN");
113 aMsg = QObject::tr(aMsg).arg(aHypName) +
114 QObject::tr(QString("SMESH_HYP_%1").arg(theHypStatus));
116 SUIT_MessageBox::warn1(SMESHGUI::desktop(),
117 QObject::tr("SMESH_WRN_WARNING"),
119 QObject::tr("SMESH_BUT_OK"));
124 void InitAvailableHypotheses()
126 SUIT_OverrideCursor wc;
127 if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
129 SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
132 // Find name of a resource XML file ("SMESH_Meshers.xml");
134 char* cenv = getenv("SMESH_MeshersList");
136 HypsXml.sprintf("%s", cenv);
138 QStringList HypsXmlList = QStringList::split(":", HypsXml, false);
139 if (HypsXmlList.count() == 0)
141 SUIT_MessageBox::error1(SMESHGUI::desktop(),
142 QObject::tr("SMESH_WRN_WARNING"),
143 QObject::tr("MESHERS_FILE_NO_VARIABLE"),
144 QObject::tr("SMESH_BUT_OK"));
148 // loop on files in HypsXml
149 QString aNoAccessFiles;
150 for (int i = 0; i < HypsXmlList.count(); i++) {
151 QString HypsXml = HypsXmlList[ i ];
153 // Find full path to the resource XML file
154 QString xmlFile = resMgr->path("resources", "SMESH", HypsXml + ".xml");
155 if ( xmlFile.isEmpty() ) // try PLUGIN resources
156 xmlFile = resMgr->path("resources", HypsXml, HypsXml + ".xml");
158 QFile file (xmlFile);
159 if (file.exists() && file.open(IO_ReadOnly)) {
162 SMESHGUI_XmlHandler* aXmlHandler = new SMESHGUI_XmlHandler();
165 QXmlInputSource source (file);
166 QXmlSimpleReader reader;
167 reader.setContentHandler(aXmlHandler);
168 reader.setErrorHandler(aXmlHandler);
169 bool ok = reader.parse(source);
172 myHypothesesMap.insert( aXmlHandler->myHypothesesMap.begin(),
173 aXmlHandler->myHypothesesMap.end() );
174 myAlgorithmsMap.insert( aXmlHandler->myAlgorithmsMap.begin(),
175 aXmlHandler->myAlgorithmsMap.end() );
176 myListOfHypothesesSets.splice( myListOfHypothesesSets.begin(),
177 aXmlHandler->myListOfHypothesesSets );
180 SUIT_MessageBox::error1(SMESHGUI::desktop(),
181 QObject::tr("INF_PARSE_ERROR"),
182 QObject::tr(aXmlHandler->errorProtocol()),
183 QObject::tr("SMESH_BUT_OK"));
187 if (aNoAccessFiles.isEmpty())
188 aNoAccessFiles = xmlFile;
190 aNoAccessFiles += ", " + xmlFile;
195 if (!aNoAccessFiles.isEmpty()) {
196 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
197 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
199 SUIT_MessageBox::warn1(SMESHGUI::desktop(),
200 QObject::tr("SMESH_WRN_WARNING"),
202 QObject::tr("SMESH_BUT_OK"));
209 QStringList GetAvailableHypotheses( const bool isAlgo,
213 QStringList aHypList;
215 // Init list of available hypotheses, if needed
216 InitAvailableHypotheses();
218 // fill list of hypotheses/algorithms
219 THypothesisDataMap* pMap = isAlgo ? &myAlgorithmsMap : &myHypothesesMap;
220 THypothesisDataMap::iterator anIter;
221 for ( anIter = pMap->begin(); anIter != pMap->end(); anIter++ )
223 HypothesisData* aData = (*anIter).second;
224 if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux )
225 aHypList.append(((*anIter).first).c_str());
231 QStringList GetHypothesesSets()
233 QStringList aSetNameList;
235 // Init list of available hypotheses, if needed
236 InitAvailableHypotheses();
238 list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
239 for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
241 HypothesesSet* aSet = *hypoSet;
242 if ( aSet && aSet->AlgoList.count() ) {
243 aSetNameList.append( aSet->HypoSetName );
250 HypothesesSet* GetHypothesesSet(const QString theSetName)
252 list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
253 for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
255 HypothesesSet* aSet = *hypoSet;
256 if ( aSet && aSet->HypoSetName == theSetName )
262 HypothesisData* GetHypothesisData (const char* aHypType)
264 HypothesisData* aHypData = 0;
266 // Init list of available hypotheses, if needed
267 InitAvailableHypotheses();
269 THypothesisDataMap::iterator type_data = myHypothesesMap.find(aHypType);
270 if (type_data != myHypothesesMap.end()) {
271 aHypData = type_data->second;
274 type_data = myAlgorithmsMap.find(aHypType);
275 if (type_data != myAlgorithmsMap.end())
276 aHypData = type_data->second;
281 bool IsAvailableHypothesis(const HypothesisData* algoData,
282 const QString& hypType,
288 if ( algoData->NeededHypos.contains( hypType ))
290 if ( algoData->OptionalHypos.contains( hypType)) {
297 bool IsCompatibleAlgorithm(const HypothesisData* algo1Data,
298 const HypothesisData* algo2Data)
300 if ( !algo1Data || !algo2Data )
302 const HypothesisData* algoIn = algo1Data, *algoMain = algo2Data;
303 if ( algoIn->Dim.first() > algoMain->Dim.first() ) {
304 algoIn = algo2Data; algoMain = algo1Data;
306 // look for any output type of algoIn between input types of algoMain
307 QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
308 for ( ; inElemType != algoIn->OutputTypes.end(); ++inElemType )
309 if ( algoMain->InputTypes.contains( *inElemType ))
314 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const char* aHypType)
316 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType);
318 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
320 // check, if creator for this hypothesis type already exists
321 if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
322 aCreator = myHypCreatorMap[aHypType];
325 // 1. Init list of available hypotheses, if needed
326 InitAvailableHypotheses();
328 // 2. Get names of plugin libraries
329 HypothesisData* aHypData = GetHypothesisData(aHypType);
332 QString aClientLibName = aHypData->ClientLibName;
333 QString aServerLibName = aHypData->ServerLibName;
335 // 3. Load Client Plugin Library
337 // load plugin library
338 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
339 LibHandle libHandle = LoadLib( aClientLibName );
341 // report any error, if occured
345 const char* anError = "Can't load client meshers plugin library";
347 const char* anError = dlerror();
353 // get method, returning hypothesis creator
354 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
355 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
357 GetHypothesisCreator procHandle =
358 (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
360 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
361 UnLoadLib(libHandle);
364 // get hypothesis creator
365 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType);
366 aCreator = procHandle( aHypType );
368 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
371 // map hypothesis creator to a hypothesis name
372 myHypCreatorMap[aHypType] = aCreator;
377 catch (const SALOME::SALOME_Exception& S_ex) {
378 SalomeApp_Tools::QtCatchCorbaException(S_ex);
386 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const char* aHypType,
387 const char* aHypName,
390 if(MYDEBUG) MESSAGE("Create " << aHypType << " with name " << aHypName);
392 SMESH::SMESH_Hypothesis_var Hyp;
394 HypothesisData* aHypData = GetHypothesisData(aHypType);
395 QString aServLib = aHypData->ServerLibName;
398 Hyp = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType, aServLib);
399 if (!Hyp->_is_nil()) {
400 _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
402 //if (strcmp(aHypName,"") != 0)
403 if (strlen(aHypName) > 0)
404 SMESH::SetName(SHyp, aHypName);
405 //SalomeApp_Application* app =
406 // dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
408 // app->objectBrowser()->updateTree();
409 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
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 if (!aShapeObject->_is_nil()) {
546 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
547 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
549 if (!aSubMesh->_is_nil())
550 aMesh = aSubMesh->GetFather();
552 if (!aMesh->_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);
559 if (res > SMESH::HYP_OK) {
561 processHypothesisStatus(res, anHyp, false);
566 } catch(const SALOME::SALOME_Exception& S_ex) {
568 SalomeApp_Tools::QtCatchCorbaException(S_ex);
569 res = SMESH::HYP_UNKNOWN_FATAL;
572 return res < SMESH::HYP_UNKNOWN_FATAL;
575 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
577 SObjectList listSOmesh;
578 listSOmesh.resize(0);
580 unsigned int index = 0;
581 if (!AlgoOrHyp->_is_nil()) {
582 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
585 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
587 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
588 for (unsigned int i = 0; i < listSO.size(); i++) {
589 _PTR(SObject) SO = listSO[i];
591 _PTR(SObject) aFather = SO->GetFather();
593 _PTR(SObject) SOfatherFather = aFather->GetFather();
594 if (SOfatherFather) {
595 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
597 listSOmesh.resize(index);
598 listSOmesh[index - 1] = SOfatherFather;
605 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
609 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
610 QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
612 QString resMsg = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
613 for ( int i = 0; i < errors.length(); ++i ) {
614 const SMESH::AlgoStateError & error = errors[ i ];
615 const bool hasAlgo = ( strlen( error.algoName ) != 0 );
618 msg = QObject::tr( "STATE_ALGO_MISSING" );
620 switch( error.state ) {
621 CASE2MESSAGE( HYP_MISSING );
622 CASE2MESSAGE( HYP_NOTCONFORM );
623 CASE2MESSAGE( HYP_BAD_PARAMETER );
626 // apply args to message:
629 msg = msg.arg( error.algoName.in() );
631 msg = msg.arg( error.algoDim );
633 msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
634 // %4 - hypothesis dim == algoDim
635 msg = msg.arg( error.algoDim );
637 if ( i ) resMsg += ";\n";