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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
23 #include "SMESHGUI_Hypotheses.h"
24 #include "SMESHGUI_Utils.h"
27 static int MYDEBUG = 0;
29 static int MYDEBUG = 0;
36 typedef map<string,HypothesisData*> THypothesisDataMap;
37 THypothesisDataMap myHypothesesMap;
38 THypothesisDataMap myAlgorithmsMap;
40 typedef map<string,SMESHGUI_GenericHypothesisCreator*> THypCreatorMap;
41 THypCreatorMap myHypCreatorMap;
43 void InitAvailableHypotheses()
46 if (myHypothesesMap.empty() && myAlgorithmsMap.empty()) {
48 QAD_ResourceMgr* resMgr = QAD_Desktop::createResourceManager();
51 // Find name of a resource XML file ("SMESH_Meshers.xml");
53 char* cenv = getenv("SMESH_MeshersList");
55 HypsXml.sprintf("%s", cenv);
57 QStringList HypsXmlList = QStringList::split( ":", HypsXml, false );
58 if (HypsXmlList.count() == 0)
60 QAD_MessageBox::error1(QAD_Application::getDesktop(),
61 tr("SMESH_WRN_WARNING"),
62 tr("MESHERS_FILE_NO_VARIABLE"),
67 // loop on files in HypsXml
68 QString aNoAccessFiles;
69 for ( int i = 0; i < HypsXmlList.count(); i++ ) {
70 QString HypsXml = HypsXmlList[ i ];
72 // Find full path to the resource XML file
73 QString xmlFile = HypsXml + ".xml";
74 xmlFile = QAD_Tools::addSlash(resMgr->findFile(xmlFile, HypsXml)) + xmlFile;
76 QFile file (QAD_Tools::unix2win(xmlFile));
77 if (file.exists() && file.open(IO_ReadOnly)) {
80 SMESHGUI_XmlHandler* myXmlHandler = new SMESHGUI_XmlHandler();
83 QXmlInputSource source (file);
84 QXmlSimpleReader reader;
85 reader.setContentHandler(myXmlHandler);
86 reader.setErrorHandler(myXmlHandler);
87 bool ok = reader.parse(source);
90 addMap( myXmlHandler->myHypothesesMap, myHypothesesMap );
91 addMap( myXmlHandler->myAlgorithmsMap, myAlgorithmsMap );
94 QAD_MessageBox::error1(myDesktop,
95 tr("INF_PARSE_ERROR"),
96 tr(myXmlHandler->errorProtocol()),
101 if (aNoAccessFiles.isEmpty())
102 aNoAccessFiles = xmlFile;
104 aNoAccessFiles += ", " + xmlFile;
109 if (!aNoAccessFiles.isEmpty()) {
110 QString aMess = tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
111 aMess += tr("MESHERS_FILE_CHECK_VARIABLE");
113 QAD_MessageBox::warn1(QAD_Application::getDesktop(),
114 tr("SMESH_WRN_WARNING"),
123 GetAvailableHypotheses (const bool isAlgo)
125 QStringList aHypList;
127 // Init list of available hypotheses, if needed
128 InitAvailableHypotheses();
130 // fill list of hypotheses/algorithms
131 THypothesisDataMap::iterator anIter;
133 anIter = myAlgorithmsMap.begin();
134 for (; anIter != myAlgorithmsMap.end(); anIter++) {
135 aHypList.append(((*anIter).first).c_str());
139 anIter = myHypothesesMap.begin();
140 for (; anIter != myHypothesesMap.end(); anIter++) {
141 aHypList.append(((*anIter).first).c_str());
149 HypothesisData* GetHypothesisData (const char* aHypType)
151 HypothesisData* aHypData = 0;
153 // Init list of available hypotheses, if needed
154 InitAvailableHypotheses();
156 if (myHypothesesMap.find(aHypType) == myHypothesesMap.end()) {
157 if (myAlgorithmsMap.find(aHypType) != myAlgorithmsMap.end()) {
158 aHypData = myAlgorithmsMap[aHypType];
162 aHypData = myHypothesesMap[aHypType];
168 SMESHGUI_GenericHypothesisCreator* GetHypothesisCreator (const QString& aHypType)
170 const char* sHypType = aHypType.latin1();
171 if(MYDEBUG) MESSAGE("Get HypothesisCreator for " << sHypType);
173 SMESHGUI_GenericHypothesisCreator* aCreator = 0;
175 // check, if creator for this hypothesis type already exists
176 if (myHypCreatorMap.find(sHypType) != myHypCreatorMap.end()) {
177 aCreator = myHypCreatorMap[sHypType];
180 // 1. Init list of available hypotheses, if needed
181 InitAvailableHypotheses();
183 // 2. Get names of plugin libraries
184 HypothesisData* aHypData = GetHypothesisData(sHypType);
188 QString aClientLibName = aHypData->ClientLibName;
189 QString aServerLibName = aHypData->ServerLibName;
191 // 3. Load Client Plugin Library
193 // load plugin library
194 if(MYDEBUG) MESSAGE("Loading client meshers plugin library ...");
195 void* libHandle = dlopen (aClientLibName, RTLD_LAZY);
197 // report any error, if occured
198 const char* anError = dlerror();
199 if(MYDEBUG) MESSAGE(anError);
202 // get method, returning hypothesis creator
203 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
204 typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
205 (QString aHypType, QString aServerLibName, SMESHGUI* aSMESHGUI);
206 GetHypothesisCreator procHandle =
207 (GetHypothesisCreator)dlsym( libHandle, "GetHypothesisCreator" );
209 if(MYDEBUG) MESSAGE("bad hypothesis client plugin library");
213 // get hypothesis creator
214 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << aHypType);
215 aCreator = procHandle(aHypType, aServerLibName, smeshGUI);
217 if(MYDEBUG) MESSAGE("no such a hypothesis in this plugin");
220 // map hypothesis creator to a hypothesis name
221 myHypCreatorMap[sHypType] = aCreator;
226 catch (const SALOME::SALOME_Exception& S_ex) {
227 QtCatchCorbaException(S_ex);
235 SMESH::SMESH_Hypothesis_ptr CreateHypothesis (const QString& aHypType,
236 const QString& aHypName,
237 const bool isAlgo = false)
239 if(MYDEBUG) MESSAGE("Create " << aHypType << " with name " << aHypName);
241 SMESH::SMESH_Hypothesis_var Hyp;
243 HypothesisData* aHypData = GetHypothesisData((char*)aHypType.latin1());
244 QString aServLib = aHypData->ServerLibName;
247 Hyp = SMESH::GetSMESHGen()->CreateHypothesis(aHypType, aServLib);
248 if (!Hyp->_is_nil()) {
249 SALOMEDS::SObject_var SHyp = SMESH::FindSObject(Hyp.in());
250 if (!SHyp->_is_nil()) {
251 if ( !aHypName.isEmpty() )
252 SMESH::SetName( SHyp, aHypName );
253 myActiveStudy->updateObjBrowser(true);
258 catch (const SALOME::SALOME_Exception & S_ex) {
259 QtCatchCorbaException(S_ex);
262 return SMESH::SMESH_Hypothesis::_nil();
266 bool AddHypothesisOnMesh (SMESH::SMESH_Mesh_ptr aMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
268 if(MYDEBUG) MESSAGE ("SMESHGUI::AddHypothesisOnMesh");
269 int res = SMESH::HYP_UNKNOWN_FATAL;
272 if ( !aMesh->_is_nil() ) {
273 SALOMEDS::SObject_var SM = SMESH::FindSObject( aMesh );
274 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(SM);
276 res = aMesh->AddHypothesis( aShapeObject, aHyp );
277 if ( res < SMESH::HYP_UNKNOWN_FATAL ) {
278 SALOMEDS::SObject_var SH = SMESH::FindSObject(aHyp);
279 if ( !SM->_is_nil() && !SH->_is_nil() ) {
280 SMESH::ModifiedMesh(SM, false);
283 if ( res > SMESH::HYP_OK ) {
285 processHypothesisStatus( res, aHyp, this, true );
289 catch( const SALOME::SALOME_Exception& S_ex ) {
291 QtCatchCorbaException( S_ex );
292 res = SMESH::HYP_UNKNOWN_FATAL;
295 return res < SMESH::HYP_UNKNOWN_FATAL;
299 bool AddHypothesisOnSubMesh (SMESH::SMESH_subMesh_ptr aSubMesh, SMESH::SMESH_Hypothesis_ptr aHyp)
301 if(MYDEBUG) MESSAGE( "SMESHGUI::AddHypothesisOnSubMesh() ");
302 int res = SMESH::HYP_UNKNOWN_FATAL;
305 if ( !aSubMesh->_is_nil() && ! aHyp->_is_nil() ) {
307 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
308 SALOMEDS::SObject_var SsubM = SMESH::FindSObject( aSubMesh );
309 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh( SsubM );
310 if ( !aMesh->_is_nil() && !SsubM->_is_nil() && !aShapeObject->_is_nil() ) {
311 res = aMesh->AddHypothesis( aShapeObject, aHyp );
312 if ( res < SMESH::HYP_UNKNOWN_FATAL ) {
313 SMESH::ModifiedMesh( SsubM, false );
315 if ( res > SMESH::HYP_OK ) {
317 processHypothesisStatus( res, aHyp, this, true );
322 SCRUTE( aHyp->_is_nil() );
323 SCRUTE( aMesh->_is_nil() );
324 SCRUTE( SsubM->_is_nil() );
325 SCRUTE( aShapeObject->_is_nil() );
328 catch( const SALOME::SALOME_Exception& S_ex ) {
330 QtCatchCorbaException( S_ex );
331 res = SMESH::HYP_UNKNOWN_FATAL;
335 SCRUTE( aSubMesh->_is_nil() );
336 SCRUTE( aHyp->_is_nil() );
338 return res < SMESH::HYP_UNKNOWN_FATAL;
341 bool RemoveHypothesisOrAlgorithmOnMesh (const Handle(SALOME_InteractiveObject)& IObject)
343 int res = SMESH::HYP_UNKNOWN_FATAL;
346 if (IObject->hasReference()) {
348 SMESH::SMESH_Hypothesis_var anHyp =
349 SMESH::IObjectToInterface<SMESH::SMESH_Hypothesis>(IObject);
350 SALOMEDS::SObject_var SO_Applied_Hypothesis =
351 smeshGUI->myStudy->FindObjectID(IObject->getReference());
352 if (!SO_Applied_Hypothesis->_is_nil()) {
353 SALOMEDS::SObject_var MorSM = SMESH::GetMeshOrSubmesh(SO_Applied_Hypothesis);
354 if (!MorSM->_is_nil()) {
355 GEOM::GEOM_Object_var aShape = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
356 if (!aShape->_is_nil()){
357 SMESH::SMESH_Mesh_var aMesh =
358 SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
359 SMESH::SMESH_subMesh_var aSubMesh =
360 SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
362 if (!aSubMesh->_is_nil())
363 aMesh = aSubMesh->GetFather();
365 if (!aMesh->_is_nil()) {
366 res = aMesh->RemoveHypothesis(aShape, anHyp);
367 if ( res < SMESH::HYP_UNKNOWN_FATAL )
368 SMESH::ModifiedMesh(MorSM, false);
369 if ( res > SMESH::HYP_OK ) {
371 processHypothesisStatus( res, anHyp, this, false );
378 SALOMEDS::SObject_var SO_Applied_Hypothesis =
379 smeshGUI->myStudy->FindObjectID(IObject->getReference());
380 if (!SO_Applied_Hypothesis->_is_nil()) {
381 SALOMEDS::SObject_var MorSM = SMESH::GetMeshOrSubmesh(SO_Applied_Hypothesis);
382 if (!MorSM->_is_nil()) {
383 GEOM::GEOM_Object_var aShapeObject =SMESH::GetShapeOnMeshOrSubMesh(MorSM);
384 if (!aShapeObject->_is_nil()){
385 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
386 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
388 if (!aSubMesh->_is_nil())
389 aMesh = aSubMesh->GetFather();
391 if (!aMesh->_is_nil()) {
392 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
393 if ( res < SMESH::HYP_UNKNOWN_FATAL )
394 SMESH::ModifiedMesh(MorSM, false);
395 if ( res > SMESH::HYP_OK ) {
397 processHypothesisStatus( res, anHyp, this, false );
406 catch( const SALOME::SALOME_Exception& S_ex ) {
408 QtCatchCorbaException( S_ex );
409 res = SMESH::HYP_UNKNOWN_FATAL;
412 else if (IObject->hasEntry()) {
413 if(MYDEBUG) MESSAGE("IObject entry " << IObject->getEntry());
415 return res < SMESH::HYP_UNKNOWN_FATAL;
418 bool RemoveHypothesisOrAlgorithmOnMesh (SALOMEDS::SObject_ptr MorSM,
419 SMESH::SMESH_Hypothesis_ptr anHyp)
421 SALOMEDS::SObject_var AHR, aRef;
422 SALOMEDS::GenericAttribute_var anAttr;
423 SALOMEDS::AttributeIOR_var anIOR;
424 int res = SMESH::HYP_UNKNOWN_FATAL;
427 if (!MorSM->_is_nil()) {
429 GEOM::GEOM_Object_var aShapeObject = SMESH::GetShapeOnMeshOrSubMesh(MorSM);
430 if (!aShapeObject->_is_nil()) {
431 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>(MorSM);
432 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>(MorSM);
434 if ( !aSubMesh->_is_nil() )
435 aMesh = aSubMesh->GetFather();
437 if (!aMesh->_is_nil()) {
438 res = aMesh->RemoveHypothesis(aShapeObject, anHyp);
439 if ( res < SMESH::HYP_UNKNOWN_FATAL )
440 SMESH::ModifiedMesh(MorSM, false);
441 if ( res > SMESH::HYP_OK ) {
443 processHypothesisStatus( res, anHyp, this, false );
448 } catch( const SALOME::SALOME_Exception& S_ex ) {
450 QtCatchCorbaException( S_ex );
451 res = SMESH::HYP_UNKNOWN_FATAL;
454 return res < SMESH::HYP_UNKNOWN_FATAL;
457 SALOMEDS::Study::ListOfSObject* GetMeshesUsingAlgoOrHypothesis( SMESH::SMESH_Hypothesis_ptr AlgoOrHyp )
459 SALOMEDS::Study::ListOfSObject_var listSOmesh =
460 new SALOMEDS::Study::ListOfSObject;
461 listSOmesh->length(0);
462 unsigned int index = 0;
463 if (!AlgoOrHyp->_is_nil()) {
464 SALOMEDS::SObject_var SO_Hypothesis = SMESH::FindSObject(AlgoOrHyp);
465 if (!SO_Hypothesis->_is_nil()) {
466 SALOMEDS::Study::ListOfSObject_var listSO =
467 smeshGUI->myStudy->FindDependances(SO_Hypothesis);
468 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO->length());
469 for (unsigned int i = 0; i < listSO->length(); i++) {
470 SALOMEDS::SObject_ptr SO = listSO[i];
471 if (!SO->_is_nil()) {
472 SALOMEDS::SObject_var aFather = SO->GetFather();
473 if (!aFather->_is_nil()) {
474 SALOMEDS::SObject_var SOfatherFather = aFather->GetFather();
475 if (!SOfatherFather->_is_nil()) {
476 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
478 listSOmesh->length(index);
479 listSOmesh[index - 1] = SOfatherFather;
486 if(MYDEBUG) MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
487 return listSOmesh._retn();