]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESHGUI/SMESHGUI_InitMeshOp.cxx
Salome HOME
*** empty log message ***
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_InitMeshOp.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESHGUI_InitMeshDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_InitMeshOp.h"
30 #include <SMESHGUI_InitMeshDlg.h>
31 #include <SMESHGUI.h>
32 #include <SMESHGUI_Utils.h>
33 #include <SMESHGUI_HypothesesUtils.h>
34
35 #include <SMESH_NumberFilter.hxx>
36 #include <SMESH_TypeFilter.hxx>
37
38 #include <SALOMEDS_SObject.hxx>
39
40 #include <SUIT_SelectionFilter.h>
41 #include <SUIT_MessageBox.h>
42 #include <SUIT_OverrideCursor.h>
43
44 #include <SalomeApp_SelectionMgr.h>
45 #include <SalomeApp_Tools.h>
46 #include <SalomeApp_UpdateFlags.h>
47
48 #include <TColStd_MapOfInteger.hxx>
49
50 #include <GEOMBase.h>
51
52 //=================================================================================
53 // function : Constructor
54 // purpose  :
55 //=================================================================================
56 SMESHGUI_InitMeshOp::SMESHGUI_InitMeshOp()
57 : SMESHGUI_SelectionOp(),
58   myDlg( 0 )
59 {
60   setAutoResumed( true );
61 }
62
63 //=================================================================================
64 // function : Destructor
65 // purpose  :
66 //=================================================================================
67 SMESHGUI_InitMeshOp::~SMESHGUI_InitMeshOp()
68 {
69   if( myDlg )
70     delete myDlg;
71 }
72
73 //=================================================================================
74 // function : startOperation
75 // purpose  :
76 //=================================================================================
77 void SMESHGUI_InitMeshOp::startOperation()
78 {
79   if( !myDlg )
80   {
81     myDlg = new SMESHGUI_InitMeshDlg();
82     connect( myDlg, SIGNAL( nameChanged( const QString& ) ), this, SLOT( onNameChanged( const QString& ) ) );
83   }
84
85   SMESHGUI_SelectionOp::startOperation();
86
87   myDlg->show();
88 }
89
90 //=================================================================================
91 // function : dlg
92 // purpose  :
93 //=================================================================================
94 SalomeApp_Dialog* SMESHGUI_InitMeshOp::dlg() const
95 {
96   return myDlg;
97 }
98
99 //=================================================================================
100 // function : selectionDone
101 // purpose  :
102 //=================================================================================
103 void SMESHGUI_InitMeshOp::selectionDone()
104 {
105   SMESHGUI_SelectionOp::selectionDone();
106   if( myDlg )
107     updateDialog();
108 }
109
110 //=================================================================================
111 // function : createFilter
112 // purpose  :
113 //=================================================================================
114 SUIT_SelectionFilter* SMESHGUI_InitMeshOp::createFilter( const int id ) const
115 {
116   if( id==SMESHGUI_InitMeshDlg::GeomObj )
117   {
118     TColStd_MapOfInteger allTypesMap;
119     for (int i = 0; i < 10; i++)
120       allTypesMap.Add(i);
121     return new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 0, allTypesMap );
122   }
123
124   else if( id==SMESHGUI_InitMeshDlg::Hypo )
125     return new SMESH_TypeFilter (HYPOTHESIS);
126
127   else if( id==SMESHGUI_InitMeshDlg::Algo )
128     return new SMESH_TypeFilter (ALGORITHM);
129
130   else
131     return 0;
132 }
133
134 //=================================================================================
135 // function : commitOperation
136 // purpose  :
137 //=================================================================================
138 bool SMESHGUI_InitMeshOp::onApply()
139 {
140   if( getSMESHGUI()->isActiveStudyLocked() )
141     return false;
142
143   QString myNameMesh = myDlg->meshName();
144   if (myNameMesh.isEmpty())
145   {
146     SUIT_MessageBox::warn1( dlg(), tr("SMESH_WRN_WARNING"),
147                             tr("SMESH_WRN_EMPTY_NAME"), tr("SMESH_BUT_OK") );
148     return false;
149   }
150
151   SUIT_OverrideCursor wc;
152
153   QStringList selGeom, selHypo, selAlgo;
154   myDlg->selectedObject( SMESHGUI_InitMeshDlg::GeomObj, selGeom );
155   myDlg->selectedObject( SMESHGUI_InitMeshDlg::Hypo, selHypo );
156   myDlg->selectedObject( SMESHGUI_InitMeshDlg::Algo, selAlgo );
157
158   _PTR(Study) study = studyDS();
159   _PTR(SObject) aGeomSO = study->FindObjectID( selGeom.first() );
160   GEOM::GEOM_Object_var myGeomShape = GEOM::GEOM_Object::_narrow( _CAST(SObject,aGeomSO)->GetObject() );
161     
162   // create mesh
163   SMESH::SMESH_Mesh_var aMesh = initMesh(myGeomShape, myNameMesh);
164
165   if (!aMesh->_is_nil())
166   {
167     // assign hypotheses
168     for (int i = 0; i < selHypo.count(); i++)
169     {
170       _PTR(SObject) aHypSOClient =
171         study->FindObjectID(selHypo[i].latin1());
172       if (aHypSOClient)
173       {
174         CORBA::Object_var anObject = _CAST(SObject,aHypSOClient)->GetObject();
175         if (!CORBA::is_nil(anObject))
176         {
177           SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
178           if (!aHyp->_is_nil())
179           {
180             if (!SMESH::AddHypothesisOnMesh(aMesh, aHyp))
181               return false;
182           }
183         }
184       }
185     }
186     // assign algorithms
187     for (int i = 0; i < selAlgo.count(); i++)
188     {
189       _PTR(SObject) aHypSOClient =
190         study->FindObjectID(selAlgo[i].latin1());
191       if (aHypSOClient)
192       {
193         CORBA::Object_var anObject = _CAST(SObject,aHypSOClient)->GetObject();
194         if (!CORBA::is_nil(anObject))
195         {
196           SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
197           if (!aHyp->_is_nil())
198           {
199             if (!SMESH::AddHypothesisOnMesh(aMesh, aHyp))
200               return false;
201           }
202         }
203       }
204     }
205   }
206
207   update( UF_Model | UF_ObjBrowser );
208
209   initDialog();
210   return true;
211 }
212
213 //=================================================================================
214 // function : defaultMeshName()
215 // purpose  :
216 //=================================================================================
217 QString SMESHGUI_InitMeshOp::defaultMeshName() const
218 {
219   _PTR(Study) aStudy = studyDS();
220   int aNumber = 0;
221   QString aMeshName;
222   _PTR(SObject) obj;
223
224   do {
225     aMeshName = QString(tr("SMESH_OBJECT_MESH")) + "_" + QString::number(++aNumber);
226     obj = aStudy->FindObject(aMeshName.latin1());
227   } while (obj);
228
229   return aMeshName;
230 }
231
232 //=================================================================================
233 // function : init()
234 // purpose  :
235 //=================================================================================
236 void SMESHGUI_InitMeshOp::initDialog()
237 {
238   SMESHGUI_SelectionOp::initDialog();
239   if( myDlg )
240   {
241     myDlg->setMeshName( defaultMeshName() );
242     updateDialog();
243   }    
244 }
245
246 //=================================================================================
247 // function : defaultMeshName()
248 // purpose  :
249 //=================================================================================
250 SMESH::SMESH_Mesh_var SMESHGUI_InitMeshOp::initMesh ( GEOM::GEOM_Object_ptr theShapeObject,
251                                                       const QString& theMeshName )
252 {
253   SMESH::SMESH_Mesh_var aMesh;
254   try
255   {
256     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
257     if (!aSMESHGen->_is_nil() && !theShapeObject->_is_nil())
258     {
259       aMesh = aSMESHGen->CreateMesh(theShapeObject);
260       if (!aMesh->_is_nil())
261       {
262         _PTR(SObject) aMeshSObject = SMESH::FindSObject(aMesh.in());
263         SMESH::SetName(aMeshSObject, theMeshName);
264       }
265     }
266   }
267   catch( const SALOME::SALOME_Exception& S_ex )
268   {
269     SalomeApp_Tools::QtCatchCorbaException(S_ex);
270   }
271   
272   return aMesh._retn();
273 }
274
275 //=================================================================================
276 // function : defaultMeshName()
277 // purpose  :
278 //=================================================================================
279 void SMESHGUI_InitMeshOp::onSelectionChanged( int id )
280 {
281   if( !myDlg->hasSelection( id ) )
282     return;
283     
284   if( id==SMESHGUI_InitMeshDlg::GeomObj )
285   {
286     QStringList selGeom;
287     myDlg->selectedObject( SMESHGUI_InitMeshDlg::GeomObj, selGeom );
288
289     _PTR(SObject) aGeomSO = studyDS()->FindObjectID( selGeom.first() );
290     GEOM::GEOM_Object_var myGeomShape = GEOM::GEOM_Object::_narrow( _CAST(SObject,aGeomSO)->GetObject() );
291     if( myGeomShape->_is_nil() || !GEOMBase::IsShape( myGeomShape ) )
292       myDlg->clearSelection( id );
293   }
294 }
295
296 //=================================================================================
297 // function : onNameChanged()
298 // purpose  :
299 //=================================================================================
300 void SMESHGUI_InitMeshOp::onNameChanged( const QString& )
301 {
302   updateDialog();
303 }
304
305 //=================================================================================
306 // function : updateDialog()
307 // purpose  :
308 //=================================================================================
309 void SMESHGUI_InitMeshOp::updateDialog()
310 {
311   if( !myDlg )
312     return;
313     
314   bool isEnabled = !myDlg->meshName().isEmpty() &&
315                    myDlg->hasSelection( SMESHGUI_InitMeshDlg::GeomObj ) &&
316                    myDlg->hasSelection( SMESHGUI_InitMeshDlg::Hypo ) &&
317                    myDlg->hasSelection( SMESHGUI_InitMeshDlg::Algo );
318   myDlg->setButtonEnabled( isEnabled, QtxDialog::OK | QtxDialog::Apply );
319 }
320
321 //=================================================================================
322 // function : isValid
323 // purpose  :
324 //=================================================================================
325 bool SMESHGUI_InitMeshOp::isValid( SUIT_Operation* theOtherOp ) const
326 {
327   //if ( theOtherOp && theOtherOp->inherits( "SMESHGUI_AddSubMeshOp" ) )
328     return true;
329   /*else
330     return false;
331     */
332 }