Salome HOME
Update copyright information
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_FindElemByPointDlg.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File   : SMESHGUI_FindElemByPointDlg.cxx
21 // Author : Edward AGAPOV, Open CASCADE S.A.S.
22 // SMESH includes
23 //
24 #include "SMESHGUI_FindElemByPointDlg.h"
25
26 #include "SMESHGUI.h"
27 #include "SMESHGUI_MeshUtils.h"
28 #include "SMESHGUI_VTKUtils.h"
29 #include "SMESHGUI_SpinBox.h"
30 #include "SMESHGUI_MeshEditPreview.h"
31
32 #include "SMESH_Actor.h"
33 #include "SMESH_ActorUtils.h"
34 #include "SMESH_TypeFilter.hxx"
35 #include "SMESH_LogicalFilter.hxx"
36
37 // SALOME GUI includes
38 #include <LightApp_SelectionMgr.h>
39 #include <QtxComboBox.h>
40 #include <SALOME_ListIO.hxx>
41 #include <SUIT_Desktop.h>
42 #include <SUIT_MessageBox.h>
43 #include <SUIT_OverrideCursor.h>
44 #include <SUIT_ResourceMgr.h>
45 #include <SVTK_ViewModel.h>
46 #include <SVTK_ViewWindow.h>
47 #include <SalomeApp_Tools.h>
48 #include <SalomeApp_TypeFilter.h>
49
50 // Qt includes
51 #include <QAbstractButton>
52 #include <QGridLayout>
53 #include <QGroupBox>
54 #include <QHBoxLayout>
55 #include <QLabel>
56 #include <QLineEdit>
57 #include <QListWidget>
58 #include <QPushButton>
59 #include <QVBoxLayout>
60
61 // VTK includes
62 #include <vtkProperty.h>
63
64 // IDL includes
65 #include <SALOMEconfig.h>
66 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
67
68 #define SPACING 6
69 #define MARGIN  11
70
71 //=======================================================================
72 /*!
73  * \brief Dialog to find elements by a point coordinates
74  */
75 //=======================================================================
76
77 SMESHGUI_FindElemByPointDlg::SMESHGUI_FindElemByPointDlg()
78   : SMESHGUI_Dialog( 0, false, true, OK | Help )
79 {
80   setWindowTitle(tr("CAPTION"));
81
82   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
83   aDlgLay->setMargin(0);
84   aDlgLay->setSpacing(SPACING);
85
86   QWidget* aMainFrame = createMainFrame  (mainFrame());
87
88   aDlgLay->addWidget(aMainFrame);
89   aDlgLay->setStretchFactor(aMainFrame, 1);
90 }
91
92 //================================================================================
93 /*!
94  * \brief Set types of elements available for search
95  */
96 //================================================================================
97
98 void SMESHGUI_FindElemByPointDlg::setTypes(SMESH::array_of_ElementType_var & types)
99 {
100   myElemTypeCombo->blockSignals(true);
101   myElemTypeCombo->clear();
102   int nbTypes = 0, hasNodes = 0;
103   for ( int i = 0; i < types->length(); ++i )
104   {
105     switch ( types[i] ) {
106     case SMESH::NODE:
107       myElemTypeCombo->addItem( tr( "MEN_NODE" ));
108       myElemTypeCombo->setId( nbTypes++, int( SMESH::NODE  ));
109       hasNodes = 1;
110       break;
111     case SMESH::EDGE:
112       myElemTypeCombo->addItem( tr( "MEN_EDGE" ));
113       myElemTypeCombo->setId( nbTypes++, int( SMESH::EDGE  ));
114       break;
115     case SMESH::FACE:
116       myElemTypeCombo->addItem( tr( "MEN_FACE" ));
117       myElemTypeCombo->setId( nbTypes++, int( SMESH::FACE  ));
118       break;
119     case SMESH::VOLUME:
120       myElemTypeCombo->addItem( tr( "MEN_VOLUME_3D" ));
121       myElemTypeCombo->setId( nbTypes++, int( SMESH::VOLUME  ));
122       break;
123     case SMESH::ELEM0D:
124       myElemTypeCombo->addItem( tr( "MEN_ELEM0D" ));
125       myElemTypeCombo->setId( nbTypes++, int( SMESH::ELEM0D  ));
126       break;
127     default:;
128     }
129   }
130   if ( nbTypes - hasNodes > 1 )
131   {
132     myElemTypeCombo->addItem( tr( "MEN_ALL" ));
133     myElemTypeCombo->setId( nbTypes++, int( SMESH::ALL   ));
134   }
135   if ( !hasNodes && nbTypes > 0 )
136   {
137     myElemTypeCombo->addItem( tr( "MEN_NODE" ));
138     myElemTypeCombo->setId( nbTypes++, int( SMESH::NODE  ));
139   }
140   myElemTypeCombo->blockSignals(false);
141 }
142
143 //=======================================================================
144 // function : createMainFrame()
145 // purpose  : Create frame containing dialog's input fields
146 //=======================================================================
147 QWidget* SMESHGUI_FindElemByPointDlg::createMainFrame (QWidget* theParent)
148 {
149   QWidget* aFrame = new QWidget(theParent);
150
151   //mesh name
152
153   QGroupBox* aMeshGrp = new QGroupBox(tr("MESH_GROUP"), aFrame);
154   QHBoxLayout* aMeshGrpLayout = new QHBoxLayout(aMeshGrp);
155   aMeshGrpLayout->setMargin(MARGIN);
156   aMeshGrpLayout->setSpacing(SPACING);
157
158   myMeshName = new QLineEdit(aMeshGrp);
159   aMeshGrpLayout->addWidget(myMeshName);
160
161   // coordinates
162
163   QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
164   QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
165   aCoordGrpLayout->setMargin(MARGIN);
166   aCoordGrpLayout->setSpacing(SPACING);
167
168   QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
169   myX = new SMESHGUI_SpinBox(aCoordGrp);
170
171   QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
172   myY = new SMESHGUI_SpinBox(aCoordGrp);
173
174   QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
175   myZ = new SMESHGUI_SpinBox(aCoordGrp);
176
177   myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
178   myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
179   myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
180   myX->SetValue(0);
181   myY->SetValue(0);
182   myZ->SetValue(0);
183
184   aCoordGrpLayout->addWidget(aXLabel);
185   aCoordGrpLayout->addWidget(myX);
186   aCoordGrpLayout->addWidget(aYLabel);
187   aCoordGrpLayout->addWidget(myY);
188   aCoordGrpLayout->addWidget(aZLabel);
189   aCoordGrpLayout->addWidget(myZ);
190
191   // Elements
192
193   QGroupBox* elementGrp = new QGroupBox(tr("Elements"), aFrame);
194   QGridLayout* elementGrpLayout = new QGridLayout(elementGrp);
195   elementGrpLayout->setSpacing(SPACING);
196   elementGrpLayout->setMargin(MARGIN);
197
198
199   myFindBtn = new QPushButton(elementGrp);
200   myFindBtn->setText(tr("Find"));
201   //myFindBtn->setCheckable(false);
202
203   myElemTypeCombo = new QtxComboBox(elementGrp);
204
205   myFoundList = new QListWidget(elementGrp);
206
207   elementGrpLayout->addWidget( myFindBtn,       0, 0 );
208   elementGrpLayout->addWidget( myElemTypeCombo, 0, 1 );
209   elementGrpLayout->addWidget( myFoundList,     1, 0, 2, 2 );
210
211
212   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
213   aLay->setMargin( 0 );
214   aLay->setSpacing( SPACING );
215   aLay->addWidget(aMeshGrp);
216   aLay->addWidget(aCoordGrp);
217   aLay->addWidget(elementGrp);
218
219   // OK instead of "Apply and Close"
220   if ( QAbstractButton* but = button(OK) )
221     but->setText( tr("SMESH_BUT_OK"));
222
223   return aFrame;
224 }
225
226 //================================================================================
227 /*!
228  * \brief Constructor
229 */
230 //================================================================================
231
232 SMESHGUI_FindElemByPointOp::SMESHGUI_FindElemByPointOp()
233   :SMESHGUI_SelectionOp()
234 {
235   mySimulation = 0;
236   myDlg = new SMESHGUI_FindElemByPointDlg;
237   myHelpFileName = "find_element_by_point_page.html";
238
239   QList<SUIT_SelectionFilter*> filters;
240   filters.append( new SMESH_TypeFilter( MESH ) );
241   filters.append( new SMESH_TypeFilter( GROUP ) );
242   myFilter = new SMESH_LogicalFilter( filters, SMESH_LogicalFilter::LO_OR );
243
244   myPreview = new SMESH::MeshPreviewStruct();
245
246   myPreview->nodesXYZ.length(1);
247   myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
248   myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
249   myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
250
251   myPreview->elementTypes.length(1);
252   myPreview->elementTypes[0].SMDS_ElementType = SMESH::NODE;
253   myPreview->elementTypes[0].isPoly = false;
254   myPreview->elementTypes[0].nbNodesInElement = 1;
255
256   myPreview->elementConnectivities.length(1);
257   myPreview->elementConnectivities[0] = 0;
258
259   // connect signals and slots
260   connect(myDlg->myFindBtn,      SIGNAL(clicked()),               this, SLOT(onFind()));
261   connect(myDlg->myX,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
262   connect(myDlg->myY,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
263   connect(myDlg->myZ,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
264   connect(myDlg->myFoundList,    SIGNAL(itemSelectionChanged()),  this, SLOT(onElemSelected()));
265   connect(myDlg->myElemTypeCombo,SIGNAL(currentIndexChanged(int)),this, SLOT(onElemTypeChange(int)));
266   connect(myDlg,                 SIGNAL(rejectedDlg()),           this, SLOT(onRejectedDlg()));
267 }
268
269 //=======================================================================
270 // function : startOperation()
271 // purpose  : Init dialog fields, connect signals and slots, show dialog
272 //=======================================================================
273 void SMESHGUI_FindElemByPointOp::startOperation()
274 {
275   // init simulation with a current View
276   if ( mySimulation ) delete mySimulation;
277   mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
278   vtkProperty* aProp = vtkProperty::New();
279   aProp->SetRepresentationToWireframe();
280   aProp->SetColor(250, 0, 250);
281   aProp->SetPointSize(5);
282   aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
283   mySimulation->GetActor()->SetProperty(aProp);
284   aProp->Delete();
285
286   SMESHGUI_SelectionOp::startOperation();
287   myDlg->show();
288   redisplayPreview();
289
290   onSelectionDone(); // init myMeshOrPart
291 }
292
293 //================================================================================
294 /*!
295  * \brief Stops operation
296  */
297 //================================================================================
298
299 void SMESHGUI_FindElemByPointOp::stopOperation()
300 {
301   if ( mySimulation )
302   {
303     mySimulation->SetVisibility(false);
304     delete mySimulation;
305     mySimulation = 0;
306   }
307   selectionMgr()->removeFilter( myFilter );
308   SMESHGUI_SelectionOp::stopOperation();
309 }
310
311 //================================================================================
312 /*!
313  * \brief hilight found selected elements
314  */
315 //================================================================================
316
317 void SMESHGUI_FindElemByPointOp::onElemSelected()
318 {
319   if ( !myMeshIO.IsNull() )
320   {
321     Selection_Mode selMode =
322       myDlg->myElemTypeCombo->currentId() == int(SMESH::NODE) ? NodeSelection : CellSelection;
323     if ( selectionMode() != selMode )
324       setSelectionMode( selMode );
325
326     QList<QListWidgetItem *> ids = myDlg->myFoundList->selectedItems();
327     QList<QListWidgetItem*>::iterator id = ids.begin();
328     TColStd_MapOfInteger idMap;
329     for ( ; id != ids.end(); ++id )
330       idMap.Add( (*id)->text().toInt() );
331
332     addOrRemoveIndex( myMeshIO, idMap, false );
333
334     SALOME_ListIO aList;
335     aList.Append(myMeshIO);
336     selectionMgr()->setSelectedObjects(aList,false);
337   }
338 }
339
340 //================================================================================
341 /*!
342  * \brief Set selection mode according to element type
343  */
344 //================================================================================
345
346 void SMESHGUI_FindElemByPointOp::onElemTypeChange(int index)
347 {
348   Selection_Mode newMode = (index == 1) ? NodeSelection : CellSelection;
349   if ( selectionMode() != newMode )
350   {
351     selectionMgr()->clearFilters();
352     setSelectionMode( newMode );
353   }
354   myDlg->myFoundList->clear();
355 }
356
357 //================================================================================
358 /*!
359  * \brief Method needed for internal cuisine
360  */
361 //================================================================================
362
363 void SMESHGUI_FindElemByPointDlg::reject()
364 {
365   emit rejectedDlg();
366   QtxDialog::reject();
367 }
368
369 //================================================================================
370 /*!
371  * \brief Method needed for internal cuisine
372  */
373 //================================================================================
374
375 void SMESHGUI_FindElemByPointOp::onRejectedDlg()
376 {
377   myMeshIO.Nullify(); 
378 }
379
380 //================================================================================
381 /*!
382  * \brief perform it's intention action: find elements
383  */
384 //================================================================================
385
386 void SMESHGUI_FindElemByPointOp::onFind()
387 {
388   if ( myMeshOrPart->_is_nil() )
389     return;
390
391   try {
392     SUIT_OverrideCursor wc;
393
394     SMESH::SMESH_Mesh_var aMesh = myMeshOrPart->GetMesh();
395     if ( aMesh->_is_nil() )
396       return;
397     SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
398     if (aMeshEditor->_is_nil())
399       return;
400
401     SMESH::long_array_var foundIds;
402     if ( aMesh->_is_equivalent( myMeshOrPart ) )
403       foundIds =
404         aMeshEditor->FindElementsByPoint( myDlg->myX->GetValue(),
405                                           myDlg->myY->GetValue(),
406                                           myDlg->myZ->GetValue(),
407                                           SMESH::ElementType( myDlg->myElemTypeCombo->currentId()));
408     else
409       foundIds =
410         aMeshEditor->FindAmongElementsByPoint( myMeshOrPart,
411                                                myDlg->myX->GetValue(),
412                                                myDlg->myY->GetValue(),
413                                                myDlg->myZ->GetValue(),
414                                                SMESH::ElementType( myDlg->myElemTypeCombo->currentId()));
415     myDlg->myFoundList->clear();
416     for ( int i = 0; i < foundIds->length(); ++i )
417       myDlg->myFoundList->addItem( QString::number( foundIds[i] ));
418
419     if ( foundIds->length() > 0 )
420       myDlg->myFoundList->setCurrentRow(0);
421   }
422   catch (const SALOME::SALOME_Exception& S_ex) {
423     SalomeApp_Tools::QtCatchCorbaException(S_ex);
424   }
425   catch (...) {
426   }
427 }
428
429 //================================================================================
430 /*!
431  * \brief Method needed for internal cuisine
432  */
433 //================================================================================
434
435 bool SMESHGUI_FindElemByPointOp::onApply()
436 {
437   onRejectedDlg();
438   return true;
439 }
440
441 //================================================================================
442 /*!
443  * \brief SLOT called when selection changed
444  */
445 //================================================================================
446
447 void SMESHGUI_FindElemByPointOp::onSelectionDone()
448 {
449   if ( !myDlg->isVisible() || !myDlg->isEnabled() )
450     return;
451
452   QString oldMeshEntry, newMeshEntry;
453   if ( !myMeshIO.IsNull() && myMeshIO->hasEntry() )
454     oldMeshEntry = myMeshIO->getEntry();
455
456   try {
457     SALOME_ListIO aList;
458     selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
459     if (aList.Extent() == 1 && aList.First()->hasEntry())
460     {
461       Handle(SALOME_InteractiveObject) anIO = aList.First();
462       _PTR(SObject) pObj = studyDS()->FindObjectID(anIO->getEntry());
463       CORBA::Object_var anObj = SMESH::IObjectToObject( anIO );
464       newMeshEntry = anIO->getEntry();
465       SMESH::SMESH_IDSource_var aMeshOrPart = SMESH::SMESH_IDSource::_narrow(anObj);
466       if ( pObj && !aMeshOrPart->_is_nil() && oldMeshEntry != newMeshEntry )
467       {
468         myMeshOrPart = aMeshOrPart;
469         myMeshIO.Nullify();
470         myMeshIO = anIO;
471         std::string name = pObj->GetName();
472         myDlg->myMeshName->setText("");
473         myDlg->myMeshName->setText( QString( name.c_str() ).trimmed() );
474         SMESH::array_of_ElementType_var  types = myMeshOrPart->GetTypes();
475         myDlg->setTypes( types );
476       }
477     }
478   }
479   catch (...) {
480   }
481
482   if ( oldMeshEntry != newMeshEntry || newMeshEntry.isEmpty() )
483     myDlg->myFoundList->clear();
484
485   myDlg->myFindBtn->setEnabled( !myMeshIO.IsNull() );
486 }
487
488 //================================================================================
489 /*!
490  * \brief show point by coordinates
491  */
492 //================================================================================
493
494 void SMESHGUI_FindElemByPointOp::redisplayPreview()
495 {
496   myDlg->myFoundList->clear();
497
498   myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
499   myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
500   myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
501
502   mySimulation->SetData(&myPreview.in());
503 }
504
505 //================================================================================
506 /*!
507  * \brief install filter on meshes
508  */
509 //================================================================================
510
511 void SMESHGUI_FindElemByPointOp::activateSelection()
512 {
513   selectionMgr()->clearFilters();
514   selectionMgr()->installFilter( myFilter );
515 }
516
517 //================================================================================
518 /*!
519  * \brief Destructor
520 */
521 //================================================================================
522
523 SMESHGUI_FindElemByPointOp::~SMESHGUI_FindElemByPointOp()
524 {
525   if ( myDlg )         { delete myDlg;        myDlg = 0; }
526   if ( mySimulation )  { delete mySimulation; mySimulation = 0; }
527   if ( myFilter )
528   {
529     QList<SUIT_SelectionFilter*> sub =  ((SMESH_LogicalFilter*)myFilter)->getFilters();
530     delete sub.front();
531     delete sub.back();
532     delete myFilter;
533     myFilter = 0;
534   }
535 }
536
537 //================================================================================
538 /*!
539  * \brief Gets dialog of this operation
540  * \retval LightApp_Dialog* - pointer to dialog of this operation
541  */
542 //================================================================================
543
544 LightApp_Dialog* SMESHGUI_FindElemByPointOp::dlg() const
545 {
546   return myDlg;
547 }
548