1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESHGUI_AddSubMeshDlg.cxx
25 // Author : Nicolas REJNERI
29 #include "SMESHGUI_AddSubMeshOp.h"
30 #include <SMESHGUI_AddSubMeshDlg.h>
31 #include <SMESHGUI_GEOMGenUtils.h>
32 #include <SMESHGUI_HypothesesUtils.h>
33 #include <SMESHGUI_Utils.h>
36 #include <SMESH_NumberFilter.hxx>
37 #include <SMESH_TypeFilter.hxx>
39 #include <SUIT_SelectionFilter.h>
40 #include <SUIT_OverrideCursor.h>
41 #include <SUIT_MessageBox.h>
43 #include <SalomeApp_SelectionMgr.h>
44 #include <SalomeApp_UpdateFlags.h>
45 #include <SalomeApp_Tools.h>
47 #include <TColStd_MapOfInteger.hxx>
49 #include <SALOMEDS_SObject.hxx>
51 #include <utilities.h>
53 //=================================================================================
54 // function : Constructor
56 //=================================================================================
57 SMESHGUI_AddSubMeshOp::SMESHGUI_AddSubMeshOp()
58 : SMESHGUI_Operation(),
62 myHypothesisFilter( 0 ),
63 myAlgorithmFilter( 0 )
67 //=================================================================================
68 // function : Destructor
70 //=================================================================================
71 SMESHGUI_AddSubMeshOp::~SMESHGUI_AddSubMeshOp()
82 if( myAlgorithmFilter )
83 delete myAlgorithmFilter;
85 if( myHypothesisFilter )
86 delete myHypothesisFilter;
89 //=================================================================================
90 // function : startOperation
92 //=================================================================================
93 void SMESHGUI_AddSubMeshOp::startOperation()
97 myDlg = new SMESHGUI_AddSubMeshDlg( getSMESHGUI() );
98 connect( myDlg, SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
99 connect( myDlg, SIGNAL( selectionChanged( int ) ), this, SLOT( onSelectionChanged( int ) ) );
102 SMESHGUI_Operation::startOperation();
106 TColStd_MapOfInteger allTypesMap;
107 for (int i = 0; i < 10; i++)
109 myGeomFilter = new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, 0, allTypesMap);
113 myMeshFilter = new SMESH_TypeFilter (MESH);
115 if( !myAlgorithmFilter )
116 myAlgorithmFilter = new SMESH_TypeFilter (ALGORITHM);
118 if( !myHypothesisFilter )
119 myHypothesisFilter = new SMESH_TypeFilter (HYPOTHESIS);
125 //=================================================================================
128 //=================================================================================
129 SalomeApp_Dialog* SMESHGUI_AddSubMeshOp::dlg() const
134 //=================================================================================
135 // function : selectionDone
137 //=================================================================================
138 void SMESHGUI_AddSubMeshOp::selectionDone()
140 QStringList names, ids;
141 SMESHGUI_Dialog::TypesList types;
142 selected( names, types, ids );
145 myDlg->selectObject( names, types, ids );
146 myDlg->updateControlState( isValid() );
150 //=================================================================================
151 // function : onActivateObject
153 //=================================================================================
154 void SMESHGUI_AddSubMeshOp::onActivateObject( int obj )
156 SalomeApp_SelectionMgr* mgr = selectionMgr();
163 if( obj==SMESHGUI_AddSubMeshDlg::MeshObj )
164 mgr->installFilter( myMeshFilter );
166 else if( obj==SMESHGUI_AddSubMeshDlg::GeomObj )
167 mgr->installFilter( myGeomFilter );
169 else if( obj==SMESHGUI_AddSubMeshDlg::Hypo )
170 mgr->installFilter( myHypothesisFilter );
172 else if( obj==SMESHGUI_AddSubMeshDlg::Algo )
173 mgr->installFilter( myAlgorithmFilter );
176 //=================================================================================
177 // function : commitOperation
179 //=================================================================================
180 bool SMESHGUI_AddSubMeshOp::onApply()
182 if( getSMESHGUI()->isActiveStudyLocked() )
185 QString myNameSubMesh = myDlg->subMeshName();
186 if (myNameSubMesh.isEmpty())
188 SUIT_MessageBox::warn1( dlg(), tr("SMESH_WRN_WARNING"),
189 tr("SMESH_WRN_EMPTY_NAME"), tr("SMESH_BUT_OK"));
193 QStringList selMesh, selGeom, selHypo, selAlgo;
194 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::MeshObj, selMesh );
195 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::GeomObj, selGeom );
196 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::Hypo, selHypo );
197 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::Algo, selAlgo );
199 _PTR(Study) study = studyDS();
200 _PTR(SObject) aMeshSO = study->FindObjectID( selMesh.first() );
201 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( _CAST( SObject, aMeshSO )->GetObject() );
202 GEOM::GEOM_Object_var myMainShape = SMESH::GetShapeOnMeshOrSubMesh(aMeshSO);
203 if (myMainShape->_is_nil())
206 SUIT_OverrideCursor wc;
208 _PTR(SObject) aGeomSO = study->FindObjectID( selGeom.first() );
209 GEOM::GEOM_Object_var myGeomShape = GEOM::GEOM_Object::_narrow( _CAST( SObject, aGeomSO )->GetObject() );
212 SMESH::SMESH_subMesh_var aSubMesh = addSubMesh(myMesh, myGeomShape, myNameSubMesh);
215 if (!aSubMesh->_is_nil())
218 int nbAlgo = selAlgo.count();
219 int nbHyps = selHypo.count() + nbAlgo;
220 for (int i = 0; i < nbHyps; i++)
222 _PTR(SObject) aHypSOClient =
224 (i < nbAlgo ? selAlgo[i].latin1() : selHypo[i-nbAlgo].latin1());
227 CORBA::Object_var anObject = _CAST(SObject,aHypSOClient)->GetObject();
228 if(!CORBA::is_nil(anObject))
230 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
232 if (SMESH::AddHypothesisOnSubMesh(aSubMesh, aHyp))
237 SCRUTE(CORBA::is_nil(anObject));
242 SCRUTE(!aHypSOClient);
248 SCRUTE(aSubMesh->_is_nil());
251 update( UF_Model | UF_ObjBrowser );
254 return (nbSuccess > 0);
257 //=================================================================================
260 //=================================================================================
261 void SMESHGUI_AddSubMeshOp::init()
266 myDlg->updateControlState( false );
270 //=================================================================================
271 // function : isValid()
273 //=================================================================================
274 bool SMESHGUI_AddSubMeshOp::isValid() const
276 bool isEnabled = !myDlg->subMeshName().isEmpty() &&
277 myDlg->hasSelection( SMESHGUI_AddSubMeshDlg::MeshObj ) &&
278 myDlg->hasSelection( SMESHGUI_AddSubMeshDlg::GeomObj ) &&
279 myDlg->hasSelection( SMESHGUI_AddSubMeshDlg::Hypo ) &&
280 myDlg->hasSelection( SMESHGUI_AddSubMeshDlg::Algo );
282 bool isImportedMesh = false;
285 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::MeshObj, selMesh );
286 _PTR(SObject) SO = studyDS()->FindObjectID( selMesh.first() );
287 GEOM::GEOM_Object_var myGeomShape = SMESH::GetShapeOnMeshOrSubMesh(SO);
288 isImportedMesh = myGeomShape->_is_nil();
290 return isEnabled && !isImportedMesh;
293 //=================================================================================
294 // function : IsFatherOf()
296 //=================================================================================
297 static bool IsFatherOf (_PTR(SObject) SO, _PTR(SObject) fatherSO)
301 _PTR(SObject) aSO = SO->GetFather();
302 while (aSO->GetID().length() >= fatherSO->GetID().length())
304 if (aSO->GetID() == fatherSO->GetID())
306 aSO = aSO->GetFather();
312 //=================================================================================
313 // function : onSelectionChanged()
315 //=================================================================================
316 void SMESHGUI_AddSubMeshOp::onSelectionChanged( int id )
318 if( !myDlg->hasSelection( id ) )
321 if( id==SMESHGUI_AddSubMeshDlg::MeshObj )
322 myDlg->clearSelection( SMESHGUI_AddSubMeshDlg::GeomObj );
324 else if( id==SMESHGUI_AddSubMeshDlg::GeomObj )
326 QStringList selMesh, selGeom;
327 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::MeshObj, selMesh );
328 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::GeomObj, selGeom );
330 _PTR(SObject) aMeshSO = studyDS()->FindObjectID( selMesh.first() );
332 GEOM::GEOM_Object_var aMainGeomShape = SMESH::GetShapeOnMeshOrSubMesh(aMeshSO);
333 _PTR(SObject) aMainGeomShapeSO = SMESH::FindSObject(aMainGeomShape);
334 if( !aMainGeomShapeSO || !IsFatherOf( studyDS()->FindObjectID( selGeom.first() ), aMainGeomShapeSO ) )
335 myDlg->clearSelection( id );
339 //=================================================================================
340 // function : onSelectionChanged()
342 //=================================================================================
343 SMESH::SMESH_subMesh_var SMESHGUI_AddSubMeshOp::addSubMesh( SMESH::SMESH_Mesh_ptr theMesh,
344 GEOM::GEOM_Object_ptr theShapeObject,
345 const QString& theMeshName )
347 SMESH::SMESH_subMesh_var aSubMesh;
350 if(!theMesh->_is_nil() && !theShapeObject->_is_nil())
351 aSubMesh = theMesh->GetSubMesh(theShapeObject, theMeshName);
353 catch (const SALOME::SALOME_Exception& S_ex)
355 SalomeApp_Tools::QtCatchCorbaException(S_ex);
358 return aSubMesh._retn();