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