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