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