Salome HOME
Win32 Porting.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ShapeByMeshDlg.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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : SMESHGUI_ShapeByMeshDlg.cxx
25 //  Author : Edward AGAPOV
26 //  Module : SMESH
27
28 #include "SMESHGUI_ShapeByMeshDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_GEOMGenUtils.h"
32 #include "SMESHGUI_IdValidator.h"
33 #include "SMESHGUI_MeshUtils.h"
34 #include "SMESHGUI_Utils.h"
35 #include "SMESHGUI_VTKUtils.h"
36
37 #include "SMDS_Mesh.hxx"
38 #include "SMDS_MeshNode.hxx"
39 #include "SMESH_Actor.h"
40
41 #include "GEOMBase.h"
42 #include "GeometryGUI.h"
43
44 #include "LightApp_DataOwner.h"
45 #include "LightApp_SelectionMgr.h"
46 #include "SALOMEDSClient_SObject.hxx"
47 #include "SALOME_ListIO.hxx"
48 #include "SUIT_Desktop.h"
49 #include "SVTK_Selector.h"
50 #include "SVTK_ViewWindow.h"
51 #include "SVTK_ViewModel.h"
52 #include "SalomeApp_Tools.h"
53
54 // OCCT Includes
55 #include <TColStd_MapOfInteger.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <TopExp_Explorer.hxx>
58
59 // QT Includes
60 #include <qframe.h>
61 #include <qlayout.h>
62 #include <qlineedit.h>
63 #include <qpushbutton.h>
64 #include <qlabel.h>
65 #include <qradiobutton.h>
66 #include <qbuttongroup.h>
67 #include <qapplication.h>
68 #include <qstringlist.h>
69
70 using namespace std;
71
72 #define SPACING 5
73 #define MARGIN  10
74
75 enum { EDGE = 0, FACE, VOLUME };
76
77 /*!
78  * \brief Dialog to publish a sub-shape of the mesh main shape
79  *        by selecting mesh elements
80  */
81 SMESHGUI_ShapeByMeshDlg::SMESHGUI_ShapeByMeshDlg()
82   : SMESHGUI_Dialog( 0, false, true, OK | Close )
83 {
84   setCaption(tr("CAPTION"));
85
86   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame(), MARGIN, SPACING);
87
88   QFrame* aMainFrame = createMainFrame  (mainFrame());
89
90   aDlgLay->addWidget(aMainFrame);
91
92   aDlgLay->setStretchFactor(aMainFrame, 1);
93 }
94
95 //=======================================================================
96 // function : createMainFrame()
97 // purpose  : Create frame containing dialog's input fields
98 //=======================================================================
99 QFrame* SMESHGUI_ShapeByMeshDlg::createMainFrame (QWidget* theParent)
100 {
101   QFrame* aMainGrp = new QFrame(theParent, "main frame");
102   QGridLayout* aLayout = new QGridLayout(aMainGrp, 3, 2);
103   aLayout->setSpacing(6);
104   aLayout->setAutoAdd(false);
105
106   // elem type
107   myElemTypeGroup = new QButtonGroup(1, Qt::Vertical, aMainGrp, "Group types");
108   myElemTypeGroup->setTitle(tr("SMESH_ELEMENT_TYPE"));
109   myElemTypeGroup->setExclusive(true);
110
111   (new QRadioButton( tr("SMESH_EDGE")  , myElemTypeGroup))->setChecked(true);
112   new QRadioButton( tr("SMESH_FACE")  , myElemTypeGroup);
113   new QRadioButton( tr("SMESH_VOLUME"), myElemTypeGroup);
114
115   // element id
116   QLabel* anIdLabel = new QLabel( aMainGrp, "element id label");
117   anIdLabel->setText( tr("ELEMENT_ID") );
118   myElementId = new QLineEdit( aMainGrp, "element id");
119   if (!myIsMultipleAllowed)
120     myElementId->setValidator( new SMESHGUI_IdValidator( theParent, "id validator", 1 ));
121   else
122     myElementId->setValidator( new SMESHGUI_IdValidator( theParent, "id validator" ));
123
124   // shape name
125   QLabel* aNameLabel = new QLabel( aMainGrp, "geom name label");
126   aNameLabel->setText( tr("GEOMETRY_NAME") );
127   myGeomName = new QLineEdit( aMainGrp, "geom name");
128
129   aLayout->addMultiCellWidget(myElemTypeGroup, 0, 0, 0, 1);
130   aLayout->addWidget(anIdLabel,   1, 0);
131   aLayout->addWidget(myElementId, 1, 1);
132   aLayout->addWidget(aNameLabel,  2, 0);
133   aLayout->addWidget(myGeomName,  2, 1);
134
135   return aMainGrp;
136 }
137
138 //=======================================================================
139 // function : ~SMESHGUI_ShapeByMeshDlg()
140 // purpose  : Destructor
141 //=======================================================================
142 SMESHGUI_ShapeByMeshDlg::~SMESHGUI_ShapeByMeshDlg()
143 {
144   // no need to delete child widgets, Qt does it all for us
145 }
146
147 //================================================================================
148 /*!
149  * \brief Constructor
150 */
151 //================================================================================
152 SMESHGUI_ShapeByMeshOp::SMESHGUI_ShapeByMeshOp(bool isMultipleAllowed):
153   myIsMultipleAllowed(isMultipleAllowed)
154 {
155   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
156     GeometryGUI::InitGeomGen();
157
158   myDlg = new SMESHGUI_ShapeByMeshDlg;
159   myDlg->setMultipleAllowed(myIsMultipleAllowed);
160
161   connect(myDlg->myElemTypeGroup, SIGNAL(clicked(int)), SLOT(onTypeChanged(int)));
162   connect(myDlg->myElementId, SIGNAL(textChanged(const QString&)), SLOT(onElemIdChanged(const QString&)));
163 }
164
165
166 //=======================================================================
167 // function : startOperation()
168 // purpose  : Init dialog fields, connect signals and slots, show dialog
169 //=======================================================================
170 void SMESHGUI_ShapeByMeshOp::startOperation()
171 {
172   //SetMesh( SMESH::SMESH_Mesh::_nil() );
173   myIsManualIdEnter = false;
174
175   SMESHGUI_SelectionOp::startOperation();
176
177   //activateSelection(); // set filters
178   onSelectionDone(); // desable/enable [ OK ]
179
180   myDlg->show();
181 }
182
183 //================================================================================
184 /*!
185  * \brief Destructor
186 */
187 //================================================================================
188 SMESHGUI_ShapeByMeshOp::~SMESHGUI_ShapeByMeshOp()
189 {
190   if ( myDlg )
191     delete myDlg;
192 }
193
194 //================================================================================
195 /*!
196  * \brief Gets dialog of this operation
197   * \retval LightApp_Dialog* - pointer to dialog of this operation
198 */
199 //================================================================================
200 LightApp_Dialog* SMESHGUI_ShapeByMeshOp::dlg() const
201 {
202   return myDlg;
203 }
204
205 //=======================================================================
206 // function : GetShape()
207 // purpose  : Get published sub-shape
208 //=======================================================================
209 GEOM::GEOM_Object_ptr SMESHGUI_ShapeByMeshOp::GetShape()
210 {
211   return myGeomObj.in();
212 }
213
214 //=======================================================================
215 // function : SetMesh()
216 // purpose  : Set mesh to dialog
217 //=======================================================================
218
219 void SMESHGUI_ShapeByMeshOp::SetMesh (SMESH::SMESH_Mesh_ptr thePtr)
220 {
221   myMesh    = SMESH::SMESH_Mesh::_duplicate(thePtr);
222   myGeomObj = GEOM::GEOM_Object::_nil();
223   myHasSolids = false;
224
225   vector< bool > hasElement (myDlg->myElemTypeGroup->count(), false);
226   if (!myMesh->_is_nil() )
227   {
228 //     _PTR(SObject) aSobj = SMESH::FindSObject(myMesh.in());
229 //     SUIT_DataOwnerPtr anIObj (new LightApp_DataOwner(aSobj->GetID().c_str()));
230
231     vector< int > nbShapes( TopAbs_SHAPE, 0 );
232     int shapeDim = 0; // max dim with several shapes
233     //if ( /*mySelectionMgr*/ selectionMgr()->isOk(anIObj) ) // check that the mesh has a valid shape
234     {
235       _PTR(SObject) aSO = SMESH::FindSObject(myMesh.in());
236       GEOM::GEOM_Object_var mainShape = SMESH::GetGeom(aSO);
237       if ( !mainShape->_is_nil() ) 
238       {
239         TopoDS_Shape aShape;
240         if ( GEOMBase::GetShape(mainShape, aShape))
241         {
242           TopAbs_ShapeEnum types[4] = { TopAbs_EDGE, TopAbs_FACE, TopAbs_SHELL, TopAbs_SOLID };
243           for ( int dim = 4; dim > 0; --dim ) {
244             TopAbs_ShapeEnum type = types[ dim - 1 ];
245             TopAbs_ShapeEnum avoid = ( type == TopAbs_SHELL ) ? TopAbs_SOLID : TopAbs_SHAPE;
246             TopExp_Explorer exp( aShape, type, avoid );
247             for ( ; nbShapes[ type ] < 2 && exp.More(); exp.Next() )
248               ++nbShapes[ type ];
249             if ( nbShapes[ type ] > 1 ) {
250               shapeDim = dim;
251               break;
252             }
253           }
254         }
255       }
256     }
257     if (shapeDim > 0)
258     {
259       if ( nbShapes[ TopAbs_SHELL ] + nbShapes[ TopAbs_SOLID ] > 1 )
260         shapeDim = 3;
261       hasElement[ EDGE ]   = shapeDim > 0 && myMesh->NbEdges()  ;
262       hasElement[ FACE ]   = shapeDim > 1 && myMesh->NbFaces()  ;
263       hasElement[ VOLUME ] = shapeDim > 2 && myMesh->NbVolumes();
264     }
265     myHasSolids = nbShapes[ TopAbs_SOLID ];
266   }
267
268   // disable inexistant elem types
269   for ( int i = 0; i < myDlg->myElemTypeGroup->count(); ++i ) {
270     if ( QButton* button = myDlg->myElemTypeGroup->find( i ) )
271       button->setEnabled( hasElement[ i ] );
272   }
273   myDlg->myElementId->setEnabled( hasElement[ EDGE ] );
274   myDlg->myGeomName-> setEnabled( hasElement[ EDGE ] );
275
276   setElementID("");
277 }
278
279 //=======================================================================
280 // function : commitOperation()
281 // purpose  : called when "Ok" button pressed.
282 //=======================================================================
283
284 void SMESHGUI_ShapeByMeshOp::commitOperation()
285 {
286   SMESHGUI_SelectionOp::commitOperation();
287   try {
288     QStringList aListId = QStringList::split( " ", myDlg->myElementId->text(), false);
289     if (aListId.count() == 1)
290       {
291         int elemID = (aListId.first()).toInt();
292         myGeomObj = GEOM::GEOM_Object::_duplicate(
293             SMESHGUI::GetSMESHGen()->GetGeometryByMeshElement
294           ( myMesh.in(), elemID, myDlg->myGeomName->text().latin1()) );
295       }
296     else
297       {
298         GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
299         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
300         
301         if (geomGen->_is_nil() || !aStudy)
302           return;
303         
304         GEOM::GEOM_IShapesOperations_var aShapesOp =
305           geomGen->GetIShapesOperations(aStudy->StudyId());
306         if (aShapesOp->_is_nil() )
307           return;
308         
309         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
310         
311         std::map<double, GEOM::GEOM_Object_var> aGeomObjectsMap;
312         GEOM::GEOM_Object_var aGeomObject;
313
314         GEOM::GEOM_Object_var aMeshShape = myMesh->GetShapeToMesh();
315         
316         for ( int i = 0; i < aListId.count(); i++ )
317           {
318             aGeomObject =
319               SMESHGUI::GetSMESHGen()->FindGeometryByMeshElement(myMesh.in(), aListId[i].toInt());
320
321             if (aGeomObject->_is_nil()) continue;
322             
323             double anId = aShapesOp->GetSubShapeIndex(aMeshShape, aGeomObject);
324             if (aShapesOp->IsDone() && aGeomObjectsMap.find(anId) == aGeomObjectsMap.end())
325               {
326                 aGeomObjectsMap[anId] = aGeomObject;
327
328                 TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aGeomObject->GetShapeType();
329                 if (i == 0)
330                   aGroupType = aSubShapeType;
331                 else if (aSubShapeType != aGroupType)
332                   aGroupType = TopAbs_SHAPE;
333               }
334           }
335         
336         int aNumberOfGO = aGeomObjectsMap.size();
337         if (aNumberOfGO == 1)
338           myGeomObj = (*aGeomObjectsMap.begin()).second;
339         else if (aNumberOfGO > 1)
340           {
341             GEOM::GEOM_IGroupOperations_var aGroupOp =
342               geomGen->GetIGroupOperations(aStudy->StudyId());
343             if(aGroupOp->_is_nil())
344               return;
345             
346             GEOM::ListOfGO_var aGeomObjects = new GEOM::ListOfGO();
347             aGeomObjects->length( aNumberOfGO );
348
349             int i = 0;
350             std::map<double, GEOM::GEOM_Object_var>::iterator anIter;
351             for (anIter = aGeomObjectsMap.begin(); anIter!=aGeomObjectsMap.end(); anIter++)
352               aGeomObjects[i++] = (*anIter).second;
353           
354             //create geometry group
355             myGeomObj = aGroupOp->CreateGroup(aMeshShape, aGroupType);
356             aGroupOp->UnionList(myGeomObj, aGeomObjects);
357
358             if (!aGroupOp->IsDone())
359               return;
360           }
361         
362         // publish the GEOM object in study
363         QString aNewGeomGroupName ( myDlg->myGeomName->text().latin1() );
364           
365         SALOMEDS::SObject_var aNewGroupSO =
366         geomGen->AddInStudy(SMESHGUI::GetSMESHGen()->GetCurrentStudy(), myGeomObj, aNewGeomGroupName, aMeshShape);
367       }
368   }
369   catch (const SALOME::SALOME_Exception& S_ex) {
370     SalomeApp_Tools::QtCatchCorbaException(S_ex);
371   }
372   catch (...) {
373   }
374
375 }
376
377 //=======================================================================
378 // function : onSelectionDone()
379 // purpose  : SLOT called when selection changed. Enable/desable [ OK ]
380 //=======================================================================
381 void SMESHGUI_ShapeByMeshOp::onSelectionDone()
382 {
383   myDlg->setButtonEnabled( false, QtxDialog::OK );
384   setElementID("");
385
386   try {
387     SALOME_ListIO aList;
388     selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
389     if (!myIsMultipleAllowed && aList.Extent() != 1)
390       return;
391
392     SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(aList.First());
393     if (aMesh->_is_nil() || myMesh->_is_nil() || aMesh->GetId() != myMesh->GetId() )
394       return;
395
396     QString aString;
397     int nbElems = SMESH::GetNameOfSelectedElements(selector(),//myViewWindow->GetSelector(),
398                                                    aList.First(), aString);
399     if (nbElems > 0) {
400       if (!myIsMultipleAllowed && nbElems != 1 )
401         return;
402       setElementID( aString );
403       myDlg->setButtonEnabled( true, QtxDialog::OK );
404     }
405   } catch (...) {
406   }
407 }
408
409 //=======================================================================
410 // function : activateSelection()
411 // purpose  : Activate selection in accordance with current pattern type
412 //=======================================================================
413 void SMESHGUI_ShapeByMeshOp::activateSelection()
414 {
415   selectionMgr()->clearFilters();
416   //SMESH::SetPointRepresentation(false);
417
418   myDlg->myGeomName->setText("");
419
420   QString geomName;
421   Selection_Mode mode = EdgeSelection;
422   switch ( myDlg->myElemTypeGroup->id( myDlg->myElemTypeGroup->selected() )) {
423   case EDGE  :
424     mode = EdgeSelection;   geomName = tr("GEOM_EDGE"); break;
425   case FACE  :
426     mode = FaceSelection;   geomName = tr("GEOM_FACE"); break;
427   case VOLUME:
428     mode = VolumeSelection; geomName = tr(myHasSolids ? "GEOM_SOLID" : "GEOM_SHELL"); break;
429   default: return;
430   }
431   if ( selectionMode() != mode )
432     setSelectionMode( mode );
433
434   myDlg->myGeomName->setText( GEOMBase::GetDefaultName( geomName ));
435 }
436
437 //=======================================================================
438 //function : onTypeChanged
439 //purpose  : SLOT. Called when element type changed.
440 //=======================================================================
441
442 void SMESHGUI_ShapeByMeshOp::onTypeChanged (int theType)
443 {
444   setElementID("");
445   activateSelection();
446 }
447
448 //=======================================================================
449 //function : onTypeChanged
450 //purpose  : SLOT. Called when element id is entered
451 //           Highlight the element whose Ids the user entered manually
452 //=======================================================================
453
454 void SMESHGUI_ShapeByMeshOp::onElemIdChanged(const QString& theNewText)
455 {
456   myDlg->setButtonEnabled( false, QtxDialog::OK );
457
458   if ( myIsManualIdEnter && !myMesh->_is_nil() )
459     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) )
460       if ( SMDS_Mesh* aMesh = actor->GetObject()->GetMesh() )
461       {
462         SMDSAbs_ElementType type = SMDSAbs_Edge;
463         switch ( myDlg->myElemTypeGroup->id( myDlg->myElemTypeGroup->selected() )) {
464         case EDGE  : type = SMDSAbs_Edge;   break;
465         case FACE  : type = SMDSAbs_Face;   break;
466         case VOLUME: type = SMDSAbs_Volume; break;
467         default: return;
468         }
469         TColStd_MapOfInteger newIndices;
470         QStringList aListId = QStringList::split( " ", theNewText, false);
471         for ( int i = 0; i < aListId.count(); i++ ) {
472           if ( const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() ))
473             if ( e->GetType() == type )
474               newIndices.Add( e->GetID() );
475         }
476
477         if ( !newIndices.IsEmpty() )
478           {
479             if (!myIsMultipleAllowed && newIndices.Extent() != 1)
480               return;
481             if ( SVTK_Selector* s = selector() ) {
482               s->AddOrRemoveIndex( actor->getIO(), newIndices, false );
483               viewWindow()->highlight( actor->getIO(), true, true );
484               myDlg->setButtonEnabled( true, QtxDialog::OK );
485             }
486           }
487       }
488 }
489
490 //=======================================================================
491 //function : setElementID
492 //purpose  : programmatically set element id
493 //=======================================================================
494
495 void SMESHGUI_ShapeByMeshOp::setElementID(const QString& theText)
496 {
497   myIsManualIdEnter = false;
498   myDlg->myElementId->setText(theText);
499   myIsManualIdEnter = true;
500 }