1 // Copyright (C) 2007-2013 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.
25 #include "SMESHGUI_FindElemByPointDlg.h"
28 #include "SMESHGUI_MeshUtils.h"
29 #include "SMESHGUI_VTKUtils.h"
30 #include "SMESHGUI_SpinBox.h"
31 #include "SMESHGUI_MeshEditPreview.h"
33 #include "SMESH_Actor.h"
34 #include "SMESH_ActorUtils.h"
35 #include "SMESH_TypeFilter.hxx"
36 #include "SMESH_LogicalFilter.hxx"
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>
52 #include <QAbstractButton>
53 #include <QGridLayout>
55 #include <QHBoxLayout>
58 #include <QListWidget>
59 #include <QPushButton>
60 #include <QVBoxLayout>
63 #include <vtkProperty.h>
66 #include <SALOMEconfig.h>
67 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
72 //=======================================================================
74 * \brief Dialog to find elements by a point coordinates
76 //=======================================================================
78 SMESHGUI_FindElemByPointDlg::SMESHGUI_FindElemByPointDlg()
79 : SMESHGUI_Dialog( 0, false, true, OK | Help )
81 setWindowTitle(tr("CAPTION"));
83 QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
84 aDlgLay->setMargin(0);
85 aDlgLay->setSpacing(SPACING);
87 QWidget* aMainFrame = createMainFrame (mainFrame());
89 aDlgLay->addWidget(aMainFrame);
90 aDlgLay->setStretchFactor(aMainFrame, 1);
93 //================================================================================
95 * \brief Set types of elements available for search
97 //================================================================================
99 void SMESHGUI_FindElemByPointDlg::setTypes(SMESH::array_of_ElementType_var & types)
101 myElemTypeCombo->blockSignals(true);
102 myElemTypeCombo->clear();
103 int nbTypes = 0, hasNodes = 0;
104 for ( int i = 0; i < types->length(); ++i )
106 switch ( types[i] ) {
108 myElemTypeCombo->addItem( tr( "MEN_NODE" ));
109 myElemTypeCombo->setId( nbTypes++, int( SMESH::NODE ));
113 myElemTypeCombo->addItem( tr( "MEN_EDGE" ));
114 myElemTypeCombo->setId( nbTypes++, int( SMESH::EDGE ));
117 myElemTypeCombo->addItem( tr( "MEN_FACE" ));
118 myElemTypeCombo->setId( nbTypes++, int( SMESH::FACE ));
121 myElemTypeCombo->addItem( tr( "MEN_VOLUME_3D" ));
122 myElemTypeCombo->setId( nbTypes++, int( SMESH::VOLUME ));
125 myElemTypeCombo->addItem( tr( "MEN_ELEM0D" ));
126 myElemTypeCombo->setId( nbTypes++, int( SMESH::ELEM0D ));
129 myElemTypeCombo->addItem( tr( "MEN_BALL" ));
130 myElemTypeCombo->setId( nbTypes++, int( SMESH::BALL ));
135 if ( nbTypes - hasNodes > 1 )
137 myElemTypeCombo->addItem( tr( "MEN_ALL" ));
138 myElemTypeCombo->setId( nbTypes++, int( SMESH::ALL ));
140 if ( !hasNodes && nbTypes > 0 )
142 myElemTypeCombo->addItem( tr( "MEN_NODE" ));
143 myElemTypeCombo->setId( nbTypes++, int( SMESH::NODE ));
145 myElemTypeCombo->blockSignals(false);
148 //=======================================================================
149 // function : createMainFrame()
150 // purpose : Create frame containing dialog's input fields
151 //=======================================================================
152 QWidget* SMESHGUI_FindElemByPointDlg::createMainFrame (QWidget* theParent)
154 QWidget* aFrame = new QWidget(theParent);
158 QGroupBox* aMeshGrp = new QGroupBox(tr("MESH_GROUP"), aFrame);
159 QHBoxLayout* aMeshGrpLayout = new QHBoxLayout(aMeshGrp);
160 aMeshGrpLayout->setMargin(MARGIN);
161 aMeshGrpLayout->setSpacing(SPACING);
163 myMeshName = new QLineEdit(aMeshGrp);
164 aMeshGrpLayout->addWidget(myMeshName);
168 QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
169 QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
170 aCoordGrpLayout->setMargin(MARGIN);
171 aCoordGrpLayout->setSpacing(SPACING);
173 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
174 myX = new SMESHGUI_SpinBox(aCoordGrp);
176 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
177 myY = new SMESHGUI_SpinBox(aCoordGrp);
179 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
180 myZ = new SMESHGUI_SpinBox(aCoordGrp);
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");
189 aCoordGrpLayout->addWidget(aXLabel);
190 aCoordGrpLayout->addWidget(myX);
191 aCoordGrpLayout->addWidget(aYLabel);
192 aCoordGrpLayout->addWidget(myY);
193 aCoordGrpLayout->addWidget(aZLabel);
194 aCoordGrpLayout->addWidget(myZ);
198 QGroupBox* elementGrp = new QGroupBox(tr("Elements"), aFrame);
199 QGridLayout* elementGrpLayout = new QGridLayout(elementGrp);
200 elementGrpLayout->setSpacing(SPACING);
201 elementGrpLayout->setMargin(MARGIN);
204 myFindBtn = new QPushButton(elementGrp);
205 myFindBtn->setText(tr("Find"));
206 //myFindBtn->setCheckable(false);
208 myElemTypeCombo = new QtxComboBox(elementGrp);
210 myFoundList = new QListWidget(elementGrp);
212 elementGrpLayout->addWidget( myFindBtn, 0, 0 );
213 elementGrpLayout->addWidget( myElemTypeCombo, 0, 1 );
214 elementGrpLayout->addWidget( myFoundList, 1, 0, 2, 2 );
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);
224 // OK instead of "Apply and Close"
225 if ( QAbstractButton* but = button(OK) )
226 but->setText( tr("SMESH_BUT_OK"));
231 //================================================================================
235 //================================================================================
237 SMESHGUI_FindElemByPointOp::SMESHGUI_FindElemByPointOp()
238 :SMESHGUI_SelectionOp()
241 myDlg = new SMESHGUI_FindElemByPointDlg;
242 myHelpFileName = "find_element_by_point_page.html";
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 );
249 myPreview = new SMESH::MeshPreviewStruct();
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();
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;
261 myPreview->elementConnectivities.length(1);
262 myPreview->elementConnectivities[0] = 0;
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()));
274 //=======================================================================
275 // function : startOperation()
276 // purpose : Init dialog fields, connect signals and slots, show dialog
277 //=======================================================================
278 void SMESHGUI_FindElemByPointOp::startOperation()
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);
291 SMESHGUI_SelectionOp::startOperation();
295 onSelectionDone(); // init myMeshOrPart
298 //================================================================================
300 * \brief Stops operation
302 //================================================================================
304 void SMESHGUI_FindElemByPointOp::stopOperation()
308 mySimulation->SetVisibility(false);
312 selectionMgr()->removeFilter( myFilter );
313 SMESHGUI_SelectionOp::stopOperation();
316 //================================================================================
318 * \brief hilight found selected elements
320 //================================================================================
322 void SMESHGUI_FindElemByPointOp::onElemSelected()
324 if ( !myMeshIO.IsNull() )
326 Selection_Mode selMode =
327 myDlg->myElemTypeCombo->currentId() == int(SMESH::NODE) ? NodeSelection : CellSelection;
328 if ( selectionMode() != selMode )
329 setSelectionMode( selMode );
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() );
337 addOrRemoveIndex( myMeshIO, idMap, false );
340 aList.Append(myMeshIO);
341 selectionMgr()->setSelectedObjects(aList,false);
345 //================================================================================
347 * \brief Set selection mode according to element type
349 //================================================================================
351 void SMESHGUI_FindElemByPointOp::onElemTypeChange(int index)
353 Selection_Mode newMode = (index == 1) ? NodeSelection : CellSelection;
354 if ( selectionMode() != newMode )
356 selectionMgr()->clearFilters();
357 setSelectionMode( newMode );
359 myDlg->myFoundList->clear();
362 //================================================================================
364 * \brief Method needed for internal cuisine
366 //================================================================================
368 void SMESHGUI_FindElemByPointDlg::reject()
374 //================================================================================
376 * \brief Method needed for internal cuisine
378 //================================================================================
380 void SMESHGUI_FindElemByPointOp::onRejectedDlg()
385 //================================================================================
387 * \brief perform it's intention action: find elements
389 //================================================================================
391 void SMESHGUI_FindElemByPointOp::onFind()
393 if ( myMeshOrPart->_is_nil() )
397 SUIT_OverrideCursor wc;
399 SMESH::SMESH_Mesh_var aMesh = myMeshOrPart->GetMesh();
400 if ( aMesh->_is_nil() )
402 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
403 if (aMeshEditor->_is_nil())
406 SMESH::long_array_var foundIds;
407 if ( aMesh->_is_equivalent( myMeshOrPart ) )
409 aMeshEditor->FindElementsByPoint( myDlg->myX->GetValue(),
410 myDlg->myY->GetValue(),
411 myDlg->myZ->GetValue(),
412 SMESH::ElementType( myDlg->myElemTypeCombo->currentId()));
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] ));
424 if ( foundIds->length() > 0 )
425 myDlg->myFoundList->setCurrentRow(0);
427 catch (const SALOME::SALOME_Exception& S_ex) {
428 SalomeApp_Tools::QtCatchCorbaException(S_ex);
434 //================================================================================
436 * \brief Method needed for internal cuisine
438 //================================================================================
440 bool SMESHGUI_FindElemByPointOp::onApply()
446 //================================================================================
448 * \brief SLOT called when selection changed
450 //================================================================================
452 void SMESHGUI_FindElemByPointOp::onSelectionDone()
454 if ( !myDlg->isVisible() || !myDlg->isEnabled() )
457 QString oldMeshEntry, newMeshEntry;
458 if ( !myMeshIO.IsNull() && myMeshIO->hasEntry() )
459 oldMeshEntry = myMeshIO->getEntry();
463 selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
464 if (aList.Extent() == 1 && aList.First()->hasEntry())
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 )
473 myMeshOrPart = aMeshOrPart;
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 );
487 if ( oldMeshEntry != newMeshEntry || newMeshEntry.isEmpty() )
488 myDlg->myFoundList->clear();
490 myDlg->myFindBtn->setEnabled( !myMeshIO.IsNull() );
493 //================================================================================
495 * \brief show point by coordinates
497 //================================================================================
499 void SMESHGUI_FindElemByPointOp::redisplayPreview()
501 myDlg->myFoundList->clear();
503 myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
504 myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
505 myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
507 mySimulation->SetData(&myPreview.in());
510 //================================================================================
512 * \brief install filter on meshes
514 //================================================================================
516 void SMESHGUI_FindElemByPointOp::activateSelection()
518 selectionMgr()->clearFilters();
519 selectionMgr()->installFilter( myFilter );
522 //================================================================================
526 //================================================================================
528 SMESHGUI_FindElemByPointOp::~SMESHGUI_FindElemByPointOp()
530 if ( myDlg ) { delete myDlg; myDlg = 0; }
531 if ( mySimulation ) { delete mySimulation; mySimulation = 0; }
534 QList<SUIT_SelectionFilter*> sub = ((SMESH_LogicalFilter*)myFilter)->getFilters();
542 //================================================================================
544 * \brief Gets dialog of this operation
545 * \retval LightApp_Dialog* - pointer to dialog of this operation
547 //================================================================================
549 LightApp_Dialog* SMESHGUI_FindElemByPointOp::dlg() const