Back to index

salome-smesh  6.5.0
SMESHGUI_ShapeByMeshDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File   : SMESHGUI_ShapeByMeshDlg.cxx
00024 // Author : Edward AGAPOV, Open CASCADE S.A.S.
00025 // SMESH includes
00026 //
00027 #include "SMESHGUI_ShapeByMeshDlg.h"
00028 
00029 #include "SMESHGUI.h"
00030 #include "SMESHGUI_GEOMGenUtils.h"
00031 #include "SMESHGUI_IdValidator.h"
00032 #include "SMESHGUI_MeshUtils.h"
00033 #include "SMESHGUI_Utils.h"
00034 #include "SMESHGUI_VTKUtils.h"
00035 
00036 #include <SMDS_Mesh.hxx>
00037 #include <SMDS_MeshNode.hxx>
00038 #include <SMESH_Actor.h>
00039 
00040 // SALOME GEOM includes
00041 #include <GEOMBase.h>
00042 #include <GeometryGUI.h>
00043 
00044 // SALOME GUI includes
00045 #include <LightApp_DataOwner.h>
00046 #include <LightApp_SelectionMgr.h>
00047 #include <SALOME_ListIO.hxx>
00048 #include <SUIT_Desktop.h>
00049 #include <SVTK_Selector.h>
00050 #include <SVTK_ViewWindow.h>
00051 #include <SVTK_ViewModel.h>
00052 #include <SalomeApp_Tools.h>
00053 
00054 // SALOME KERNEL includes
00055 #include <SALOMEDSClient_SObject.hxx>
00056 
00057 // OCCT includes
00058 #include <TColStd_MapOfInteger.hxx>
00059 #include <TopoDS_Shape.hxx>
00060 #include <TopExp_Explorer.hxx>
00061 
00062 // Qt includes
00063 #include <QFrame>
00064 #include <QVBoxLayout>
00065 #include <QHBoxLayout>
00066 #include <QGridLayout>
00067 #include <QLineEdit>
00068 #include <QLabel>
00069 #include <QRadioButton>
00070 #include <QButtonGroup>
00071 #include <QGroupBox>
00072 #include <QStringList>
00073 
00074 #define SPACING 6
00075 #define MARGIN  11
00076 
00077 enum { EDGE = 0, FACE, VOLUME };
00078 
00083 SMESHGUI_ShapeByMeshDlg::SMESHGUI_ShapeByMeshDlg()
00084   : SMESHGUI_Dialog( 0, false, true, OK | Close )
00085 {
00086   setWindowTitle(tr("CAPTION"));
00087 
00088   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
00089   aDlgLay->setMargin(MARGIN);
00090   aDlgLay->setSpacing(SPACING);
00091 
00092   QFrame* aMainFrame = createMainFrame  (mainFrame());
00093 
00094   aDlgLay->addWidget(aMainFrame);
00095 
00096   aDlgLay->setStretchFactor(aMainFrame, 1);
00097 }
00098 
00099 //=======================================================================
00100 // function : createMainFrame()
00101 // purpose  : Create frame containing dialog's input fields
00102 //=======================================================================
00103 QFrame* SMESHGUI_ShapeByMeshDlg::createMainFrame (QWidget* theParent)
00104 {
00105   QFrame* aMainGrp = new QFrame(theParent);
00106   QGridLayout* aLayout = new QGridLayout(aMainGrp);
00107   aLayout->setMargin(0);
00108   aLayout->setSpacing(SPACING);
00109 
00110   // elem type
00111   myElemTypeBox = new QGroupBox(tr("SMESH_ELEMENT_TYPE"), aMainGrp);
00112   myElemTypeGroup = new QButtonGroup(aMainGrp);
00113   QHBoxLayout* myElemTypeBoxLayout = new QHBoxLayout(myElemTypeBox);
00114   myElemTypeBoxLayout->setMargin(MARGIN);
00115   myElemTypeBoxLayout->setSpacing(SPACING);
00116 
00117   QRadioButton* aEdgeRb   = new QRadioButton( tr("SMESH_EDGE"),   myElemTypeBox);
00118   QRadioButton* aFaceRb   = new QRadioButton( tr("SMESH_FACE"),   myElemTypeBox);
00119   QRadioButton* aVolumeRb = new QRadioButton( tr("SMESH_VOLUME"), myElemTypeBox);
00120   
00121   myElemTypeBoxLayout->addWidget(aEdgeRb);
00122   myElemTypeBoxLayout->addWidget(aFaceRb);
00123   myElemTypeBoxLayout->addWidget(aVolumeRb);
00124   myElemTypeGroup->addButton(aEdgeRb, 0);
00125   myElemTypeGroup->addButton(aFaceRb, 1);
00126   myElemTypeGroup->addButton(aVolumeRb, 2);
00127   aEdgeRb->setChecked(true);
00128   
00129   // element id
00130   QLabel* anIdLabel = new QLabel( tr("ELEMENT_ID"), aMainGrp );
00131   myElementId = new QLineEdit( aMainGrp );
00132   myElementId->setValidator( new SMESHGUI_IdValidator( theParent, 
00133                                                        !myIsMultipleAllowed ? 1 : 0 ) ); // 0 for any number of entities
00134 
00135   // shape name
00136   QLabel* aNameLabel = new QLabel( tr("GEOMETRY_NAME"), aMainGrp );
00137   myGeomName = new QLineEdit( aMainGrp );
00138 
00139   aLayout->addWidget(myElemTypeBox, 0, 0, 1, 2);
00140   aLayout->addWidget(anIdLabel,   1, 0);
00141   aLayout->addWidget(myElementId, 1, 1);
00142   aLayout->addWidget(aNameLabel,  2, 0);
00143   aLayout->addWidget(myGeomName,  2, 1);
00144 
00145   return aMainGrp;
00146 }
00147 
00148 //=======================================================================
00149 // function : ~SMESHGUI_ShapeByMeshDlg()
00150 // purpose  : Destructor
00151 //=======================================================================
00152 SMESHGUI_ShapeByMeshDlg::~SMESHGUI_ShapeByMeshDlg()
00153 {
00154 }
00155 
00156 void SMESHGUI_ShapeByMeshDlg:: setMultipleAllowed( bool isAllowed )
00157 {
00158   myIsMultipleAllowed = isAllowed;
00159 }
00160 
00161 //================================================================================
00165 //================================================================================
00166 SMESHGUI_ShapeByMeshOp::SMESHGUI_ShapeByMeshOp(bool isMultipleAllowed):
00167   myIsMultipleAllowed(isMultipleAllowed)
00168 {
00169   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
00170     GeometryGUI::InitGeomGen();
00171 
00172   myDlg = new SMESHGUI_ShapeByMeshDlg;
00173   myDlg->setMultipleAllowed(myIsMultipleAllowed);
00174 
00175   connect(myDlg->myElemTypeGroup, SIGNAL(buttonClicked(int)), SLOT(onTypeChanged(int)));
00176   connect(myDlg->myElementId, SIGNAL(textChanged(const QString&)), SLOT(onElemIdChanged(const QString&)));
00177 }
00178 
00179 
00180 //=======================================================================
00181 // function : startOperation()
00182 // purpose  : Init dialog fields, connect signals and slots, show dialog
00183 //=======================================================================
00184 void SMESHGUI_ShapeByMeshOp::startOperation()
00185 {
00186   //SetMesh( SMESH::SMESH_Mesh::_nil() );
00187   myIsManualIdEnter = false;
00188 
00189   SMESHGUI_SelectionOp::startOperation();
00190 
00191   //activateSelection(); // set filters
00192   onSelectionDone(); // desable/enable [ OK ]
00193 
00194   myDlg->show();
00195 }
00196 
00197 //================================================================================
00201 //================================================================================
00202 SMESHGUI_ShapeByMeshOp::~SMESHGUI_ShapeByMeshOp()
00203 {
00204   if ( myDlg )
00205     delete myDlg;
00206 }
00207 
00208 //================================================================================
00213 //================================================================================
00214 LightApp_Dialog* SMESHGUI_ShapeByMeshOp::dlg() const
00215 {
00216   return myDlg;
00217 }
00218 
00219 SMESH::SMESH_Mesh_ptr SMESHGUI_ShapeByMeshOp::GetMesh()
00220 {
00221   return myMesh;
00222 }
00223 
00224 //=======================================================================
00225 // function : GetShape()
00226 // purpose  : Get published sub-shape
00227 //=======================================================================
00228 GEOM::GEOM_Object_ptr SMESHGUI_ShapeByMeshOp::GetShape()
00229 {
00230   return myGeomObj.in();
00231 }
00232 
00233 //=======================================================================
00234 // function : SetMesh()
00235 // purpose  : Set mesh to dialog
00236 //=======================================================================
00237 
00238 void SMESHGUI_ShapeByMeshOp::SetMesh (SMESH::SMESH_Mesh_ptr thePtr)
00239 {
00240   myMesh    = SMESH::SMESH_Mesh::_duplicate(thePtr);
00241   myGeomObj = GEOM::GEOM_Object::_nil();
00242   myHasSolids = false;
00243 
00244   std::vector< bool > hasElement (myDlg->myElemTypeGroup->buttons().count(), false);
00245   if (!myMesh->_is_nil() )
00246     {
00247       //     _PTR(SObject) aSobj = SMESH::FindSObject(myMesh.in());
00248       //     SUIT_DataOwnerPtr anIObj (new LightApp_DataOwner(aSobj->GetID().c_str()));
00249 
00250       std::vector< int > nbShapes( TopAbs_SHAPE, 0 );
00251       int shapeDim = 0; // max dim with several shapes
00252       //if ( /*mySelectionMgr*/ selectionMgr()->isOk(anIObj) ) // check that the mesh has a valid shape
00253       {
00254         _PTR(SObject) aSO = SMESH::FindSObject(myMesh.in());
00255         GEOM::GEOM_Object_var mainShape = SMESH::GetGeom(aSO);
00256         if ( !mainShape->_is_nil() ) 
00257           {
00258             TopoDS_Shape aShape;
00259             if ( GEOMBase::GetShape(mainShape, aShape))
00260               {
00261                 TopAbs_ShapeEnum types[4] = { TopAbs_EDGE, TopAbs_FACE, TopAbs_SHELL, TopAbs_SOLID };
00262                 for ( int dim = 4; dim > 0; --dim ) {
00263                   TopAbs_ShapeEnum type = types[ dim - 1 ];
00264                   TopAbs_ShapeEnum avoid = ( type == TopAbs_SHELL ) ? TopAbs_SOLID : TopAbs_SHAPE;
00265                   TopExp_Explorer exp( aShape, type, avoid );
00266                   for ( ; nbShapes[ type ] < 2 && exp.More(); exp.Next() )
00267                     ++nbShapes[ type ];
00268                   if ( nbShapes[ type ] > 1 ) {
00269                     shapeDim = dim;
00270                     break;
00271                   }
00272                 }
00273               }
00274           }
00275       }
00276       if (shapeDim > 0)
00277         {
00278           if ( nbShapes[ TopAbs_SHELL ] + nbShapes[ TopAbs_SOLID ] > 1 )
00279             shapeDim = 3;
00280           hasElement[ EDGE ]   = shapeDim > 0 && myMesh->NbEdges();
00281           hasElement[ FACE ]   = shapeDim > 1 && myMesh->NbFaces();
00282           hasElement[ VOLUME ] = shapeDim > 2 && myMesh->NbVolumes();
00283         }
00284       myHasSolids = nbShapes[ TopAbs_SOLID ];
00285     }
00286 
00287   // disable inexistant elem types
00288   for ( int i = 0; i < myDlg->myElemTypeGroup->buttons().count(); ++i ) {
00289     if ( QAbstractButton* button = myDlg->myElemTypeGroup->button( i ) )
00290       button->setEnabled( hasElement[ i ] );
00291   }
00292   myDlg->myElementId->setEnabled( hasElement[ EDGE ] );
00293   myDlg->myGeomName-> setEnabled( hasElement[ EDGE ] );
00294 
00295   setElementID("");
00296 }
00297 
00298 //=======================================================================
00299 // function : commitOperation()
00300 // purpose  : called when "Ok" button pressed.
00301 //=======================================================================
00302 
00303 void SMESHGUI_ShapeByMeshOp::commitOperation()
00304 {
00305   SMESHGUI_SelectionOp::commitOperation();
00306   try {
00307     QStringList aListId = myDlg->myElementId->text().split( " ", QString::SkipEmptyParts);
00308     if (aListId.count() == 1)
00309       {
00310         int elemID = (aListId.first()).toInt();
00311         myGeomObj = GEOM::GEOM_Object::_duplicate(
00312             SMESHGUI::GetSMESHGen()->GetGeometryByMeshElement
00313           ( myMesh.in(), elemID, myDlg->myGeomName->text().toLatin1().constData()) );
00314       }
00315     else
00316       {
00317         GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
00318         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
00319         
00320         if (geomGen->_is_nil() || !aStudy)
00321           return;
00322         
00323         GEOM::GEOM_IShapesOperations_var aShapesOp =
00324           geomGen->GetIShapesOperations(aStudy->StudyId());
00325         if (aShapesOp->_is_nil() )
00326           return;
00327         
00328         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
00329         
00330         std::map<double, GEOM::GEOM_Object_var> aGeomObjectsMap;
00331         GEOM::GEOM_Object_var aGeomObject;
00332 
00333         GEOM::GEOM_Object_var aMeshShape = myMesh->GetShapeToMesh();
00334         
00335         for ( int i = 0; i < aListId.count(); i++ )
00336           {
00337             aGeomObject =
00338               SMESHGUI::GetSMESHGen()->FindGeometryByMeshElement(myMesh.in(), aListId[i].toInt());
00339 
00340             if (aGeomObject->_is_nil()) continue;
00341             
00342             double anId = aShapesOp->GetSubShapeIndex(aMeshShape, aGeomObject);
00343             if (aShapesOp->IsDone() && aGeomObjectsMap.find(anId) == aGeomObjectsMap.end())
00344               {
00345                 aGeomObjectsMap[anId] = aGeomObject;
00346 
00347                 TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aGeomObject->GetShapeType();
00348                 if (i == 0)
00349                   aGroupType = aSubShapeType;
00350                 else if (aSubShapeType != aGroupType)
00351                   aGroupType = TopAbs_SHAPE;
00352               }
00353           }
00354         
00355         int aNumberOfGO = aGeomObjectsMap.size();
00356         if (aNumberOfGO == 1)
00357           myGeomObj = (*aGeomObjectsMap.begin()).second;
00358         else if (aNumberOfGO > 1)
00359           {
00360             GEOM::GEOM_IGroupOperations_var aGroupOp =
00361               geomGen->GetIGroupOperations(aStudy->StudyId());
00362             if(aGroupOp->_is_nil())
00363               return;
00364             
00365             GEOM::ListOfGO_var aGeomObjects = new GEOM::ListOfGO();
00366             aGeomObjects->length( aNumberOfGO );
00367 
00368             int i = 0;
00369             std::map<double, GEOM::GEOM_Object_var>::iterator anIter;
00370             for (anIter = aGeomObjectsMap.begin(); anIter!=aGeomObjectsMap.end(); anIter++)
00371               aGeomObjects[i++] = (*anIter).second;
00372           
00373             //create geometry group
00374             myGeomObj = aGroupOp->CreateGroup(aMeshShape, aGroupType);
00375             aGroupOp->UnionList(myGeomObj, aGeomObjects);
00376 
00377             if (!aGroupOp->IsDone())
00378               return;
00379           }
00380         
00381         // publish the GEOM object in study
00382         QString aNewGeomGroupName ( myDlg->myGeomName->text() );
00383           
00384         SALOMEDS::SObject_var aNewGroupSO =
00385           geomGen->AddInStudy(SMESHGUI::GetSMESHGen()->GetCurrentStudy(), myGeomObj, 
00386                               aNewGeomGroupName.toLatin1().data(), aMeshShape);
00387       }
00388   }
00389   catch (const SALOME::SALOME_Exception& S_ex) {
00390     SalomeApp_Tools::QtCatchCorbaException(S_ex);
00391   }
00392   catch (...) {
00393   }
00394 
00395 }
00396 
00397 bool SMESHGUI_ShapeByMeshOp::onApply()
00398 {
00399   return true;
00400 }
00401 
00402 //=======================================================================
00403 // function : onSelectionDone()
00404 // purpose  : SLOT called when selection changed. Enable/desable [ OK ]
00405 //=======================================================================
00406 void SMESHGUI_ShapeByMeshOp::onSelectionDone()
00407 {
00408   myDlg->setButtonEnabled( false, QtxDialog::OK );
00409   setElementID("");
00410 
00411   try {
00412     SALOME_ListIO aList;
00413     selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
00414     if (!myIsMultipleAllowed && aList.Extent() != 1)
00415       return;
00416 
00417     SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(aList.First());
00418     if (aMesh->_is_nil() || myMesh->_is_nil() || aMesh->GetId() != myMesh->GetId() )
00419       return;
00420 
00421     QString aString;
00422     int nbElems = SMESH::GetNameOfSelectedElements(selector(),//myViewWindow->GetSelector(),
00423                                                    aList.First(), aString);
00424     if (nbElems > 0) {
00425       if (!myIsMultipleAllowed && nbElems != 1 )
00426         return;
00427       setElementID( aString );
00428       myDlg->setButtonEnabled( true, QtxDialog::OK );
00429     }
00430   } catch (...) {
00431   }
00432 }
00433 
00434 //=======================================================================
00435 // function : activateSelection()
00436 // purpose  : Activate selection in accordance with current pattern type
00437 //=======================================================================
00438 void SMESHGUI_ShapeByMeshOp::activateSelection()
00439 {
00440   selectionMgr()->clearFilters();
00441   //SMESH::SetPointRepresentation(false);
00442 
00443   myDlg->myGeomName->setText("");
00444 
00445   QString geomName;
00446   Selection_Mode mode = EdgeSelection;
00447   switch ( myDlg->myElemTypeGroup->checkedId() ) {
00448   case EDGE  :
00449     mode = EdgeSelection;   geomName = tr("GEOM_EDGE"); break;
00450   case FACE  :
00451     mode = FaceSelection;   geomName = tr("GEOM_FACE"); break;
00452   case VOLUME:
00453     mode = VolumeSelection; geomName = tr(myHasSolids ? "GEOM_SOLID" : "GEOM_SHELL"); break;
00454   default: return;
00455   }
00456   if ( selectionMode() != mode )
00457     setSelectionMode( mode );
00458 
00459   myDlg->myGeomName->setText( GEOMBase::GetDefaultName( geomName ));
00460 }
00461 
00462 //=======================================================================
00463 //function : onTypeChanged
00464 //purpose  : SLOT. Called when element type changed.
00465 //=======================================================================
00466 
00467 void SMESHGUI_ShapeByMeshOp::onTypeChanged (int theType)
00468 {
00469   setElementID("");
00470   activateSelection();
00471 }
00472 
00473 //=======================================================================
00474 //function : onTypeChanged
00475 //purpose  : SLOT. Called when element id is entered
00476 //           Highlight the element whose Ids the user entered manually
00477 //=======================================================================
00478 
00479 void SMESHGUI_ShapeByMeshOp::onElemIdChanged(const QString& theNewText)
00480 {
00481   myDlg->setButtonEnabled( false, QtxDialog::OK );
00482 
00483   if ( myIsManualIdEnter && !myMesh->_is_nil() )
00484   {
00485     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) )
00486     {
00487       if ( SMDS_Mesh* aMesh = actor->GetObject()->GetMesh() )
00488       {
00489         SMDSAbs_ElementType type = SMDSAbs_Edge;
00490         switch ( myDlg->myElemTypeGroup->checkedId() ) {
00491         case EDGE  : type = SMDSAbs_Edge;   break;
00492         case FACE  : type = SMDSAbs_Face;   break;
00493         case VOLUME: type = SMDSAbs_Volume; break;
00494         default: return;
00495         }
00496         TColStd_MapOfInteger newIndices;
00497         QStringList aListId = theNewText.split( " ", QString::SkipEmptyParts);
00498         for ( int i = 0; i < aListId.count(); i++ ) {
00499           if ( const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() ))
00500             if ( e->GetType() == type )
00501               newIndices.Add( e->GetID() );
00502         }
00503         
00504         if ( !newIndices.IsEmpty() )
00505         {
00506           if (!myIsMultipleAllowed && newIndices.Extent() != 1)
00507             return;
00508           if ( SVTK_Selector* s = selector() ) {
00509             s->AddOrRemoveIndex( actor->getIO(), newIndices, false );
00510             viewWindow()->highlight( actor->getIO(), true, true );
00511             myDlg->setButtonEnabled( true, QtxDialog::OK );
00512           }
00513         }
00514       }
00515     }
00516   }
00517 }
00518 
00519 //=======================================================================
00520 //function : setElementID
00521 //purpose  : programmatically set element id
00522 //=======================================================================
00523 
00524 void SMESHGUI_ShapeByMeshOp::setElementID(const QString& theText)
00525 {
00526   myIsManualIdEnter = false;
00527   myDlg->myElementId->setText(theText);
00528   myIsManualIdEnter = true;
00529 }