Salome HOME
eac1ae86cf4903fd1670db4b1109c56fbca87ca4
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ConvToQuadOp.cxx
1 // Copyright (C) 2007-2012  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_MeshEditPreview.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESH_ActorUtils.h"
35 #include "SMESH_TypeFilter.hxx"
36 #include "SMDSAbs_ElementType.hxx"
37
38 // SALOME GUI includes
39 #include <LightApp_UpdateFlags.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_OverrideCursor.h>
42 #include <SalomeApp_Tools.h>
43 #include <SALOME_Actor.h>
44
45 // IDL includes
46 #include <SALOMEconfig.h>
47 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
48
49 // VTK includes
50 #include <vtkProperty.h>
51
52 //================================================================================
53 /*!
54  * \brief Constructor
55  *
56  * Initialize operation
57 */
58 //================================================================================
59 SMESHGUI_ConvToQuadOp::SMESHGUI_ConvToQuadOp()
60   : SMESHGUI_SelectionOp(), 
61     myDlg( 0 ),
62     myBadElemsPreview(0)
63 {
64 }
65
66 //================================================================================
67 /*!
68  * \brief Destructor
69 */
70 //================================================================================
71 SMESHGUI_ConvToQuadOp::~SMESHGUI_ConvToQuadOp()
72 {
73   if ( myDlg ) delete myDlg;
74   if ( myBadElemsPreview ) delete myBadElemsPreview;
75 }
76
77 //================================================================================
78 /*!
79  * \brief Gets dialog of this operation
80   * \retval LightApp_Dialog* - pointer to dialog of this operation
81 */
82 //================================================================================
83 LightApp_Dialog* SMESHGUI_ConvToQuadOp::dlg() const
84 {
85   return myDlg;
86 }
87
88 //================================================================================
89 /*!
90  * \brief Creates dialog if necessary and shows it
91  *
92  * Virtual method redefined from base class called when operation is started creates
93  * dialog if necessary and shows it, activates selection
94  */
95 //================================================================================
96 void SMESHGUI_ConvToQuadOp::startOperation()
97 {
98   if( !myDlg )
99   {
100     myDlg = new SMESHGUI_ConvToQuadDlg( );
101   }
102   connect( myDlg, SIGNAL( onClicked( int ) ), SLOT( ConnectRadioButtons( int ) ) );
103
104   myHelpFileName = "convert_to_from_quadratic_mesh_page.html";
105
106   SMESHGUI_SelectionOp::startOperation();
107
108   myDlg->SetMediumNdsOnGeom( false );
109   myDlg->activateObject( 0 );
110   myDlg->ShowWarning( false );
111   myDlg->show();
112
113   selectionDone();
114 }
115
116 //================================================================================
117 /*!
118  * \brief Updates dialog's look and feel
119  *
120  * Virtual method redefined from the base class updates dialog's look and feel
121  */
122 //================================================================================
123 void SMESHGUI_ConvToQuadOp::selectionDone()
124 {
125   if ( !dlg()->isVisible() )
126     return;
127
128   SMESHGUI_SelectionOp::selectionDone();
129   try
130   {
131     QString anObjEntry = myDlg->selectedObject( 0 );
132     _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
133     if ( !pObj ) return;
134
135     SMESH::SMESH_IDSource_var idSource = 
136       SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( pObj );  
137
138     myDlg->setButtonEnabled( true, QtxDialog::OK | QtxDialog::Apply );
139     if( idSource->_is_nil() )
140     {
141       myDlg->SetEnabledControls( false );
142       myDlg->setButtonEnabled( false, QtxDialog::OK | QtxDialog::Apply );
143       return;
144     }
145     MeshType meshType = ConsistMesh( idSource );
146     if( meshType == SMESHGUI_ConvToQuadOp::Quadratic )
147     {
148       myDlg->SetEnabledRB( 0, false );
149     }
150     else if( meshType == SMESHGUI_ConvToQuadOp::Linear )
151     {
152       myDlg->SetEnabledRB( 1, false );
153     }
154     else 
155     {
156       myDlg->SetEnabledControls( true );
157     }
158
159     // show warning on non-conformal result mesh
160     if ( ! idSource->_is_nil() )
161     {
162       SMESH::SMESH_subMesh_var subMesh = 
163         SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pObj );
164       bool toShow = false;
165       if ( !subMesh->_is_nil() )
166       {
167         SMESH::SMESH_Mesh_var mesh = idSource->GetMesh();
168         idSource = SMESH::SMESH_IDSource::_narrow( mesh );
169         MeshType fullMeshType = ConsistMesh( idSource );
170         toShow = ( fullMeshType != Comp );
171       }
172       myDlg->ShowWarning( toShow );
173     }
174   }
175   catch ( const SALOME::SALOME_Exception& S_ex )
176   {
177     SalomeApp_Tools::QtCatchCorbaException( S_ex );
178   }
179   catch ( ... )
180   {
181   }
182 }
183
184 //================================================================================
185 /*!
186  * \brief Creates selection filter
187   * \param theId - identifier of current selection widget
188   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
189  *
190  * Creates selection filter in accordance with identifier of current selection widget
191  */
192 //================================================================================
193 SUIT_SelectionFilter* SMESHGUI_ConvToQuadOp::createFilter( const int theId ) const
194 {
195   if ( theId == 0 )
196     return new SMESH_TypeFilter( SMESH::MESHorSUBMESH );
197   else
198     return 0;
199 }
200
201 //================================================================================
202 /*!
203  * \brief Edits mesh
204  *
205  * Virtual slot redefined from the base class called when "Apply" button is clicked
206  */
207 //================================================================================
208 bool SMESHGUI_ConvToQuadOp::onApply()
209 {
210   SUIT_OverrideCursor aWaitCursor;
211
212   QString aMess;
213
214   QString anObjEntry = myDlg->selectedObject( 0 );
215   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
216   if ( !pObj )
217   {
218     dlg()->show();
219     SUIT_MessageBox::warning( myDlg,
220                               tr( "SMESH_WRN_WARNING" ), tr("MESH_IS_NOT_SELECTED") );
221     return false;
222   }
223
224   SMESH::SMESH_Mesh_var mesh;
225   SMESH::SMESH_IDSource_var idSource = 
226     SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( pObj );  
227   if( !CORBA::is_nil(idSource) )
228     mesh = idSource->GetMesh();
229
230   if( CORBA::is_nil(mesh) )
231   {
232     SUIT_MessageBox::warning( myDlg,
233                               tr( "SMESH_WRN_WARNING" ), tr("REF_IS_NULL") );
234     return false;
235   }
236
237   bool aResult = false;
238
239   try
240   {
241     SMESH::SMESH_MeshEditor_var aEditor = mesh->GetMeshEditor();
242     aResult = true; 
243     SMESH::SMESH_Mesh_var sourceMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pObj );  
244     if( !myDlg->CurrentRB() )
245     {
246       bool force3d = true;
247       if( myDlg->IsEnabledCheck() )
248         force3d = myDlg->IsMediumNdsOnGeom();
249
250       if ( sourceMesh->_is_nil() )
251         aEditor->ConvertToQuadraticObject( force3d, idSource );
252       else
253         aEditor->ConvertToQuadratic( force3d );
254
255       if ( !force3d )
256       {
257         SMESH::ComputeError_var error = aEditor->GetLastError();
258         if ( error->hasBadMesh )
259         {
260           if ( myBadElemsPreview ) delete myBadElemsPreview; // viewWindow may change
261           myBadElemsPreview = new SMESHGUI_MeshEditPreview( viewWindow() );
262           
263           vtkFloatingPointType aPointSize = SMESH::GetFloat("SMESH:node_size",3);
264           vtkFloatingPointType aLineWidth = SMESH::GetFloat("SMESH:element_width",1);
265           vtkProperty* prop = vtkProperty::New();
266           prop->SetLineWidth( aLineWidth * 3 );
267           prop->SetPointSize( aPointSize * 3 );
268           prop->SetColor( 250, 0, 250 );
269           myBadElemsPreview->GetActor()->SetProperty( prop );
270           prop->Delete();
271
272           SMESH::MeshPreviewStruct_var previewData = aEditor->GetPreviewData();
273           myBadElemsPreview->SetData( & previewData.in() );
274           myBadElemsPreview->SetVisibility(true);
275
276           SUIT_MessageBox* mb = new SUIT_MessageBox(SUIT_MessageBox::Warning,
277                                                     tr( "SMESH_WRN_WARNING" ),
278                                                     tr("EDITERR_NO_MEDIUM_ON_GEOM"),
279                                                     SUIT_MessageBox::Ok, myDlg);
280           mb->setWindowModality( Qt::NonModal );
281           mb->setAttribute( Qt::WA_DeleteOnClose );
282           mb->show();
283           connect ( mb, SIGNAL( finished(int) ), this, SLOT( onWarningWinFinished() ));
284           //connect ( mb, SIGNAL( rejected() ), this, SLOT( onWarningWinFinished() ));
285         }
286       }
287     }
288     else
289     {
290       if ( sourceMesh->_is_nil() )
291         aEditor->ConvertFromQuadraticObject( idSource );
292       else
293         aEditor->ConvertFromQuadratic();
294     }
295   }
296   catch ( const SALOME::SALOME_Exception& S_ex )
297   {
298     SalomeApp_Tools::QtCatchCorbaException( S_ex );
299     aResult = false;
300   }
301   catch ( ... )
302   {
303     aResult = false;
304   }
305   if( aResult )
306   {
307     SMESHGUI::Modified();
308     update( UF_ObjBrowser | UF_Model | UF_Viewer );
309     selectionDone();
310   }
311   return aResult;
312 }
313
314 //================================================================================
315 /*!
316  * \brief SLOT called when a warning window is closed
317  */
318 //================================================================================
319
320 void SMESHGUI_ConvToQuadOp::onWarningWinFinished()
321 {
322   if ( myBadElemsPreview )
323     myBadElemsPreview->SetVisibility(false);
324 }
325
326 //================================================================================
327 /*! ConsistMesh
328  *  Determines, what elements this mesh contains. 
329  */
330 //================================================================================
331 SMESHGUI_ConvToQuadOp::MeshType SMESHGUI_ConvToQuadOp::ConsistMesh( const SMESH::SMESH_IDSource_var& idSource) const
332 {
333   SMESH::long_array_var nbElemOfType = idSource->GetMeshInfo();
334   bool hasQuad = ( nbElemOfType[SMDSEntity_Quad_Edge      ] ||
335                    nbElemOfType[SMDSEntity_Quad_Triangle  ] ||
336                    nbElemOfType[SMDSEntity_Quad_Quadrangle] ||
337                    nbElemOfType[SMDSEntity_Quad_Tetra     ] ||
338                    nbElemOfType[SMDSEntity_Quad_Hexa      ] ||
339                    nbElemOfType[SMDSEntity_Quad_Pyramid   ] ||
340                    nbElemOfType[SMDSEntity_Quad_Penta     ] );
341
342   bool hasLin  = ( nbElemOfType[SMDSEntity_Edge      ] ||
343                    nbElemOfType[SMDSEntity_Triangle  ] ||
344                    nbElemOfType[SMDSEntity_Quadrangle] ||
345                    nbElemOfType[SMDSEntity_Tetra     ] ||
346                    nbElemOfType[SMDSEntity_Hexa      ] ||
347                    nbElemOfType[SMDSEntity_Pyramid   ] ||
348                    nbElemOfType[SMDSEntity_Penta     ] );
349
350   if ( hasQuad && hasLin )
351     return Comp;
352   return hasQuad ? Quadratic : Linear;
353 }
354
355 void SMESHGUI_ConvToQuadOp::ConnectRadioButtons( int id )
356 {
357   QString anObjEntry = myDlg->selectedObject( 0 );
358   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
359   if ( !pObj ) return;
360
361   SMESH::SMESH_IDSource_var idSource = 
362     SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( pObj );  
363   SMESH::SMESH_Mesh_var mesh = idSource->GetMesh();
364
365   bool hasGeom = mesh->HasShapeToMesh();
366
367   if( id || !hasGeom )
368     myDlg->SetEnabledCheck( false );
369   else
370     myDlg->SetEnabledCheck( true );
371 }