Back to index

salome-smesh  6.5.0
SMESHGUI_FindElemByPointDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File   : SMESHGUI_FindElemByPointDlg.cxx
00021 // Author : Edward AGAPOV, Open CASCADE S.A.S.
00022 // SMESH includes
00023 //
00024 #include "SMESHGUI_FindElemByPointDlg.h"
00025 
00026 #include "SMESHGUI.h"
00027 #include "SMESHGUI_MeshUtils.h"
00028 #include "SMESHGUI_VTKUtils.h"
00029 #include "SMESHGUI_SpinBox.h"
00030 #include "SMESHGUI_MeshEditPreview.h"
00031 
00032 #include "SMESH_Actor.h"
00033 #include "SMESH_ActorUtils.h"
00034 #include "SMESH_TypeFilter.hxx"
00035 #include "SMESH_LogicalFilter.hxx"
00036 
00037 // SALOME GUI includes
00038 #include <LightApp_SelectionMgr.h>
00039 #include <QtxComboBox.h>
00040 #include <SALOME_ListIO.hxx>
00041 #include <SUIT_Desktop.h>
00042 #include <SUIT_MessageBox.h>
00043 #include <SUIT_OverrideCursor.h>
00044 #include <SUIT_ResourceMgr.h>
00045 #include <SVTK_ViewModel.h>
00046 #include <SVTK_ViewWindow.h>
00047 #include <SalomeApp_Tools.h>
00048 #include <SalomeApp_TypeFilter.h>
00049 
00050 // Qt includes
00051 #include <QAbstractButton>
00052 #include <QGridLayout>
00053 #include <QGroupBox>
00054 #include <QHBoxLayout>
00055 #include <QLabel>
00056 #include <QLineEdit>
00057 #include <QListWidget>
00058 #include <QPushButton>
00059 #include <QVBoxLayout>
00060 
00061 // VTK includes
00062 #include <vtkProperty.h>
00063 
00064 // IDL includes
00065 #include <SALOMEconfig.h>
00066 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
00067 
00068 #define SPACING 6
00069 #define MARGIN  11
00070 
00071 //=======================================================================
00075 //=======================================================================
00076 
00077 SMESHGUI_FindElemByPointDlg::SMESHGUI_FindElemByPointDlg()
00078   : SMESHGUI_Dialog( 0, false, true, OK | Help )
00079 {
00080   setWindowTitle(tr("CAPTION"));
00081 
00082   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
00083   aDlgLay->setMargin(0);
00084   aDlgLay->setSpacing(SPACING);
00085 
00086   QWidget* aMainFrame = createMainFrame  (mainFrame());
00087 
00088   aDlgLay->addWidget(aMainFrame);
00089   aDlgLay->setStretchFactor(aMainFrame, 1);
00090 }
00091 
00092 //================================================================================
00096 //================================================================================
00097 
00098 void SMESHGUI_FindElemByPointDlg::setTypes(SMESH::array_of_ElementType_var & types)
00099 {
00100   myElemTypeCombo->blockSignals(true);
00101   myElemTypeCombo->clear();
00102   int nbTypes = 0, hasNodes = 0;
00103   for ( int i = 0; i < types->length(); ++i )
00104   {
00105     switch ( types[i] ) {
00106     case SMESH::NODE:
00107       myElemTypeCombo->addItem( tr( "MEN_NODE" ));
00108       myElemTypeCombo->setId( nbTypes++, int( SMESH::NODE  ));
00109       hasNodes = 1;
00110       break;
00111     case SMESH::EDGE:
00112       myElemTypeCombo->addItem( tr( "MEN_EDGE" ));
00113       myElemTypeCombo->setId( nbTypes++, int( SMESH::EDGE  ));
00114       break;
00115     case SMESH::FACE:
00116       myElemTypeCombo->addItem( tr( "MEN_FACE" ));
00117       myElemTypeCombo->setId( nbTypes++, int( SMESH::FACE  ));
00118       break;
00119     case SMESH::VOLUME:
00120       myElemTypeCombo->addItem( tr( "MEN_VOLUME_3D" ));
00121       myElemTypeCombo->setId( nbTypes++, int( SMESH::VOLUME  ));
00122       break;
00123     case SMESH::ELEM0D:
00124       myElemTypeCombo->addItem( tr( "MEN_ELEM0D" ));
00125       myElemTypeCombo->setId( nbTypes++, int( SMESH::ELEM0D  ));
00126       break;
00127     default:;
00128     }
00129   }
00130   if ( nbTypes - hasNodes > 1 )
00131   {
00132     myElemTypeCombo->addItem( tr( "MEN_ALL" ));
00133     myElemTypeCombo->setId( nbTypes++, int( SMESH::ALL   ));
00134   }
00135   if ( !hasNodes && nbTypes > 0 )
00136   {
00137     myElemTypeCombo->addItem( tr( "MEN_NODE" ));
00138     myElemTypeCombo->setId( nbTypes++, int( SMESH::NODE  ));
00139   }
00140   myElemTypeCombo->blockSignals(false);
00141 }
00142 
00143 //=======================================================================
00144 // function : createMainFrame()
00145 // purpose  : Create frame containing dialog's input fields
00146 //=======================================================================
00147 QWidget* SMESHGUI_FindElemByPointDlg::createMainFrame (QWidget* theParent)
00148 {
00149   QWidget* aFrame = new QWidget(theParent);
00150 
00151   //mesh name
00152 
00153   QGroupBox* aMeshGrp = new QGroupBox(tr("MESH_GROUP"), aFrame);
00154   QHBoxLayout* aMeshGrpLayout = new QHBoxLayout(aMeshGrp);
00155   aMeshGrpLayout->setMargin(MARGIN);
00156   aMeshGrpLayout->setSpacing(SPACING);
00157 
00158   myMeshName = new QLineEdit(aMeshGrp);
00159   aMeshGrpLayout->addWidget(myMeshName);
00160 
00161   // coordinates
00162 
00163   QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
00164   QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
00165   aCoordGrpLayout->setMargin(MARGIN);
00166   aCoordGrpLayout->setSpacing(SPACING);
00167 
00168   QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
00169   myX = new SMESHGUI_SpinBox(aCoordGrp);
00170 
00171   QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
00172   myY = new SMESHGUI_SpinBox(aCoordGrp);
00173 
00174   QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
00175   myZ = new SMESHGUI_SpinBox(aCoordGrp);
00176 
00177   myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
00178   myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
00179   myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
00180   myX->SetValue(0);
00181   myY->SetValue(0);
00182   myZ->SetValue(0);
00183 
00184   aCoordGrpLayout->addWidget(aXLabel);
00185   aCoordGrpLayout->addWidget(myX);
00186   aCoordGrpLayout->addWidget(aYLabel);
00187   aCoordGrpLayout->addWidget(myY);
00188   aCoordGrpLayout->addWidget(aZLabel);
00189   aCoordGrpLayout->addWidget(myZ);
00190 
00191   // Elements
00192 
00193   QGroupBox* elementGrp = new QGroupBox(tr("Elements"), aFrame);
00194   QGridLayout* elementGrpLayout = new QGridLayout(elementGrp);
00195   elementGrpLayout->setSpacing(SPACING);
00196   elementGrpLayout->setMargin(MARGIN);
00197 
00198 
00199   myFindBtn = new QPushButton(elementGrp);
00200   myFindBtn->setText(tr("Find"));
00201   //myFindBtn->setCheckable(false);
00202 
00203   myElemTypeCombo = new QtxComboBox(elementGrp);
00204 
00205   myFoundList = new QListWidget(elementGrp);
00206 
00207   elementGrpLayout->addWidget( myFindBtn,       0, 0 );
00208   elementGrpLayout->addWidget( myElemTypeCombo, 0, 1 );
00209   elementGrpLayout->addWidget( myFoundList,     1, 0, 2, 2 );
00210 
00211 
00212   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
00213   aLay->setMargin( 0 );
00214   aLay->setSpacing( SPACING );
00215   aLay->addWidget(aMeshGrp);
00216   aLay->addWidget(aCoordGrp);
00217   aLay->addWidget(elementGrp);
00218 
00219   // OK instead of "Apply and Close"
00220   if ( QAbstractButton* but = button(OK) )
00221     but->setText( tr("SMESH_BUT_OK"));
00222 
00223   return aFrame;
00224 }
00225 
00226 //================================================================================
00230 //================================================================================
00231 
00232 SMESHGUI_FindElemByPointOp::SMESHGUI_FindElemByPointOp()
00233   :SMESHGUI_SelectionOp()
00234 {
00235   mySimulation = 0;
00236   myDlg = new SMESHGUI_FindElemByPointDlg;
00237   myHelpFileName = "find_element_by_point_page.html";
00238 
00239   QList<SUIT_SelectionFilter*> filters;
00240   filters.append( new SMESH_TypeFilter( MESH ) );
00241   filters.append( new SMESH_TypeFilter( GROUP ) );
00242   myFilter = new SMESH_LogicalFilter( filters, SMESH_LogicalFilter::LO_OR );
00243 
00244   myPreview = new SMESH::MeshPreviewStruct();
00245 
00246   myPreview->nodesXYZ.length(1);
00247   myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
00248   myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
00249   myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
00250 
00251   myPreview->elementTypes.length(1);
00252   myPreview->elementTypes[0].SMDS_ElementType = SMESH::NODE;
00253   myPreview->elementTypes[0].isPoly = false;
00254   myPreview->elementTypes[0].nbNodesInElement = 1;
00255 
00256   myPreview->elementConnectivities.length(1);
00257   myPreview->elementConnectivities[0] = 0;
00258 
00259   // connect signals and slots
00260   connect(myDlg->myFindBtn,      SIGNAL(clicked()),               this, SLOT(onFind()));
00261   connect(myDlg->myX,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
00262   connect(myDlg->myY,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
00263   connect(myDlg->myZ,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
00264   connect(myDlg->myFoundList,    SIGNAL(itemSelectionChanged()),  this, SLOT(onElemSelected()));
00265   connect(myDlg->myElemTypeCombo,SIGNAL(currentIndexChanged(int)),this, SLOT(onElemTypeChange(int)));
00266   connect(myDlg,                 SIGNAL(rejectedDlg()),           this, SLOT(onRejectedDlg()));
00267 }
00268 
00269 //=======================================================================
00270 // function : startOperation()
00271 // purpose  : Init dialog fields, connect signals and slots, show dialog
00272 //=======================================================================
00273 void SMESHGUI_FindElemByPointOp::startOperation()
00274 {
00275   // init simulation with a current View
00276   if ( mySimulation ) delete mySimulation;
00277   mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
00278   vtkProperty* aProp = vtkProperty::New();
00279   aProp->SetRepresentationToWireframe();
00280   aProp->SetColor(250, 0, 250);
00281   aProp->SetPointSize(5);
00282   aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
00283   mySimulation->GetActor()->SetProperty(aProp);
00284   aProp->Delete();
00285 
00286   SMESHGUI_SelectionOp::startOperation();
00287   myDlg->show();
00288   redisplayPreview();
00289 
00290   onSelectionDone(); // init myMeshOrPart
00291 }
00292 
00293 //================================================================================
00297 //================================================================================
00298 
00299 void SMESHGUI_FindElemByPointOp::stopOperation()
00300 {
00301   if ( mySimulation )
00302   {
00303     mySimulation->SetVisibility(false);
00304     delete mySimulation;
00305     mySimulation = 0;
00306   }
00307   selectionMgr()->removeFilter( myFilter );
00308   SMESHGUI_SelectionOp::stopOperation();
00309 }
00310 
00311 //================================================================================
00315 //================================================================================
00316 
00317 void SMESHGUI_FindElemByPointOp::onElemSelected()
00318 {
00319   if ( !myMeshIO.IsNull() )
00320   {
00321     Selection_Mode selMode =
00322       myDlg->myElemTypeCombo->currentId() == int(SMESH::NODE) ? NodeSelection : CellSelection;
00323     if ( selectionMode() != selMode )
00324       setSelectionMode( selMode );
00325 
00326     QList<QListWidgetItem *> ids = myDlg->myFoundList->selectedItems();
00327     QList<QListWidgetItem*>::iterator id = ids.begin();
00328     TColStd_MapOfInteger idMap;
00329     for ( ; id != ids.end(); ++id )
00330       idMap.Add( (*id)->text().toInt() );
00331 
00332     addOrRemoveIndex( myMeshIO, idMap, false );
00333 
00334     SALOME_ListIO aList;
00335     aList.Append(myMeshIO);
00336     selectionMgr()->setSelectedObjects(aList,false);
00337   }
00338 }
00339 
00340 //================================================================================
00344 //================================================================================
00345 
00346 void SMESHGUI_FindElemByPointOp::onElemTypeChange(int index)
00347 {
00348   Selection_Mode newMode = (index == 1) ? NodeSelection : CellSelection;
00349   if ( selectionMode() != newMode )
00350   {
00351     selectionMgr()->clearFilters();
00352     setSelectionMode( newMode );
00353   }
00354   myDlg->myFoundList->clear();
00355 }
00356 
00357 //================================================================================
00361 //================================================================================
00362 
00363 void SMESHGUI_FindElemByPointDlg::reject()
00364 {
00365   emit rejectedDlg();
00366   QtxDialog::reject();
00367 }
00368 
00369 //================================================================================
00373 //================================================================================
00374 
00375 void SMESHGUI_FindElemByPointOp::onRejectedDlg()
00376 {
00377   myMeshIO.Nullify(); 
00378 }
00379 
00380 //================================================================================
00384 //================================================================================
00385 
00386 void SMESHGUI_FindElemByPointOp::onFind()
00387 {
00388   if ( myMeshOrPart->_is_nil() )
00389     return;
00390 
00391   try {
00392     SUIT_OverrideCursor wc;
00393 
00394     SMESH::SMESH_Mesh_var aMesh = myMeshOrPart->GetMesh();
00395     if ( aMesh->_is_nil() )
00396       return;
00397     SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
00398     if (aMeshEditor->_is_nil())
00399       return;
00400 
00401     SMESH::long_array_var foundIds;
00402     if ( aMesh->_is_equivalent( myMeshOrPart ) )
00403       foundIds =
00404         aMeshEditor->FindElementsByPoint( myDlg->myX->GetValue(),
00405                                           myDlg->myY->GetValue(),
00406                                           myDlg->myZ->GetValue(),
00407                                           SMESH::ElementType( myDlg->myElemTypeCombo->currentId()));
00408     else
00409       foundIds =
00410         aMeshEditor->FindAmongElementsByPoint( myMeshOrPart,
00411                                                myDlg->myX->GetValue(),
00412                                                myDlg->myY->GetValue(),
00413                                                myDlg->myZ->GetValue(),
00414                                                SMESH::ElementType( myDlg->myElemTypeCombo->currentId()));
00415     myDlg->myFoundList->clear();
00416     for ( int i = 0; i < foundIds->length(); ++i )
00417       myDlg->myFoundList->addItem( QString::number( foundIds[i] ));
00418 
00419     if ( foundIds->length() > 0 )
00420       myDlg->myFoundList->setCurrentRow(0);
00421   }
00422   catch (const SALOME::SALOME_Exception& S_ex) {
00423     SalomeApp_Tools::QtCatchCorbaException(S_ex);
00424   }
00425   catch (...) {
00426   }
00427 }
00428 
00429 //================================================================================
00433 //================================================================================
00434 
00435 bool SMESHGUI_FindElemByPointOp::onApply()
00436 {
00437   onRejectedDlg();
00438   return true;
00439 }
00440 
00441 //================================================================================
00445 //================================================================================
00446 
00447 void SMESHGUI_FindElemByPointOp::onSelectionDone()
00448 {
00449   if ( !myDlg->isVisible() || !myDlg->isEnabled() )
00450     return;
00451 
00452   QString oldMeshEntry, newMeshEntry;
00453   if ( !myMeshIO.IsNull() && myMeshIO->hasEntry() )
00454     oldMeshEntry = myMeshIO->getEntry();
00455 
00456   try {
00457     SALOME_ListIO aList;
00458     selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
00459     if (aList.Extent() == 1 && aList.First()->hasEntry())
00460     {
00461       Handle(SALOME_InteractiveObject) anIO = aList.First();
00462       _PTR(SObject) pObj = studyDS()->FindObjectID(anIO->getEntry());
00463       CORBA::Object_var anObj = SMESH::IObjectToObject( anIO );
00464       newMeshEntry = anIO->getEntry();
00465       SMESH::SMESH_IDSource_var aMeshOrPart = SMESH::SMESH_IDSource::_narrow(anObj);
00466       if ( pObj && !aMeshOrPart->_is_nil() && oldMeshEntry != newMeshEntry )
00467       {
00468         myMeshOrPart = aMeshOrPart;
00469         myMeshIO.Nullify();
00470         myMeshIO = anIO;
00471         std::string name = pObj->GetName();
00472         myDlg->myMeshName->setText("");
00473         myDlg->myMeshName->setText( QString( name.c_str() ).trimmed() );
00474         SMESH::array_of_ElementType_var  types = myMeshOrPart->GetTypes();
00475         myDlg->setTypes( types );
00476       }
00477     }
00478   }
00479   catch (...) {
00480   }
00481 
00482   if ( oldMeshEntry != newMeshEntry || newMeshEntry.isEmpty() )
00483     myDlg->myFoundList->clear();
00484 
00485   myDlg->myFindBtn->setEnabled( !myMeshIO.IsNull() );
00486 }
00487 
00488 //================================================================================
00492 //================================================================================
00493 
00494 void SMESHGUI_FindElemByPointOp::redisplayPreview()
00495 {
00496   myDlg->myFoundList->clear();
00497 
00498   myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
00499   myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
00500   myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
00501 
00502   mySimulation->SetData(&myPreview.in());
00503 }
00504 
00505 //================================================================================
00509 //================================================================================
00510 
00511 void SMESHGUI_FindElemByPointOp::activateSelection()
00512 {
00513   selectionMgr()->clearFilters();
00514   selectionMgr()->installFilter( myFilter );
00515 }
00516 
00517 //================================================================================
00521 //================================================================================
00522 
00523 SMESHGUI_FindElemByPointOp::~SMESHGUI_FindElemByPointOp()
00524 {
00525   if ( myDlg )         { delete myDlg;        myDlg = 0; }
00526   if ( mySimulation )  { delete mySimulation; mySimulation = 0; }
00527   if ( myFilter )
00528   {
00529     QList<SUIT_SelectionFilter*> sub =  ((SMESH_LogicalFilter*)myFilter)->getFilters();
00530     delete sub.front();
00531     delete sub.back();
00532     delete myFilter;
00533     myFilter = 0;
00534   }
00535 }
00536 
00537 //================================================================================
00542 //================================================================================
00543 
00544 LightApp_Dialog* SMESHGUI_FindElemByPointOp::dlg() const
00545 {
00546   return myDlg;
00547 }
00548