1 // Copyright (C) 2011-2023 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "SMESHGUI_HomardUtils.h"
23 #include <utilities.h>
25 #include "OB_Browser.h"
27 #include "SUIT_Desktop.h"
28 #include "SUIT_Application.h"
29 #include "SUIT_Session.h"
31 #include "LightApp_SelectionMgr.h"
32 #include "SalomeApp_Application.h"
33 #include "SalomeApp_Module.h"
34 #include "SalomeApp_Study.h"
36 #include "SALOME_ListIO.hxx"
38 #include "SALOMEconfig.h"
41 #include <qmessagebox.h>
42 #include <qcombobox.h>
43 #include <qfiledialog.h>
46 #include <qstringlist.h>
54 #include "SalomeApp_Tools.h"
58 SALOME_ListIO SMESH_HOMARD_UTILS::mySelected;
60 //================================================================
61 // Function : GetActiveStudy
62 // Returne un pointeur sur l'etude active
63 //================================================================
64 SUIT_Study* SMESH_HOMARD_UTILS::GetActiveStudy()
66 SUIT_Application* app = SUIT_Session::session()->activeApplication();
68 return app->activeStudy();
73 //================================================================
74 // Function : getStudy
75 // Returne un pointeur sur l'etude active
76 //================================================================
77 _PTR(Study) SMESH_HOMARD_UTILS::getStudy()
79 static _PTR(Study) _study;
81 _study = SalomeApp_Application::getStudy();
85 //================================================================
86 // Function : updateObjBrowser
87 // Purpose : met a jour l arbre d 'etude pour Homard
88 //================================================================
89 void SMESH_HOMARD_UTILS::updateObjBrowser()
91 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
93 // Le nom identifiant doit etre la valeur du parametre
94 // name de la section HOMARD du fichier SalomeApp.xml
95 CAM_Module* module = app->module("Homard" );
96 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
98 app->updateObjectBrowser();
99 appMod->updateObjBrowser( true );
102 MESSAGE( "--- SMESHHOMARD::updateObjBrowser: appMod = NULL");
106 //================================================================
107 // Function : selectedIO
108 // Return the list of selected SALOME_InteractiveObject's
109 //================================================================
110 const SALOME_ListIO& SMESH_HOMARD_UTILS::selectedIO()
112 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* > ( SUIT_Session::session()->activeApplication() );
113 LightApp_SelectionMgr* aSelectionMgr = app->selectionMgr();
116 aSelectionMgr->selectedObjects( mySelected );
117 for (SALOME_ListIteratorOfListIO it (mySelected); it.More(); it.Next())
118 SCRUTE(it.Value()->getEntry());
123 //================================================================
124 // Function : IObjectCount
125 // Return the number of selected objects
126 //================================================================
127 int SMESH_HOMARD_UTILS::IObjectCount()
129 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
130 LightApp_SelectionMgr* aSelectionMgr = app->selectionMgr();
133 aSelectionMgr->selectedObjects( mySelected );
134 SCRUTE(mySelected.Extent());
135 return mySelected.Extent();
140 //================================================================
141 // Function : firstIObject
142 // Purpose : Return the first selected object in the selected object list
143 //================================================================
144 Handle(SALOME_InteractiveObject) SMESH_HOMARD_UTILS::firstIObject()
146 const SALOME_ListIO& aList = selectedIO();
147 return aList.Extent() > 0 ? aList.First() : Handle(SALOME_InteractiveObject)();
150 //================================================================
151 // Function : lastIObject
152 // Return the last selected object in the selected object list
153 //================================================================
154 Handle(SALOME_InteractiveObject) SMESH_HOMARD_UTILS::lastIObject()
156 const SALOME_ListIO& aList = selectedIO();
157 return aList.Extent() > 0 ? aList.Last() : Handle(SALOME_InteractiveObject)();
160 //================================================================
161 // Retourne vrai si l'objet est du type voulu
162 // . Dans le cas d'un cas, d'une hypothese, d'une zone, on se contente
163 // d'une comparaison simple entre le type stocke et TypeObject.
164 // . Pour l'iteration, le type stocke en attribut est sous la forme
165 // "IterationHomard" + le nom de l'iteration precedente.
166 // Il faut donc regarder si la chaine commence par TypeObject.
167 // . Idem pour le fichier de messages : "Mess " + numero d'iteration
169 // On filtre ce genre de situation avec option :
170 // -1 : comparaison totale
171 // 0 : comparaison sur le debut de la chaine
172 //================================================================
173 bool SMESH_HOMARD_UTILS::isObject(_PTR(SObject) MonObj, QString TypeObject, int option)
175 // MESSAGE("isObject, TypeObject = "<<TypeObject.toStdString().c_str()<<", option = "<<option);
176 // Existence d'un attribut ?
177 _PTR(GenericAttribute) anAttr;
178 if ( !MonObj->FindAttribute(anAttr, "AttributeComment") ) return false;
180 _PTR(AttributeComment) aComment (anAttr);
181 QString Type = QString(aComment->Value().c_str());
182 // MESSAGE("Type = "<<Type.toStdString().c_str());
187 int position = Type.lastIndexOf(TypeObject);
188 // MESSAGE("position = "<<position);
189 if ( position == 0 ) { bOK = true ; }
193 if ( Type == TypeObject ) { bOK = true ; }
197 //================================================================
198 // Retourne vrai si l'objet est une frontiere CAO
199 //================================================================
200 bool SMESH_HOMARD_UTILS::isBoundaryCAO(_PTR(SObject) MonObj)
202 return isObject( MonObj, QString("BoundaryCAOHomard"), -1 ) ;
204 //================================================================
205 // Retourne vrai si l'objet est une frontiere analytique
206 //================================================================
207 bool SMESH_HOMARD_UTILS::isBoundaryAn(_PTR(SObject) MonObj)
209 return isObject( MonObj, QString("BoundaryAnHomard"), -1 ) ;
211 //================================================================
212 // Retourne vrai si l'objet est une frontiere discrete
213 //================================================================
214 bool SMESH_HOMARD_UTILS::isBoundaryDi(_PTR(SObject) MonObj)
216 return isObject( MonObj, QString("BoundaryDiHomard"), -1 ) ;
218 //================================================================
219 // Retourne vrai si l'objet est un cas
220 //================================================================
221 bool SMESH_HOMARD_UTILS::isCase(_PTR(SObject) MonObj)
223 return isObject( MonObj, QString("CasHomard"), -1 ) ;
225 //================================================================
226 // Retourne vrai si l'objet est une Hypothese
227 //================================================================
228 bool SMESH_HOMARD_UTILS::isHypo(_PTR(SObject) MonObj)
230 return isObject( MonObj, QString("HypoHomard"), -1 ) ;
232 //================================================================
233 // Retourne vrai si l'objet est une iteration
234 //================================================================
235 bool SMESH_HOMARD_UTILS::isIter(_PTR(SObject) MonObj)
237 return isObject( MonObj, QString("IterationHomard"), 0 ) ;
239 //================================================================
240 // Retourne vrai si l'objet est un fichier de type TypeFile
241 //================================================================
242 bool SMESH_HOMARD_UTILS::isFileType(_PTR(SObject) MonObj, QString TypeFile)
244 return isObject( MonObj, TypeFile, 0 ) ;
247 // ============================================================================
248 QString SMESH_HOMARD_QT_COMMUN::SelectionArbreEtude(QString commentaire, int option )
249 // ============================================================================
250 // Retourne l'objet selectionne dans l'arbre d'etudes
252 // . si le commentaire est une chaine vide, on ne tient pas compte du type de l'objet
253 // et on retourne le nom de cet objet
255 // . si l'objet est du type defini par commentaire, retourne le nom de cet objet
256 // . sinon on retourne une QString("")
258 // . Si option = 0, ce n'est pas grave de ne rien trouver ; aucun message n'est emis
259 // . Si option = 1, ce n'est pas grave de ne rien trouver mais on emet un message
261 // MESSAGE("SelectionArbreEtude : commentaire = " << commentaire.toStdString().c_str() << " et option = " << option);
262 int nbSel = SMESH_HOMARD_UTILS::IObjectCount() ;
267 QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
268 QObject::tr("HOM_SELECT_OBJECT_1") );
274 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
275 QObject::tr("HOM_SELECT_OBJECT_2") );
279 Handle(SALOME_InteractiveObject) aIO = SMESH_HOMARD_UTILS::firstIObject();
280 if ( aIO->hasEntry() )
282 // MESSAGE("aIO->getEntry() = " << aIO->getEntry());
283 _PTR(Study) aStudy = SMESH_HOMARD_UTILS::getStudy();
284 _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
285 _PTR(GenericAttribute) anAttr;
286 if (aSO->FindAttribute(anAttr, "AttributeComment") )
288 if ( commentaire != "" )
290 _PTR(AttributeComment) attributComment = anAttr;
291 QString aComment= QString(attributComment->Value().data());
292 // MESSAGE("... aComment = " << aComment.toStdString().c_str());
293 int iaux = aComment.lastIndexOf(commentaire);
294 // MESSAGE("... iaux = " << iaux);
297 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
298 QObject::tr("HOM_SELECT_OBJECT_3").arg(commentaire) );
302 if (aSO->FindAttribute(anAttr, "AttributeName") )
304 _PTR(AttributeName) attributName = anAttr;
305 QString aName= QString(attributName->Value().data());
314 // =======================================================================
315 QString SMESH_HOMARD_QT_COMMUN::SelectionCasEtude()
316 // =======================================================================
318 QString aName = QString("");
319 int nbSel = SMESH_HOMARD_UTILS::IObjectCount() ;
322 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
323 QObject::tr("HOM_SELECT_OBJECT_1") );
328 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
329 QObject::tr("HOM_SELECT_OBJECT_2") );
332 Handle(SALOME_InteractiveObject) aIO = SMESH_HOMARD_UTILS::firstIObject();
333 if ( aIO->hasEntry() )
335 _PTR(Study) aStudy = SMESH_HOMARD_UTILS::getStudy();
336 _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
337 _PTR(SObject) aSObjCas = aSO->GetFather();
338 _PTR(GenericAttribute) anAttr;
339 if (aSObjCas->FindAttribute(anAttr, "AttributeName") )
341 _PTR(AttributeName) attributName = anAttr;
342 aName= QString(attributName->Value().data());
349 // =======================================================================
350 QString SMESH_HOMARD_QT_COMMUN::PushNomFichier(bool avertir, QString TypeFichier)
351 // =======================================================================
352 // Gestion les boutons qui permettent de
353 // 1) retourne le nom d'un fichier par une fenetre de dialogue si aucun
354 // objet est selectionne dans l arbre d etude
355 // 2) retourne le nom du fichier asocie a l objet
356 // selectionne dans l arbre d etude
358 // MESSAGE("PushNomFichier avec avertir "<<avertir<<" et TypeFichier = "<<TypeFichier.toStdString().c_str());
359 QString aFile = QString::null;
364 if ( TypeFichier == "med" ) { filtre = QString("Med") ; }
365 else if ( TypeFichier == "py" ) { filtre = QString("Python") ; }
366 else { filtre = TypeFichier ; }
368 if ( TypeFichier != "" ) { filtre += QString(" files (*.") + TypeFichier + QString(");;") ; }
370 filtre += QString("all (*) ") ;
373 //int nbSel = SMESH_HOMARD_UTILS::IObjectCount() ;
375 // MESSAGE("nbSel ="<<nbSel);
376 // B.1. Rien n'est selectionne
379 //aFile = QFileDialog::getOpenFileName(0, QObject::tr("HOM_SELECT_FILE_0"), QString(""), QString("Med files (*.med);;all (*) ") );
380 aFile = QFileDialog::getOpenFileName(0, QObject::tr("HOM_SELECT_FILE_0"), QString(""), filtre );
382 // B.2. Un objet est selectionne
385 Handle(SALOME_InteractiveObject) aIO = SMESH_HOMARD_UTILS::firstIObject();
386 if ( aIO->hasEntry() )
388 _PTR(Study) aStudy = SMESH_HOMARD_UTILS::getStudy();
389 _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
390 _PTR(GenericAttribute) anAttr;
391 _PTR(AttributeFileType) aFileType;
392 _PTR(AttributeExternalFileDef) aFileName;
394 if (aSO->FindAttribute(anAttr, "AttributeFileType") ) {
396 QString fileType=QString(aFileType->Value().data());
397 if ( fileType==QString("FICHIERMED")) {
398 if (aSO->FindAttribute(anAttr,"AttributeExternalFileDef")) {
400 aFile= QString(aFileName->Value().data()); }
406 if ( aFile==QString::null )
409 QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
410 QObject::tr("HOM_SELECT_STUDY") );
412 aFile = QFileDialog::getOpenFileName(0, QObject::tr("HOM_SELECT_FILE_0"), QString(""), filtre );
418 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
419 QObject::tr("HOM_SELECT_FILE_2") );
424 // =======================================================================
425 med_idt SMESH_HOMARD_QT_COMMUN::OuvrirFichier(QString aFile)
426 // =======================================================================
427 // renvoie le medId associe au fichier Med apres ouverture
429 med_idt medIdt = MEDfileOpen(aFile.toStdString().c_str(),MED_ACC_RDONLY);
432 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
433 QObject::tr("HOM_MED_FILE_1") );
438 // ======================================================
439 QString SMESH_HOMARD_QT_COMMUN::LireNomMaillage(QString aFile)
440 // ========================================================
442 QString nomMaillage = "" ;
445 while ( erreur == 0 )
447 // Ouverture du fichier
448 medIdt = SMESH_HOMARD_QT_COMMUN::OuvrirFichier(aFile);
454 med_int numberOfMeshes = MEDnMesh(medIdt) ;
455 if (numberOfMeshes == 0 )
457 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
458 QObject::tr("HOM_MED_FILE_2") );
462 if (numberOfMeshes > 1 )
464 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
465 QObject::tr("HOM_MED_FILE_3") );
470 nomMaillage = SMESH_HOMARD_QT_COMMUN::LireNomMaillage2(medIdt,1);
473 // Fermeture du fichier
474 if ( medIdt > 0 ) MEDfileClose(medIdt);
478 // =======================================================================
479 QString SMESH_HOMARD_QT_COMMUN::LireNomMaillage2(med_idt medIdt ,int meshId)
480 // =======================================================================
482 QString NomMaillage=QString::null;
483 char meshname[MED_NAME_SIZE+1];
484 med_int spacedim,meshdim;
485 med_mesh_type meshtype;
486 char descriptionription[MED_COMMENT_SIZE+1];
487 char dtunit[MED_SNAME_SIZE+1];
488 med_sorting_type sortingtype;
490 med_axis_type axistype;
491 int naxis = MEDmeshnAxis(medIdt,1);
492 char *axisname=new char[naxis*MED_SNAME_SIZE+1];
493 char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
494 med_err aRet = MEDmeshInfo(medIdt,
508 if ( aRet < 0 ) { QMessageBox::critical( 0, QObject::tr("HOM_ERROR"), \
509 QObject::tr("HOM_MED_FILE_4") ); }
510 else { NomMaillage=QString(meshname); }
519 // =======================================================================
520 std::list<QString> SMESH_HOMARD_QT_COMMUN::GetListeChamps(QString aFile)
521 // =======================================================================
523 // Il faut voir si plusieurs maillages
525 MESSAGE("GetListeChamps");
526 std::list<QString> ListeChamp ;
531 while ( erreur == 0 )
533 // Ouverture du fichier
534 SCRUTE(aFile.toStdString());
535 medIdt = SMESH_HOMARD_QT_COMMUN::OuvrirFichier(aFile);
541 // Lecture du nombre de champs
542 med_int ncha = MEDnField(medIdt) ;
545 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
546 QObject::tr("HOM_MED_FILE_5") );
550 // Lecture des caracteristiques des champs
551 for (int i=0; i< ncha; i++)
553 // Lecture du nombre de composantes
554 med_int ncomp = MEDfieldnComponent(medIdt,i+1);
555 // Lecture du type du champ, des noms des composantes et du nom de l'unite
556 char nomcha [MED_NAME_SIZE+1];
557 char meshname[MED_NAME_SIZE+1];
558 char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
559 char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
560 char dtunit[MED_SNAME_SIZE+1];
562 med_field_type typcha;
564 erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
569 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
570 QObject::tr("HOM_MED_FILE_6") );
573 ListeChamp.push_back(QString(nomcha));
577 // Fermeture du fichier
578 if ( medIdt > 0 ) MEDfileClose(medIdt);
583 // ======================================================================================
584 std::list<QString> SMESH_HOMARD_QT_COMMUN::GetListeComposants(QString aFile, QString aChamp)
585 // ======================================================================================
587 MESSAGE ( "GetListeComposants pour le fichier " << aFile.toStdString().c_str());
588 MESSAGE ( "GetListeComposants pour le champ " << aChamp.toStdString().c_str());
590 std::list<QString> ListeComposants;
595 while ( erreur == 0 )
597 // Ouverture du fichier
598 SCRUTE(aFile.toStdString());
599 medIdt = SMESH_HOMARD_QT_COMMUN::OuvrirFichier(aFile);
605 // Lecture du nombre de champs
606 med_int ncha = MEDnField(medIdt) ;
609 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
610 QObject::tr("HOM_MED_FILE_5") );
614 // Lecture des caracteristiques des champs
615 for (int i=0; i< ncha; i++)
617 // Lecture du nombre de composantes
618 med_int ncomp = MEDfieldnComponent(medIdt,i+1);
619 // Lecture du type du champ, des noms des composantes et du nom de l'unite
620 char nomcha [MED_NAME_SIZE+1];
621 char meshname[MED_NAME_SIZE+1];
622 char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
623 char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
624 char dtunit[MED_SNAME_SIZE+1];
626 med_field_type typcha;
628 erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
633 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
634 QObject::tr("HOM_MED_FILE_6") );
637 // Lecture des composantes si c'est le bon champ
638 if ( QString(nomcha) == aChamp )
640 for (int j = 0; j <ncomp; j++)
642 char cible[MED_SNAME_SIZE +1];
643 strncpy(cible,comp+j*MED_SNAME_SIZE,MED_SNAME_SIZE );
644 cible[MED_SNAME_SIZE ]='\0';
645 ListeComposants.push_back(QString(cible));
650 // Sortie si c'est bon
651 if ( QString(nomcha) == aChamp ) { break ; }
655 // Fermeture du fichier
656 if ( medIdt > 0 ) MEDfileClose(medIdt);
658 return ListeComposants;