]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESHGUI/SMESHGUI_EditHypothesesOp.cxx
Salome HOME
Dialogs were divided to "operation and dialog"
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_EditHypothesesOp.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_EditHypothesesOp.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
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>
36 #include <SMESHGUI.h>
37
38 #include <SalomeApp_UpdateFlags.h>
39
40 #include <SUIT_OverrideCursor.h>
41
42 #include <qlistbox.h>
43
44 #include <SALOMEDS_SObject.hxx>
45
46 #define ALLOW_CHANGE_SHAPE 0
47
48 //=================================================================================
49 // function : 
50 // purpose  :
51 //=================================================================================
52 SMESHGUI_EditHypothesesOp::SMESHGUI_EditHypothesesOp()
53 : SMESHGUI_SelectionOp(),
54   myDlg( 0 ),
55   myImportedMesh( false )
56 {
57 }
58
59 //=================================================================================
60 // function :
61 // purpose  :
62 //=================================================================================
63 SMESHGUI_EditHypothesesOp::~SMESHGUI_EditHypothesesOp()
64 {
65   if( myDlg )
66     delete myDlg;
67 }
68
69 //=================================================================================
70 // function :
71 // purpose  :
72 //=================================================================================
73 SalomeApp_Dialog* SMESHGUI_EditHypothesesOp::dlg() const
74 {
75   return myDlg;
76 }
77
78 //=================================================================================
79 // function :
80 // purpose  :
81 //=================================================================================
82 void SMESHGUI_EditHypothesesOp::startOperation()
83 {
84   if( !myDlg )
85   {
86     myDlg = new SMESHGUI_EditHypothesesDlg( getSMESHGUI() );
87     connect( myDlg, SIGNAL( needToUpdate() ), this, SLOT( onUpdate() ) );
88   }
89
90   SMESHGUI_SelectionOp::startOperation();
91
92   myDlg->show();
93 }
94
95 //=================================================================================
96 // function :
97 // purpose  :
98 //=================================================================================
99 void SMESHGUI_EditHypothesesOp::selectionDone()
100 {
101   SMESHGUI_SelectionOp::selectionDone();
102   if( myDlg )
103     updateDialog();
104 }
105
106 //=================================================================================
107 // function :
108 // purpose  :
109 //=================================================================================
110 bool SMESHGUI_EditHypothesesOp::onApply()
111 {
112   if( getSMESHGUI()->isActiveStudyLocked() )
113     return false;
114
115   bool aRes = false;
116
117   SUIT_OverrideCursor wc;
118
119   aRes = storeMeshOrSubMesh();
120   if( aRes )
121   {
122     initHypAssignation();
123     initAlgoAssignation();
124   }
125
126   update( UF_Model | UF_ObjBrowser );
127
128   initDialog();
129   return aRes;
130 }
131
132 //=================================================================================
133 // function : onSelectionChanged
134 // purpose  :
135 //=================================================================================
136 void SMESHGUI_EditHypothesesOp::onSelectionChanged( int id )
137 {
138   if( id==SMESHGUI_EditHypothesesDlg::MeshOrSubMesh )
139   {
140     initGeom();
141     myImportedMesh = !myDlg->hasSelection( SMESHGUI_EditHypothesesDlg::GeomShape );
142     initHypAssignation();
143     initAlgoAssignation();
144     updateDialog();
145   }
146   else if( id==SMESHGUI_EditHypothesesDlg::GeomShape )
147   {
148   }
149 }
150
151 //=================================================================================
152 // function : initGeom
153 // purpose  :
154 //=================================================================================
155 void SMESHGUI_EditHypothesesOp::initGeom()
156 {
157   QStringList selMesh; myDlg->selectedObject( SMESHGUI_EditHypothesesDlg::MeshOrSubMesh, selMesh );
158   if( selMesh.isEmpty() )
159     return;
160     
161   _PTR(SObject) aMesh = studyDS()->FindObjectID( selMesh.first() );
162   GEOM::GEOM_Object_var aGeomShape = SMESH::GetShapeOnMeshOrSubMesh( aMesh );
163   if( !aGeomShape->_is_nil() )
164   {
165     QString name, id = aGeomShape->GetStudyEntry();
166     
167     _PTR(GenericAttribute) anAttr;
168     _PTR(AttributeName)    aName;
169     _PTR(SObject) aSO = studyDS()->FindObjectID(aGeomShape->GetStudyEntry());
170     if (aSO)
171       if (aSO->FindAttribute(anAttr, "AttributeName"))
172       {
173         aName = anAttr;
174         name = aName->Value().c_str();
175       }
176       
177     int type = aGeomShape->GetType();
178     myDlg->selectObject( SMESHGUI_EditHypothesesDlg::GeomShape, name, type, id );
179   }
180
181
182 //=================================================================================
183 // function : initHypDefinition
184 // purpose  :
185 //=================================================================================
186 void SMESHGUI_EditHypothesesOp::initHypDefinition()
187 {
188   myDlg->ListHypDefinition->clear();
189
190   _PTR(Study) study = studyDS();  
191   _PTR(SComponent) father = study->FindComponent("SMESH");
192   if (!father)
193     return;
194
195   _PTR(SObject)          HypothesisRoot;
196   _PTR(GenericAttribute) anAttr;
197   _PTR(AttributeName)    aName;
198   _PTR(AttributeIOR)     anIOR;
199
200   //int Tag_HypothesisRoot = 1;
201   if (father->FindSubObject(1, HypothesisRoot)) {
202     _PTR(ChildIterator) it =
203       study->NewChildIterator(HypothesisRoot);
204     ListBoxIOR* anItem;
205     for (; it->More();it->Next()) {
206       _PTR(SObject) Obj = it->Value();
207       if (Obj->FindAttribute(anAttr, "AttributeName")) {
208         aName = anAttr;
209         if (Obj->FindAttribute(anAttr, "AttributeIOR")) {
210           anIOR = anAttr;
211           anItem = new ListBoxIOR (myDlg->ListHypDefinition,
212                                    anIOR->Value().c_str(),
213                                    aName->Value().c_str());
214         }
215       }
216     }
217   }
218 }
219
220 //=================================================================================
221 // function : initAlgoDefinition
222 // purpose  :
223 //=================================================================================
224 void SMESHGUI_EditHypothesesOp::initAlgoDefinition()
225 {
226   myDlg->ListAlgoDefinition->clear();
227
228   _PTR(Study) study = studyDS();  
229   _PTR(SComponent) father = study->FindComponent("SMESH");
230   if (!father)
231     return;
232
233   _PTR(SObject)          AlgorithmsRoot;
234   _PTR(GenericAttribute) anAttr;
235   _PTR(AttributeName)    aName;
236   _PTR(AttributeIOR)     anIOR;
237
238   if (father->FindSubObject (2, AlgorithmsRoot)) {
239     _PTR(ChildIterator) it =
240       study->NewChildIterator(AlgorithmsRoot);
241     ListBoxIOR* anItem;
242     for (; it->More();it->Next()) {
243       _PTR(SObject) Obj = it->Value();
244       if (Obj->FindAttribute(anAttr, "AttributeName")) {
245         aName = anAttr;
246         if (Obj->FindAttribute(anAttr, "AttributeIOR")) {
247           anIOR = anAttr;
248           anItem = new ListBoxIOR (myDlg->ListAlgoDefinition,
249                                    anIOR->Value().c_str(),
250                                    aName->Value().c_str());
251         }
252       }
253     }
254   }
255 }
256
257 //=================================================================================
258 // function : initHypAssignation
259 // purpose  :
260 //=================================================================================
261 void SMESHGUI_EditHypothesesOp::initHypAssignation()
262 {
263   myDlg->myNbModification = 0;
264
265   myDlg->myMapOldHypos.clear();
266   myDlg->ListHypAssignation->clear();
267   if (myImportedMesh)
268     return;
269
270   _PTR(Study) study = studyDS();  
271   _PTR(SObject)          aMorSM, AHR, aRef;
272   _PTR(GenericAttribute) anAttr;
273   _PTR(AttributeName)    aName;
274   _PTR(AttributeIOR)     anIOR;
275
276   QStringList selMesh;
277   myDlg->selectedObject( SMESHGUI_EditHypothesesDlg::MeshOrSubMesh, selMesh );
278   aMorSM = studyDS()->FindObjectID( selMesh.first() );
279   
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")) {
287           aName = anAttr;
288           if (aRef->FindAttribute(anAttr, "AttributeIOR")) {
289             anIOR = anAttr;
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);
294           }
295         }
296       }
297     }
298   }
299 }
300
301 //=================================================================================
302 // function : initAlgoAssignation
303 // purpose  :
304 //=================================================================================
305 void SMESHGUI_EditHypothesesOp::initAlgoAssignation()
306 {
307   myDlg->myMapOldAlgos.clear();
308   myDlg->ListAlgoAssignation->clear();
309   if (myImportedMesh)
310     return;
311
312   _PTR(Study) study = studyDS();  
313   _PTR(SObject)          aMorSM, AHR, aRef;
314   _PTR(GenericAttribute) anAttr;
315   _PTR(AttributeName)    aName;
316   _PTR(AttributeIOR)     anIOR;
317
318   QStringList selMesh;
319   myDlg->selectedObject( SMESHGUI_EditHypothesesDlg::MeshOrSubMesh, selMesh );
320   aMorSM = studyDS()->FindObjectID( selMesh.first() );
321   
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")) {
329           aName = anAttr;
330           if (aRef->FindAttribute(anAttr, "AttributeIOR")) {
331             anIOR = anAttr;
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);
336           }
337         }
338       }
339     }
340   }
341 }
342
343 //=================================================================================
344 // function : createFilter
345 // purpose  :
346 //=================================================================================
347 SUIT_SelectionFilter* SMESHGUI_EditHypothesesOp::createFilter( const int id ) const
348 {
349   if( id==SMESHGUI_EditHypothesesDlg::GeomShape )
350   {
351     TColStd_MapOfInteger allTypesMap;
352     for (int i = 0; i < 10; i++)
353       allTypesMap.Add(i);
354     return new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, 0, allTypesMap);
355   }
356   
357   else if( id==SMESHGUI_EditHypothesesDlg::MeshOrSubMesh )
358     return new SMESH_TypeFilter (MESHorSUBMESH);
359
360   else
361     return 0;
362 }
363
364 //=================================================================================
365 // function : updateDialog
366 // purpose  :
367 //=================================================================================
368 void SMESHGUI_EditHypothesesOp::updateDialog()
369 {
370   bool isEnabled = myDlg->hasSelection( SMESHGUI_EditHypothesesDlg::MeshOrSubMesh ) &&
371                    myDlg->hasSelection( SMESHGUI_EditHypothesesDlg::GeomShape ) &&
372                    ( myDlg->hypoCount() || myDlg->algoCount() );
373
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 );
377 }
378
379 //=================================================================================
380 // function : initDialog
381 // purpose  :
382 //=================================================================================
383 void SMESHGUI_EditHypothesesOp::initDialog()
384 {
385   SMESHGUI_SelectionOp::initDialog();
386
387   initHypDefinition();
388   initAlgoDefinition();
389   
390   updateDialog();
391 }
392
393 //=================================================================================
394 // function : storeMeshOrSubMesh
395 // purpose  :
396 //=================================================================================
397 bool SMESHGUI_EditHypothesesOp::storeMeshOrSubMesh()
398 {
399   MapIOR anOldHypos, aNewHypos;
400   if( !myDlg->hasSelection( SMESHGUI_EditHypothesesDlg::GeomShape ) )
401     return false;
402
403   QStringList selMesh, selGeom;
404   myDlg->selectedObject( SMESHGUI_EditHypothesesDlg::MeshOrSubMesh, selMesh );
405   myDlg->selectedObject( SMESHGUI_EditHypothesesDlg::GeomShape, selGeom );
406
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() );
410
411   GEOM::GEOM_Object_var aGeomShape = GEOM::GEOM_Object::_narrow( _CAST(SObject,aGeom)->GetObject() );
412   
413   GEOM::GEOM_Object_var aIniGeomShape = SMESH::GetShapeOnMeshOrSubMesh(SO);
414   bool bShapeChanged = aIniGeomShape->_is_nil() || !aIniGeomShape->_is_equivalent(aGeomShape);
415   if (bShapeChanged) {
416     // VSR : TODO : Set new shape - not supported yet by engine
417     // 1. remove all old hypotheses and algorithms
418     // 2. set new shape
419   }
420   int nbFail = 0;
421   MapIOR::iterator it;
422
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);
427     if (index < 0) {
428       SMESH::SMESH_Hypothesis_var aHyp = SMESH::IORToInterface<SMESH::SMESH_Hypothesis>(ior.latin1());
429       if (!aHyp->_is_nil()) {
430         if (!SMESH::RemoveHypothesisOrAlgorithmOnMesh( SO, aHyp))
431           nbFail++;
432       }
433     }
434   }
435
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);
440     if (index < 0) {
441       SMESH::SMESH_Hypothesis_var aHyp = SMESH::IORToInterface<SMESH::SMESH_Hypothesis>(ior.latin1());
442       if (!aHyp->_is_nil()){
443         if (!SMESH::RemoveHypothesisOrAlgorithmOnMesh( SO, aHyp ) )
444           nbFail++;
445       }
446     }
447   }
448
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() );
451
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));
456       if (anItem) {
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))
462               nbFail++;
463       if( !aSubMesh->_is_nil() && !SMESH::AddHypothesisOnSubMesh( aSubMesh, aHyp))
464               nbFail++;
465           }
466         }
467       }
468     }
469   }
470
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));
475       if (anItem) {
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))
481               nbFail++;
482       if( !aSubMesh->_is_nil() && !SMESH::AddHypothesisOnSubMesh( aSubMesh, aHyp))
483               nbFail++;
484           }
485         }
486       }
487     }
488   }
489   return (nbFail == 0);
490 }
491
492 //=================================================================================
493 // function : onUpdate
494 // purpose  :
495 //=================================================================================
496 void SMESHGUI_EditHypothesesOp::onUpdate()
497 {
498   updateDialog();
499 }