Salome HOME
Adding user documentation
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_CreateDualMeshOp.cxx
1 // Copyright (C) 2007-2021  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_CreateDualMeshOp.cxx
25 // Author : Yoann AUDOUIN (EDF)
26 // SMESH includes
27 //
28 #include "SMESHGUI_CreateDualMeshOp.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_CreateDualMeshDlg.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 <SalomeApp_Application.h>
44 #include <SALOME_Actor.h>
45
46 // Qt includes
47 #include <QLineEdit>
48 #include <QCheckBox>
49
50 // IDL includes
51 #include <SALOMEconfig.h>
52 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
53
54 // VTK includes
55 #include <vtkProperty.h>
56
57 //================================================================================
58 /*!
59  * \brief Constructor
60  *
61  * Initialize operation
62 */
63 //================================================================================
64 SMESHGUI_CreateDualMeshOp::SMESHGUI_CreateDualMeshOp()
65   : SMESHGUI_SelectionOp(),
66     myDlg( 0 )
67 {
68 }
69
70 //================================================================================
71 /*!
72  * \brief Destructor
73 */
74 //================================================================================
75 SMESHGUI_CreateDualMeshOp::~SMESHGUI_CreateDualMeshOp()
76 {
77   if ( myDlg ) delete myDlg;
78 }
79
80 //================================================================================
81 /*!
82  * \brief Gets dialog of this operation
83   * \retval LightApp_Dialog* - pointer to dialog of this operation
84 */
85 //================================================================================
86 LightApp_Dialog* SMESHGUI_CreateDualMeshOp::dlg() const
87 {
88   return myDlg;
89 }
90
91 //================================================================================
92 /*!
93  * \brief Creates dialog if necessary and shows it
94  *
95  * Virtual method redefined from base class called when operation is started creates
96  * dialog if necessary and shows it, activates selection
97  */
98 //================================================================================
99 void SMESHGUI_CreateDualMeshOp::startOperation()
100 {
101   if( !myDlg )
102   {
103     myDlg = new SMESHGUI_CreateDualMeshDlg( );
104   }
105   connect( myDlg, SIGNAL( onClicked( int ) ), SLOT( ConnectRadioButtons( int ) ) );
106
107   myHelpFileName = "create_dual_mesh.html";
108
109   SMESHGUI_SelectionOp::startOperation();
110
111   myDlg->activateObject( 0 );
112   myDlg->ShowWarning( false );
113   myDlg->show();
114
115   selectionDone();
116 }
117
118 //================================================================================
119 /*!
120  * \brief Updates dialog's look and feel
121  *
122  * Virtual method redefined from the base class updates dialog's look and feel
123  */
124 //================================================================================
125 void SMESHGUI_CreateDualMeshOp::selectionDone()
126 {
127   if ( !dlg()->isVisible() )
128     return;
129
130   SMESHGUI_SelectionOp::selectionDone();
131   try
132   {
133     QString anObjEntry = myDlg->selectedObject( 0 );
134     _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID( anObjEntry.toUtf8().data() );
135     if ( !pObj ) return;
136
137     SMESH::SMESH_IDSource_var idSource =
138       SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( pObj );
139
140     myDlg->setButtonEnabled( true, QtxDialog::OK | QtxDialog::Apply );
141     if( idSource->_is_nil() )
142     {
143       myDlg->setButtonEnabled( false, QtxDialog::OK | QtxDialog::Apply );
144       return;
145     }
146     SMESH::SMESH_Mesh_var      mesh = idSource->GetMesh();
147
148     // show warning on non-conformal result mesh
149     if ( ! idSource->_is_nil() )
150     {
151       SMESH::SMESH_subMesh_var subMesh =
152         SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pObj );
153       // Check that mesh is only tetra
154       if (!checkMesh(idSource)){
155         myDlg->ShowWarning( true );
156         myDlg->setButtonEnabled(false, QtxDialog::OK|QtxDialog::Apply);
157       }
158     }
159     std::string mesh_name = "dual_" + pObj->GetName();
160     myDlg->myMeshName->setText(QString(mesh_name.c_str()));
161
162   }
163   catch ( const SALOME::SALOME_Exception& S_ex )
164   {
165     SalomeApp_Tools::QtCatchCorbaException( S_ex );
166   }
167   catch ( ... )
168   {
169   }
170
171 }
172
173 //================================================================================
174 /*!
175  * \brief Creates selection filter
176   * \param theId - identifier of current selection widget
177   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
178  *
179  * Creates selection filter in accordance with identifier of current selection widget
180  */
181 //================================================================================
182 SUIT_SelectionFilter* SMESHGUI_CreateDualMeshOp::createFilter( const int theId ) const
183 {
184   if ( theId == 0 )
185     return new SMESH_TypeFilter( SMESH::MESHorSUBMESH );
186   else
187     return 0;
188 }
189
190 //================================================================================
191 /*!
192  * \brief Edits mesh
193  *
194  * Virtual slot redefined from the base class called when "Apply" button is clicked
195  */
196 //================================================================================
197 bool SMESHGUI_CreateDualMeshOp::onApply()
198 {
199   SUIT_OverrideCursor aWaitCursor;
200
201   QString aMess;
202   QStringList anEntryList;
203
204   QString anObjEntry = myDlg->selectedObject( 0 );
205   _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID( anObjEntry.toUtf8().data() );
206   if ( !pObj )
207   {
208     dlg()->show();
209     SUIT_MessageBox::warning( myDlg,
210                               tr( "SMESH_WRN_WARNING" ), tr("MESH_IS_NOT_SELECTED") );
211     return false;
212   }
213
214   SMESH::SMESH_Mesh_var mesh;
215   SMESH::SMESH_IDSource_var idSource =
216     SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( pObj );
217   if( !CORBA::is_nil(idSource) )
218     mesh = idSource->GetMesh();
219
220   if( CORBA::is_nil(mesh) )
221   {
222     SUIT_MessageBox::warning( myDlg,
223                               tr( "SMESH_WRN_WARNING" ), tr("REF_IS_NULL") );
224     return false;
225   }
226
227   bool aResult = false;
228   SMESH::SMESH_Gen_var gen = SMESHGUI::GetSMESHGen();
229   SMESH::SMESH_Mesh_var newMesh;
230   QByteArray newMeshName=myDlg->myMeshName->text().toUtf8();
231   bool adapt_to_shape=myDlg->myProjShape->isChecked();
232   try
233   {
234     newMesh = gen->CreateDualMesh(mesh, newMeshName.constData(), adapt_to_shape);
235
236     if ( !newMesh->_is_nil() )
237       if ( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
238       {
239         anEntryList.append( aSObject->GetID().c_str() );
240
241         SMESH::SetName( aSObject, newMeshName );
242       }
243     aResult = true;
244   }
245   catch ( const SALOME::SALOME_Exception& S_ex )
246   {
247     SalomeApp_Tools::QtCatchCorbaException( S_ex );
248     aResult = false;
249   }
250   catch ( ... )
251   {
252     aResult = false;
253   }
254   if( aResult )
255   {
256     SMESHGUI::Modified();
257     selectionDone();
258     update( UF_ObjBrowser | UF_Model | UF_Viewer );
259
260   }
261   SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser();
262
263   // updateObjBrowser(true);
264   // SMESHGUI::Modified();
265
266   // if( LightApp_Application* anApp =
267   //     dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
268   //   anApp->browseObjects( anEntryList, true );
269
270   return true;
271
272 }
273
274 //================================================================================
275 /*! checkMesh
276  *  Verify that mesh as only tetraheadrons as 3D elements
277  */
278 //================================================================================
279
280 bool
281 SMESHGUI_CreateDualMeshOp::checkMesh( const SMESH::SMESH_IDSource_var& idSource)
282 {
283   SMESH::smIdType_array_var nbElemOfType = idSource->GetMeshInfo();
284   // Checking that the mesh only has Tetrahedron
285   bool hasOnlyTetra  = (
286                     nbElemOfType[SMDSEntity_Tetra     ] &&
287                    !nbElemOfType[SMDSEntity_Hexa      ] &&
288                    !nbElemOfType[SMDSEntity_Pyramid   ] &&
289                    !nbElemOfType[SMDSEntity_Polygon   ] &&
290                    !nbElemOfType[SMDSEntity_Penta     ] );
291
292   return hasOnlyTetra;
293 }