1 // Copyright (C) 2011-2016 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"
42 // ============================================================================
43 QString HOMARD_QT_COMMUN::SelectionArbreEtude(QString commentaire, int option )
44 // ============================================================================
45 // Retourne l'objet selectionne dans l'arbre d'etudes
47 // . si le commentaire est une chaine vide, on ne tient pas compte du type de l'objet
48 // et on retourne le nom de cet objet
50 // . si l'objet est du type defini par commentaire, retourne le nom de cet objet
51 // . sinon on retourne une QString("")
53 // . Si option = 0, ce n'est pas grave de ne rien trouver ; aucun message n'est emis
54 // . Si option = 1, ce n'est pas grave de ne rien trouver mais on emet un message
56 // MESSAGE("SelectionArbreEtude : commentaire = " << commentaire.toStdString().c_str() << " et option = " << option);
57 int nbSel = HOMARD_UTILS::IObjectCount() ;
62 QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
63 QObject::tr("HOM_SELECT_OBJECT_1") );
69 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
70 QObject::tr("HOM_SELECT_OBJECT_2") );
74 Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
75 if ( aIO->hasEntry() )
77 // MESSAGE("aIO->getEntry() = " << aIO->getEntry());
78 _PTR(Study) aStudy = HOMARD_UTILS::getStudy();
79 _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
80 _PTR(GenericAttribute) anAttr;
81 if (aSO->FindAttribute(anAttr, "AttributeComment") )
83 if ( commentaire != "" )
85 _PTR(AttributeComment) attributComment = anAttr;
86 QString aComment= QString(attributComment->Value().data());
87 // MESSAGE("... aComment = " << aComment.toStdString().c_str());
88 int iaux = aComment.lastIndexOf(commentaire);
89 // MESSAGE("... iaux = " << iaux);
92 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
93 QObject::tr("HOM_SELECT_OBJECT_3").arg(commentaire) );
97 if (aSO->FindAttribute(anAttr, "AttributeName") )
99 _PTR(AttributeName) attributName = anAttr;
100 QString aName= QString(attributName->Value().data());
109 // =======================================================================
110 QString HOMARD_QT_COMMUN::SelectionCasEtude()
111 // =======================================================================
113 QString aName = QString("");
114 int nbSel = HOMARD_UTILS::IObjectCount() ;
117 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
118 QObject::tr("HOM_SELECT_OBJECT_1") );
123 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
124 QObject::tr("HOM_SELECT_OBJECT_2") );
127 Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
128 if ( aIO->hasEntry() )
130 _PTR(Study) aStudy = HOMARD_UTILS::getStudy();
131 _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
132 _PTR(SObject) aSObjCas = aSO->GetFather();
133 _PTR(GenericAttribute) anAttr;
134 if (aSObjCas->FindAttribute(anAttr, "AttributeName") )
136 _PTR(AttributeName) attributName = anAttr;
137 aName= QString(attributName->Value().data());
144 // =======================================================================
145 QString HOMARD_QT_COMMUN::PushNomFichier(bool avertir, QString TypeFichier)
146 // =======================================================================
147 // Gestion les boutons qui permettent de
148 // 1) retourne le nom d'un fichier par une fenetre de dialogue si aucun
149 // objet est selectionne dans l arbre d etude
150 // 2) retourne le nom du fichier asocie a l objet
151 // selectionne dans l arbre d etude
153 // MESSAGE("PushNomFichier avec avertir "<<avertir<<" et TypeFichier = "<<TypeFichier.toStdString().c_str());
154 QString aFile = QString::null;
159 if ( TypeFichier == "med" ) { filtre = QString("Med") ; }
160 else if ( TypeFichier == "py" ) { filtre = QString("Python") ; }
161 else { filtre = TypeFichier ; }
163 if ( TypeFichier != "" ) { filtre += QString(" files (*.") + TypeFichier + QString(");;") ; }
165 filtre += QString("all (*) ") ;
168 int nbSel = HOMARD_UTILS::IObjectCount() ;
169 // MESSAGE("nbSel ="<<nbSel);
170 // B.1. Rien n'est selectionne
173 // aFile = QFileDialog::getOpenFileName(0, QObject::tr("HOM_SELECT_FILE_0"), QString(""), QString("Med files (*.med);;all (*) ") );
174 aFile = QFileDialog::getOpenFileName(0, QObject::tr("HOM_SELECT_FILE_0"), QString(""), filtre );
176 // B.2. Un objet est selectionne
179 Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
180 if ( aIO->hasEntry() )
182 _PTR(Study) aStudy = HOMARD_UTILS::getStudy();
183 _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
184 _PTR(GenericAttribute) anAttr;
185 _PTR(AttributeFileType) aFileType;
186 _PTR(AttributeExternalFileDef) aFileName;
188 if (aSO->FindAttribute(anAttr, "AttributeFileType") ) {
190 QString fileType=QString(aFileType->Value().data());
191 if ( fileType==QString("FICHIERMED")) {
192 if (aSO->FindAttribute(anAttr,"AttributeExternalFileDef")) {
194 aFile= QString(aFileName->Value().data()); }
200 if ( aFile==QString::null )
203 QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
204 QObject::tr("HOM_SELECT_STUDY") );
206 aFile = QFileDialog::getOpenFileName(0, QObject::tr("HOM_SELECT_FILE_0"), QString(""), filtre );
212 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
213 QObject::tr("HOM_SELECT_FILE_2") );
218 // =======================================================================
219 med_idt HOMARD_QT_COMMUN::OuvrirFichier(QString aFile)
220 // =======================================================================
221 // renvoie le medId associe au fichier Med apres ouverture
223 med_idt medIdt = MEDfileOpen(aFile.toStdString().c_str(),MED_ACC_RDONLY);
226 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
227 QObject::tr("HOM_MED_FILE_1") );
232 // ======================================================
233 QString HOMARD_QT_COMMUN::LireNomMaillage(QString aFile)
234 // ========================================================
236 QString nomMaillage = "" ;
239 while ( erreur == 0 )
241 // Ouverture du fichier
242 medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
248 med_int numberOfMeshes = MEDnMesh(medIdt) ;
249 if (numberOfMeshes == 0 )
251 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
252 QObject::tr("HOM_MED_FILE_2") );
256 if (numberOfMeshes > 1 )
258 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
259 QObject::tr("HOM_MED_FILE_3") );
264 nomMaillage = HOMARD_QT_COMMUN::LireNomMaillage(medIdt,1);
267 // Fermeture du fichier
268 if ( medIdt > 0 ) MEDfileClose(medIdt);
272 // =======================================================================
273 QString HOMARD_QT_COMMUN::LireNomMaillage2(med_idt medIdt ,int meshId)
274 // =======================================================================
276 QString NomMaillage=QString::null;
277 char meshname[MED_NAME_SIZE+1];
278 med_int spacedim,meshdim;
279 med_mesh_type meshtype;
280 char descriptionription[MED_COMMENT_SIZE+1];
281 char dtunit[MED_SNAME_SIZE+1];
282 med_sorting_type sortingtype;
284 med_axis_type axistype;
285 int naxis = MEDmeshnAxis(medIdt,1);
286 char *axisname=new char[naxis*MED_SNAME_SIZE+1];
287 char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
288 med_err aRet = MEDmeshInfo(medIdt,
302 if ( aRet < 0 ) { QMessageBox::critical( 0, QObject::tr("HOM_ERROR"), \
303 QObject::tr("HOM_MED_FILE_4") ); }
304 else { NomMaillage=QString(meshname); }
313 // =======================================================================
314 std::list<QString> HOMARD_QT_COMMUN::GetListeChamps(QString aFile)
315 // =======================================================================
317 // Il faut voir si plusieurs maillages
319 MESSAGE("GetListeChamps");
320 std::list<QString> ListeChamp ;
325 while ( erreur == 0 )
327 // Ouverture du fichier
328 SCRUTE(aFile.toStdString());
329 medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
335 // Lecture du nombre de champs
336 med_int ncha = MEDnField(medIdt) ;
339 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
340 QObject::tr("HOM_MED_FILE_5") );
344 // Lecture des caracteristiques des champs
345 for (int i=0; i< ncha; i++)
347 // Lecture du nombre de composantes
348 med_int ncomp = MEDfieldnComponent(medIdt,i+1);
349 // Lecture du type du champ, des noms des composantes et du nom de l'unite
350 char nomcha [MED_NAME_SIZE+1];
351 char meshname[MED_NAME_SIZE+1];
352 char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
353 char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
354 char dtunit[MED_SNAME_SIZE+1];
356 med_field_type typcha;
358 erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
363 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
364 QObject::tr("HOM_MED_FILE_6") );
367 ListeChamp.push_back(QString(nomcha));
371 // Fermeture du fichier
372 if ( medIdt > 0 ) MEDfileClose(medIdt);
377 // ======================================================================================
378 std::list<QString> HOMARD_QT_COMMUN::GetListeComposants(QString aFile, QString aChamp)
379 // ======================================================================================
381 MESSAGE ( "GetListeComposants pour le fichier " << aFile.toStdString().c_str());
382 MESSAGE ( "GetListeComposants pour le champ " << aChamp.toStdString().c_str());
384 std::list<QString> ListeComposants;
389 while ( erreur == 0 )
391 // Ouverture du fichier
392 SCRUTE(aFile.toStdString());
393 medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
399 // Lecture du nombre de champs
400 med_int ncha = MEDnField(medIdt) ;
403 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
404 QObject::tr("HOM_MED_FILE_5") );
408 // Lecture des caracteristiques des champs
409 for (int i=0; i< ncha; i++)
411 // Lecture du nombre de composantes
412 med_int ncomp = MEDfieldnComponent(medIdt,i+1);
413 // Lecture du type du champ, des noms des composantes et du nom de l'unite
414 char nomcha [MED_NAME_SIZE+1];
415 char meshname[MED_NAME_SIZE+1];
416 char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
417 char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
418 char dtunit[MED_SNAME_SIZE+1];
420 med_field_type typcha;
422 erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
427 QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
428 QObject::tr("HOM_MED_FILE_6") );
431 // Lecture des composantes si c'est le bon champ
432 if ( QString(nomcha) == aChamp )
434 for (int j = 0; j <ncomp; j++)
436 char cible[MED_SNAME_SIZE +1];
437 strncpy(cible,comp+j*MED_SNAME_SIZE,MED_SNAME_SIZE );
438 cible[MED_SNAME_SIZE ]='\0';
439 ListeComposants.push_back(QString(cible));
444 // Sortie si c'est bon
445 if ( QString(nomcha) == aChamp ) { break ; }
449 // Fermeture du fichier
450 if ( medIdt > 0 ) MEDfileClose(medIdt);
452 return ListeComposants;