Salome HOME
Portation on new based dialog
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ExportMeshOp.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
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   : SMESHGUI_ExportMeshOp.cxx
25 //  Author : Alexander SOLOVYOV
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_ExportMeshOp.h"
30 #include <SMESHGUI_MeshUtils.h>
31 #include <SMESHGUI_Utils.h>
32
33 #include <SalomeApp_SelectionMgr.h>
34 #include <SUIT_FileDlg.h>
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_Desktop.h>
37 #include <SUIT_OverrideCursor.h>
38
39 #include <SALOME_ListIO.hxx>
40
41 //=================================================================================
42 // function :
43 // purpose  :
44 //=================================================================================
45 SMESHGUI_ExportMeshOp::SMESHGUI_ExportMeshOp( const Type t )
46 : SMESHGUI_Operation(),
47   myType( t )
48 {
49 }
50
51 //=================================================================================
52 // function :
53 // purpose  :
54 //=================================================================================
55 SMESHGUI_ExportMeshOp::~SMESHGUI_ExportMeshOp()
56 {
57 }
58
59 //=================================================================================
60 // function :
61 // purpose  :
62 //=================================================================================
63 void SMESHGUI_ExportMeshOp::startOperation()
64 {
65   SMESHGUI_Operation::startOperation();
66
67   SalomeApp_SelectionMgr *aSel = selectionMgr();
68   SALOME_ListIO selected;
69   if( aSel )
70     aSel->selectedObjects( selected );
71
72   if( selected.Extent()==0 )
73   {
74     abort();
75     return;
76   }
77   
78   Handle(SALOME_InteractiveObject) anIObject = selected.First();
79   SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIObject);
80   if( !aMesh->_is_nil() )
81   {
82     QString aFilter, aTitle = QObject::tr("Export mesh");
83     QMap<QString, SMESH::MED_VERSION> aFilterMap;
84     switch( myType )
85     {
86       case MED:
87         aFilterMap.insert( QObject::tr("MED 2.1 (*.med)"), SMESH::MED_V2_1 );
88         aFilterMap.insert( QObject::tr("MED 2.2 (*.med)"), SMESH::MED_V2_2 );
89         break;
90         
91       case DAT:
92         aFilter = QObject::tr("DAT files (*.dat)");
93         break;
94         
95       case UNV:
96         if( aMesh->NbPyramids() )
97         {
98           int aRet = SUIT_MessageBox::warn2( desktop(), tr("SMESH_WRN_WARNING"),
99                     tr("SMESH_EXPORT_UNV").arg(anIObject->getName()), tr("SMESH_BUT_YES"),
100                     tr("SMESH_BUT_NO"), 0,1,0 );
101           if(aRet)
102           {
103             abort();
104             return;
105           }
106         }
107         aFilter = QObject::tr("IDEAS files (*.unv)");
108         break;
109       default:
110         abort();
111         return;
112     }
113
114     QString aFilename;
115     SMESH::MED_VERSION aFormat;
116
117     if( myType!=MED )
118       aFilename = SUIT_FileDlg::getFileName( desktop(), "", aFilter, aTitle, false );
119     else
120     {
121       QStringList filters;
122       for( QMap<QString, SMESH::MED_VERSION>::const_iterator it = aFilterMap.begin(); it != aFilterMap.end(); ++it )
123         filters.push_back( it.key() );
124
125       SUIT_FileDlg* fd = new SUIT_FileDlg( desktop(), false, true, true );
126       fd->setCaption( aTitle );
127       fd->setFilters( filters );
128       bool is_ok = false;
129       while( !is_ok )
130       {
131         fd->exec();
132         aFilename = fd->selectedFile();
133         aFormat = aFilterMap[fd->selectedFilter()];
134         is_ok = true;
135         if( !aFilename.isEmpty() && (aMesh->NbPolygons()>0 or aMesh->NbPolyhedrons()>0 ) && aFormat==SMESH::MED_V2_1)
136         {
137           int aRet = SUIT_MessageBox::warn2( desktop(), tr("SMESH_WRN_WARNING"),
138                      tr("SMESH_EXPORT_MED_V2_1").arg(anIObject->getName()), tr("SMESH_BUT_YES"),
139                      tr("SMESH_BUT_NO"), 0,1,0 );
140           if(aRet)
141             is_ok = false;
142         }
143       }
144       delete fd;
145     }
146   
147     if( !aFilename.isEmpty() )
148     {
149       // Check whether the file already exists and delete it if yes
150       QFile aFile( aFilename );
151       if( aFile.exists() )
152         aFile.remove();
153       SUIT_OverrideCursor wc;
154       switch( myType )
155       {
156         case MED:
157           aMesh->ExportToMED( aFilename.latin1(), false, aFormat ); // currently, automatic groups are never created
158           break;
159         
160         case DAT:
161           aMesh->ExportDAT( aFilename.latin1() );
162           break;
163
164         case UNV:
165           aMesh->ExportUNV( aFilename.latin1() );
166           break;
167         
168         default:
169           abort();
170           return;
171       }
172       commit();
173       return;  
174     }
175   }
176   abort();
177 }