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 if ( theHypStatus == SMESH::HYP_HIDDEN_ALGO ) // PAL18501
117 aMsg = aMsg.arg( GetHypothesisData(theHyp->GetName())->Dim[0] );
119 SUIT_MessageBox::warn1(SMESHGUI::desktop(),
120 QObject::tr("SMESH_WRN_WARNING"),
122 QObject::tr("SMESH_BUT_OK"));
127 void InitAvailableHypotheses()
129 SUIT_OverrideCursor wc;
130 if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
132 SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
135 // Find name of a resource XML file ("SMESH_Meshers.xml");
137 char* cenv = getenv("SMESH_MeshersList");
139 HypsXml.sprintf("%s", cenv);
141 QStringList HypsXmlList = QStringList::split(":", HypsXml, false);
142 if (HypsXmlList.count() == 0)
144 SUIT_MessageBox::error1(SMESHGUI::desktop(),
145 QObject::tr("SMESH_WRN_WARNING"),
146 QObject::tr("MESHERS_FILE_NO_VARIABLE"),
147 QObject::tr("SMESH_BUT_OK"));
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(IO_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.insert( aXmlHandler->myHypothesesMap.begin(),
176 aXmlHandler->myHypothesesMap.end() );
177 myAlgorithmsMap.insert( aXmlHandler->myAlgorithmsMap.begin(),
178 aXmlHandler->myAlgorithmsMap.end() );
179 myListOfHypothesesSets.splice( myListOfHypothesesSets.begin(),
180 aXmlHandler->myListOfHypothesesSets );
183 SUIT_MessageBox::error1(SMESHGUI::desktop(),
184 QObject::tr("INF_PARSE_ERROR"),
185 QObject::tr(aXmlHandler->errorProtocol()),
186 QObject::tr("SMESH_BUT_OK"));
190 if (aNoAccessFiles.isEmpty())
191 aNoAccessFiles = xmlFile;
193 aNoAccessFiles += ", " + xmlFile;
198 if (!aNoAccessFiles.isEmpty()) {
199 QString aMess = QObject::tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
200 aMess += QObject::tr("MESHERS_FILE_CHECK_VARIABLE");
202 SUIT_MessageBox::warn1(SMESHGUI::desktop(),
203 QObject::tr("SMESH_WRN_WARNING"),
205 QObject::tr("SMESH_BUT_OK"));
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;
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++ )
227 HypothesisData* aData = (*anIter).second;
228 if ( ( theDim < 0 || aData->Dim.contains( theDim ) ) && aData->IsAux == isAux)
230 if (aData->IsNeedGeometry == isNeedGeometry)
231 aHypList.append(((*anIter).first).c_str());
234 aHypList.append(((*anIter).first).c_str());
240 QStringList GetHypothesesSets()
242 QStringList aSetNameList;
244 // Init list of available hypotheses, if needed
245 InitAvailableHypotheses();
247 list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
248 for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
250 HypothesesSet* aSet = *hypoSet;
251 if ( aSet && aSet->AlgoList.count() ) {
252 aSetNameList.append( aSet->HypoSetName );
259 HypothesesSet* GetHypothesesSet(const QString theSetName)
261 list<HypothesesSet*>::iterator hypoSet = myListOfHypothesesSets.begin();
262 for ( ; hypoSet != myListOfHypothesesSets.end(); ++hypoSet )
264 HypothesesSet* aSet = *hypoSet;
265 if ( aSet && aSet->HypoSetName == theSetName )
271 HypothesisData* GetHypothesisData (const char* aHypType)
273 HypothesisData* aHypData = 0;
275 // Init list of available hypotheses, if needed
276 InitAvailableHypotheses();
278 THypothesisDataMap::iterator type_data = myHypothesesMap.find(aHypType);
279 if (type_data != myHypothesesMap.end()) {
280 aHypData = type_data->second;
283 type_data = myAlgorithmsMap.find(aHypType);
284 if (type_data != myAlgorithmsMap.end())
285 aHypData = type_data->second;
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 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator(const char* aHypType)
325 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << aHypType);
327 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
329 // check, if creator for this hypothesis type already exists
330 if (myHypCreatorMap.find(aHypType) != myHypCreatorMap.end()) {
331 aCreator = myHypCreatorMap[aHypType];
334 // 1. Init list of available hypotheses, if needed
335 InitAvailableHypotheses();
337 // 2. Get names of plugin libraries
338 HypothesisData* aHypData = GetHypothesisData(aHypType);
341 QString aClientLibName = aHypData->ClientLibName;
342 QString aServerLibName = aHypData->ServerLibName;
344 // 3. Load Client Plugin Library
346 // load plugin library
347 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
348 LibHandle libHandle = LoadLib( aClientLibName );
350 // report any error, if occured
354 const char* anError = "Can't load client meshers plugin library";
356 const char* anError = dlerror();
362 // get method, returning hypothesis creator
363 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
364 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
366 GetHypothesisCreator procHandle =
367 (GetHypothesisCreator)GetProc(libHandle, "GetHypothesisCreator");
369 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
370 UnLoadLib(libHandle);
373 // get hypothesis creator
374 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType);
375 aCreator = procHandle( aHypType );
377 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
380 // map hypothesis creator to a hypothesis name
381 myHypCreatorMap[aHypType] = aCreator;
386 catch (const SALOME::SALOME_Exception& S_ex) {
387 SalomeApp_Tools::QtCatchCorbaException(S_ex);
395 SMESH::SMESH_Hypothesis_ptr CreateHypothesis(const char* aHypType,
396 const char* aHypName,
399 if(MYDEBUG) MESSAGE("Create " << aHypType << " with name " << aHypName);
400 HypothesisData* aHypData = GetHypothesisData(aHypType);
401 QString aServLib = aHypData->ServerLibName;
403 SMESH::SMESH_Hypothesis_var aHypothesis;
404 aHypothesis = SMESHGUI::GetSMESHGen()->CreateHypothesis(aHypType, aServLib);
405 if (!aHypothesis->_is_nil()) {
406 _PTR(SObject) aHypSObject = SMESH::FindSObject(aHypothesis.in());
408 if (strlen(aHypName) > 0)
409 SMESH::SetName(aHypSObject, aHypName);
410 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
411 return aHypothesis._retn();
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 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
546 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
548 if (!aSubMesh->_is_nil())
549 aMesh = aSubMesh->GetFather();
551 if (!aMesh->_is_nil()) {
552 if (aMesh->HasShapeToMesh() && !aShapeObject->_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);
561 else if(!aMesh->HasShapeToMesh()){
562 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
563 if (res < SMESH::HYP_UNKNOWN_FATAL) {
564 _PTR(SObject) meshSO = SMESH::FindSObject(aMesh);
566 SMESH::ModifiedMesh(meshSO, false, aMesh->NbNodes()==0);
569 if (res > SMESH::HYP_OK) {
571 processHypothesisStatus(res, anHyp, false);
575 } catch(const SALOME::SALOME_Exception& S_ex) {
577 SalomeApp_Tools::QtCatchCorbaException(S_ex);
578 res = SMESH::HYP_UNKNOWN_FATAL;
581 return res < SMESH::HYP_UNKNOWN_FATAL;
584 SObjectList GetMeshesUsingAlgoOrHypothesis(SMESH::SMESH_Hypothesis_ptr AlgoOrHyp)
586 SObjectList listSOmesh;
587 listSOmesh.resize(0);
589 unsigned int index = 0;
590 if (!AlgoOrHyp->_is_nil()) {
591 _PTR(SObject) SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
594 SMESHGUI::activeStudy()->studyDS()->FindDependances(SO_Hypothesis);
596 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO.size());
597 for (unsigned int i = 0; i < listSO.size(); i++) {
598 _PTR(SObject) SO = listSO[i];
600 _PTR(SObject) aFather = SO->GetFather();
602 _PTR(SObject) SOfatherFather = aFather->GetFather();
603 if (SOfatherFather) {
604 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
606 listSOmesh.resize(index);
607 listSOmesh[index - 1] = SOfatherFather;
614 if (MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
618 #define CASE2MESSAGE(enum) case SMESH::enum: msg = QObject::tr( "STATE_" #enum ); break;
619 QString GetMessageOnAlgoStateErrors(const algo_error_array& errors)
621 QString resMsg; // PAL14861 = QObject::tr("SMESH_WRN_MISSING_PARAMETERS") + ":\n";
622 for ( int i = 0; i < errors.length(); ++i ) {
623 const SMESH::AlgoStateError & error = errors[ i ];
624 const bool hasAlgo = ( strlen( error.algoName ) != 0 );
627 msg = QObject::tr( "STATE_ALGO_MISSING" );
629 switch( error.state ) {
630 CASE2MESSAGE( HYP_MISSING );
631 CASE2MESSAGE( HYP_NOTCONFORM );
632 CASE2MESSAGE( HYP_BAD_PARAMETER );
633 CASE2MESSAGE( HYP_BAD_GEOMETRY );
636 // apply args to message:
639 msg = msg.arg( error.algoName.in() );
641 msg = msg.arg( error.algoDim );
643 msg = msg.arg( QObject::tr( error.isGlobalAlgo ? "GLOBAL_ALGO" : "LOCAL_ALGO" ));
644 // %4 - hypothesis dim == algoDim
645 msg = msg.arg( error.algoDim );
647 if ( i ) resMsg += ";\n";