1 // Copyright (C) 2007-2010 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>
36 // SALOME GUI includes
37 #include <LightApp_SelectionMgr.h>
38 #include <QtxComboBox.h>
39 #include <SALOME_ListIO.hxx>
40 #include <SUIT_Desktop.h>
41 #include <SUIT_MessageBox.h>
42 #include <SUIT_OverrideCursor.h>
43 #include <SUIT_ResourceMgr.h>
44 #include <SVTK_ViewModel.h>
45 #include <SVTK_ViewWindow.h>
46 #include <SalomeApp_Tools.h>
47 #include <SalomeApp_TypeFilter.h>
50 #include <QAbstractButton>
51 #include <QGridLayout>
53 #include <QHBoxLayout>
56 #include <QListWidget>
57 #include <QPushButton>
58 #include <QVBoxLayout>
61 #include <vtkProperty.h>
64 #include <SALOMEconfig.h>
65 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
70 //=======================================================================
72 * \brief Dialog to find elements by a point coordinates
74 //=======================================================================
76 SMESHGUI_FindElemByPointDlg::SMESHGUI_FindElemByPointDlg()
77 : SMESHGUI_Dialog( 0, false, true, OK | Help )
79 setWindowTitle(tr("CAPTION"));
81 QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
82 aDlgLay->setMargin(MARGIN);;
83 aDlgLay->setSpacing(SPACING);
85 QWidget* aMainFrame = createMainFrame (mainFrame());
87 aDlgLay->addWidget(aMainFrame);
88 aDlgLay->setStretchFactor(aMainFrame, 1);
91 //=======================================================================
92 // function : createMainFrame()
93 // purpose : Create frame containing dialog's input fields
94 //=======================================================================
95 QWidget* SMESHGUI_FindElemByPointDlg::createMainFrame (QWidget* theParent)
97 QWidget* aFrame = new QWidget(theParent);
101 QGroupBox* aMeshGrp = new QGroupBox(tr("SMESH_MESH"), aFrame);
102 QHBoxLayout* aMeshGrpLayout = new QHBoxLayout(aMeshGrp);
103 aMeshGrpLayout->setMargin(MARGIN);
104 aMeshGrpLayout->setSpacing(SPACING);
106 myMeshName = new QLineEdit(aMeshGrp);
107 aMeshGrpLayout->addWidget(myMeshName);
111 QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
112 QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
113 aCoordGrpLayout->setMargin(MARGIN);
114 aCoordGrpLayout->setSpacing(SPACING);
116 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
117 myX = new SMESHGUI_SpinBox(aCoordGrp);
119 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
120 myY = new SMESHGUI_SpinBox(aCoordGrp);
122 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
123 myZ = new SMESHGUI_SpinBox(aCoordGrp);
125 myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
126 myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
127 myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
132 aCoordGrpLayout->addWidget(aXLabel);
133 aCoordGrpLayout->addWidget(myX);
134 aCoordGrpLayout->addWidget(aYLabel);
135 aCoordGrpLayout->addWidget(myY);
136 aCoordGrpLayout->addWidget(aZLabel);
137 aCoordGrpLayout->addWidget(myZ);
141 QGroupBox* elementGrp = new QGroupBox(tr("Elements"), aFrame);
142 QGridLayout* elementGrpLayout = new QGridLayout(elementGrp);
143 elementGrpLayout->setSpacing(SPACING);
144 elementGrpLayout->setMargin(MARGIN);
147 myFindBtn = new QPushButton(elementGrp);
148 myFindBtn->setText(tr("Find"));
149 //myFindBtn->setCheckable(false);
151 myElemTypeCombo = new QtxComboBox(elementGrp);
152 myElemTypeCombo->addItem( tr( "MEN_ALL" )); myElemTypeCombo->setId( 0, int( SMESH::ALL ));
153 myElemTypeCombo->addItem( tr( "MEN_NODE" )); myElemTypeCombo->setId( 1, int( SMESH::NODE ));
154 myElemTypeCombo->addItem( tr( "MEN_EDGE" )); myElemTypeCombo->setId( 2, int( SMESH::EDGE ));
155 myElemTypeCombo->addItem( tr( "MEN_FACE" )); myElemTypeCombo->setId( 3, int( SMESH::FACE ));
156 myElemTypeCombo->addItem( tr("MEN_VOLUME_3D"));myElemTypeCombo->setId( 4, int( SMESH::VOLUME));
157 myElemTypeCombo->addItem( tr( "MEN_ELEM0D" )); myElemTypeCombo->setId( 5, int( SMESH::ELEM0D));
159 myFoundList = new QListWidget(elementGrp);
161 elementGrpLayout->addWidget( myFindBtn, 0, 0 );
162 elementGrpLayout->addWidget( myElemTypeCombo, 0, 1 );
163 elementGrpLayout->addWidget( myFoundList, 1, 0, 2, 2 );
166 QVBoxLayout* aLay = new QVBoxLayout(aFrame);
167 aLay->addWidget(aMeshGrp);
168 aLay->addWidget(aCoordGrp);
169 aLay->addWidget(elementGrp);
171 // OK instead of "Apply and Close"
172 if ( QAbstractButton* but = button(OK) )
173 but->setText( tr("SMESH_BUT_OK"));
178 //================================================================================
182 //================================================================================
184 SMESHGUI_FindElemByPointOp::SMESHGUI_FindElemByPointOp()
185 :SMESHGUI_SelectionOp()
188 myDlg = new SMESHGUI_FindElemByPointDlg;
189 myHelpFileName = "find_element_by_point_page.html";
190 myFilter = new SMESH_TypeFilter( MESH );
192 myPreview = new SMESH::MeshPreviewStruct();
194 myPreview->nodesXYZ.length(1);
195 myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
196 myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
197 myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
199 myPreview->elementTypes.length(1);
200 myPreview->elementTypes[0].SMDS_ElementType = SMESH::NODE;
201 myPreview->elementTypes[0].isPoly = false;
202 myPreview->elementTypes[0].nbNodesInElement = 1;
204 myPreview->elementConnectivities.length(1);
205 myPreview->elementConnectivities[0] = 0;
207 // connect signals and slots
208 connect(myDlg->myFindBtn, SIGNAL(clicked()), this, SLOT(onFind()));
209 connect(myDlg->myX, SIGNAL(valueChanged(double)), this, SLOT(redisplayPreview()));
210 connect(myDlg->myY, SIGNAL(valueChanged(double)), this, SLOT(redisplayPreview()));
211 connect(myDlg->myZ, SIGNAL(valueChanged(double)), this, SLOT(redisplayPreview()));
212 connect(myDlg->myFoundList, SIGNAL(itemSelectionChanged()), this, SLOT(onElemSelected()));
213 connect(myDlg->myElemTypeCombo,SIGNAL(currentIndexChanged(int)),this, SLOT(onElemTypeChange(int)));
216 //=======================================================================
217 // function : startOperation()
218 // purpose : Init dialog fields, connect signals and slots, show dialog
219 //=======================================================================
220 void SMESHGUI_FindElemByPointOp::startOperation()
222 // init simulation with a current View
223 if ( mySimulation ) delete mySimulation;
224 mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
225 vtkProperty* aProp = vtkProperty::New();
226 aProp->SetRepresentationToWireframe();
227 aProp->SetColor(250, 0, 250);
228 aProp->SetPointSize(5);
229 aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
230 mySimulation->GetActor()->SetProperty(aProp);
233 myDlg->myElemTypeCombo->setCurrentId( int(SMESH::ALL));
235 SMESHGUI_SelectionOp::startOperation();
239 onSelectionDone(); // init myMesh
242 //================================================================================
244 * \brief Stops operation
246 //================================================================================
248 void SMESHGUI_FindElemByPointOp::stopOperation()
252 mySimulation->SetVisibility(false);
256 selectionMgr()->removeFilter( myFilter );
257 SMESHGUI_SelectionOp::stopOperation();
260 //================================================================================
262 * \brief hilight found selected elements
264 //================================================================================
266 void SMESHGUI_FindElemByPointOp::onElemSelected()
268 if ( !myMeshIO.IsNull() )
270 Selection_Mode selMode =
271 myDlg->myElemTypeCombo->currentId() == int(SMESH::NODE) ? NodeSelection : CellSelection;
272 if ( selectionMode() != selMode )
273 setSelectionMode( selMode );
275 QList<QListWidgetItem *> ids = myDlg->myFoundList->selectedItems();
276 QList<QListWidgetItem*>::iterator id = ids.begin();
277 TColStd_MapOfInteger idMap;
278 for ( ; id != ids.end(); ++id )
279 idMap.Add( (*id)->text().toInt() );
281 addOrRemoveIndex( myMeshIO, idMap, false );
284 aList.Append(myMeshIO);
285 selectionMgr()->setSelectedObjects(aList,false);
289 //================================================================================
291 * \brief Set selection mode according to element type
293 //================================================================================
295 void SMESHGUI_FindElemByPointOp::onElemTypeChange(int index)
297 Selection_Mode newMode = (index == 1) ? NodeSelection : CellSelection;
298 if ( selectionMode() != newMode )
300 selectionMgr()->clearFilters();
301 setSelectionMode( newMode );
303 myDlg->myFoundList->clear();
306 //================================================================================
308 * \brief perform it's intention action: find elements
310 //================================================================================
312 void SMESHGUI_FindElemByPointOp::onFind()
314 if ( myMesh->_is_nil() ) {
319 SUIT_OverrideCursor wc;
321 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
322 if (aMeshEditor->_is_nil())
325 SMESH::long_array_var foundIds =
326 aMeshEditor->FindElementsByPoint( myDlg->myX->GetValue(),
327 myDlg->myY->GetValue(),
328 myDlg->myZ->GetValue(),
329 SMESH::ElementType( myDlg->myElemTypeCombo->currentId() ));
330 myDlg->myFoundList->clear();
331 for ( int i = 0; i < foundIds->length(); ++i )
332 myDlg->myFoundList->addItem( QString::number( foundIds[i] ));
334 if ( foundIds->length() > 0 )
335 myDlg->myFoundList->setCurrentRow(0);
337 catch (const SALOME::SALOME_Exception& S_ex) {
338 SalomeApp_Tools::QtCatchCorbaException(S_ex);
344 //================================================================================
346 * \brief Method needed for internal cuisine
348 //================================================================================
350 bool SMESHGUI_FindElemByPointOp::onApply()
355 //================================================================================
357 * \brief SLOT called when selection changed
359 //================================================================================
361 void SMESHGUI_FindElemByPointOp::onSelectionDone()
363 if ( !myDlg->isVisible() || !myDlg->isEnabled() )
366 QString oldMeshEntry, newMeshEntry;
367 if ( !myMeshIO.IsNull() && myMeshIO->hasEntry() )
368 oldMeshEntry = myMeshIO->getEntry();
370 myDlg->myMeshName->setText("");
375 selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
376 if (aList.Extent() == 1 && aList.First()->hasEntry())
378 Handle(SALOME_InteractiveObject) anIO = aList.First();
379 _PTR(SObject) pObj = studyDS()->FindObjectID(anIO->getEntry());
380 myMesh = SMESH::GetMeshByIO( anIO );
381 if ( pObj && !myMesh->_is_nil() )
384 myDlg->myMeshName->setText( pObj->GetName().c_str() );
385 newMeshEntry = anIO->getEntry();
392 if ( oldMeshEntry != newMeshEntry || newMeshEntry.isEmpty() )
393 myDlg->myFoundList->clear();
395 myDlg->myFindBtn->setEnabled( !myMeshIO.IsNull() );
398 //================================================================================
400 * \brief show point by coordinates
402 //================================================================================
404 void SMESHGUI_FindElemByPointOp::redisplayPreview()
406 myDlg->myFoundList->clear();
408 myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
409 myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
410 myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
412 mySimulation->SetData(&myPreview.in());
415 //================================================================================
417 * \brief install filter on meshes
419 //================================================================================
421 void SMESHGUI_FindElemByPointOp::activateSelection()
423 selectionMgr()->clearFilters();
424 selectionMgr()->installFilter( myFilter );
427 //================================================================================
431 //================================================================================
433 SMESHGUI_FindElemByPointOp::~SMESHGUI_FindElemByPointOp()
435 if ( myDlg ) { delete myDlg; myDlg = 0; }
436 if ( mySimulation ) { delete mySimulation; mySimulation = 0; }
437 if ( myFilter ) { delete myFilter; myFilter = 0; }
440 //================================================================================
442 * \brief Gets dialog of this operation
443 * \retval LightApp_Dialog* - pointer to dialog of this operation
445 //================================================================================
447 LightApp_Dialog* SMESHGUI_FindElemByPointOp::dlg() const