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_EditHypothesesOp.cxx
25 // Author : Nicolas REJNERI
29 #include "SMESHGUI_EditHypothesesOp.h"
30 #include <SMESHGUI_EditHypothesesDlg.h>
31 #include <SMESH_NumberFilter.hxx>
32 #include <SMESH_TypeFilter.hxx>
33 #include <SMESHGUI_GEOMGenUtils.h>
34 #include <SMESHGUI_HypothesesUtils.h>
35 #include <SMESHGUI_Utils.h>
38 #include <SalomeApp_UpdateFlags.h>
40 #include <SUIT_OverrideCursor.h>
44 #include <SALOMEDS_SObject.hxx>
46 #define ALLOW_CHANGE_SHAPE 0
48 //=================================================================================
51 //=================================================================================
52 SMESHGUI_EditHypothesesOp::SMESHGUI_EditHypothesesOp()
53 : SMESHGUI_SelectionOp(),
55 myImportedMesh( false )
59 //=================================================================================
62 //=================================================================================
63 SMESHGUI_EditHypothesesOp::~SMESHGUI_EditHypothesesOp()
69 //=================================================================================
72 //=================================================================================
73 SalomeApp_Dialog* SMESHGUI_EditHypothesesOp::dlg() const
78 //=================================================================================
81 //=================================================================================
82 void SMESHGUI_EditHypothesesOp::startOperation()
86 myDlg = new SMESHGUI_EditHypothesesDlg();
87 connect( myDlg, SIGNAL( needToUpdate() ), this, SLOT( onUpdate() ) );
90 SMESHGUI_SelectionOp::startOperation();
95 //=================================================================================
98 //=================================================================================
99 void SMESHGUI_EditHypothesesOp::selectionDone()
101 SMESHGUI_SelectionOp::selectionDone();
106 //=================================================================================
109 //=================================================================================
110 bool SMESHGUI_EditHypothesesOp::onApply()
112 if( getSMESHGUI()->isActiveStudyLocked() )
117 SUIT_OverrideCursor wc;
119 aRes = storeMeshOrSubMesh();
122 initHypAssignation();
123 initAlgoAssignation();
126 update( UF_Model | UF_ObjBrowser );
132 //=================================================================================
133 // function : onSelectionChanged
135 //=================================================================================
136 void SMESHGUI_EditHypothesesOp::onSelectionChanged( int id )
138 if( id==SMESHGUI_EditHypothesesDlg::MeshOrSubMesh )
141 myImportedMesh = !myDlg->hasSelection( SMESHGUI_EditHypothesesDlg::GeomShape );
142 initHypAssignation();
143 initAlgoAssignation();
146 else if( id==SMESHGUI_EditHypothesesDlg::GeomShape )
151 //=================================================================================
152 // function : initGeom
154 //=================================================================================
155 void SMESHGUI_EditHypothesesOp::initGeom()
157 QStringList selMesh; myDlg->selectedObject( SMESHGUI_EditHypothesesDlg::MeshOrSubMesh, selMesh );
158 if( selMesh.isEmpty() )
161 _PTR(SObject) aMesh = studyDS()->FindObjectID( selMesh.first() );
162 GEOM::GEOM_Object_var aGeomShape = SMESH::GetShapeOnMeshOrSubMesh( aMesh );
163 if( !aGeomShape->_is_nil() )
165 QString name, id = aGeomShape->GetStudyEntry();
167 _PTR(GenericAttribute) anAttr;
168 _PTR(AttributeName) aName;
169 _PTR(SObject) aSO = studyDS()->FindObjectID(aGeomShape->GetStudyEntry());
171 if (aSO->FindAttribute(anAttr, "AttributeName"))
174 name = aName->Value().c_str();
177 int type = aGeomShape->GetType();
178 myDlg->selectObject( SMESHGUI_EditHypothesesDlg::GeomShape, name, type, id );
182 //=================================================================================
183 // function : initHypDefinition
185 //=================================================================================
186 void SMESHGUI_EditHypothesesOp::initHypDefinition()
188 myDlg->ListHypDefinition->clear();
190 _PTR(Study) study = studyDS();
191 _PTR(SComponent) father = study->FindComponent("SMESH");
195 _PTR(SObject) HypothesisRoot;
196 _PTR(GenericAttribute) anAttr;
197 _PTR(AttributeName) aName;
198 _PTR(AttributeIOR) anIOR;
200 //int Tag_HypothesisRoot = 1;
201 if (father->FindSubObject(1, HypothesisRoot)) {
202 _PTR(ChildIterator) it =
203 study->NewChildIterator(HypothesisRoot);
205 for (; it->More();it->Next()) {
206 _PTR(SObject) Obj = it->Value();
207 if (Obj->FindAttribute(anAttr, "AttributeName")) {
209 if (Obj->FindAttribute(anAttr, "AttributeIOR")) {
211 anItem = new ListBoxIOR (myDlg->ListHypDefinition,
212 anIOR->Value().c_str(),
213 aName->Value().c_str());
220 //=================================================================================
221 // function : initAlgoDefinition
223 //=================================================================================
224 void SMESHGUI_EditHypothesesOp::initAlgoDefinition()
226 myDlg->ListAlgoDefinition->clear();
228 _PTR(Study) study = studyDS();
229 _PTR(SComponent) father = study->FindComponent("SMESH");
233 _PTR(SObject) AlgorithmsRoot;
234 _PTR(GenericAttribute) anAttr;
235 _PTR(AttributeName) aName;
236 _PTR(AttributeIOR) anIOR;
238 if (father->FindSubObject (2, AlgorithmsRoot)) {
239 _PTR(ChildIterator) it =
240 study->NewChildIterator(AlgorithmsRoot);
242 for (; it->More();it->Next()) {
243 _PTR(SObject) Obj = it->Value();
244 if (Obj->FindAttribute(anAttr, "AttributeName")) {
246 if (Obj->FindAttribute(anAttr, "AttributeIOR")) {
248 anItem = new ListBoxIOR (myDlg->ListAlgoDefinition,
249 anIOR->Value().c_str(),
250 aName->Value().c_str());
257 //=================================================================================
258 // function : initHypAssignation
260 //=================================================================================
261 void SMESHGUI_EditHypothesesOp::initHypAssignation()
263 myDlg->myNbModification = 0;
265 myDlg->myMapOldHypos.clear();
266 myDlg->ListHypAssignation->clear();
270 _PTR(Study) study = studyDS();
271 _PTR(SObject) aMorSM, AHR, aRef;
272 _PTR(GenericAttribute) anAttr;
273 _PTR(AttributeName) aName;
274 _PTR(AttributeIOR) anIOR;
277 myDlg->selectedObject( SMESHGUI_EditHypothesesDlg::MeshOrSubMesh, selMesh );
278 aMorSM = studyDS()->FindObjectID( selMesh.first() );
280 if (aMorSM && aMorSM->FindSubObject(2, AHR)) {
281 _PTR(ChildIterator) it =
282 study->NewChildIterator(AHR);
283 for (; it->More();it->Next()) {
284 _PTR(SObject) Obj = it->Value();
285 if (Obj->ReferencedObject(aRef)) {
286 if (aRef->FindAttribute(anAttr, "AttributeName")) {
288 if (aRef->FindAttribute(anAttr, "AttributeIOR")) {
290 ListBoxIOR* anItem = new ListBoxIOR (myDlg->ListHypAssignation,
291 anIOR->Value().c_str(),
292 aName->Value().c_str());
293 myDlg->myMapOldHypos[ anIOR->Value() ] = myDlg->ListHypAssignation->index(anItem);
301 //=================================================================================
302 // function : initAlgoAssignation
304 //=================================================================================
305 void SMESHGUI_EditHypothesesOp::initAlgoAssignation()
307 myDlg->myMapOldAlgos.clear();
308 myDlg->ListAlgoAssignation->clear();
312 _PTR(Study) study = studyDS();
313 _PTR(SObject) aMorSM, AHR, aRef;
314 _PTR(GenericAttribute) anAttr;
315 _PTR(AttributeName) aName;
316 _PTR(AttributeIOR) anIOR;
319 myDlg->selectedObject( SMESHGUI_EditHypothesesDlg::MeshOrSubMesh, selMesh );
320 aMorSM = studyDS()->FindObjectID( selMesh.first() );
322 if (aMorSM && aMorSM->FindSubObject(3, AHR)) {
323 _PTR(ChildIterator) it =
324 study->NewChildIterator(AHR);
325 for (; it->More();it->Next()) {
326 _PTR(SObject) Obj = it->Value();
327 if (Obj->ReferencedObject(aRef)) {
328 if (aRef->FindAttribute(anAttr, "AttributeName")) {
330 if (aRef->FindAttribute(anAttr, "AttributeIOR")) {
332 ListBoxIOR* anItem = new ListBoxIOR (myDlg->ListAlgoAssignation,
333 anIOR->Value().c_str(),
334 aName->Value().c_str());
335 myDlg->myMapOldAlgos[ anIOR->Value() ] = myDlg->ListAlgoAssignation->index(anItem);
343 //=================================================================================
344 // function : createFilter
346 //=================================================================================
347 SUIT_SelectionFilter* SMESHGUI_EditHypothesesOp::createFilter( const int id ) const
349 if( id==SMESHGUI_EditHypothesesDlg::GeomShape )
351 TColStd_MapOfInteger allTypesMap;
352 for (int i = 0; i < 10; i++)
354 return new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, 0, allTypesMap);
357 else if( id==SMESHGUI_EditHypothesesDlg::MeshOrSubMesh )
358 return new SMESH_TypeFilter (MESHorSUBMESH);
364 //=================================================================================
365 // function : updateDialog
367 //=================================================================================
368 void SMESHGUI_EditHypothesesOp::updateDialog()
370 bool isEnabled = myDlg->hasSelection( SMESHGUI_EditHypothesesDlg::MeshOrSubMesh ) &&
371 myDlg->hasSelection( SMESHGUI_EditHypothesesDlg::GeomShape ) &&
372 ( myDlg->hypoCount() || myDlg->algoCount() );
374 myDlg->setButtonEnabled( myDlg->isModified() && isEnabled && !myImportedMesh, QtxDialog::OK | QtxDialog::Apply );
375 myDlg->setObjectEnabled( SMESHGUI_EditHypothesesDlg::GeomShape, ALLOW_CHANGE_SHAPE && !myImportedMesh );
376 myDlg->setListsEnabled( myDlg->hasSelection( SMESHGUI_EditHypothesesDlg::MeshOrSubMesh ) && !myImportedMesh );
379 //=================================================================================
380 // function : initDialog
382 //=================================================================================
383 void SMESHGUI_EditHypothesesOp::initDialog()
385 SMESHGUI_SelectionOp::initDialog();
388 initAlgoDefinition();
393 //=================================================================================
394 // function : storeMeshOrSubMesh
396 //=================================================================================
397 bool SMESHGUI_EditHypothesesOp::storeMeshOrSubMesh()
399 MapIOR anOldHypos, aNewHypos;
400 if( !myDlg->hasSelection( SMESHGUI_EditHypothesesDlg::GeomShape ) )
403 QStringList selMesh, selGeom;
404 myDlg->selectedObject( SMESHGUI_EditHypothesesDlg::MeshOrSubMesh, selMesh );
405 myDlg->selectedObject( SMESHGUI_EditHypothesesDlg::GeomShape, selGeom );
407 // 1. Check whether the geometric shape has changed
408 _PTR(SObject) SO = studyDS()->FindObjectID( selMesh.first() );
409 _PTR(SObject) aGeom = studyDS()->FindObjectID( selGeom.first() );
411 GEOM::GEOM_Object_var aGeomShape = GEOM::GEOM_Object::_narrow( _CAST(SObject,aGeom)->GetObject() );
413 GEOM::GEOM_Object_var aIniGeomShape = SMESH::GetShapeOnMeshOrSubMesh(SO);
414 bool bShapeChanged = aIniGeomShape->_is_nil() || !aIniGeomShape->_is_equivalent(aGeomShape);
416 // VSR : TODO : Set new shape - not supported yet by engine
417 // 1. remove all old hypotheses and algorithms
423 // 2. remove not used hypotheses from the submesh
424 for (it = myDlg->myMapOldHypos.begin(); it != myDlg->myMapOldHypos.end(); ++it) {
425 QString ior = it.key();
426 int index = myDlg->findItem(myDlg->ListHypAssignation, ior);
428 SMESH::SMESH_Hypothesis_var aHyp = SMESH::IORToInterface<SMESH::SMESH_Hypothesis>(ior.latin1());
429 if (!aHyp->_is_nil()) {
430 if (!SMESH::RemoveHypothesisOrAlgorithmOnMesh( SO, aHyp))
436 // 3. remove not used algorithms from the submesh
437 for (it = myDlg->myMapOldAlgos.begin(); it != myDlg->myMapOldAlgos.end(); ++it) {
438 QString ior = it.key();
439 int index = myDlg->findItem(myDlg->ListAlgoAssignation, ior);
441 SMESH::SMESH_Hypothesis_var aHyp = SMESH::IORToInterface<SMESH::SMESH_Hypothesis>(ior.latin1());
442 if (!aHyp->_is_nil()){
443 if (!SMESH::RemoveHypothesisOrAlgorithmOnMesh( SO, aHyp ) )
449 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( _CAST(SObject,SO)->GetObject() );
450 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( _CAST(SObject,SO)->GetObject() );
452 // 4. Add new algorithms
453 for (int i = 0; i < myDlg->ListAlgoAssignation->count(); i++) {
454 if (myDlg->ListAlgoAssignation->item(i)->rtti() == ListBoxIOR::RTTI_IOR) {
455 ListBoxIOR* anItem = (ListBoxIOR*)(myDlg->ListAlgoAssignation->item(i));
457 QString ior = anItem->GetIOR();
458 if (myDlg->myMapOldAlgos.find(ior) == myDlg->myMapOldAlgos.end()) {
459 SMESH::SMESH_Hypothesis_var aHyp = SMESH::IORToInterface<SMESH::SMESH_Hypothesis>(ior.latin1());
460 if (!aHyp->_is_nil()){
461 if( !aMesh->_is_nil() && !SMESH::AddHypothesisOnMesh( aMesh, aHyp))
463 if( !aSubMesh->_is_nil() && !SMESH::AddHypothesisOnSubMesh( aSubMesh, aHyp))
471 // 5. Add new hypotheses
472 for (int i = 0; i < myDlg->ListHypAssignation->count(); i++) {
473 if (myDlg->ListHypAssignation->item(i)->rtti() == ListBoxIOR::RTTI_IOR) {
474 ListBoxIOR* anItem = (ListBoxIOR*)(myDlg->ListHypAssignation->item(i));
476 QString ior = anItem->GetIOR();
477 if (myDlg->myMapOldHypos.find(ior) == myDlg->myMapOldHypos.end()) {
478 SMESH::SMESH_Hypothesis_var aHyp = SMESH::IORToInterface<SMESH::SMESH_Hypothesis>(ior.latin1());
479 if (!aHyp->_is_nil()){
480 if( !aMesh->_is_nil() && !SMESH::AddHypothesisOnMesh( aMesh, aHyp))
482 if( !aSubMesh->_is_nil() && !SMESH::AddHypothesisOnSubMesh( aSubMesh, aHyp))
489 return (nbFail == 0);
492 //=================================================================================
493 // function : onUpdate
495 //=================================================================================
496 void SMESHGUI_EditHypothesesOp::onUpdate()