Salome HOME
Method isValid is redefined to allow other operations is started above this one.
[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_Operation(),
58   myDlg( 0 ),
59   myGeomFilter( 0 ),
60   myHypothesisFilter( 0 ),
61   myAlgorithmFilter( 0 )
62 {
63   setAutoResumed( true );
64 }
65
66 //=================================================================================
67 // function : Destructor
68 // purpose  :
69 //=================================================================================
70 SMESHGUI_InitMeshOp::~SMESHGUI_InitMeshOp()
71 {
72   if( myDlg )
73     delete myDlg;
74     
75   if( myGeomFilter )
76     delete myGeomFilter;
77   
78   if( myAlgorithmFilter )
79     delete myAlgorithmFilter;
80
81   if( myHypothesisFilter )
82     delete myHypothesisFilter;    
83 }
84
85 //=================================================================================
86 // function : startOperation
87 // purpose  :
88 //=================================================================================
89 void SMESHGUI_InitMeshOp::startOperation()
90 {
91   if( !myDlg )
92   {
93     myDlg = new SMESHGUI_InitMeshDlg( getSMESHGUI() );
94     connect( myDlg, SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
95   }
96
97   SMESHGUI_Operation::startOperation();
98
99   if( !myGeomFilter )
100   {
101     TColStd_MapOfInteger allTypesMap;
102     for (int i = 0; i < 10; i++)
103       allTypesMap.Add(i);
104     myGeomFilter       = new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, 0, allTypesMap);
105   }
106
107   if( !myAlgorithmFilter )
108     myAlgorithmFilter  = new SMESH_TypeFilter (ALGORITHM);
109
110   if( !myHypothesisFilter )
111     myHypothesisFilter = new SMESH_TypeFilter (HYPOTHESIS);
112     
113   init();
114   myDlg->show();
115 }
116
117 //=================================================================================
118 // function : dlg
119 // purpose  :
120 //=================================================================================
121 SalomeApp_Dialog* SMESHGUI_InitMeshOp::dlg() const
122 {
123   return myDlg;
124 }
125
126 //=================================================================================
127 // function : selectionDone
128 // purpose  :
129 //=================================================================================
130 void SMESHGUI_InitMeshOp::selectionDone()
131 {
132   QStringList names, ids;
133   SMESHGUI_Dialog::TypesList types;
134   selected( names, types, ids );
135   if( myDlg )
136   {
137     myDlg->selectObject( names, types, ids );
138     myDlg->updateControlState();
139   }
140 }
141
142 //=================================================================================
143 // function : onActivateObject
144 // purpose  :
145 //=================================================================================
146 void SMESHGUI_InitMeshOp::onActivateObject( int obj )
147 {
148   SalomeApp_SelectionMgr* mgr = selectionMgr();
149
150   if( !mgr )
151     return;
152     
153   mgr->clearFilters();
154   if( obj==SMESHGUI_InitMeshDlg::GeomObj )
155     mgr->installFilter( myGeomFilter );
156     
157   else if( obj==SMESHGUI_InitMeshDlg::Hypo )
158     mgr->installFilter( myHypothesisFilter );
159
160   else if( obj==SMESHGUI_InitMeshDlg::Algo )
161     mgr->installFilter( myAlgorithmFilter );
162 }
163
164 //=================================================================================
165 // function : commitOperation
166 // purpose  :
167 //=================================================================================
168 bool SMESHGUI_InitMeshOp::onApply()
169 {
170   if( getSMESHGUI()->isActiveStudyLocked() )
171     return false;
172
173   QString myNameMesh = myDlg->meshName();
174   if (myNameMesh.isEmpty())
175   {
176     SUIT_MessageBox::warn1( dlg(), tr("SMESH_WRN_WARNING"),
177                             tr("SMESH_WRN_EMPTY_NAME"), tr("SMESH_BUT_OK") );
178     return false;
179   }
180
181   SUIT_OverrideCursor wc;
182
183   QStringList selGeom, selHypo, selAlgo;
184   myDlg->selectedObject( SMESHGUI_InitMeshDlg::GeomObj, selGeom );
185   myDlg->selectedObject( SMESHGUI_InitMeshDlg::Hypo, selHypo );
186   myDlg->selectedObject( SMESHGUI_InitMeshDlg::Algo, selAlgo );
187
188   _PTR(Study) study = studyDS();
189   _PTR(SObject) aGeomSO = study->FindObjectID( selGeom.first() );
190   GEOM::GEOM_Object_var myGeomShape = GEOM::GEOM_Object::_narrow( _CAST(SObject,aGeomSO)->GetObject() );
191     
192   // create mesh
193   SMESH::SMESH_Mesh_var aMesh = initMesh(myGeomShape, myNameMesh);
194
195   if (!aMesh->_is_nil())
196   {
197     // assign hypotheses
198     for (int i = 0; i < selHypo.count(); i++)
199     {
200       _PTR(SObject) aHypSOClient =
201         study->FindObjectID(selHypo[i].latin1());
202       if (aHypSOClient)
203       {
204         CORBA::Object_var anObject = _CAST(SObject,aHypSOClient)->GetObject();
205         if (!CORBA::is_nil(anObject))
206         {
207           SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
208           if (!aHyp->_is_nil())
209           {
210             if (!SMESH::AddHypothesisOnMesh(aMesh, aHyp))
211               return false;
212           }
213         }
214       }
215     }
216     // assign algorithms
217     for (int i = 0; i < selAlgo.count(); i++)
218     {
219       _PTR(SObject) aHypSOClient =
220         study->FindObjectID(selAlgo[i].latin1());
221       if (aHypSOClient)
222       {
223         CORBA::Object_var anObject = _CAST(SObject,aHypSOClient)->GetObject();
224         if (!CORBA::is_nil(anObject))
225         {
226           SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
227           if (!aHyp->_is_nil())
228           {
229             if (!SMESH::AddHypothesisOnMesh(aMesh, aHyp))
230               return false;
231           }
232         }
233       }
234     }
235   }
236
237   update( UF_Model | UF_ObjBrowser );
238
239   init();
240   return true;
241 }
242
243 //=================================================================================
244 // function : defaultMeshName()
245 // purpose  :
246 //=================================================================================
247 QString SMESHGUI_InitMeshOp::defaultMeshName() const
248 {
249   _PTR(Study) aStudy = studyDS();
250   int aNumber = 0;
251   QString aMeshName;
252   _PTR(SObject) obj;
253
254   do {
255     aMeshName = QString(tr("SMESH_OBJECT_MESH")) + "_" + QString::number(++aNumber);
256     obj = aStudy->FindObject(aMeshName.latin1());
257   } while (obj);
258
259   return aMeshName;
260 }
261
262 //=================================================================================
263 // function : init()
264 // purpose  :
265 //=================================================================================
266 void SMESHGUI_InitMeshOp::init()
267 {
268   if( myDlg )
269   {
270     myDlg->setMeshName( defaultMeshName() );
271     myDlg->clearSelection();
272     myDlg->updateControlState();
273   }    
274 }
275
276 //=================================================================================
277 // function : defaultMeshName()
278 // purpose  :
279 //=================================================================================
280 SMESH::SMESH_Mesh_var SMESHGUI_InitMeshOp::initMesh ( GEOM::GEOM_Object_ptr theShapeObject,
281                                                       const QString& theMeshName )
282 {
283   SMESH::SMESH_Mesh_var aMesh;
284   try
285   {
286     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
287     if (!aSMESHGen->_is_nil() && !theShapeObject->_is_nil())
288     {
289       aMesh = aSMESHGen->CreateMesh(theShapeObject);
290       if (!aMesh->_is_nil())
291       {
292         _PTR(SObject) aMeshSObject = SMESH::FindSObject(aMesh.in());
293         SMESH::SetName(aMeshSObject, theMeshName);
294       }
295     }
296   }
297   catch( const SALOME::SALOME_Exception& S_ex )
298   {
299     SalomeApp_Tools::QtCatchCorbaException(S_ex);
300   }
301   
302   return aMesh._retn();
303 }
304
305 //=================================================================================
306 // function : defaultMeshName()
307 // purpose  :
308 //=================================================================================
309 void SMESHGUI_InitMeshOp::onSelectionChanged( int id )
310 {
311   if( !myDlg->hasSelection( id ) )
312     return;
313     
314   if( id==SMESHGUI_InitMeshDlg::GeomObj )
315   {
316     QStringList selGeom;
317     myDlg->selectedObject( SMESHGUI_InitMeshDlg::GeomObj, selGeom );
318
319     _PTR(SObject) aGeomSO = studyDS()->FindObjectID( selGeom.first() );
320     GEOM::GEOM_Object_var myGeomShape = GEOM::GEOM_Object::_narrow( _CAST(SObject,aGeomSO)->GetObject() );
321     if( myGeomShape->_is_nil() || !GEOMBase::IsShape( myGeomShape ) )
322       myDlg->clearSelection( id );
323   }
324 }
325
326 //=================================================================================
327 // function : isValid
328 // purpose  :
329 //=================================================================================
330 bool SMESHGUI_InitMeshOp::isValid( SUIT_Operation* theOtherOp ) const
331 {
332   if ( theOtherOp && theOtherOp->inherits( "SMESHGUI_AddSubMeshOp" ) )
333     return true;
334   else
335     return false;
336     
337 }
338
339
340
341
342
343
344
345
346
347
348
349
350