Salome HOME
sources v1.2
[modules/med.git] / src / MEDGUI / MedGUI.cxx
1 //  MED MEDGUI : MED component GUI implemetation 
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : MedGUI.cxx
25 //  Module : MED
26
27 using namespace std;
28 #include "MedGUI.h"
29
30 // SALOME Includes
31 #include "Utils_ORB_INIT.hxx"
32 #include "Utils_SINGLETON.hxx"
33 #include "utilities.h"
34
35 #include "SALOME_Selection.h"
36 #include "SALOME_InteractiveObject.hxx"
37 #include "SALOMEGUI_QtCatchCorbaException.hxx"
38
39 #include "QAD_MessageBox.h"
40 #include "QAD_Tools.h"
41 #include "QAD_FileDlg.h"
42
43 #include "SMESH_TypeFilter.hxx"
44
45 // QT Includes
46 #include <qinputdialog.h>
47
48 //VRV: porting on Qt 3.0.5
49 #if QT_VERSION >= 0x030005
50 #include <qlineedit.h>
51 #endif
52 //VRV: porting on Qt 3.0.5
53
54 static CORBA::ORB_var   _orb;
55
56 //=============================================================================
57 /*!
58  *
59  */
60 //=============================================================================
61 bool MedGUI::OnGUIEvent (int theCommandID, QAD_Desktop* parent)
62 {
63   setOrb();
64   
65   QAD_Study* myActiveStudy   = parent->getActiveStudy();
66   SALOMEDS::Study_var aStudy = myActiveStudy->getStudyDocument();
67   SALOME_NamingService* myNameService = parent->getNameService();
68
69   QString file;
70   QStringList filtersList ;     
71
72   filtersList.append( tr("MED_MEN_IMPORT_MED") );
73   filtersList.append( tr("MED_MEN_ALL_FILES") ) ;
74
75   switch (theCommandID)
76     {
77     case 4031:
78     case 9031:
79     case 931:
80       {
81         MESSAGE("command " << theCommandID << " activated");
82
83         QString myStudyName = myActiveStudy->getTitle();
84         bool ok=FALSE;
85         int myStudyId = myActiveStudy->getStudyId();
86
87         // load MED engine
88         Engines::Med_Gen_ptr medgen = InitMedGen(parent);
89
90         // Selection du Fichier
91         file = QAD_FileDlg::getFileName(parent,
92                                         "",
93                                         filtersList,
94                                         tr("MED_MEN_IMPORT"),
95                                         true);
96
97         // Selection du Maillage
98         if (!file.isEmpty() )
99           {
100             SCRUTE(file);
101             QString meshName;
102             meshName = QInputDialog::getText( QString( tr("MED_INF_MESHNAME") ), 
103                                               QString::null,
104                                               //VRV: porting on Qt 3.0.5
105 #if QT_VERSION >= 0x030005
106                                               QLineEdit::Normal,
107 #endif
108                                               //VRV: porting on Qt 3.0.5
109                                               QString::null, &ok);
110             if ( ! meshName.isEmpty())
111               {
112                   try
113                     {
114                       medgen->readMeshInFile(file.latin1(),myStudyName,meshName);
115                       if (myActiveStudy->getStudyDocument()->GetProperties()->IsLocked()) {
116                         QAD_MessageBox::warn1 ((QWidget*)QAD_Application::getDesktop(),
117                                                QObject::tr("WRN_WARNING"), 
118                                                QObject::tr("WRN_STUDY_LOCKED"),
119                                                QObject::tr("BUT_OK"));
120                       }
121                     }
122                   catch (const SALOME::SALOME_Exception & S_ex)
123                     {
124                       QtCatchCorbaException(S_ex);
125                     }
126                 }
127                 myActiveStudy->updateObjBrowser();
128           }
129         break;
130       }
131     case 4032:
132     case 9032:
133     case 932:
134       {
135         MESSAGE("command " << theCommandID << " activated");
136
137         QString myStudyName = myActiveStudy->getTitle();
138         bool ok=FALSE;
139         int myStudyId = myActiveStudy->getStudyId();
140
141         // load MED engine
142         Engines::Med_Gen_ptr medgen = InitMedGen(parent);
143
144         // Selection du Fichier
145         file = QAD_FileDlg::getFileName(parent,
146                                         "",
147                                         filtersList,
148                                         tr("MED_MEN_IMPORT"),
149                                         true);
150
151         // Selection du Maillage
152         if (!file.isEmpty() )
153           {
154             SCRUTE(file);
155             QString fieldName;
156             fieldName = QInputDialog::getText(
157                                               QString( tr("MED_INF_FIELDNAME") ), QString::null,
158                                               //VRV: porting on Qt 3.0.5
159 #if QT_VERSION >= 0x030005
160                                               QLineEdit::Normal,
161 #endif
162                                               //VRV: porting on Qt 3.0.5
163                                               QString::null, &ok);
164             if ( ! fieldName.isEmpty())
165               {
166                 try
167                   {
168                     medgen->readFieldInFile(file.latin1(),myStudyName,fieldName,-1,-1);
169                     if (myActiveStudy->getStudyDocument()->GetProperties()->IsLocked()) {
170                       QAD_MessageBox::warn1 ((QWidget*)QAD_Application::getDesktop(),
171                                              QObject::tr("WRN_WARNING"), 
172                                              QObject::tr("WRN_STUDY_LOCKED"),
173                                              QObject::tr("BUT_OK"));
174                     }
175                   }
176                 catch (const SALOME::SALOME_Exception & S_ex)
177                   {
178                     QtCatchCorbaException(S_ex);
179                   }
180
181                 myActiveStudy->updateObjBrowser();
182               }
183           }
184         break;
185       }
186     case 4033:
187     case 933:
188       {
189         MESSAGE("command " << theCommandID << " activated");
190
191         QString myStudyName = myActiveStudy->getTitle();
192         int myStudyId = myActiveStudy->getStudyId();
193
194         // load MED engine
195         Engines::Med_Gen_ptr medgen = InitMedGen(parent);
196
197         // Selection du Fichier
198         file = QAD_FileDlg::getFileName(parent,
199                                         "",
200                                         filtersList,
201                                         tr("MED_MEN_IMPORT"),
202                                         true);
203         if (!file.isEmpty() )
204           {
205             SCRUTE(file);
206             try
207               {
208 //              medgen->readStructFile(file.latin1(),myStudyName);
209                 medgen->readStructFileWithFieldType(file.latin1(),myStudyName);
210                 if (myActiveStudy->getStudyDocument()->GetProperties()->IsLocked()) {
211                   QAD_MessageBox::warn1 ((QWidget*)QAD_Application::getDesktop(),
212                                          QObject::tr("WRN_WARNING"), 
213                                          QObject::tr("WRN_STUDY_LOCKED"),
214                                          QObject::tr("BUT_OK"));
215                 }
216               }
217             catch (const SALOME::SALOME_Exception & S_ex)
218               {
219                 QtCatchCorbaException(S_ex);
220               }
221             myActiveStudy->updateObjBrowser();
222           }
223         break;
224       }
225       
226     case 934:
227       {
228         //Handle(SMESH_TypeFilter) aMeshFilter = new SMESH_TypeFilter( MESH );
229         SALOME_Selection* Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
230         //Sel->AddFilter(aMeshFilter) ;
231         
232         int nbSel = Sel->IObjectCount();
233         if ( nbSel == 1 ) 
234           {
235             SMESH::SMESH_Mesh_var aM;
236             SALOME_MED::MESH_var aMesh;
237             Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
238             if ( IObject->hasEntry() ) 
239               {
240                 SALOMEDS::SObject_var aMorSM = aStudy->FindObjectID( IObject->getEntry() );
241                 if ( !aMorSM->_is_nil() ) 
242                   {
243                     SALOMEDS::GenericAttribute_var anAttr;
244                     SALOMEDS::AttributeIOR_var     anIOR;
245                     if (aMorSM->FindAttribute(anAttr, "AttributeIOR")) 
246                       {
247                         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
248                         aMesh = SALOME_MED::MESH::_narrow( _orb->string_to_object(anIOR->Value()) );
249                         if ( aMesh->_is_nil() )
250                           {
251                             aM = SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(anIOR->Value()));
252                             if ( aM->_is_nil() )
253                               {
254                                 QAD_MessageBox::warn1
255                                   ( QAD_Application::getDesktop(),
256                                     tr ("MED_WRN_WARNING"),
257                                     tr ("MED_INF_NOTIMPL"),
258                                     tr ("MED_BUT_OK") );
259                                 break;
260                               }
261                             aMesh = aM->GetMEDMesh();
262                             if ( aMesh->_is_nil() )
263                               {
264                                 QAD_MessageBox::warn1
265                                   ( QAD_Application::getDesktop(),
266                                     tr ("MED_WRN_WARNING"),
267                                     tr ("MED_INF_NOTIMPL"),
268                                     tr ("MED_BUT_OK") );
269                                 break;
270                               }
271                           }
272                         DumpMesh( aMesh );
273                         //Sel->ClearFilters() ;
274                       }
275                     else
276                       {
277                         QAD_MessageBox::warn1
278                           ( QAD_Application::getDesktop(),
279                             tr ("MED_WRN_WARNING"),
280                             tr ("MED_INF_NOIOR"),
281                             tr ("MED_BUT_OK") );
282                         break;
283                       }
284                   }
285               }
286           } 
287         break;
288       }
289
290     case 935:
291       {
292         //Handle(SMESH_TypeFilter) aSubMeshFilter = new SMESH_TypeFilter( SUBMESH );
293         SALOME_Selection* Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
294         //Sel->AddFilter(aSubMeshFilter) ;
295         
296         int nbSel = Sel->IObjectCount();
297         if ( nbSel == 1 ) 
298           {
299             SMESH::SMESH_subMesh_var aSubM;
300             SALOME_MED::FAMILY_var aFam;
301             Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
302             if ( IObject->hasEntry() ) 
303               {
304                 SALOMEDS::SObject_var aMorSM = aStudy->FindObjectID( IObject->getEntry() );
305                 if ( !aMorSM->_is_nil() ) 
306                   {
307                     SALOMEDS::GenericAttribute_var anAttr;
308                     SALOMEDS::AttributeIOR_var     anIOR;
309                     if (aMorSM->FindAttribute(anAttr, "AttributeIOR")) 
310                       {
311                         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
312                         aSubM = SMESH::SMESH_subMesh::_narrow( _orb->string_to_object(anIOR->Value()) );
313                         if ( aSubM->_is_nil() )
314                           {
315                             aFam=SALOME_MED::FAMILY::_narrow( _orb->string_to_object(anIOR->Value()));
316                             if ( aFam->_is_nil() )
317                               {
318                                 QAD_MessageBox::warn1
319                                   ( QAD_Application::getDesktop(),
320                                     tr ("MED_WRN_WARNING"),
321                                     tr ("MED_INF_NOTIMPL"),
322                                     tr ("MED_BUT_OK") );
323                                 break;
324                               }
325                             DumpSubMesh( aFam );
326                           }
327                         else
328                           {
329                             DumpSubMesh( aSubM );
330                             //Sel->ClearFilters() ;
331                           }
332                       }
333                     else
334                       {
335                         QAD_MessageBox::warn1
336                           ( QAD_Application::getDesktop(),
337                             tr ("MED_WRN_WARNING"),
338                             tr ("MED_INFNOIOR"),
339                             tr ("MED_BUT_OK") );
340                         break;
341                       }
342                   }
343               }
344           } 
345         break;
346       }
347     }
348   return true;
349 }
350
351
352 //=============================================================================
353 /*!
354  *
355  */
356 //=============================================================================
357 bool MedGUI::OnMousePress (QMouseEvent* pe ,
358                            QAD_Desktop* parent, 
359                            QAD_StudyFrame* studyFrame)
360 {
361   MESSAGE("MedGUI::OnMousePress");
362   return true;
363 }
364  
365 //=============================================================================
366 /*!
367  *
368  */
369 //=============================================================================
370 bool MedGUI::OnMouseMove (QMouseEvent* pe ,
371                           QAD_Desktop* parent, 
372                           QAD_StudyFrame* studyFrame)
373 {
374   //   MESSAGE("MedGUI::OnMouseMouve");
375   return true;
376 }
377
378 //=============================================================================
379 /*!
380  *
381  */
382 //=============================================================================
383 bool MedGUI::OnKeyPress (QKeyEvent* pe,
384                          QAD_Desktop* parent,
385                          QAD_StudyFrame* studyFrame)
386 {
387   MESSAGE("MedGUI::OnKeyPress");
388   return true;
389 }
390
391 //=============================================================================
392 /*!
393  *
394  */
395 //=============================================================================
396 bool MedGUI::SetSettings (QAD_Desktop* parent)
397 {
398   MESSAGE("MedGUI::SetSettings");
399   return true;
400 }
401
402 //=============================================================================
403 /*!
404  *
405  */
406 //=============================================================================
407 bool MedGUI::CustomPopup ( QAD_Desktop* parent,
408                            QPopupMenu* popup,
409                            const QString & theContext,
410                            const QString & theParent,
411                            const QString & theObject )
412 {
413   MESSAGE("MedGUI::CustomPopup");
414   return true;
415 }
416
417 //=============================================================================
418 /*!
419  *
420  */
421 //=============================================================================
422 void MedGUI::ActiveStudyChanged( QAD_Desktop* parent )
423 {
424 }
425
426 //=============================================================================
427 /*!
428  *
429  */
430 //=============================================================================
431 void MedGUI::DefinePopup( QString & theContext, QString & theParent, QString & theObject )
432 {
433   theObject = "";
434   theContext = "";
435 }
436
437 //=============================================================================
438 /*!
439  *
440  */
441 //=============================================================================
442 bool MedGUI::DumpMesh( SALOME_MED::MESH_var MEDMesh)
443 {
444   
445   if ( MEDMesh->_is_nil() )
446     {
447       return false;
448     }
449
450   //SALOME_MED::MESH_var MEDMesh = aMesh->GetMEDMesh();
451   string name = MEDMesh->getName();
452   SCRUTE(name);
453
454   int dim = MEDMesh->getMeshDimension();
455   SCRUTE(dim);
456   int dim2 = MEDMesh->getSpaceDimension();
457   SCRUTE(dim2);
458
459   int k = MEDMesh->getNumberOfNodes() ;
460   SCRUTE(k);
461   Engines::double_array_var coords = MEDMesh->getCoordinates( SALOME_MED::MED_FULL_INTERLACE );
462   int i = 0;
463   int lu = 0;
464   while (lu < k ) {
465     if (dim2==3)
466       {
467         MESSAGE ( " Coordinates  X = " << coords[i] << " Y = " << coords[i+1] << " Z = " << coords[i+2] ); 
468         i = i + 3; // Only for triangles
469       }
470     else
471       {
472         MESSAGE ( " Coordinates  X = " << coords[i] << " Y = " << coords[i+1] ); 
473         i = i + 2; 
474       }
475     lu=lu+1;
476   }
477
478   int nbfam=MEDMesh->getNumberOfFamilies(SALOME_MED::MED_NODE);
479   SCRUTE(nbfam);
480   SALOME_MED::Family_array_var Families=MEDMesh->getFamilies(SALOME_MED::MED_NODE) ;
481   
482   for (k=0;  k < nbfam; k++) {
483     SCRUTE(k);
484     string nomFam=Families[k]->getName();
485     SCRUTE(nomFam);
486     int identfam=Families[k]->getIdentifier();
487     SCRUTE(identfam);
488     int nbelemnts=Families[k]->getNumberOfElements(SALOME_MED::MED_NONE);
489     SCRUTE(nbelemnts);
490     Engines::long_array_var tabnoeuds=Families[k]->getNumber(SALOME_MED::MED_NONE);
491     for (int l=0;l<tabnoeuds->length();l++)
492       SCRUTE(tabnoeuds[l]); 
493   }
494    
495   //     int famIdent = 1;
496   //     SALOME_MED::FAMILY_ptr Family=MEDMesh->getFamily(SALOME_MED::MED_NODE,1) ;
497   //     MESSAGE("ici");
498   //     string nomFam=Family->getName();
499   //     SCRUTE(nomFam);
500   //     int identfam=Family->getIdentifier();
501   //     SCRUTE(identfam);
502   //     Engines::long_array_var tabnoeuds=Family->getNumber(SALOME_MED::MED_NONE);
503   //     for (int l=0;l<tabnoeuds->length();l++)
504   //       SCRUTE(tabnoeuds[l]); 
505
506   return true;
507 }
508
509 //=============================================================================
510 /*!
511  *
512  */
513 //=============================================================================
514 bool MedGUI::DumpSubMesh( SMESH::SMESH_subMesh_ptr aSubMesh )
515 {
516   if ( aSubMesh->_is_nil() )
517     return false;
518
519   SALOME_MED::FAMILY_var Fam = aSubMesh->GetFamily();
520   if ( Fam->_is_nil() )
521     return false;
522
523   Engines::long_array_var tabnoeuds=Fam->getNumber(SALOME_MED::MED_NONE);
524   for (int l=0;l<tabnoeuds->length();l++)
525     SCRUTE(tabnoeuds[l]); 
526
527   return true;
528 }
529 //=============================================================================
530 /*!
531  *
532  */
533 //=============================================================================
534 bool MedGUI::DumpSubMesh( SALOME_MED::FAMILY_var Fam )
535 {
536
537   if ( Fam->_is_nil() )
538     return false;
539
540   Engines::long_array_var tabnoeuds=Fam->getNumber(SALOME_MED::MED_NONE);
541   for (int l=0;l<tabnoeuds->length();l++)
542     SCRUTE(tabnoeuds[l]); 
543
544   return true;
545 }
546
547 //=============================================================================
548 /*!
549  *
550  */
551 //=============================================================================
552 Engines::Med_Gen_ptr MedGUI::InitMedGen(QAD_Desktop* parent)
553 {
554   Engines::Component_var comp = 
555     parent->getEngine("FactoryServer", "Med");
556   MESSAGE("_________________________________________");
557   Engines::Med_Gen_var clr = Engines::Med_Gen::_narrow(comp);
558   ASSERT(!CORBA::is_nil(clr));
559   return clr._retn();
560 }
561
562 //=============================================================================
563 /*!
564  *
565  */
566 //=============================================================================
567 void MedGUI::setOrb()
568 {
569   try {
570     ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
571     ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
572     _orb = init( 0 , 0 );
573   } catch (...) {
574     INFOS("internal error : orb not found");
575     _orb = 0;
576   }
577   ASSERT(! CORBA::is_nil(_orb));
578 }
579
580 //=============================================================================
581 /*!
582  *
583  */
584 //=============================================================================
585 extern "C"
586 {
587   bool OnGUIEvent(int theCommandID, QAD_Desktop* parent)
588   {
589     return MedGUI::OnGUIEvent(theCommandID, parent);
590   }
591
592   bool OnKeyPress (QKeyEvent* pe,
593                    QAD_Desktop* parent, 
594                    QAD_StudyFrame* studyFrame)
595   {
596     return MedGUI::OnKeyPress (pe, parent, studyFrame);
597   }
598
599   bool OnMousePress (QMouseEvent* pe,
600                      QAD_Desktop* parent,
601                      QAD_StudyFrame* studyFrame)
602   {
603     return MedGUI::OnMousePress (pe, parent, studyFrame);
604   }
605
606   bool OnMouseMove (QMouseEvent* pe,
607                     QAD_Desktop* parent, 
608                     QAD_StudyFrame* studyFrame)
609   {
610     return MedGUI::OnMouseMove (pe, parent, studyFrame);
611   }
612
613   bool SetSettings ( QAD_Desktop* parent )
614   {
615     return MedGUI::SetSettings( parent );
616   }
617
618   bool customPopup ( QAD_Desktop* parent,
619                      QPopupMenu* popup,
620                      const QString & theContext,
621                      const QString & theParent,
622                      const QString & theObject )
623   {
624     return MedGUI::CustomPopup( parent, popup, theContext,
625                                 theParent, theObject );
626   }
627
628   void definePopup ( QString & theContext, QString & theParent, QString & theObject )
629   {
630     MedGUI::DefinePopup( theContext, theParent, theObject );
631   }
632   
633   bool activeStudyChanged ( QAD_Desktop* parent )
634   {
635     MedGUI::ActiveStudyChanged( parent );
636   }
637 }