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