Salome HOME
3afa881c15178f5b4a3e82a8ed7b627e900d95e9
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ConvToQuadOp.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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( true );
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     SMESH::SMESH_Mesh_var      mesh = idSource->GetMesh();
146     bool                    hasGeom = mesh->HasShapeToMesh();
147     MeshDestinationType meshTgtType = DestinationMesh( idSource );
148     myDlg->SetEnabledRB( meshTgtType, hasGeom && ( meshTgtType & ( BiQuadratic | Quadratic )));
149
150     // show warning on non-conformal result mesh
151     if ( ! idSource->_is_nil() )
152     {
153       SMESH::SMESH_subMesh_var subMesh = 
154         SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pObj );
155       bool toShow = false;
156       if ( !subMesh->_is_nil() )
157       {
158         SMESH::SMESH_Mesh_var mesh = idSource->GetMesh();
159         idSource = SMESH::SMESH_IDSource::_narrow( mesh );
160         bool isMixOrder;
161         DestinationMesh( idSource, &isMixOrder );
162         toShow = !isMixOrder;
163       }
164       myDlg->ShowWarning( toShow );
165     }
166   }
167   catch ( const SALOME::SALOME_Exception& S_ex )
168   {
169     SalomeApp_Tools::QtCatchCorbaException( S_ex );
170   }
171   catch ( ... )
172   {
173   }
174 }
175
176 //================================================================================
177 /*!
178  * \brief Creates selection filter
179   * \param theId - identifier of current selection widget
180   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
181  *
182  * Creates selection filter in accordance with identifier of current selection widget
183  */
184 //================================================================================
185 SUIT_SelectionFilter* SMESHGUI_ConvToQuadOp::createFilter( const int theId ) const
186 {
187   if ( theId == 0 )
188     return new SMESH_TypeFilter( SMESH::MESHorSUBMESH );
189   else
190     return 0;
191 }
192
193 //================================================================================
194 /*!
195  * \brief Edits mesh
196  *
197  * Virtual slot redefined from the base class called when "Apply" button is clicked
198  */
199 //================================================================================
200 bool SMESHGUI_ConvToQuadOp::onApply()
201 {
202   SUIT_OverrideCursor aWaitCursor;
203
204   QString aMess;
205
206   QString anObjEntry = myDlg->selectedObject( 0 );
207   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
208   if ( !pObj )
209   {
210     dlg()->show();
211     SUIT_MessageBox::warning( myDlg,
212                               tr( "SMESH_WRN_WARNING" ), tr("MESH_IS_NOT_SELECTED") );
213     return false;
214   }
215
216   SMESH::SMESH_Mesh_var mesh;
217   SMESH::SMESH_IDSource_var idSource = 
218     SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( pObj );  
219   if( !CORBA::is_nil(idSource) )
220     mesh = idSource->GetMesh();
221
222   if( CORBA::is_nil(mesh) )
223   {
224     SUIT_MessageBox::warning( myDlg,
225                               tr( "SMESH_WRN_WARNING" ), tr("REF_IS_NULL") );
226     return false;
227   }
228
229   bool aResult = false;
230
231   try
232   {
233     SMESH::SMESH_MeshEditor_var aEditor = mesh->GetMeshEditor();
234     aResult = true; 
235     SMESH::SMESH_Mesh_var sourceMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pObj );  
236     if( myDlg->CurrentRB()==1 || myDlg->CurrentRB()==2)
237     {
238       bool force3d = true;
239       if( myDlg->IsEnabledCheck() )
240         force3d = myDlg->IsMediumNdsOnGeom();
241       bool theToBiQuad = myDlg->IsBiQuadratic();
242       if ( sourceMesh->_is_nil() ) {
243         if ( theToBiQuad ) aEditor->ConvertToBiQuadratic    ( force3d, idSource );
244         else               aEditor->ConvertToQuadraticObject( force3d, idSource );
245       }
246       else {
247         if ( theToBiQuad ) aEditor->ConvertToBiQuadratic( force3d, sourceMesh );
248         else               aEditor->ConvertToQuadratic  ( force3d );
249       }
250       if ( !force3d )
251       {
252         SMESH::ComputeError_var error = aEditor->GetLastError();
253         if ( error->hasBadMesh )
254         {
255           if ( myBadElemsPreview ) delete myBadElemsPreview; // viewWindow may change
256           myBadElemsPreview = new SMESHGUI_MeshEditPreview( viewWindow() );
257
258           double aPointSize = SMESH::GetFloat("SMESH:node_size",3);
259           double aLineWidth = SMESH::GetFloat("SMESH:element_width",1);
260           vtkProperty* prop = vtkProperty::New();
261           prop->SetLineWidth( aLineWidth * 3 );
262           prop->SetPointSize( aPointSize * 3 );
263           prop->SetColor( 250, 0, 250 );
264           myBadElemsPreview->GetActor()->SetProperty( prop );
265           prop->Delete();
266
267           SMESH::MeshPreviewStruct_var previewData = aEditor->GetPreviewData();
268           myBadElemsPreview->SetData( & previewData.in() );
269           myBadElemsPreview->SetVisibility(true);
270
271           SUIT_MessageBox* mb = new SUIT_MessageBox(SUIT_MessageBox::Warning,
272                                                     tr( "SMESH_WRN_WARNING" ),
273                                                     tr("EDITERR_NO_MEDIUM_ON_GEOM"),
274                                                     SUIT_MessageBox::Ok, myDlg);
275           mb->setWindowModality( Qt::NonModal );
276           mb->setAttribute( Qt::WA_DeleteOnClose );
277           mb->show();
278           connect ( mb, SIGNAL( finished(int) ), this, SLOT( onWarningWinFinished() ));
279           //connect ( mb, SIGNAL( rejected() ), this, SLOT( onWarningWinFinished() ));
280         }
281       }
282     }
283     else
284     {
285       if ( sourceMesh->_is_nil() )
286         aEditor->ConvertFromQuadraticObject( idSource );
287       else
288         aEditor->ConvertFromQuadratic();
289     }
290   }
291   catch ( const SALOME::SALOME_Exception& S_ex )
292   {
293     SalomeApp_Tools::QtCatchCorbaException( S_ex );
294     aResult = false;
295   }
296   catch ( ... )
297   {
298     aResult = false;
299   }
300   if( aResult )
301   {
302     SMESHGUI::Modified();
303     update( UF_ObjBrowser | UF_Model | UF_Viewer );
304     selectionDone();
305   }
306   return aResult;
307 }
308
309 //================================================================================
310 /*!
311  * \brief SLOT called when a warning window is closed
312  */
313 //================================================================================
314
315 void SMESHGUI_ConvToQuadOp::onWarningWinFinished()
316 {
317   if ( myBadElemsPreview )
318     myBadElemsPreview->SetVisibility(false);
319 }
320
321 //================================================================================
322 /*! ConsistMesh
323  *  Determines, what elements this mesh does not contain.
324  */
325 //================================================================================
326
327 SMESHGUI_ConvToQuadOp::MeshDestinationType
328 SMESHGUI_ConvToQuadOp::DestinationMesh( const SMESH::SMESH_IDSource_var& idSource,
329                                         bool*                            isMixOrder)
330 {
331   SMESH::long_array_var nbElemOfType = idSource->GetMeshInfo();
332
333   bool hasBiQuad     = ( nbElemOfType[SMDSEntity_BiQuad_Triangle   ] ||
334                          nbElemOfType[SMDSEntity_BiQuad_Quadrangle ] ||
335                          nbElemOfType[SMDSEntity_TriQuad_Hexa      ] );
336   bool hasLinBiQuad  = ( nbElemOfType[SMDSEntity_Triangle   ] ||
337                          nbElemOfType[SMDSEntity_Quadrangle ] ||
338                          nbElemOfType[SMDSEntity_Hexa       ] );
339   bool hasQuadBiQuad = ( nbElemOfType[SMDSEntity_Quad_Triangle   ] ||
340                          nbElemOfType[SMDSEntity_Quad_Quadrangle ] ||
341                          nbElemOfType[SMDSEntity_Quad_Hexa       ] );
342
343   bool hasQuad = ( nbElemOfType[SMDSEntity_Quad_Edge      ] ||
344                    nbElemOfType[SMDSEntity_Quad_Triangle  ] ||
345                    nbElemOfType[SMDSEntity_Quad_Quadrangle] ||
346                    nbElemOfType[SMDSEntity_Quad_Tetra     ] ||
347                    nbElemOfType[SMDSEntity_Quad_Hexa      ] ||
348                    nbElemOfType[SMDSEntity_Quad_Pyramid   ] ||
349                    nbElemOfType[SMDSEntity_Quad_Polygon   ] ||
350                    nbElemOfType[SMDSEntity_Quad_Penta     ] );
351
352   bool hasLin  = ( nbElemOfType[SMDSEntity_Edge      ] ||
353                    nbElemOfType[SMDSEntity_Triangle  ] ||
354                    nbElemOfType[SMDSEntity_Quadrangle] ||
355                    nbElemOfType[SMDSEntity_Tetra     ] ||
356                    nbElemOfType[SMDSEntity_Hexa      ] ||
357                    nbElemOfType[SMDSEntity_Pyramid   ] ||
358                    nbElemOfType[SMDSEntity_Polygon   ] ||
359                    nbElemOfType[SMDSEntity_Penta     ] );
360
361   int tgtType = 0;
362   if ( hasBiQuad )
363     tgtType |= ( Quadratic | Linear );
364   if ( hasLinBiQuad )
365     tgtType |= ( BiQuadratic | Quadratic );
366   if ( hasQuadBiQuad )
367     tgtType |= ( BiQuadratic | Linear );
368   if ( hasQuad )
369     tgtType |= Linear;
370   if ( hasLin )
371     tgtType |= Quadratic;
372
373   if ( tgtType == 0 )
374     tgtType = Quadratic;
375
376   if ( isMixOrder )
377     *isMixOrder = ( hasLin && ( hasQuad || hasBiQuad ));
378
379   return MeshDestinationType( tgtType );
380 }
381
382 void SMESHGUI_ConvToQuadOp::ConnectRadioButtons( int id )
383 {
384   QString anObjEntry = myDlg->selectedObject( 0 );
385   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
386   if ( !pObj ) return;
387
388   SMESH::SMESH_IDSource_var idSource = 
389     SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( pObj );  
390   SMESH::SMESH_Mesh_var mesh = idSource->GetMesh();
391
392   bool hasGeom = mesh->HasShapeToMesh();
393
394   if( id==0 || !hasGeom )
395     myDlg->SetEnabledCheck( false );
396   else
397     myDlg->SetEnabledCheck( true );
398 }