Salome HOME
Merge from BR_phase16 branch (09/12/09)
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ConvToQuadOp.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // SMESH SMESHGUI : GUI for SMESH component
23 // File   : SMESHGUI_ConvToQuadOp.cxx
24 // Author : Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "SMESHGUI_ConvToQuadOp.h"
28
29 #include "SMESHGUI_ConvToQuadDlg.h"
30 #include "SMESHGUI_Utils.h"
31
32 #include <SMESH_TypeFilter.hxx>
33
34 // SALOME GUI includes
35 #include <LightApp_UpdateFlags.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_OverrideCursor.h>
38 #include <SalomeApp_Tools.h>
39
40 // IDL includes
41 #include <SALOMEconfig.h>
42 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
43
44 //================================================================================
45 /*!
46  * \brief Constructor
47  *
48  * Initialize operation
49 */
50 //================================================================================
51 SMESHGUI_ConvToQuadOp::SMESHGUI_ConvToQuadOp()
52   : SMESHGUI_SelectionOp(), 
53     myDlg( 0 )
54 {
55 }
56
57 //================================================================================
58 /*!
59  * \brief Destructor
60 */
61 //================================================================================
62 SMESHGUI_ConvToQuadOp::~SMESHGUI_ConvToQuadOp()
63 {
64   if ( myDlg )
65     delete myDlg;
66 }
67
68 //================================================================================
69 /*!
70  * \brief Gets dialog of this operation
71   * \retval LightApp_Dialog* - pointer to dialog of this operation
72 */
73 //================================================================================
74 LightApp_Dialog* SMESHGUI_ConvToQuadOp::dlg() const
75 {
76   return myDlg;
77 }
78
79 //================================================================================
80 /*!
81  * \brief Creates dialog if necessary and shows it
82  *
83  * Virtual method redefined from base class called when operation is started creates
84  * dialog if necessary and shows it, activates selection
85  */
86 //================================================================================
87 void SMESHGUI_ConvToQuadOp::startOperation()
88 {
89   if( !myDlg )
90   {
91     myDlg = new SMESHGUI_ConvToQuadDlg( );
92   }
93   connect( myDlg, SIGNAL( onClicked( int ) ), SLOT( ConnectRadioButtons( int ) ) );
94
95   myHelpFileName = "convert_to_from_quadratic_mesh_page.html";
96
97   SMESHGUI_SelectionOp::startOperation();
98
99   myDlg->SetMediumNdsOnGeom( false );
100   myDlg->activateObject( 0 );
101   myDlg->show();
102
103   selectionDone();
104 }
105
106 //================================================================================
107 /*!
108  * \brief Updates dialog's look and feel
109  *
110  * Virtual method redefined from the base class updates dialog's look and feel
111  */
112 //================================================================================
113 void SMESHGUI_ConvToQuadOp::selectionDone()
114 {
115   if ( !dlg()->isVisible() )
116     return;
117
118   SMESHGUI_SelectionOp::selectionDone();
119   try
120   {
121     QString anMeshEntry = myDlg->selectedObject( 0 );
122     _PTR(SObject) pMesh = studyDS()->FindObjectID( anMeshEntry.toLatin1().data() );
123     if ( !pMesh ) return;
124
125     SMESH::SMESH_Mesh_var mesh =
126     SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );  
127
128     if( mesh->_is_nil() )
129     {
130       myDlg->SetEnabledControls( false );
131     }
132     else if( ConsistMesh( mesh ) == SMESHGUI_ConvToQuadOp::Quadratic )
133     {
134       myDlg->SetEnabledRB( 0, false );
135     }
136     else if( ConsistMesh( mesh ) == SMESHGUI_ConvToQuadOp::Linear )
137     {
138       myDlg->SetEnabledRB( 1, false );
139     }
140     else 
141     {
142       myDlg->SetEnabledControls( true );
143     }
144   }
145   catch ( const SALOME::SALOME_Exception& S_ex )
146   {
147     SalomeApp_Tools::QtCatchCorbaException( S_ex );
148   }
149   catch ( ... )
150   {
151   }
152 }
153
154 //================================================================================
155 /*!
156  * \brief Creates selection filter
157   * \param theId - identifier of current selection widget
158   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
159  *
160  * Creates selection filter in accordance with identifier of current selection widget
161  */
162 //================================================================================
163 SUIT_SelectionFilter* SMESHGUI_ConvToQuadOp::createFilter( const int theId ) const
164 {
165   if ( theId == 0 )
166     return new SMESH_TypeFilter( MESH );
167   else
168     return 0;
169 }
170
171 //================================================================================
172 /*!
173  * \brief Edits mesh
174  *
175  * Virtual slot redefined from the base class called when "Apply" button is clicked
176  */
177 //================================================================================
178 bool SMESHGUI_ConvToQuadOp::onApply()
179 {
180   SUIT_OverrideCursor aWaitCursor;
181
182   QString aMess;
183
184   QString anMeshEntry = myDlg->selectedObject( 0 );
185   _PTR(SObject) pMesh = studyDS()->FindObjectID( anMeshEntry.toLatin1().data() );
186   if ( !pMesh )
187   {
188     dlg()->show();
189     SUIT_MessageBox::warning( myDlg,
190                               tr( "SMESH_WRN_WARNING" ), tr("MESH_IS_NOT_SELECTED") );
191    
192     return false;
193   }
194
195   SMESH::SMESH_Mesh_var mesh =
196   SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );  
197
198   if( CORBA::is_nil(mesh) )
199   {
200     SUIT_MessageBox::warning( myDlg,
201                               tr( "SMESH_WRN_WARNING" ), tr("REF_IS_NULL") );
202
203     return false;
204   } 
205
206   bool aResult = false;
207
208   try
209   {
210     SMESH::SMESH_MeshEditor_var aEditor = mesh->GetMeshEditor();
211     if( !myDlg->CurrentRB() )
212     {
213       bool aParam = true;
214       if( myDlg->IsEnabledCheck() )
215         aParam = myDlg->IsMediumNdsOnGeom();
216
217       aEditor->ConvertToQuadratic( aParam );
218       aResult = true; 
219     }
220     else
221     {
222       aResult = aEditor->ConvertFromQuadratic();
223     }
224   }
225   catch ( const SALOME::SALOME_Exception& S_ex )
226   {
227     SalomeApp_Tools::QtCatchCorbaException( S_ex );
228     aResult = false;
229   }
230   catch ( ... )
231   {
232     aResult = false;
233   }
234   if( aResult )
235   {
236     update( UF_ObjBrowser | UF_Model | UF_Viewer );
237     selectionDone();
238   }
239   return aResult;
240 }
241
242 //================================================================================
243 /*! ConsistMesh
244  *  Determines, what elements this mesh contains. 
245  */
246 //================================================================================
247 SMESHGUI_ConvToQuadOp::MeshType SMESHGUI_ConvToQuadOp::ConsistMesh( const SMESH::SMESH_Mesh_var& mesh) const
248 {
249   int nbAllElem = 0, nbQEdges =0, nbQFaces =0, nbQVolum = 0;
250   int nbEdges = 0, nbFaces = 0, nbVolum = 0;
251
252   nbAllElem = (int)mesh->NbElements();
253   nbQEdges = (int)mesh->NbEdgesOfOrder(SMESH::ORDER_QUADRATIC);
254   nbQFaces = (int)mesh->NbFacesOfOrder(SMESH::ORDER_QUADRATIC);
255   nbQVolum = (int)mesh->NbVolumesOfOrder(SMESH::ORDER_QUADRATIC);
256
257   nbEdges = (int)mesh->NbEdgesOfOrder(SMESH::ORDER_LINEAR);
258   nbFaces = (int)mesh->NbFacesOfOrder(SMESH::ORDER_LINEAR);
259   nbVolum = (int)mesh->NbVolumesOfOrder(SMESH::ORDER_LINEAR);
260
261   if( nbAllElem == (nbQEdges+nbQFaces+nbQVolum) )
262     return SMESHGUI_ConvToQuadOp::Quadratic;
263   else if ( nbAllElem == (nbEdges+nbFaces+nbVolum) )
264     return SMESHGUI_ConvToQuadOp::Linear;
265   else 
266     return SMESHGUI_ConvToQuadOp::Comp;
267 }
268
269
270 void SMESHGUI_ConvToQuadOp::ConnectRadioButtons( int id )
271 {
272   QString anMeshEntry = myDlg->selectedObject( 0 );
273   _PTR(SObject) pMesh = studyDS()->FindObjectID( anMeshEntry.toLatin1().data() );
274   if ( !pMesh ) return;
275
276   SMESH::SMESH_Mesh_var mesh =
277     SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );  
278
279   GEOM::GEOM_Object_var mainGeom;
280   mainGeom = mesh->GetShapeToMesh();
281
282   if( id || mainGeom->_is_nil() )
283     myDlg->SetEnabledCheck( false );
284   else
285     myDlg->SetEnabledCheck( true );
286 }