Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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 #define SPACING 5
71 #define MARGIN  10
72
73 enum { EDGE = 0, FACE, VOLUME };
74
75 /*!
76  * \brief Dialog to publish a sub-shape of the mesh main shape
77  *        by selecting mesh elements
78  */
79 SMESHGUI_ShapeByMeshDlg::SMESHGUI_ShapeByMeshDlg()
80   : SMESHGUI_Dialog( 0, false, true, OK | Close )
81 {
82   setCaption(tr("CAPTION"));
83
84   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame(), MARGIN, SPACING);
85
86   QFrame* aMainFrame = createMainFrame  (mainFrame());
87
88   aDlgLay->addWidget(aMainFrame);
89
90   aDlgLay->setStretchFactor(aMainFrame, 1);
91 }
92
93 //=======================================================================
94 // function : createMainFrame()
95 // purpose  : Create frame containing dialog's input fields
96 //=======================================================================
97 QFrame* SMESHGUI_ShapeByMeshDlg::createMainFrame (QWidget* theParent)
98 {
99   QFrame* aMainGrp = new QFrame(theParent, "main frame");
100   QGridLayout* aLayout = new QGridLayout(aMainGrp, 3, 2);
101   aLayout->setSpacing(6);
102   aLayout->setAutoAdd(false);
103
104   // elem type
105   myElemTypeGroup = new QButtonGroup(1, Qt::Vertical, aMainGrp, "Group types");
106   myElemTypeGroup->setTitle(tr("SMESH_ELEMENT_TYPE"));
107   myElemTypeGroup->setExclusive(true);
108
109   (new QRadioButton( tr("SMESH_EDGE")  , myElemTypeGroup))->setChecked(true);
110   new QRadioButton( tr("SMESH_FACE")  , myElemTypeGroup);
111   new QRadioButton( tr("SMESH_VOLUME"), myElemTypeGroup);
112
113   // element id
114   QLabel* anIdLabel = new QLabel( aMainGrp, "element id label");
115   anIdLabel->setText( tr("ELEMENT_ID") );
116   myElementId = new QLineEdit( aMainGrp, "element id");
117   myElementId->setValidator( new SMESHGUI_IdValidator( theParent, "id validator", 1 ));
118
119   // shape name
120   QLabel* aNameLabel = new QLabel( aMainGrp, "geom name label");
121   aNameLabel->setText( tr("GEOMETRY_NAME") );
122   myGeomName = new QLineEdit( aMainGrp, "geom name");
123
124   aLayout->addMultiCellWidget(myElemTypeGroup, 0, 0, 0, 1);
125   aLayout->addWidget(anIdLabel,   1, 0);
126   aLayout->addWidget(myElementId, 1, 1);
127   aLayout->addWidget(aNameLabel,  2, 0);
128   aLayout->addWidget(myGeomName,  2, 1);
129
130   return aMainGrp;
131 }
132
133 //=======================================================================
134 // function : ~SMESHGUI_ShapeByMeshDlg()
135 // purpose  : Destructor
136 //=======================================================================
137 SMESHGUI_ShapeByMeshDlg::~SMESHGUI_ShapeByMeshDlg()
138 {
139   // no need to delete child widgets, Qt does it all for us
140 }
141
142 //================================================================================
143 /*!
144  * \brief Constructor
145   * \param theToCreate - if this parameter is true then operation is used for creation,
146   * for editing otherwise
147  *
148  * Initialize operation
149 */
150 //================================================================================
151 SMESHGUI_ShapeByMeshOp::SMESHGUI_ShapeByMeshOp()
152 {
153   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
154     GeometryGUI::InitGeomGen();
155
156   myDlg = new SMESHGUI_ShapeByMeshDlg;
157
158   connect(myDlg->myElemTypeGroup, SIGNAL(clicked(int)), SLOT(onTypeChanged(int)));
159   connect(myDlg->myElementId, SIGNAL(textChanged(const QString&)), SLOT(onElemIdChanged(const QString&)));
160 }
161
162
163 //=======================================================================
164 // function : startOperation()
165 // purpose  : Init dialog fields, connect signals and slots, show dialog
166 //=======================================================================
167 void SMESHGUI_ShapeByMeshOp::startOperation()
168 {
169   //SetMesh( SMESH::SMESH_Mesh::_nil() );
170   myIsManualIdEnter = false;
171
172   SMESHGUI_SelectionOp::startOperation();
173
174   //activateSelection(); // set filters
175   onSelectionDone(); // desable/enable [ OK ]
176
177   myDlg->show();
178 }
179
180 //================================================================================
181 /*!
182  * \brief Destructor
183 */
184 //================================================================================
185 SMESHGUI_ShapeByMeshOp::~SMESHGUI_ShapeByMeshOp()
186 {
187   if ( myDlg )
188     delete myDlg;
189 }
190
191 //================================================================================
192 /*!
193  * \brief Gets dialog of this operation
194   * \retval LightApp_Dialog* - pointer to dialog of this operation
195 */
196 //================================================================================
197 LightApp_Dialog* SMESHGUI_ShapeByMeshOp::dlg() const
198 {
199   return myDlg;
200 }
201
202 //=======================================================================
203 // function : GetShape()
204 // purpose  : Get published sub-shape
205 //=======================================================================
206 GEOM::GEOM_Object_ptr SMESHGUI_ShapeByMeshOp::GetShape()
207 {
208   return myGeomObj.in();
209 }
210
211 //=======================================================================
212 // function : SetMesh()
213 // purpose  : Set mesh to dialog
214 //=======================================================================
215
216 void SMESHGUI_ShapeByMeshOp::SetMesh (SMESH::SMESH_Mesh_ptr thePtr)
217 {
218   myMesh    = SMESH::SMESH_Mesh::_duplicate(thePtr);
219   myGeomObj = GEOM::GEOM_Object::_nil();
220   myHasSolids = false;
221
222   vector< bool > hasElement (myDlg->myElemTypeGroup->count(), false);
223   if (!myMesh->_is_nil() )
224   {
225 //     _PTR(SObject) aSobj = SMESH::FindSObject(myMesh.in());
226 //     SUIT_DataOwnerPtr anIObj (new LightApp_DataOwner(aSobj->GetID().c_str()));
227
228     vector< int > nbShapes( TopAbs_SHAPE, 0 );
229     int shapeDim = 0; // max dim with several shapes
230     //if ( /*mySelectionMgr*/ selectionMgr()->isOk(anIObj) ) // check that the mesh has a valid shape
231     {
232       _PTR(SObject) aSO = SMESH::FindSObject(myMesh.in());
233       GEOM::GEOM_Object_var mainShape = SMESH::GetGeom(aSO);
234       if ( !mainShape->_is_nil() ) 
235       {
236         TopoDS_Shape aShape;
237         if ( GEOMBase::GetShape(mainShape, aShape))
238         {
239           TopAbs_ShapeEnum types[4] = { TopAbs_EDGE, TopAbs_FACE, TopAbs_SHELL, TopAbs_SOLID };
240           for ( int dim = 4; dim > 0; --dim ) {
241             TopAbs_ShapeEnum type = types[ dim - 1 ];
242             TopAbs_ShapeEnum avoid = ( type == TopAbs_SHELL ) ? TopAbs_SOLID : TopAbs_SHAPE;
243             TopExp_Explorer exp( aShape, type, avoid );
244             for ( ; nbShapes[ type ] < 2 && exp.More(); exp.Next() )
245               ++nbShapes[ type ];
246             if ( nbShapes[ type ] > 1 ) {
247               shapeDim = dim;
248               break;
249             }
250           }
251         }
252       }
253     }
254     if (shapeDim > 0)
255     {
256       if ( nbShapes[ TopAbs_SHELL ] + nbShapes[ TopAbs_SOLID ] > 1 )
257         shapeDim = 3;
258       hasElement[ EDGE ]   = shapeDim > 0 && myMesh->NbEdges()  ;
259       hasElement[ FACE ]   = shapeDim > 1 && myMesh->NbFaces()  ;
260       hasElement[ VOLUME ] = shapeDim > 2 && myMesh->NbVolumes();
261     }
262     myHasSolids = nbShapes[ TopAbs_SOLID ];
263   }
264
265   // disable inexistant elem types
266   for ( int i = 0; i < myDlg->myElemTypeGroup->count(); ++i ) {
267     if ( QButton* button = myDlg->myElemTypeGroup->find( i ) )
268       button->setEnabled( hasElement[ i ] );
269   }
270   myDlg->myElementId->setEnabled( hasElement[ EDGE ] );
271   myDlg->myGeomName-> setEnabled( hasElement[ EDGE ] );
272
273   setElementID("");
274 }
275
276 //=======================================================================
277 // function : commitOperation()
278 // purpose  : called when "Ok" button pressed.
279 //=======================================================================
280
281 void SMESHGUI_ShapeByMeshOp::commitOperation()
282 {
283   SMESHGUI_SelectionOp::commitOperation();
284   try {
285     int elemID = myDlg->myElementId->text().toInt();
286     myGeomObj = SMESHGUI::GetSMESHGen()->GetGeometryByMeshElement
287       ( myMesh.in(), elemID, myDlg->myGeomName->text().latin1());
288   }
289   catch (const SALOME::SALOME_Exception& S_ex) {
290     SalomeApp_Tools::QtCatchCorbaException(S_ex);
291   }
292   catch (...) {
293   }
294
295 }
296
297 //=======================================================================
298 // function : onSelectionDone()
299 // purpose  : SLOT called when selection changed. Enable/desable [ OK ]
300 //=======================================================================
301 void SMESHGUI_ShapeByMeshOp::onSelectionDone()
302 {
303   myDlg->setButtonEnabled( false, QtxDialog::OK );
304   setElementID("");
305
306   try {
307     SALOME_ListIO aList;
308     selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
309     if (aList.Extent() != 1)
310       return;
311
312     SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(aList.First());
313     if (aMesh->_is_nil() || myMesh->_is_nil() || aMesh->GetId() != myMesh->GetId() )
314       return;
315
316     QString aString;
317     int nbElems = SMESH::GetNameOfSelectedElements(selector(),//myViewWindow->GetSelector(),
318                                                    aList.First(), aString);
319     if ( nbElems == 1 ) {
320       setElementID( aString );
321       myDlg->setButtonEnabled( true, QtxDialog::OK );
322     }
323   } catch (...) {
324   }
325 }
326
327 //=======================================================================
328 // function : activateSelection()
329 // purpose  : Activate selection in accordance with current pattern type
330 //=======================================================================
331 void SMESHGUI_ShapeByMeshOp::activateSelection()
332 {
333   selectionMgr()->clearFilters();
334   //SMESH::SetPointRepresentation(false);
335
336   myDlg->myGeomName->setText("");
337
338   QString geomName;
339   Selection_Mode mode = EdgeSelection;
340   switch ( myDlg->myElemTypeGroup->id( myDlg->myElemTypeGroup->selected() )) {
341   case EDGE  :
342     mode = EdgeSelection;   geomName = tr("GEOM_EDGE"); break;
343   case FACE  :
344     mode = FaceSelection;   geomName = tr("GEOM_FACE"); break;
345   case VOLUME:
346     mode = VolumeSelection; geomName = tr(myHasSolids ? "GEOM_SOLID" : "GEOM_SHELL"); break;
347   default: return;
348   }
349   if ( selectionMode() != mode )
350     setSelectionMode( mode );
351
352   myDlg->myGeomName->setText( GEOMBase::GetDefaultName( geomName ));
353 }
354
355 //=======================================================================
356 //function : onTypeChanged
357 //purpose  : SLOT. Called when element type changed.
358 //=======================================================================
359
360 void SMESHGUI_ShapeByMeshOp::onTypeChanged (int theType)
361 {
362   setElementID("");
363   activateSelection();
364 }
365
366 //=======================================================================
367 //function : onTypeChanged
368 //purpose  : SLOT. Called when element id is entered
369 //           Highlight the element whose Ids the user entered manually
370 //=======================================================================
371
372 void SMESHGUI_ShapeByMeshOp::onElemIdChanged(const QString& theNewText)
373 {
374   myDlg->setButtonEnabled( false, QtxDialog::OK );
375
376   if ( myIsManualIdEnter && !myMesh->_is_nil() )
377     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) )
378       if ( SMDS_Mesh* aMesh = actor->GetObject()->GetMesh() )
379       {
380         SMDSAbs_ElementType type = SMDSAbs_Edge;
381         switch ( myDlg->myElemTypeGroup->id( myDlg->myElemTypeGroup->selected() )) {
382         case EDGE  : type = SMDSAbs_Edge;   break;
383         case FACE  : type = SMDSAbs_Face;   break;
384         case VOLUME: type = SMDSAbs_Volume; break;
385         default: return;
386         }
387         TColStd_MapOfInteger newIndices;
388         QStringList aListId = QStringList::split( " ", theNewText, false);
389         for ( int i = 0; i < aListId.count(); i++ ) {
390           if ( const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() ))
391             if ( e->GetType() == type )
392               newIndices.Add( e->GetID() );
393         }
394
395         if ( !newIndices.IsEmpty() && newIndices.Extent() == 1 )
396           if ( SVTK_Selector* s = selector() ) {
397             s->AddOrRemoveIndex( actor->getIO(), newIndices, false );
398             viewWindow()->highlight( actor->getIO(), true, true );
399             myDlg->setButtonEnabled( true, QtxDialog::OK );
400           }
401       }
402 }
403
404 //=======================================================================
405 //function : setElementID
406 //purpose  : programmatically set element id
407 //=======================================================================
408
409 void SMESHGUI_ShapeByMeshOp::setElementID(const QString& theText)
410 {
411   myIsManualIdEnter = false;
412   myDlg->myElementId->setText(theText);
413   myIsManualIdEnter = true;
414 }