Salome HOME
NPAL16716. Compound: To create the groups of initial meshes.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ConvToQuadOp.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 /**
21 *  SMESH SMESHGUI
22 *
23 *  Copyright (C) 2005  CEA/DEN, EDF R&D
24 *
25 *
26 *
27 *  File   : SMESHGUI_ConvToQuadOp.h
28 *  Module : SMESHGUI
29 */
30
31 #include "SMESHGUI_ConvToQuadOp.h"
32 #include "SMESHGUI_ConvToQuadDlg.h"
33
34 #include "SMESHGUI.h"
35 #include "SMESHGUI_Utils.h"
36
37 #include "SMESH_TypeFilter.hxx"
38
39 #include "SalomeApp_Tools.h"
40
41 #include "SUIT_MessageBox.h"
42
43 #include "LightApp_UpdateFlags.h"
44        
45 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
46
47 //================================================================================
48 /*!
49  * \brief Constructor
50  *
51  * Initialize operation
52 */
53 //================================================================================
54 SMESHGUI_ConvToQuadOp::SMESHGUI_ConvToQuadOp()
55   : SMESHGUI_SelectionOp(), 
56     myDlg( 0 )
57 {
58 }
59
60 //================================================================================
61 /*!
62  * \brief Destructor
63 */
64 //================================================================================
65 SMESHGUI_ConvToQuadOp::~SMESHGUI_ConvToQuadOp()
66 {
67   if ( myDlg )
68     delete myDlg;
69 }
70
71 //================================================================================
72 /*!
73  * \brief Gets dialog of this operation
74   * \retval LightApp_Dialog* - pointer to dialog of this operation
75 */
76 //================================================================================
77 LightApp_Dialog* SMESHGUI_ConvToQuadOp::dlg() const
78 {
79   return myDlg;
80 }
81
82 //================================================================================
83 /*!
84  * \brief Creates dialog if necessary and shows it
85  *
86  * Virtual method redefined from base class called when operation is started creates
87  * dialog if necessary and shows it, activates selection
88  */
89 //================================================================================
90 void SMESHGUI_ConvToQuadOp::startOperation()
91 {
92   if( !myDlg )
93   {
94     myDlg = new SMESHGUI_ConvToQuadDlg( );
95   }
96   connect( myDlg, SIGNAL( onClicked( int ) ), SLOT( ConnectRadioButtons( int ) ) );
97
98   myHelpFileName = "convert_to_from_quadratic_mesh_page.html";
99
100   SMESHGUI_SelectionOp::startOperation();
101
102   myDlg->SetMediumNdsOnGeom( false );
103   myDlg->activateObject( 0 );
104   myDlg->show();
105
106   selectionDone();
107 }
108
109 //================================================================================
110 /*!
111  * \brief Updates dialog's look and feel
112  *
113  * Virtual method redefined from the base class updates dialog's look and feel
114  */
115 //================================================================================
116 void SMESHGUI_ConvToQuadOp::selectionDone()
117 {
118   if ( !dlg()->isShown() )
119     return;
120
121   SMESHGUI_SelectionOp::selectionDone();
122   try
123   {
124     QString anMeshEntry = myDlg->selectedObject( 0 );
125     _PTR(SObject) pMesh = studyDS()->FindObjectID( anMeshEntry.latin1() );
126     if ( !pMesh ) return;
127
128     SMESH::SMESH_Mesh_var mesh =
129     SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );  
130
131     if( mesh->_is_nil() )
132     {
133       myDlg->SetEnabledControls( false );
134     }
135     else if( ConsistMesh( mesh ) == SMESHGUI_ConvToQuadOp::Quadratic )
136     {
137       myDlg->SetEnabledRB( 0, false );
138     }
139     else if( ConsistMesh( mesh ) == SMESHGUI_ConvToQuadOp::Linear )
140     {
141       myDlg->SetEnabledRB( 1, false );
142     }
143     else 
144     {
145       myDlg->SetEnabledControls( true );
146     }
147   }
148   catch ( const SALOME::SALOME_Exception& S_ex )
149   {
150     SalomeApp_Tools::QtCatchCorbaException( S_ex );
151   }
152   catch ( ... )
153   {
154   }
155 }
156
157 //================================================================================
158 /*!
159  * \brief Creates selection filter
160   * \param theId - identifier of current selection widget
161   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
162  *
163  * Creates selection filter in accordance with identifier of current selection widget
164  */
165 //================================================================================
166 SUIT_SelectionFilter* SMESHGUI_ConvToQuadOp::createFilter( const int theId ) const
167 {
168   if ( theId == 0 )
169     return new SMESH_TypeFilter( MESH );
170   else
171     return 0;
172 }
173
174 //================================================================================
175 /*!
176  * \brief Edits mesh
177  *
178  * Virtual slot redefined from the base class called when "Apply" button is clicked
179  */
180 //================================================================================
181 bool SMESHGUI_ConvToQuadOp::onApply()
182 {
183
184   QString aMess;
185
186   QString anMeshEntry = myDlg->selectedObject( 0 );
187   _PTR(SObject) pMesh = studyDS()->FindObjectID( anMeshEntry.latin1() );
188   if ( !pMesh )
189   {
190     dlg()->show();
191     SUIT_MessageBox::warn1( myDlg,
192         tr( "SMESH_WRN_WARNING" ), tr("MESH_IS_NOT_SELECTED"), tr( "SMESH_BUT_OK" ) );
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::warn1( myDlg,
203         tr( "SMESH_WRN_WARNING" ), tr("REF_IS_NULL"), tr( "SMESH_BUT_OK" ) );
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     update( UF_ObjBrowser | UF_Model | UF_Viewer );
239     selectionDone();
240   }
241   return aResult;
242 }
243
244 //================================================================================
245 /*! ConsistMesh
246  *  Determines, what elements this mesh contains. 
247  */
248 //================================================================================
249 SMESHGUI_ConvToQuadOp::MeshType SMESHGUI_ConvToQuadOp::ConsistMesh( const SMESH::SMESH_Mesh_var& mesh) const
250 {
251   int nbAllElem = 0, nbQEdges =0, nbQFaces =0, nbQVolum = 0;
252   int nbEdges = 0, nbFaces = 0, nbVolum = 0;
253
254   nbAllElem = (int)mesh->NbElements();
255   nbQEdges = (int)mesh->NbEdgesOfOrder(SMESH::ORDER_QUADRATIC);
256   nbQFaces = (int)mesh->NbFacesOfOrder(SMESH::ORDER_QUADRATIC);
257   nbQVolum = (int)mesh->NbVolumesOfOrder(SMESH::ORDER_QUADRATIC);
258
259   nbEdges = (int)mesh->NbEdgesOfOrder(SMESH::ORDER_LINEAR);
260   nbFaces = (int)mesh->NbFacesOfOrder(SMESH::ORDER_LINEAR);
261   nbVolum = (int)mesh->NbVolumesOfOrder(SMESH::ORDER_LINEAR);
262
263   if( nbAllElem == (nbQEdges+nbQFaces+nbQVolum) )
264     return SMESHGUI_ConvToQuadOp::Quadratic;
265   else if ( nbAllElem == (nbEdges+nbFaces+nbVolum) )
266     return SMESHGUI_ConvToQuadOp::Linear;
267   else 
268     return SMESHGUI_ConvToQuadOp::Comp;
269 }
270
271
272 void SMESHGUI_ConvToQuadOp::ConnectRadioButtons( int id )
273 {
274   QString anMeshEntry = myDlg->selectedObject( 0 );
275   _PTR(SObject) pMesh = studyDS()->FindObjectID( anMeshEntry.latin1() );
276   if ( !pMesh ) return;
277
278   SMESH::SMESH_Mesh_var mesh =
279     SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );  
280
281   GEOM::GEOM_Object_var mainGeom;
282   mainGeom = mesh->GetShapeToMesh();
283
284   if( id || mainGeom->_is_nil() )
285     myDlg->SetEnabledCheck( false );
286   else
287     myDlg->SetEnabledCheck( true );
288 }
289
290