1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SMESHGUI_FindElemByPointDlg.cxx
21 // Author : Edward AGAPOV, Open CASCADE S.A.S.
24 #include "SMESHGUI_FindElemByPointDlg.h"
27 #include "SMESHGUI_MeshUtils.h"
28 #include "SMESHGUI_VTKUtils.h"
29 #include "SMESHGUI_SpinBox.h"
30 #include "SMESHGUI_MeshEditPreview.h"
32 #include "SMESH_Actor.h"
33 #include "SMESH_ActorUtils.h"
34 #include "SMESH_TypeFilter.hxx"
35 #include "SMESH_LogicalFilter.hxx"
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>
51 #include <QAbstractButton>
52 #include <QGridLayout>
54 #include <QHBoxLayout>
57 #include <QListWidget>
58 #include <QPushButton>
59 #include <QVBoxLayout>
62 #include <vtkProperty.h>
65 #include <SALOMEconfig.h>
66 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
71 //=======================================================================
73 * \brief Dialog to find elements by a point coordinates
75 //=======================================================================
77 SMESHGUI_FindElemByPointDlg::SMESHGUI_FindElemByPointDlg()
78 : SMESHGUI_Dialog( 0, false, true, OK | Help )
80 setWindowTitle(tr("CAPTION"));
82 QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
83 aDlgLay->setMargin(0);
84 aDlgLay->setSpacing(SPACING);
86 QWidget* aMainFrame = createMainFrame (mainFrame());
88 aDlgLay->addWidget(aMainFrame);
89 aDlgLay->setStretchFactor(aMainFrame, 1);
92 //================================================================================
94 * \brief Set types of elements available for search
96 //================================================================================
98 void SMESHGUI_FindElemByPointDlg::setTypes(SMESH::array_of_ElementType_var & types)
100 myElemTypeCombo->blockSignals(true);
101 myElemTypeCombo->clear();
102 int nbTypes = 0, hasNodes = 0;
103 for ( int i = 0; i < types->length(); ++i )
105 switch ( types[i] ) {
107 myElemTypeCombo->addItem( tr( "MEN_NODE" ));
108 myElemTypeCombo->setId( nbTypes++, int( SMESH::NODE ));
112 myElemTypeCombo->addItem( tr( "MEN_EDGE" ));
113 myElemTypeCombo->setId( nbTypes++, int( SMESH::EDGE ));
116 myElemTypeCombo->addItem( tr( "MEN_FACE" ));
117 myElemTypeCombo->setId( nbTypes++, int( SMESH::FACE ));
120 myElemTypeCombo->addItem( tr( "MEN_VOLUME_3D" ));
121 myElemTypeCombo->setId( nbTypes++, int( SMESH::VOLUME ));
124 myElemTypeCombo->addItem( tr( "MEN_ELEM0D" ));
125 myElemTypeCombo->setId( nbTypes++, int( SMESH::ELEM0D ));
130 if ( nbTypes - hasNodes > 1 )
132 myElemTypeCombo->addItem( tr( "MEN_ALL" ));
133 myElemTypeCombo->setId( nbTypes++, int( SMESH::ALL ));
135 if ( !hasNodes && nbTypes > 0 )
137 myElemTypeCombo->addItem( tr( "MEN_NODE" ));
138 myElemTypeCombo->setId( nbTypes++, int( SMESH::NODE ));
140 myElemTypeCombo->blockSignals(false);
143 //=======================================================================
144 // function : createMainFrame()
145 // purpose : Create frame containing dialog's input fields
146 //=======================================================================
147 QWidget* SMESHGUI_FindElemByPointDlg::createMainFrame (QWidget* theParent)
149 QWidget* aFrame = new QWidget(theParent);
153 QGroupBox* aMeshGrp = new QGroupBox(tr("MESH_GROUP"), aFrame);
154 QHBoxLayout* aMeshGrpLayout = new QHBoxLayout(aMeshGrp);
155 aMeshGrpLayout->setMargin(MARGIN);
156 aMeshGrpLayout->setSpacing(SPACING);
158 myMeshName = new QLineEdit(aMeshGrp);
159 aMeshGrpLayout->addWidget(myMeshName);
163 QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
164 QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
165 aCoordGrpLayout->setMargin(MARGIN);
166 aCoordGrpLayout->setSpacing(SPACING);
168 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
169 myX = new SMESHGUI_SpinBox(aCoordGrp);
171 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
172 myY = new SMESHGUI_SpinBox(aCoordGrp);
174 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
175 myZ = new SMESHGUI_SpinBox(aCoordGrp);
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");
184 aCoordGrpLayout->addWidget(aXLabel);
185 aCoordGrpLayout->addWidget(myX);
186 aCoordGrpLayout->addWidget(aYLabel);
187 aCoordGrpLayout->addWidget(myY);
188 aCoordGrpLayout->addWidget(aZLabel);
189 aCoordGrpLayout->addWidget(myZ);
193 QGroupBox* elementGrp = new QGroupBox(tr("Elements"), aFrame);
194 QGridLayout* elementGrpLayout = new QGridLayout(elementGrp);
195 elementGrpLayout->setSpacing(SPACING);
196 elementGrpLayout->setMargin(MARGIN);
199 myFindBtn = new QPushButton(elementGrp);
200 myFindBtn->setText(tr("Find"));
201 //myFindBtn->setCheckable(false);
203 myElemTypeCombo = new QtxComboBox(elementGrp);
205 myFoundList = new QListWidget(elementGrp);
207 elementGrpLayout->addWidget( myFindBtn, 0, 0 );
208 elementGrpLayout->addWidget( myElemTypeCombo, 0, 1 );
209 elementGrpLayout->addWidget( myFoundList, 1, 0, 2, 2 );
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);
219 // OK instead of "Apply and Close"
220 if ( QAbstractButton* but = button(OK) )
221 but->setText( tr("SMESH_BUT_OK"));
226 //================================================================================
230 //================================================================================
232 SMESHGUI_FindElemByPointOp::SMESHGUI_FindElemByPointOp()
233 :SMESHGUI_SelectionOp()
236 myDlg = new SMESHGUI_FindElemByPointDlg;
237 myHelpFileName = "find_element_by_point_page.html";
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 );
244 myPreview = new SMESH::MeshPreviewStruct();
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();
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;
256 myPreview->elementConnectivities.length(1);
257 myPreview->elementConnectivities[0] = 0;
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()));
269 //=======================================================================
270 // function : startOperation()
271 // purpose : Init dialog fields, connect signals and slots, show dialog
272 //=======================================================================
273 void SMESHGUI_FindElemByPointOp::startOperation()
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);
286 SMESHGUI_SelectionOp::startOperation();
290 onSelectionDone(); // init myMeshOrPart
293 //================================================================================
295 * \brief Stops operation
297 //================================================================================
299 void SMESHGUI_FindElemByPointOp::stopOperation()
303 mySimulation->SetVisibility(false);
307 selectionMgr()->removeFilter( myFilter );
308 SMESHGUI_SelectionOp::stopOperation();
311 //================================================================================
313 * \brief hilight found selected elements
315 //================================================================================
317 void SMESHGUI_FindElemByPointOp::onElemSelected()
319 if ( !myMeshIO.IsNull() )
321 Selection_Mode selMode =
322 myDlg->myElemTypeCombo->currentId() == int(SMESH::NODE) ? NodeSelection : CellSelection;
323 if ( selectionMode() != selMode )
324 setSelectionMode( selMode );
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() );
332 addOrRemoveIndex( myMeshIO, idMap, false );
335 aList.Append(myMeshIO);
336 selectionMgr()->setSelectedObjects(aList,false);
340 //================================================================================
342 * \brief Set selection mode according to element type
344 //================================================================================
346 void SMESHGUI_FindElemByPointOp::onElemTypeChange(int index)
348 Selection_Mode newMode = (index == 1) ? NodeSelection : CellSelection;
349 if ( selectionMode() != newMode )
351 selectionMgr()->clearFilters();
352 setSelectionMode( newMode );
354 myDlg->myFoundList->clear();
357 //================================================================================
359 * \brief Method needed for internal cuisine
361 //================================================================================
363 void SMESHGUI_FindElemByPointDlg::reject()
369 //================================================================================
371 * \brief Method needed for internal cuisine
373 //================================================================================
375 void SMESHGUI_FindElemByPointOp::onRejectedDlg()
380 //================================================================================
382 * \brief perform it's intention action: find elements
384 //================================================================================
386 void SMESHGUI_FindElemByPointOp::onFind()
388 if ( myMeshOrPart->_is_nil() )
392 SUIT_OverrideCursor wc;
394 SMESH::SMESH_Mesh_var aMesh = myMeshOrPart->GetMesh();
395 if ( aMesh->_is_nil() )
397 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
398 if (aMeshEditor->_is_nil())
401 SMESH::long_array_var foundIds;
402 if ( aMesh->_is_equivalent( myMeshOrPart ) )
404 aMeshEditor->FindElementsByPoint( myDlg->myX->GetValue(),
405 myDlg->myY->GetValue(),
406 myDlg->myZ->GetValue(),
407 SMESH::ElementType( myDlg->myElemTypeCombo->currentId()));
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] ));
419 if ( foundIds->length() > 0 )
420 myDlg->myFoundList->setCurrentRow(0);
422 catch (const SALOME::SALOME_Exception& S_ex) {
423 SalomeApp_Tools::QtCatchCorbaException(S_ex);
429 //================================================================================
431 * \brief Method needed for internal cuisine
433 //================================================================================
435 bool SMESHGUI_FindElemByPointOp::onApply()
441 //================================================================================
443 * \brief SLOT called when selection changed
445 //================================================================================
447 void SMESHGUI_FindElemByPointOp::onSelectionDone()
449 if ( !myDlg->isVisible() || !myDlg->isEnabled() )
452 QString oldMeshEntry, newMeshEntry;
453 if ( !myMeshIO.IsNull() && myMeshIO->hasEntry() )
454 oldMeshEntry = myMeshIO->getEntry();
458 selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
459 if (aList.Extent() == 1 && aList.First()->hasEntry())
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 )
468 myMeshOrPart = aMeshOrPart;
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 );
482 if ( oldMeshEntry != newMeshEntry || newMeshEntry.isEmpty() )
483 myDlg->myFoundList->clear();
485 myDlg->myFindBtn->setEnabled( !myMeshIO.IsNull() );
488 //================================================================================
490 * \brief show point by coordinates
492 //================================================================================
494 void SMESHGUI_FindElemByPointOp::redisplayPreview()
496 myDlg->myFoundList->clear();
498 myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
499 myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
500 myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
502 mySimulation->SetData(&myPreview.in());
505 //================================================================================
507 * \brief install filter on meshes
509 //================================================================================
511 void SMESHGUI_FindElemByPointOp::activateSelection()
513 selectionMgr()->clearFilters();
514 selectionMgr()->installFilter( myFilter );
517 //================================================================================
521 //================================================================================
523 SMESHGUI_FindElemByPointOp::~SMESHGUI_FindElemByPointOp()
525 if ( myDlg ) { delete myDlg; myDlg = 0; }
526 if ( mySimulation ) { delete mySimulation; mySimulation = 0; }
529 QList<SUIT_SelectionFilter*> sub = ((SMESH_LogicalFilter*)myFilter)->getFilters();
537 //================================================================================
539 * \brief Gets dialog of this operation
540 * \retval LightApp_Dialog* - pointer to dialog of this operation
542 //================================================================================
544 LightApp_Dialog* SMESHGUI_FindElemByPointOp::dlg() const