1 // Copyright (C) 2011-2014 CEA/DEN, EDF R&D
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
20 #include "HomardQtCommun.h"
21 #include "HOMARDGUI_Utils.h"
23 #include <utilities.h>
25 #include <qmessagebox.h>
26 #include <qcombobox.h>
27 #include <qfiledialog.h>
36 #include "SalomeApp_Tools.h"
45 // ============================================================================
46 QString HOMARD_QT_COMMUN::SelectionArbreEtude(QString commentaire, int option )
47 // ============================================================================
48 // Retourne l'objet selectionne dans l'arbre d'etudes
50 // . si le commentaire est une chaine vide, on ne tient pas compte du type de l'objet
51 // et on retourne le nom de cet objet
53 // . si l'objet est du type defini par commentaire, retourne le nom de cet objet
54 // . sinon on retourne une QString("")
56 // . Si option = 0, ce n'est pas grave de ne rien trouver ; aucun message n'est emis
57 // . Si option = 1, ce n'est pas grave de ne rien trouver mais on emet un message
59 // MESSAGE("SelectionArbreEtude : commentaire = " << commentaire.toStdString().c_str() << " et option = " << option);
60 int nbSel = HOMARD_UTILS::IObjectCount() ;
65 QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
66 QObject::tr("HOM_SELECT_OBJECT_1") );
72 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
73 QObject::tr("HOM_SELECT_OBJECT_2") );
77 Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
78 if ( aIO->hasEntry() )
80 // MESSAGE("aIO->getEntry() = " << aIO->getEntry());
81 _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
82 _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
83 _PTR(GenericAttribute) anAttr;
84 if (aSO->FindAttribute(anAttr, "AttributeComment") )
86 if ( commentaire != "" )
88 _PTR(AttributeComment) attributComment = anAttr;
89 QString aComment= QString(attributComment->Value().data());
90 // MESSAGE("... aComment = " << aComment.toStdString().c_str());
91 int iaux = aComment.lastIndexOf(commentaire);
92 // MESSAGE("... iaux = " << iaux);
95 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
96 QObject::tr("HOM_SELECT_OBJECT_3").arg(commentaire) );
100 if (aSO->FindAttribute(anAttr, "AttributeName") )
102 _PTR(AttributeName) attributName = anAttr;
103 QString aName= QString(attributName->Value().data());
112 // =======================================================================
113 QString HOMARD_QT_COMMUN::SelectionCasEtude()
114 // =======================================================================
116 QString aName = QString("");
117 int nbSel = HOMARD_UTILS::IObjectCount() ;
120 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
121 QObject::tr("HOM_SELECT_OBJECT_1") );
126 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
127 QObject::tr("HOM_SELECT_OBJECT_2") );
130 Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
131 if ( aIO->hasEntry() )
133 _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
134 _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
135 _PTR(SObject) aSObjCas = aSO->GetFather();
136 _PTR(GenericAttribute) anAttr;
137 if (aSObjCas->FindAttribute(anAttr, "AttributeName") )
139 _PTR(AttributeName) attributName = anAttr;
140 aName= QString(attributName->Value().data());
147 // =======================================================================
148 QString HOMARD_QT_COMMUN::PushNomFichier(bool avertir, QString TypeFichier)
149 // =======================================================================
150 // Gestion les boutons qui permettent de
151 // 1) retourne le nom d'un fichier par une fenetre de dialogue si aucun
152 // objet est selectionne dans l arbre d etude
153 // 2) retourne le nom du fichier asocie a l objet
154 // selectionne dans l arbre d etude
156 // MESSAGE("PushNomFichier avec avertir "<<avertir<<" et TypeFichier = "<<TypeFichier.toStdString().c_str());
157 QString aFile = QString::null;
162 if ( TypeFichier == "med" ) { filtre = QString("Med") ; }
163 else if ( TypeFichier == "py" ) { filtre = QString("Python") ; }
164 else { filtre = TypeFichier ; }
166 if ( TypeFichier != "" ) { filtre += QString(" files (*.") + TypeFichier + QString(");;") ; }
168 filtre += QString("all (*) ") ;
171 int nbSel = HOMARD_UTILS::IObjectCount() ;
172 // MESSAGE("nbSel ="<<nbSel);
173 // B.1. Rien n'est selectionne
176 // aFile = QFileDialog::getOpenFileName(0, QObject::tr("HOM_SELECT_FILE_0"), QString(""), QString("Med files (*.med);;all (*) ") );
177 aFile = QFileDialog::getOpenFileName(0, QObject::tr("HOM_SELECT_FILE_0"), QString(""), filtre );
179 // B.2. Un objet est selectionne
182 Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
183 if ( aIO->hasEntry() )
185 _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
186 _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
187 _PTR(GenericAttribute) anAttr;
188 _PTR(AttributeFileType) aFileType;
189 _PTR(AttributeExternalFileDef) aFileName;
191 if (aSO->FindAttribute(anAttr, "AttributeFileType") ) {
193 QString fileType=QString(aFileType->Value().data());
194 if ( fileType==QString("FICHIERMED")) {
195 if (aSO->FindAttribute(anAttr,"AttributeExternalFileDef")) {
197 aFile= QString(aFileName->Value().data()); }
203 if ( aFile==QString::null )
206 QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
207 QObject::tr("HOM_SELECT_STUDY") );
209 aFile = QFileDialog::getOpenFileName(0, QObject::tr("HOM_SELECT_FILE_0"), QString(""), filtre );
215 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
216 QObject::tr("HOM_SELECT_FILE_2") );
221 // =======================================================================
222 int HOMARD_QT_COMMUN::OuvrirFichier(QString aFile)
223 // =======================================================================
224 // renvoie le medId associe au fichier Med apres ouverture
226 med_int medIdt = MEDfileOpen(aFile.toStdString().c_str(),MED_ACC_RDONLY);
229 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
230 QObject::tr("HOM_MED_FILE_1") );
235 // ======================================================
236 QString HOMARD_QT_COMMUN::LireNomMaillage(QString aFile)
237 // ========================================================
239 QString nomMaillage = "" ;
242 while ( erreur == 0 )
244 // Ouverture du fichier
245 medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
251 med_int numberOfMeshes = MEDnMesh(medIdt) ;
252 if (numberOfMeshes == 0 )
254 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
255 QObject::tr("HOM_MED_FILE_2") );
259 if (numberOfMeshes > 1 )
261 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
262 QObject::tr("HOM_MED_FILE_3") );
267 nomMaillage = HOMARD_QT_COMMUN::LireNomMaillage(medIdt,1);
270 // Fermeture du fichier
271 if ( medIdt > 0 ) MEDfileClose(medIdt);
275 // =======================================================================
276 QString HOMARD_QT_COMMUN::LireNomMaillage(int medIdt ,int meshId)
277 // =======================================================================
279 QString NomMaillage=QString::null;
280 char meshname[MED_NAME_SIZE+1];
281 med_int spacedim,meshdim;
282 med_mesh_type meshtype;
283 char descriptionription[MED_COMMENT_SIZE+1];
284 char dtunit[MED_SNAME_SIZE+1];
285 med_sorting_type sortingtype;
287 med_axis_type axistype;
288 int naxis = MEDmeshnAxis(medIdt,1);
289 char *axisname=new char[naxis*MED_SNAME_SIZE+1];
290 char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
291 med_err aRet = MEDmeshInfo(medIdt,
305 if ( aRet < 0 ) { QMessageBox::critical( 0, QObject::tr("HOM_ERROR"), \
306 QObject::tr("HOM_MED_FILE_4") ); }
307 else { NomMaillage=QString(meshname); }
316 // =======================================================================
317 std::list<QString> HOMARD_QT_COMMUN::GetListeChamps(QString aFile)
318 // =======================================================================
320 // Il faut voir si plusieurs maillages
322 MESSAGE("GetListeChamps");
323 std::list<QString> ListeChamp ;
328 while ( erreur == 0 )
330 // Ouverture du fichier
331 SCRUTE(aFile.toStdString());
332 medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
338 // Lecture du nombre de champs
339 med_int ncha = MEDnField(medIdt) ;
342 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
343 QObject::tr("HOM_MED_FILE_5") );
347 // Lecture des caracteristiques des champs
348 for (int i=0; i< ncha; i++)
350 // Lecture du nombre de composantes
351 med_int ncomp = MEDfieldnComponent(medIdt,i+1);
352 // Lecture du type du champ, des noms des composantes et du nom de l'unite
353 char nomcha [MED_NAME_SIZE+1];
354 char meshname[MED_NAME_SIZE+1];
355 char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
356 char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
357 char dtunit[MED_SNAME_SIZE+1];
359 med_field_type typcha;
361 erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
366 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
367 QObject::tr("HOM_MED_FILE_6") );
370 ListeChamp.push_back(QString(nomcha));
374 // Fermeture du fichier
375 if ( medIdt > 0 ) MEDfileClose(medIdt);
380 // ======================================================================================
381 std::list<QString> HOMARD_QT_COMMUN::GetListeComposants(QString aFile, QString aChamp)
382 // ======================================================================================
384 MESSAGE ( "GetListeComposants pour le fichier " << aFile.toStdString().c_str());
385 MESSAGE ( "GetListeComposants pour le champ " << aChamp.toStdString().c_str());
387 std::list<QString> ListeComposants;
392 while ( erreur == 0 )
394 // Ouverture du fichier
395 SCRUTE(aFile.toStdString());
396 medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
402 // Lecture du nombre de champs
403 med_int ncha = MEDnField(medIdt) ;
406 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
407 QObject::tr("HOM_MED_FILE_5") );
411 // Lecture des caracteristiques des champs
412 for (int i=0; i< ncha; i++)
414 // Lecture du nombre de composantes
415 med_int ncomp = MEDfieldnComponent(medIdt,i+1);
416 // Lecture du type du champ, des noms des composantes et du nom de l'unite
417 char nomcha [MED_NAME_SIZE+1];
418 char meshname[MED_NAME_SIZE+1];
419 char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
420 char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
421 char dtunit[MED_SNAME_SIZE+1];
423 med_field_type typcha;
425 erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
430 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
431 QObject::tr("HOM_MED_FILE_6") );
434 // Lecture des composantes si c'est le bon champ
435 if ( QString(nomcha) == aChamp )
437 for (int j = 0; j <ncomp; j++)
439 char cible[MED_SNAME_SIZE +1];
440 strncpy(cible,comp+j*MED_SNAME_SIZE,MED_SNAME_SIZE );
441 cible[MED_SNAME_SIZE ]='\0';
442 ListeComposants.push_back(QString(cible));
447 // Sortie si c'est bon
448 if ( QString(nomcha) == aChamp ) { break ; }
452 // Fermeture du fichier
453 if ( medIdt > 0 ) MEDfileClose(medIdt);
455 return ListeComposants;