]> SALOME platform Git repositories - modules/homard.git/blob - src/HOMARDGUI/HomardQtCommun.cxx
Salome HOME
Fix problems of 'make distcheck'
[modules/homard.git] / src / HOMARDGUI / HomardQtCommun.cxx
1 #include "HomardQtCommun.h"
2 #include "HOMARDGUI_Utils.h"
3
4 #include <utilities.h>
5
6 #include <qmessagebox.h>
7 #include <qcombobox.h>
8 #include <qfiledialog.h>
9 #include <qstring.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/stat.h>
13
14
15 #include "SalomeApp_Tools.h"
16
17 using namespace std;
18 extern "C"
19 {
20 #include <med.h>
21 }
22
23
24 // ============================================================================
25 QString HOMARD_QT_COMMUN::SelectionArbreEtude(QString commentaire, int grave )
26 // ============================================================================
27 // Regarde si l'objet selectionne correspond a un objet de tyoe
28 // commentaire. si c'est le cas, retourne le nom de  cet objet,
29 // sinon retourne une QString("")
30 // Si grave = 0, ce n'est pas grave de ne rien trouver et pas de message
31 // Si grave = 1, ce n'est pas grave de ne rien trouver mais on emet un message
32 {
33   int nbSel = HOMARD_UTILS::IObjectCount() ;
34   if ( nbSel == 0 )
35   {
36     if ( grave == 1 )
37     {
38       QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
39                                 QObject::tr("HOM_SELECT_OBJECT_1") );
40     }
41     return QString("");
42   }
43   if ( nbSel > 1 )
44   {
45     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
46                               QObject::tr("HOM_SELECT_OBJECT_2") );
47     return QString("");
48   }
49 //
50   Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
51   if ( aIO->hasEntry() )
52   {
53     _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
54     _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
55     _PTR(GenericAttribute) anAttr;
56     if (aSO->FindAttribute(anAttr, "AttributeComment") )
57     {
58       _PTR(AttributeComment) attributComment = anAttr;
59       QString aComment= QString(attributComment->Value().data());
60       int iteration = aComment.lastIndexOf(commentaire);
61       if ( iteration !=0  )
62       {
63         QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
64                                   QObject::tr("HOM_SELECT_OBJECT_3").arg(commentaire) );
65         return QString("");
66       }
67       if (aSO->FindAttribute(anAttr, "AttributeName") )
68       {
69         _PTR(AttributeName) attributName = anAttr;
70         QString aName= QString(attributName->Value().data());
71         return aName;
72       }
73     }
74   }
75 //
76   return QString("");
77 }
78
79 // =======================================================================
80 QString HOMARD_QT_COMMUN::SelectionCasEtude()
81 // =======================================================================
82 {
83   QString aName    = QString("");
84   int nbSel = HOMARD_UTILS::IObjectCount() ;
85   if ( nbSel == 0 )
86   {
87     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
88                               QObject::tr("HOM_SELECT_OBJECT_1") );
89     return QString("");
90   }
91   if ( nbSel > 1 )
92   {
93     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
94                               QObject::tr("HOM_SELECT_OBJECT_2") );
95     return QString("");
96   }
97   Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
98   if ( aIO->hasEntry() )
99   {
100         _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
101         _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
102         _PTR(SObject) aSObjCas = aSO->GetFather();
103         _PTR(GenericAttribute) anAttr;
104         if (aSObjCas->FindAttribute(anAttr, "AttributeName") )
105         {
106             _PTR(AttributeName) attributName = anAttr;
107             aName= QString(attributName->Value().data());
108         }
109         return aName;
110     }
111     return QString("");
112 }
113
114 // =======================================================================
115 QString HOMARD_QT_COMMUN::PushNomFichier(bool avertir)
116 // =======================================================================
117 // Gestion les boutons qui permettent  de
118 // 1) retourne le nom d'un fichier par une fenetre de dialogue si aucun
119 //    objet est selectionne dans l arbre d etude
120 // 2) retourne le nom du fichier asocie a l objet
121 //    selectionne dans l arbre d etude
122 {
123   MESSAGE("HOMARD_QT_COMMUN::PushNomFichier");
124   QString aFile=QString::null;
125   int nbSel = HOMARD_UTILS::IObjectCount() ;
126   if ( nbSel == 0 )
127   {
128     aFile = QFileDialog::getOpenFileName(0,QString("File Selection"),QString("") ,QString("Med files (*.med);;all (*) ") );
129   }
130   if (nbSel > 1)
131   {
132     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
133                               QObject::tr("HOM_SELECT_FILE_2") );
134   }
135   if (nbSel == 1)
136   {
137     Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
138     if ( aIO->hasEntry() )
139     {
140       _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
141       _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
142       _PTR(GenericAttribute) anAttr;
143       _PTR(AttributeFileType) aFileType;
144       _PTR(AttributeExternalFileDef) aFileName;
145       if (aSO) {
146         if (aSO->FindAttribute(anAttr, "AttributeFileType") ) {
147           aFileType=anAttr;
148           QString fileType=QString(aFileType->Value().data());
149           if ( fileType==QString("FICHIERMED")) {
150             if (aSO->FindAttribute(anAttr,"AttributeExternalFileDef")) {
151             aFileName=anAttr;
152             aFile= QString(aFileName->Value().data()); }
153           }
154         }
155       }
156     }
157
158     if ( aFile==QString::null )
159     {
160       if ( avertir ) {
161         QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
162                                 QObject::tr("HOM_SELECT_STUDY") );
163       }
164       aFile = QFileDialog::getOpenFileName();
165       if (!aFile.isEmpty())
166       {
167         aFile=aFile;
168       }
169     }
170   }
171   return aFile;
172
173 }
174
175
176 // =======================================================================
177 int HOMARD_QT_COMMUN::OuvrirFichier(QString aFile)
178 // =======================================================================
179 // renvoie le medId associe au fichier Med apres ouverture
180 {
181   med_int medIdt = MEDfileOpen(aFile.toStdString().c_str(),MED_ACC_RDONLY);
182   if (medIdt <0)
183   {
184     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
185                               QObject::tr("HOM_MED_FILE_1") );
186   }
187   return (int) medIdt;
188 }
189
190 // ======================================================
191 QString HOMARD_QT_COMMUN::LireNomMaillage(QString aFile)
192 // ========================================================
193 {
194   med_int medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
195   med_int numberOfMeshes = MEDnMesh(medIdt) ;
196   if (numberOfMeshes == 0 )
197   {
198     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
199                               QObject::tr("HOM_MED_FILE_2") );
200   }
201   if (numberOfMeshes > 1 )
202   {
203     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
204                               QObject::tr("HOM_MED_FILE_3") );
205   }
206
207   QString nomMaillage= HOMARD_QT_COMMUN::LireNomMaillage(medIdt,1);
208   MEDfileClose(medIdt);
209   return nomMaillage;
210 }
211 // =======================================================================
212 QString HOMARD_QT_COMMUN::LireNomMaillage(int medIdt ,int meshId)
213 // =======================================================================
214 {
215   QString NomMaillage=QString::null;
216   char meshname[MED_NAME_SIZE+1];
217   med_int spacedim,meshdim;
218   med_mesh_type meshtype;
219   char descriptionription[MED_COMMENT_SIZE+1];
220   char dtunit[MED_SNAME_SIZE+1];
221   med_sorting_type sortingtype;
222   med_int nstep;
223   med_axis_type axistype;
224   int naxis = MEDmeshnAxis(medIdt,1);
225   char *axisname=new char[naxis*MED_SNAME_SIZE+1];
226   char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
227   med_err aRet = MEDmeshInfo(medIdt,
228                           meshId,
229                           meshname,
230                           &spacedim,
231                           &meshdim,
232                           &meshtype,
233                           descriptionription,
234                           dtunit,
235                           &sortingtype,
236                           &nstep,
237                           &axistype,
238                           axisname,
239                           axisunit);
240
241   if ( aRet < 0 )
242   {
243   QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
244                             QObject::tr("HOM_MED_FILE_4") );
245   }
246   else
247   {
248     NomMaillage=QString(meshname);
249   }
250   return NomMaillage;
251 }
252
253
254 // =======================================================================
255 std::list<QString> HOMARD_QT_COMMUN::GetListeChamps(QString aFile)
256 // =======================================================================
257 {
258 // Il faut voir si plusieurs maillages
259
260   MESSAGE("HOMARD_QT_COMMUN::GetListeChamps");
261   std::list<QString> ListeChamp;
262
263   char *comp, *unit;
264   char nomcha  [MED_NAME_SIZE+1];
265   char meshname[MED_NAME_SIZE+1];
266   med_field_type typcha;
267   med_int ncomp;
268   med_bool local;
269   med_int nbofcstp;
270
271   SCRUTE(aFile.toStdString());
272   med_int medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
273   if ( medIdt < 0 ) { return ListeChamp; }
274
275   // Le fichier Med est lisible
276   // Lecture du maillage
277
278   // Lecture du nombre de champs
279   med_int ncha = MEDnField(medIdt) ;
280   if (ncha < 1 )
281   {
282     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
283                             QObject::tr("HOM_MED_FILE_5") );
284     MEDfileClose(medIdt);
285     return ListeChamp;
286   }
287
288   for (int i=0; i< ncha; i++)
289   {
290     /* Lecture du type du champ, des noms des composantes et du nom de l'unite*/
291     ncomp = MEDfieldnComponent(medIdt,i+1);
292     comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
293     unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
294     char dtunit[MED_SNAME_SIZE+1];
295     if ( MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) < 0 )
296     {
297       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
298                                 QObject::tr("HOM_MED_FILE_6") );
299       MEDfileClose(medIdt);
300       return ListeChamp;
301     }
302
303     ListeChamp.push_back(QString(nomcha));
304     free(comp);
305     free(unit);
306   }
307   MEDfileClose(medIdt);
308   return ListeChamp;
309 }
310
311 // ======================================================================================
312 std::list<QString> HOMARD_QT_COMMUN::GetListeComposants(QString aFile, QString aChamp)
313 // ======================================================================================
314 {
315   MESSAGE ( "GetListeComposants pour le fichier " << aFile.toStdString().c_str());
316   MESSAGE ( "GetListeComposants pour le champ " << aChamp.toStdString().c_str());
317
318   std::list<QString> ListeComposants;
319
320   char *comp, *unit;
321   char nomcha  [MED_NAME_SIZE+1];
322   char meshname[MED_NAME_SIZE+1];
323   med_field_type typcha;
324   med_int ncomp;
325   med_bool local;
326   med_int nbofcstp;
327
328   int medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
329   if ( medIdt < 0 ) { return ListeComposants; }
330
331
332   // Lecture du nombre de champs
333   med_int ncha = MEDnField(medIdt) ;
334   if (ncha < 1 )
335   {
336     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
337                             QObject::tr("HOM_MED_FILE_5") );
338     MEDfileClose(medIdt);
339     return ListeComposants;
340   }
341
342   for (int i=0; i< ncha; i++)
343   {
344     /* Lecture du type du champ, des noms des composantes et du nom de l'unite*/
345     ncomp = MEDfieldnComponent(medIdt,i+1);
346     comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
347     unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
348     char dtunit[MED_SNAME_SIZE+1];
349
350     if ( MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) < 0 )
351     {
352       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
353                                 QObject::tr("HOM_MED_FILE_6") );
354       MEDfileClose(medIdt);
355       return ListeComposants;
356     }
357     
358     if ( QString(nomcha) != aChamp ) {
359       free(comp);
360       free(unit);
361       continue;
362     }
363
364     for (int j = 0; j <ncomp; j++)
365     {
366       char cible[MED_SNAME_SIZE +1];
367       strncpy(cible,comp+j*MED_SNAME_SIZE,MED_SNAME_SIZE );
368       cible[MED_SNAME_SIZE ]='\0';
369       ListeComposants.push_back(QString(cible));
370     }
371     break;
372   }
373   free(comp);
374   free(unit);
375   MEDfileClose(medIdt);
376   return ListeComposants;
377 }