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