1 // Copyright (C) 2007-2021 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, or (at your option) any later version.
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 < (int) 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()
242 myDlg = new SMESHGUI_FindElemByPointDlg;
243 myHelpFileName = "find_element_by_point.html";
245 QList<SUIT_SelectionFilter*> filters;
246 filters.append( new SMESH_TypeFilter( SMESH::MESH ) );
247 filters.append( new SMESH_TypeFilter( SMESH::GROUP ) );
248 myFilter = new SMESH_LogicalFilter( filters, SMESH_LogicalFilter::LO_OR );
250 myPreview = new SMESH::MeshPreviewStruct();
252 myPreview->nodesXYZ.length(1);
253 myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
254 myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
255 myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
257 myPreview->elementTypes.length(1);
258 myPreview->elementTypes[0].SMDS_ElementType = SMESH::NODE;
259 myPreview->elementTypes[0].isPoly = false;
260 myPreview->elementTypes[0].nbNodesInElement = 1;
262 myPreview->elementConnectivities.length(1);
263 myPreview->elementConnectivities[0] = 0;
265 // connect signals and slots
266 connect(myDlg->myFindBtn, SIGNAL(clicked()), this, SLOT(onFind()));
267 connect(myDlg->myX, SIGNAL(valueChanged(double)), this, SLOT(redisplayPreview()));
268 connect(myDlg->myY, SIGNAL(valueChanged(double)), this, SLOT(redisplayPreview()));
269 connect(myDlg->myZ, SIGNAL(valueChanged(double)), this, SLOT(redisplayPreview()));
270 connect(myDlg->myFoundList, SIGNAL(itemSelectionChanged()), this, SLOT(onElemSelected()));
271 connect(myDlg->myElemTypeCombo,SIGNAL(currentIndexChanged(int)),this, SLOT(onElemTypeChange(int)));
272 connect(myDlg, SIGNAL(rejectedDlg()), this, SLOT(onRejectedDlg()));
275 //=======================================================================
276 // function : startOperation()
277 // purpose : Init dialog fields, connect signals and slots, show dialog
278 //=======================================================================
279 void SMESHGUI_FindElemByPointOp::startOperation()
281 // init simulation with a current View
282 if ( mySimulation ) delete mySimulation;
283 mySMESHGUI = getSMESHGUI();
284 mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( mySMESHGUI ) );
285 connect(mySMESHGUI, SIGNAL (SignalActivatedViewManager()), this, SLOT(onOpenView()));
286 connect(mySMESHGUI, SIGNAL (SignalCloseView()), this, SLOT(onCloseView()));
287 vtkProperty* aProp = vtkProperty::New();
288 aProp->SetRepresentationToWireframe();
289 aProp->SetColor(250, 0, 250);
290 aProp->SetPointSize(5);
291 aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
292 mySimulation->GetActor()->SetProperty(aProp);
295 SMESHGUI_SelectionOp::startOperation();
299 onSelectionDone(); // init myMeshOrPart
302 //================================================================================
304 * \brief Stops operation
306 //================================================================================
308 void SMESHGUI_FindElemByPointOp::stopOperation()
312 mySimulation->SetVisibility(false);
316 disconnect(mySMESHGUI, SIGNAL (SignalActivatedViewManager()), this, SLOT(onOpenView()));
317 disconnect(mySMESHGUI, SIGNAL (SignalCloseView()), this, SLOT(onCloseView()));
318 selectionMgr()->removeFilter( myFilter );
319 SMESHGUI_SelectionOp::stopOperation();
322 //=================================================================================
324 * \brief SLOT called when the viewer opened
326 //=================================================================================
327 void SMESHGUI_FindElemByPointOp::onOpenView()
329 if ( mySimulation ) {
330 mySimulation->SetVisibility(false);
333 mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( mySMESHGUI ));
337 //=================================================================================
339 * \brief SLOT called when the viewer closed
341 //=================================================================================
342 void SMESHGUI_FindElemByPointOp::onCloseView()
347 //================================================================================
349 * \brief highlight found selected elements
351 //================================================================================
353 void SMESHGUI_FindElemByPointOp::onElemSelected()
355 if ( !myMeshIO.IsNull() )
357 Selection_Mode selMode =
358 myDlg->myElemTypeCombo->currentId().toInt() == int(SMESH::NODE) ? NodeSelection : CellSelection;
359 if ( selectionMode() != selMode )
360 setSelectionMode( selMode );
362 QList<QListWidgetItem *> ids = myDlg->myFoundList->selectedItems();
363 QList<QListWidgetItem*>::iterator id = ids.begin();
364 SVTK_TVtkIDsMap idMap;
365 for ( ; id != ids.end(); ++id )
366 idMap.Add( (*id)->text().toInt() );
368 addOrRemoveIndex( myMeshIO, idMap, false );
371 aList.Append(myMeshIO);
372 selectionMgr()->setSelectedObjects(aList,false);
376 //================================================================================
378 * \brief Set selection mode according to element type
380 //================================================================================
382 void SMESHGUI_FindElemByPointOp::onElemTypeChange(int index)
384 Selection_Mode newMode = (index == 1) ? NodeSelection : CellSelection;
385 if ( selectionMode() != newMode )
387 selectionMgr()->clearFilters();
388 setSelectionMode( newMode );
390 myDlg->myFoundList->clear();
393 //================================================================================
395 * \brief Method needed for internal cuisine
397 //================================================================================
399 void SMESHGUI_FindElemByPointDlg::reject()
405 //================================================================================
407 * \brief Method needed for internal cuisine
409 //================================================================================
411 void SMESHGUI_FindElemByPointOp::onRejectedDlg()
416 //================================================================================
418 * \brief perform it's intention action: find elements
420 //================================================================================
422 void SMESHGUI_FindElemByPointOp::onFind()
424 if ( myMeshOrPart->_is_nil() )
428 SUIT_OverrideCursor wc;
430 SMESH::SMESH_Mesh_var aMesh = myMeshOrPart->GetMesh();
431 if ( aMesh->_is_nil() )
433 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
434 if (aMeshEditor->_is_nil())
437 SMESH::smIdType_array_var foundIds;
438 if ( aMesh->_is_equivalent( myMeshOrPart ) )
440 aMeshEditor->FindElementsByPoint( myDlg->myX->GetValue(),
441 myDlg->myY->GetValue(),
442 myDlg->myZ->GetValue(),
443 SMESH::ElementType( myDlg->myElemTypeCombo->currentId().toInt()));
446 aMeshEditor->FindAmongElementsByPoint( myMeshOrPart,
447 myDlg->myX->GetValue(),
448 myDlg->myY->GetValue(),
449 myDlg->myZ->GetValue(),
450 SMESH::ElementType( myDlg->myElemTypeCombo->currentId().toInt()));
451 myDlg->myFoundList->clear();
452 for ( int i = 0; i < (int) foundIds->length(); ++i )
453 myDlg->myFoundList->addItem( QString::number( foundIds[i] ));
455 if ( foundIds->length() > 0 )
456 myDlg->myFoundList->setCurrentRow(0);
458 catch (const SALOME::SALOME_Exception& S_ex) {
459 SalomeApp_Tools::QtCatchCorbaException(S_ex);
465 //================================================================================
467 * \brief Method needed for internal cuisine
469 //================================================================================
471 bool SMESHGUI_FindElemByPointOp::onApply()
477 //================================================================================
479 * \brief SLOT called when selection changed
481 //================================================================================
483 void SMESHGUI_FindElemByPointOp::onSelectionDone()
485 if ( !myDlg->isVisible() || !myDlg->isEnabled() )
488 QString oldMeshEntry, newMeshEntry;
489 if ( !myMeshIO.IsNull() && myMeshIO->hasEntry() )
490 oldMeshEntry = myMeshIO->getEntry();
494 selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
495 if (aList.Extent() == 1 && aList.First()->hasEntry())
497 Handle(SALOME_InteractiveObject) anIO = aList.First();
498 _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID(anIO->getEntry());
499 CORBA::Object_var anObj = SMESH::IObjectToObject( anIO );
500 newMeshEntry = anIO->getEntry();
501 SMESH::SMESH_IDSource_var aMeshOrPart = SMESH::SMESH_IDSource::_narrow(anObj);
502 if ( pObj && !aMeshOrPart->_is_nil() && oldMeshEntry != newMeshEntry )
504 myMeshOrPart = aMeshOrPart;
507 std::string name = pObj->GetName();
508 myDlg->myMeshName->setText("");
509 myDlg->myMeshName->setText( QString( name.c_str() ).trimmed() );
510 SMESH::array_of_ElementType_var types = myMeshOrPart->GetTypes();
511 myDlg->setTypes( types );
518 if ( oldMeshEntry != newMeshEntry || newMeshEntry.isEmpty() )
519 myDlg->myFoundList->clear();
521 myDlg->myFindBtn->setEnabled( !myMeshIO.IsNull() );
524 //================================================================================
526 * \brief show point by coordinates
528 //================================================================================
530 void SMESHGUI_FindElemByPointOp::redisplayPreview()
532 myDlg->myFoundList->clear();
534 myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
535 myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
536 myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
538 mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( mySMESHGUI ));
539 mySimulation->SetData( myPreview.in());
542 //================================================================================
544 * \brief install filter on meshes
546 //================================================================================
548 void SMESHGUI_FindElemByPointOp::activateSelection()
550 selectionMgr()->clearFilters();
551 selectionMgr()->installFilter( myFilter );
554 //================================================================================
558 //================================================================================
560 SMESHGUI_FindElemByPointOp::~SMESHGUI_FindElemByPointOp()
562 if ( myDlg ) { delete myDlg; myDlg = 0; }
563 if ( mySimulation ) { delete mySimulation; mySimulation = 0; }
566 QList<SUIT_SelectionFilter*> sub = ((SMESH_LogicalFilter*)myFilter)->getFilters();
574 //================================================================================
576 * \brief Gets dialog of this operation
577 * \retval LightApp_Dialog* - pointer to dialog of this operation
579 //================================================================================
581 LightApp_Dialog* SMESHGUI_FindElemByPointOp::dlg() const