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