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_AddSubMeshOp.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_SelectionOp(),
61 setAutoResumed( true );
64 //=================================================================================
65 // function : Destructor
67 //=================================================================================
68 SMESHGUI_AddSubMeshOp::~SMESHGUI_AddSubMeshOp()
74 //=================================================================================
75 // function : startOperation
77 //=================================================================================
78 void SMESHGUI_AddSubMeshOp::startOperation()
82 myDlg = new SMESHGUI_AddSubMeshDlg();
83 connect( myDlg, SIGNAL( nameChanged( const QString& ) ), this, SLOT( onNameChanged( const QString& ) ) );
86 SMESHGUI_SelectionOp::startOperation();
91 //=================================================================================
94 //=================================================================================
95 SalomeApp_Dialog* SMESHGUI_AddSubMeshOp::dlg() const
100 //=================================================================================
101 // function : selectionDone
103 //=================================================================================
104 void SMESHGUI_AddSubMeshOp::selectionDone()
106 SMESHGUI_SelectionOp::selectionDone();
111 //=================================================================================
112 // function : createFilter
114 //=================================================================================
115 SUIT_SelectionFilter* SMESHGUI_AddSubMeshOp::createFilter( const int id ) const
117 if( id==SMESHGUI_AddSubMeshDlg::GeomObj )
119 TColStd_MapOfInteger allTypesMap;
120 for (int i = 0; i < 10; i++)
122 return new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, 0, allTypesMap);
125 if( id==SMESHGUI_AddSubMeshDlg::MeshObj )
126 return new SMESH_TypeFilter (MESH);
128 else if( id==SMESHGUI_AddSubMeshDlg::Algo )
129 return new SMESH_TypeFilter (ALGORITHM);
131 else if( id==SMESHGUI_AddSubMeshDlg::Hypo )
132 return new SMESH_TypeFilter (HYPOTHESIS);
138 //=================================================================================
139 // function : commitOperation
141 //=================================================================================
142 bool SMESHGUI_AddSubMeshOp::onApply()
144 if( getSMESHGUI()->isActiveStudyLocked() )
147 QString myNameSubMesh = myDlg->subMeshName();
148 if (myNameSubMesh.isEmpty())
150 SUIT_MessageBox::warn1( dlg(), tr("SMESH_WRN_WARNING"),
151 tr("SMESH_WRN_EMPTY_NAME"), tr("SMESH_BUT_OK"));
155 QStringList selMesh, selGeom, selHypo, selAlgo;
156 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::MeshObj, selMesh );
157 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::GeomObj, selGeom );
158 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::Hypo, selHypo );
159 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::Algo, selAlgo );
161 _PTR(Study) study = studyDS();
162 _PTR(SObject) aMeshSO = study->FindObjectID( selMesh.first() );
163 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( _CAST( SObject, aMeshSO )->GetObject() );
164 GEOM::GEOM_Object_var myMainShape = SMESH::GetShapeOnMeshOrSubMesh(aMeshSO);
165 if (myMainShape->_is_nil())
168 SUIT_OverrideCursor wc;
170 _PTR(SObject) aGeomSO = study->FindObjectID( selGeom.first() );
171 GEOM::GEOM_Object_var myGeomShape = GEOM::GEOM_Object::_narrow( _CAST( SObject, aGeomSO )->GetObject() );
174 SMESH::SMESH_subMesh_var aSubMesh = addSubMesh(myMesh, myGeomShape, myNameSubMesh);
177 if (!aSubMesh->_is_nil())
180 int nbAlgo = selAlgo.count();
181 int nbHyps = selHypo.count() + nbAlgo;
182 for (int i = 0; i < nbHyps; i++)
184 _PTR(SObject) aHypSOClient =
186 (i < nbAlgo ? selAlgo[i].latin1() : selHypo[i-nbAlgo].latin1());
189 CORBA::Object_var anObject = _CAST(SObject,aHypSOClient)->GetObject();
190 if(!CORBA::is_nil(anObject))
192 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
194 if (SMESH::AddHypothesisOnSubMesh(aSubMesh, aHyp))
199 SCRUTE(CORBA::is_nil(anObject));
204 SCRUTE(!aHypSOClient);
210 SCRUTE(aSubMesh->_is_nil());
213 update( UF_Model | UF_ObjBrowser );
216 return (nbSuccess > 0);
219 //=================================================================================
220 // function : initDialog()
222 //=================================================================================
223 void SMESHGUI_AddSubMeshOp::initDialog()
225 SMESHGUI_SelectionOp::initDialog();
228 myDlg->setSubMeshName( tr( "SMESH_SUBMESH" ) );
233 //=================================================================================
234 // function : IsFatherOf()
236 //=================================================================================
237 static bool IsFatherOf (_PTR(SObject) SO, _PTR(SObject) fatherSO)
241 _PTR(SObject) aSO = SO->GetFather();
242 while (aSO->GetID().length() >= fatherSO->GetID().length())
244 if (aSO->GetID() == fatherSO->GetID())
246 aSO = aSO->GetFather();
252 //=================================================================================
253 // function : onSelectionChanged()
255 //=================================================================================
256 void SMESHGUI_AddSubMeshOp::onSelectionChanged( int id )
258 if( !myDlg->hasSelection( id ) )
261 if( id==SMESHGUI_AddSubMeshDlg::MeshObj )
262 myDlg->clearSelection( SMESHGUI_AddSubMeshDlg::GeomObj );
264 else if( id==SMESHGUI_AddSubMeshDlg::GeomObj )
266 QStringList selMesh, selGeom;
267 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::MeshObj, selMesh );
268 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::GeomObj, selGeom );
270 _PTR(SObject) aMeshSO = studyDS()->FindObjectID( selMesh.first() );
272 GEOM::GEOM_Object_var aMainGeomShape = SMESH::GetShapeOnMeshOrSubMesh(aMeshSO);
273 _PTR(SObject) aMainGeomShapeSO = SMESH::FindSObject(aMainGeomShape);
274 if( !aMainGeomShapeSO || !IsFatherOf( studyDS()->FindObjectID( selGeom.first() ), aMainGeomShapeSO ) )
275 myDlg->clearSelection( id );
279 //=================================================================================
280 // function : addSubMesh()
282 //=================================================================================
283 SMESH::SMESH_subMesh_var SMESHGUI_AddSubMeshOp::addSubMesh( SMESH::SMESH_Mesh_ptr theMesh,
284 GEOM::GEOM_Object_ptr theShapeObject,
285 const QString& theMeshName )
287 SMESH::SMESH_subMesh_var aSubMesh;
290 if(!theMesh->_is_nil() && !theShapeObject->_is_nil())
291 aSubMesh = theMesh->GetSubMesh(theShapeObject, theMeshName);
293 catch (const SALOME::SALOME_Exception& S_ex)
295 SalomeApp_Tools::QtCatchCorbaException(S_ex);
298 return aSubMesh._retn();
301 //=================================================================================
302 // function : updateDialog()
304 //=================================================================================
305 void SMESHGUI_AddSubMeshOp::updateDialog()
310 bool isEnabled = !myDlg->subMeshName().isEmpty() &&
311 myDlg->hasSelection( SMESHGUI_AddSubMeshDlg::MeshObj ) &&
312 myDlg->hasSelection( SMESHGUI_AddSubMeshDlg::GeomObj ) &&
313 myDlg->hasSelection( SMESHGUI_AddSubMeshDlg::Hypo ) &&
314 myDlg->hasSelection( SMESHGUI_AddSubMeshDlg::Algo );
316 bool isImportedMesh = false;
319 myDlg->selectedObject( SMESHGUI_AddSubMeshDlg::MeshObj, selMesh );
320 _PTR(SObject) SO = studyDS()->FindObjectID( selMesh.first() );
321 GEOM::GEOM_Object_var myGeomShape = SMESH::GetShapeOnMeshOrSubMesh(SO);
322 isImportedMesh = myGeomShape->_is_nil();
324 isEnabled = isEnabled && !isImportedMesh;
326 myDlg->setButtonEnabled( isEnabled, QtxDialog::OK | QtxDialog::Apply );
329 //=================================================================================
330 // function : onNameChanged()
332 //=================================================================================
333 void SMESHGUI_AddSubMeshOp::onNameChanged( const QString& )
338 //=================================================================================
339 // function : isValid
341 //=================================================================================
342 bool SMESHGUI_AddSubMeshOp::isValid( SUIT_Operation* theOtherOp ) const
344 //if ( theOtherOp && theOtherOp->inherits( "SMESHGUI_InitMeshOp" ) )