Salome HOME
Update copyright
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ConvToQuadOp.cxx
1 // Copyright (C) 2007-2011  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_Utils.h"
33 #include "SMDSAbs_ElementType.hxx"
34
35 #include "SMESH_TypeFilter.hxx"
36
37 // SALOME GUI includes
38 #include <LightApp_UpdateFlags.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_OverrideCursor.h>
41 #include <SalomeApp_Tools.h>
42
43 // IDL includes
44 #include <SALOMEconfig.h>
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->ShowWarning( false );
105   myDlg->show();
106
107   selectionDone();
108 }
109
110 //================================================================================
111 /*!
112  * \brief Updates dialog's look and feel
113  *
114  * Virtual method redefined from the base class updates dialog's look and feel
115  */
116 //================================================================================
117 void SMESHGUI_ConvToQuadOp::selectionDone()
118 {
119   if ( !dlg()->isVisible() )
120     return;
121
122   SMESHGUI_SelectionOp::selectionDone();
123   try
124   {
125     QString anObjEntry = myDlg->selectedObject( 0 );
126     _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
127     if ( !pObj ) return;
128
129     SMESH::SMESH_IDSource_var idSource = 
130       SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( pObj );  
131
132     myDlg->setButtonEnabled( true, QtxDialog::OK | QtxDialog::Apply );
133     if( idSource->_is_nil() )
134     {
135       myDlg->SetEnabledControls( false );
136       myDlg->setButtonEnabled( false, QtxDialog::OK | QtxDialog::Apply );
137       return;
138     }
139     MeshType meshType = ConsistMesh( idSource );
140     if( meshType == SMESHGUI_ConvToQuadOp::Quadratic )
141     {
142       myDlg->SetEnabledRB( 0, false );
143     }
144     else if( meshType == SMESHGUI_ConvToQuadOp::Linear )
145     {
146       myDlg->SetEnabledRB( 1, false );
147     }
148     else 
149     {
150       myDlg->SetEnabledControls( true );
151     }
152
153     // show warning on non-conformal result mesh
154     if ( ! idSource->_is_nil() )
155     {
156       SMESH::SMESH_subMesh_var subMesh = 
157         SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pObj );
158       bool toShow = false;
159       if ( !subMesh->_is_nil() )
160       {
161         SMESH::SMESH_Mesh_var mesh = idSource->GetMesh();
162         idSource = SMESH::SMESH_IDSource::_narrow( mesh );
163         MeshType fullMeshType = ConsistMesh( idSource );
164         toShow = ( fullMeshType != Comp );
165       }
166       myDlg->ShowWarning( toShow );
167     }
168   }
169   catch ( const SALOME::SALOME_Exception& S_ex )
170   {
171     SalomeApp_Tools::QtCatchCorbaException( S_ex );
172   }
173   catch ( ... )
174   {
175   }
176 }
177
178 //================================================================================
179 /*!
180  * \brief Creates selection filter
181   * \param theId - identifier of current selection widget
182   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
183  *
184  * Creates selection filter in accordance with identifier of current selection widget
185  */
186 //================================================================================
187 SUIT_SelectionFilter* SMESHGUI_ConvToQuadOp::createFilter( const int theId ) const
188 {
189   if ( theId == 0 )
190     return new SMESH_TypeFilter( MESHorSUBMESH );
191   else
192     return 0;
193 }
194
195 //================================================================================
196 /*!
197  * \brief Edits mesh
198  *
199  * Virtual slot redefined from the base class called when "Apply" button is clicked
200  */
201 //================================================================================
202 bool SMESHGUI_ConvToQuadOp::onApply()
203 {
204   SUIT_OverrideCursor aWaitCursor;
205
206   QString aMess;
207
208   QString anObjEntry = myDlg->selectedObject( 0 );
209   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
210   if ( !pObj )
211   {
212     dlg()->show();
213     SUIT_MessageBox::warning( myDlg,
214                               tr( "SMESH_WRN_WARNING" ), tr("MESH_IS_NOT_SELECTED") );
215     return false;
216   }
217
218   SMESH::SMESH_Mesh_var mesh;
219   SMESH::SMESH_IDSource_var idSource = 
220     SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( pObj );  
221   if( !CORBA::is_nil(idSource) )
222     mesh = idSource->GetMesh();
223
224   if( CORBA::is_nil(mesh) )
225   {
226     SUIT_MessageBox::warning( myDlg,
227                               tr( "SMESH_WRN_WARNING" ), tr("REF_IS_NULL") );
228     return false;
229   }
230
231   bool aResult = false;
232
233   try
234   {
235     SMESH::SMESH_MeshEditor_var aEditor = mesh->GetMeshEditor();
236     aResult = true; 
237     SMESH::SMESH_Mesh_var sourceMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pObj );  
238     if( !myDlg->CurrentRB() )
239     {
240       bool aParam = true;
241       if( myDlg->IsEnabledCheck() )
242         aParam = myDlg->IsMediumNdsOnGeom();
243
244       if ( sourceMesh->_is_nil() )
245         aEditor->ConvertToQuadraticObject( aParam, idSource );
246       else
247         aEditor->ConvertToQuadratic( aParam );
248     }
249     else
250     {
251       if ( sourceMesh->_is_nil() )
252         aEditor->ConvertFromQuadraticObject( idSource );
253       else
254         aEditor->ConvertFromQuadratic();
255     }
256   }
257   catch ( const SALOME::SALOME_Exception& S_ex )
258   {
259     SalomeApp_Tools::QtCatchCorbaException( S_ex );
260     aResult = false;
261   }
262   catch ( ... )
263   {
264     aResult = false;
265   }
266   if( aResult )
267   {
268     SMESHGUI::Modified();
269     update( UF_ObjBrowser | UF_Model | UF_Viewer );
270     selectionDone();
271   }
272   return aResult;
273 }
274
275 //================================================================================
276 /*! ConsistMesh
277  *  Determines, what elements this mesh contains. 
278  */
279 //================================================================================
280 SMESHGUI_ConvToQuadOp::MeshType SMESHGUI_ConvToQuadOp::ConsistMesh( const SMESH::SMESH_IDSource_var& idSource) const
281 {
282   SMESH::long_array_var nbElemOfType = idSource->GetMeshInfo();
283   bool hasQuad = ( nbElemOfType[SMDSEntity_Quad_Edge      ] ||
284                    nbElemOfType[SMDSEntity_Quad_Triangle  ] ||
285                    nbElemOfType[SMDSEntity_Quad_Quadrangle] ||
286                    nbElemOfType[SMDSEntity_Quad_Tetra     ] ||
287                    nbElemOfType[SMDSEntity_Quad_Hexa      ] ||
288                    nbElemOfType[SMDSEntity_Quad_Pyramid   ] ||
289                    nbElemOfType[SMDSEntity_Quad_Penta     ] );
290
291   bool hasLin  = ( nbElemOfType[SMDSEntity_Edge      ] ||
292                    nbElemOfType[SMDSEntity_Triangle  ] ||
293                    nbElemOfType[SMDSEntity_Quadrangle] ||
294                    nbElemOfType[SMDSEntity_Tetra     ] ||
295                    nbElemOfType[SMDSEntity_Hexa      ] ||
296                    nbElemOfType[SMDSEntity_Pyramid   ] ||
297                    nbElemOfType[SMDSEntity_Penta     ] );
298
299   if ( hasQuad && hasLin )
300     return Comp;
301   return hasQuad ? Quadratic : Linear;
302 }
303
304 void SMESHGUI_ConvToQuadOp::ConnectRadioButtons( int id )
305 {
306   QString anObjEntry = myDlg->selectedObject( 0 );
307   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
308   if ( !pObj ) return;
309
310   SMESH::SMESH_IDSource_var idSource = 
311     SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( pObj );  
312   SMESH::SMESH_Mesh_var mesh = idSource->GetMesh();
313
314   bool hasGeom = mesh->HasShapeToMesh();
315
316   if( id || !hasGeom )
317     myDlg->SetEnabledCheck( false );
318   else
319     myDlg->SetEnabledCheck( true );
320 }