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