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