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(0);
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->setMargin( 0 );
168 aLay->setSpacing( SPACING );
169 aLay->addWidget(aMeshGrp);
170 aLay->addWidget(aCoordGrp);
171 aLay->addWidget(elementGrp);
173 // OK instead of "Apply and Close"
174 if ( QAbstractButton* but = button(OK) )
175 but->setText( tr("SMESH_BUT_OK"));
180 //================================================================================
184 //================================================================================
186 SMESHGUI_FindElemByPointOp::SMESHGUI_FindElemByPointOp()
187 :SMESHGUI_SelectionOp()
190 myDlg = new SMESHGUI_FindElemByPointDlg;
191 myHelpFileName = "find_element_by_point_page.html";
192 myFilter = new SMESH_TypeFilter( MESH );
194 myPreview = new SMESH::MeshPreviewStruct();
196 myPreview->nodesXYZ.length(1);
197 myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
198 myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
199 myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
201 myPreview->elementTypes.length(1);
202 myPreview->elementTypes[0].SMDS_ElementType = SMESH::NODE;
203 myPreview->elementTypes[0].isPoly = false;
204 myPreview->elementTypes[0].nbNodesInElement = 1;
206 myPreview->elementConnectivities.length(1);
207 myPreview->elementConnectivities[0] = 0;
209 // connect signals and slots
210 connect(myDlg->myFindBtn, SIGNAL(clicked()), this, SLOT(onFind()));
211 connect(myDlg->myX, SIGNAL(valueChanged(double)), this, SLOT(redisplayPreview()));
212 connect(myDlg->myY, SIGNAL(valueChanged(double)), this, SLOT(redisplayPreview()));
213 connect(myDlg->myZ, SIGNAL(valueChanged(double)), this, SLOT(redisplayPreview()));
214 connect(myDlg->myFoundList, SIGNAL(itemSelectionChanged()), this, SLOT(onElemSelected()));
215 connect(myDlg->myElemTypeCombo,SIGNAL(currentIndexChanged(int)),this, SLOT(onElemTypeChange(int)));
218 //=======================================================================
219 // function : startOperation()
220 // purpose : Init dialog fields, connect signals and slots, show dialog
221 //=======================================================================
222 void SMESHGUI_FindElemByPointOp::startOperation()
224 // init simulation with a current View
225 if ( mySimulation ) delete mySimulation;
226 mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
227 vtkProperty* aProp = vtkProperty::New();
228 aProp->SetRepresentationToWireframe();
229 aProp->SetColor(250, 0, 250);
230 aProp->SetPointSize(5);
231 aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
232 mySimulation->GetActor()->SetProperty(aProp);
235 myDlg->myElemTypeCombo->setCurrentId( int(SMESH::ALL));
237 SMESHGUI_SelectionOp::startOperation();
241 onSelectionDone(); // init myMesh
244 //================================================================================
246 * \brief Stops operation
248 //================================================================================
250 void SMESHGUI_FindElemByPointOp::stopOperation()
254 mySimulation->SetVisibility(false);
258 selectionMgr()->removeFilter( myFilter );
259 SMESHGUI_SelectionOp::stopOperation();
262 //================================================================================
264 * \brief hilight found selected elements
266 //================================================================================
268 void SMESHGUI_FindElemByPointOp::onElemSelected()
270 if ( !myMeshIO.IsNull() )
272 Selection_Mode selMode =
273 myDlg->myElemTypeCombo->currentId() == int(SMESH::NODE) ? NodeSelection : CellSelection;
274 if ( selectionMode() != selMode )
275 setSelectionMode( selMode );
277 QList<QListWidgetItem *> ids = myDlg->myFoundList->selectedItems();
278 QList<QListWidgetItem*>::iterator id = ids.begin();
279 TColStd_MapOfInteger idMap;
280 for ( ; id != ids.end(); ++id )
281 idMap.Add( (*id)->text().toInt() );
283 addOrRemoveIndex( myMeshIO, idMap, false );
286 aList.Append(myMeshIO);
287 selectionMgr()->setSelectedObjects(aList,false);
291 //================================================================================
293 * \brief Set selection mode according to element type
295 //================================================================================
297 void SMESHGUI_FindElemByPointOp::onElemTypeChange(int index)
299 Selection_Mode newMode = (index == 1) ? NodeSelection : CellSelection;
300 if ( selectionMode() != newMode )
302 selectionMgr()->clearFilters();
303 setSelectionMode( newMode );
305 myDlg->myFoundList->clear();
308 //================================================================================
310 * \brief perform it's intention action: find elements
312 //================================================================================
314 void SMESHGUI_FindElemByPointOp::onFind()
316 if ( myMesh->_is_nil() ) {
321 SUIT_OverrideCursor wc;
323 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
324 if (aMeshEditor->_is_nil())
327 SMESH::long_array_var foundIds =
328 aMeshEditor->FindElementsByPoint( myDlg->myX->GetValue(),
329 myDlg->myY->GetValue(),
330 myDlg->myZ->GetValue(),
331 SMESH::ElementType( myDlg->myElemTypeCombo->currentId() ));
332 myDlg->myFoundList->clear();
333 for ( int i = 0; i < foundIds->length(); ++i )
334 myDlg->myFoundList->addItem( QString::number( foundIds[i] ));
336 if ( foundIds->length() > 0 )
337 myDlg->myFoundList->setCurrentRow(0);
339 catch (const SALOME::SALOME_Exception& S_ex) {
340 SalomeApp_Tools::QtCatchCorbaException(S_ex);
346 //================================================================================
348 * \brief Method needed for internal cuisine
350 //================================================================================
352 bool SMESHGUI_FindElemByPointOp::onApply()
357 //================================================================================
359 * \brief SLOT called when selection changed
361 //================================================================================
363 void SMESHGUI_FindElemByPointOp::onSelectionDone()
365 if ( !myDlg->isVisible() || !myDlg->isEnabled() )
368 QString oldMeshEntry, newMeshEntry;
369 if ( !myMeshIO.IsNull() && myMeshIO->hasEntry() )
370 oldMeshEntry = myMeshIO->getEntry();
372 myDlg->myMeshName->setText("");
377 selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
378 if (aList.Extent() == 1 && aList.First()->hasEntry())
380 Handle(SALOME_InteractiveObject) anIO = aList.First();
381 _PTR(SObject) pObj = studyDS()->FindObjectID(anIO->getEntry());
382 myMesh = SMESH::GetMeshByIO( anIO );
383 if ( pObj && !myMesh->_is_nil() )
386 myDlg->myMeshName->setText( pObj->GetName().c_str() );
387 newMeshEntry = anIO->getEntry();
394 if ( oldMeshEntry != newMeshEntry || newMeshEntry.isEmpty() )
395 myDlg->myFoundList->clear();
397 myDlg->myFindBtn->setEnabled( !myMeshIO.IsNull() );
400 //================================================================================
402 * \brief show point by coordinates
404 //================================================================================
406 void SMESHGUI_FindElemByPointOp::redisplayPreview()
408 myDlg->myFoundList->clear();
410 myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
411 myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
412 myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
414 mySimulation->SetData(&myPreview.in());
417 //================================================================================
419 * \brief install filter on meshes
421 //================================================================================
423 void SMESHGUI_FindElemByPointOp::activateSelection()
425 selectionMgr()->clearFilters();
426 selectionMgr()->installFilter( myFilter );
429 //================================================================================
433 //================================================================================
435 SMESHGUI_FindElemByPointOp::~SMESHGUI_FindElemByPointOp()
437 if ( myDlg ) { delete myDlg; myDlg = 0; }
438 if ( mySimulation ) { delete mySimulation; mySimulation = 0; }
439 if ( myFilter ) { delete myFilter; myFilter = 0; }
442 //================================================================================
444 * \brief Gets dialog of this operation
445 * \retval LightApp_Dialog* - pointer to dialog of this operation
447 //================================================================================
449 LightApp_Dialog* SMESHGUI_FindElemByPointOp::dlg() const