Back to index

salome-smesh  6.5.0
SMESHGUI_GroupOnShapeDlg.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 //  SMESH SMESHGUI : GUI for SMESH component
00021 // File      : SMESHGUI_GroupOnShapeDlg.cxx
00022 // Created   : Wed Sep 17 18:36:51 2008
00023 // Author    : Edward AGAPOV (eap)
00024 // Module    : SMESH
00025 // IDL Headers
00026 //
00027 #include "SMESHGUI_GroupOnShapeDlg.h"
00028 
00029 #include "SMESH_TypeFilter.hxx"
00030 #include "SMESHGUI.h"
00031 #include "SMESHGUI_Utils.h"
00032 #include "SMESHGUI_GEOMGenUtils.h"
00033 
00034 #include <GeometryGUI.h>
00035 #include <GEOM_SelectionFilter.h>
00036 
00037 #include <SUIT_Session.h>
00038 #include <SUIT_OverrideCursor.h>
00039 #include <LightApp_Application.h>
00040 #include <LightApp_UpdateFlags.h>
00041 #include <SUIT_ResourceMgr.h>
00042 
00043 #include <SALOMEconfig.h>
00044 #include CORBA_SERVER_HEADER(SMESH_Mesh)
00045 #include CORBA_SERVER_HEADER(SMESH_Group)
00046 
00047 #include <QGroupBox>
00048 #include <QLayout>
00049 #include <QFrame>
00050 #include <QLabel>
00051 #include <QPushButton>
00052 #include <QListWidget>
00053 #include <QLineEdit>
00054 
00055 #define SPACING 6
00056 #define MARGIN  11
00057 
00058 enum { _MESH, _ELEM_GEOM, _NODE_GEOM };
00059 
00060 SMESHGUI_GroupOnShapeDlg::SMESHGUI_GroupOnShapeDlg()
00061   : SMESHGUI_Dialog( 0, false, true )
00062 {
00063   QPixmap image (resourceMgr()->loadPixmap("SMESH", tr("ICON_SELECT")));
00064 
00065   // Name
00066   //QLabel* nameLabel = new QLabel( tr( "SMESH_NAME" ), mainFrame() );
00067   //myGrpNameLine = new QLineEdit( mainFrame() );
00068 
00069   // Mesh
00070   QLabel* meshLabel = new QLabel( tr( "SMESH_OBJECT_MESH" ), mainFrame() );
00071   myMeshBtn = new QPushButton( mainFrame() );
00072   myMeshBtn->setCheckable(true);
00073   myMeshBtn->setIcon(image);
00074   myMeshLine = new QLineEdit( mainFrame() );
00075   myMeshLine->setReadOnly(true);
00076 
00077   // Elem geom
00078 
00079   QGroupBox* elemsGrp = new QGroupBox( tr( "SMESH_ELEMENTS" ), mainFrame() );
00080   QLabel* label = new QLabel( tr( "SMESH_GEOM" ), elemsGrp );
00081   myElemGeomBtn = new QPushButton( elemsGrp );
00082   myElemGeomBtn->setCheckable(true);
00083   myElemGeomBtn->setIcon(image);
00084   myElemGeomList = new QListWidget( elemsGrp );
00085   myElemGeomList->setSelectionMode(QListWidget::NoSelection);
00086 
00087   QGridLayout* elemLay = new QGridLayout(elemsGrp);
00088   elemLay->setSpacing( SPACING );
00089   elemLay->setMargin( MARGIN );
00090   elemLay->setRowStretch( 1, 1 );
00091   elemLay->addWidget( label, 0, 0 );
00092   elemLay->addWidget( myElemGeomBtn, 0, 1 );
00093   elemLay->addWidget( myElemGeomList, 0, 2, 2, 1);
00094 
00095   // Node geom
00096 
00097   QGroupBox* nodesGrp = new QGroupBox( tr( "SMESH_NODES" ), mainFrame() );
00098 
00099   label = new QLabel( tr( "SMESH_GEOM" ), nodesGrp );
00100   myNodeGeomBtn = new QPushButton( nodesGrp );
00101   myNodeGeomBtn->setCheckable(true);
00102   myNodeGeomBtn->setIcon(image);
00103   myNodeGeomList = new QListWidget( nodesGrp );
00104   myNodeGeomList->setSelectionMode(QListWidget::NoSelection);
00105 
00106   QGridLayout* nodeLay = new QGridLayout(nodesGrp);
00107   nodeLay->setSpacing( SPACING );
00108   nodeLay->setMargin( MARGIN );
00109   nodeLay->setRowStretch( 1, 1 );
00110   nodeLay->addWidget( label, 0, 0 );
00111   nodeLay->addWidget( myNodeGeomBtn, 0, 1 );
00112   nodeLay->addWidget(myNodeGeomList, 0, 2, 2, 1);
00113 
00114   // Fill layout
00115   QGridLayout* aLay = new QGridLayout( mainFrame());
00116   aLay->setSpacing( SPACING );
00117   aLay->setMargin( MARGIN );
00118   //aLay->addWidget( nameLabel, 0, 0 );
00119   //aLay->addWidget( myGrpNameLine, 0, 2 );
00120   aLay->addWidget( meshLabel, 1, 0 );
00121   aLay->addWidget( myMeshBtn, 1, 1 );
00122   aLay->addWidget( myMeshLine,1, 2 );
00123   aLay->addWidget( elemsGrp, 2, 1, 1, 3 );
00124   aLay->addWidget( nodesGrp, 3, 1, 1, 3 );
00125 
00126   setWindowTitle( tr( "SMESH_CREATE_GROUP_FROM_GEOM" ) );
00127 }
00128 
00129 SMESHGUI_GroupOnShapeDlg::~SMESHGUI_GroupOnShapeDlg()
00130 {
00131 }
00132 
00133 //================================================================================
00137 //================================================================================
00138 
00139 void SMESHGUI_GroupOnShapeDlg::updateButtons()
00140 {
00141   bool enable = myElemGeomList->count() + myNodeGeomList->count();
00143 
00144   button(Apply)->setEnabled( enable );
00145   button(OK)->setEnabled( enable );
00146 }
00147 
00148 //================================================================================
00152 //================================================================================
00153 
00154 void SMESHGUI_GroupOnShapeDlg::init()
00155 {
00156   //myGrpNameLine->setText("");
00157 
00158   myMeshBtn->setChecked( true );
00159   myMeshLine->setText("");
00160 
00161   myElemGeomBtn->setChecked(false);
00162   myElemGeomBtn->setEnabled(false);
00163   myElemGeomList->clear();
00164   myNodeGeomBtn->setChecked(false);
00165   myNodeGeomBtn->setEnabled(false);
00166   myNodeGeomList->clear();
00167 
00168   updateButtons();
00169 }
00170 
00171 //================================================================================
00175 //================================================================================
00176 
00177 SMESHGUI_GroupOnShapeOp::SMESHGUI_GroupOnShapeOp()
00178   : SMESHGUI_SelectionOp(ActorSelection),
00179     myDlg( 0 )
00180 {
00181   myHelpFileName = "create_groups_from_geometry_page.html";
00182 }
00183 
00184 SMESHGUI_GroupOnShapeOp::~SMESHGUI_GroupOnShapeOp()
00185 {
00186   if ( myDlg )
00187     delete myDlg;
00188 }
00189 //================================================================================
00194 //================================================================================
00195 
00196 LightApp_Dialog* SMESHGUI_GroupOnShapeOp::dlg() const
00197 {
00198   return myDlg;
00199 }
00200 
00201 //================================================================================
00205 //================================================================================
00206 
00207 static SMESH::ElementType elementType(GEOM::GEOM_Object_var& geom)
00208 {
00209   if ( !geom->_is_nil() ) {
00210     switch ( geom->GetShapeType() ) {
00211     case GEOM::VERTEX:   return SMESH::NODE;
00212     case GEOM::EDGE:     return SMESH::EDGE;
00213     case GEOM::WIRE:     return SMESH::EDGE;
00214     case GEOM::FACE:     return SMESH::FACE;
00215     case GEOM::SHELL:    return SMESH::FACE;
00216     case GEOM::SOLID:    return SMESH::VOLUME;
00217     case GEOM::COMPSOLID:return SMESH::VOLUME;
00218     case GEOM::COMPOUND: break;
00219     default:             return SMESH::ALL;
00220     }
00221     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
00222     GEOM::GEOM_IShapesOperations_var aShapeOp =
00223       SMESH::GetGEOMGen()->GetIShapesOperations(aStudy->StudyId());
00224 
00225     if ( geom->GetType() == 37 ) { // geom group
00226       GEOM::GEOM_IGroupOperations_var  aGroupOp =
00227         SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
00228       if ( !aGroupOp->_is_nil() ) {
00229         GEOM::GEOM_Object_var mainShape = aGroupOp->GetMainShape( geom );
00230         GEOM::ListOfLong_var        ids = aGroupOp->GetObjects( geom );
00231         if ( ids->length() && !mainShape->_is_nil() && !aShapeOp->_is_nil() ) {
00232           GEOM::GEOM_Object_var member = aShapeOp->GetSubShape( mainShape, ids[0] );
00233           return elementType( member );
00234         }
00235       }
00236     }
00237     else if ( !aShapeOp->_is_nil() ) { // just a compoud shape
00238       GEOM::ListOfLong_var ids = aShapeOp->SubShapeAllIDs( geom, GEOM::SHAPE, false );
00239       if ( ids->length() ) {
00240         GEOM::GEOM_Object_var member = aShapeOp->GetSubShape( geom, ids[0] );
00241         return elementType( member );
00242       }
00243     }
00244   }
00245   return SMESH::ALL;
00246 }
00247 
00248 //================================================================================
00252 //================================================================================
00253 
00254 void SMESHGUI_GroupOnShapeOp::init()
00255 {
00256   myMeshID="";
00257   myElemGeoIDs.clear();
00258   myNodeGeoIDs.clear();
00259 
00260   myDlg->init();
00261   removeCustomFilters();
00262   onActivateObject( _MESH ); // install filter
00263 }
00264 
00265 //================================================================================
00269 //================================================================================
00270 
00271 void SMESHGUI_GroupOnShapeOp::startOperation()
00272 {
00273   if (!myDlg)
00274   {
00275     myDlg = new SMESHGUI_GroupOnShapeDlg();
00276     connect(myDlg->myMeshBtn,     SIGNAL(clicked()), this, SLOT(onButtonClick()));
00277     connect(myDlg->myElemGeomBtn, SIGNAL(clicked()), this, SLOT(onButtonClick()));
00278     connect(myDlg->myNodeGeomBtn, SIGNAL(clicked()), this, SLOT(onButtonClick()));
00279     //connect(myDlg->myGrpNameLine, SIGNAL(textChanged(const QString&)),myDlg,SLOT(updateButtons()));
00280   }
00281   SMESHGUI_SelectionOp::startOperation();
00282 
00283   init();
00284   myDlg->show();
00285 }
00286 
00287 //================================================================================
00291 //================================================================================
00292 
00293 bool SMESHGUI_GroupOnShapeOp::onApply()
00294 {
00295   SUIT_OverrideCursor aWaitCursor;
00296 
00297   if (isStudyLocked())
00298     return false;
00299 
00300   // study
00301   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
00302   if ( !aStudy ) return false;
00303 
00304   // mesh
00305   _PTR(SObject) meshSO = aStudy->FindObjectID( myMeshID.toLatin1().data() );
00306   SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( meshSO );
00307   if ( mesh->_is_nil() ) return false;
00308 
00309   // names of all existing groups
00310 //   QStringList groupNames;
00311 //   QString givenName = myDlg->myGrpNameLine->text();
00312 //   if ( !givenName.isEmpty() ) {
00313 //     SMESH::ListOfGroups_var groups = mesh->GetGroups();
00314 //     for ( int i = 0; i < groups->length(); ++i ) {
00315 //       CORBA::String_var name = groups[i]->GetName();
00316 //       groupNames.append( name.in() );
00317 //     }
00318 //   }
00319 
00320   // create groups
00321   SMESH::SMESH_GroupOnGeom_var group;
00322   QStringList anEntryList;
00323   for ( int isNode = 0; isNode < 2; ++isNode ) // elems and then nodes
00324   {
00325     QStringList::iterator geomID = isNode ? myNodeGeoIDs.begin() : myElemGeoIDs.begin();
00326     QStringList::iterator geomEnd = isNode ? myNodeGeoIDs.end() : myElemGeoIDs.end();
00327 
00328     for ( int i = 0; geomID != geomEnd; ++geomID, ++i )
00329     {
00330       // selected geom
00331       _PTR(SObject) geomSO = aStudy->FindObjectID( geomID->toLatin1().data() );
00332       GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>(geomSO);
00333       if ( geom->_is_nil() ) continue;
00334 
00335       // group type
00336       SMESH::ElementType elemType = isNode ? SMESH::NODE : elementType( geom );
00337       if ( elemType == SMESH::ALL )
00338         continue;
00339 
00340       // make a unique name
00341       QString name =
00342         isNode ? myDlg->myNodeGeomList->item(i)->text() : myDlg->myElemGeomList->item(i)->text();
00343 //       int nb = 1;
00344 //       QString name = myDlg->myGrpNameLine->text() + "_" + QString::number(nb);
00345 //       while ( groupNames.contains( name ))
00346 //         name = myDlg->myGrpNameLine->text() + "_" + QString::number(++nb);
00347 //       groupNames.append( name );
00348 
00349       //printf( "apply() %s %s\n", (*geomID).latin1(), name.latin1() );
00350       group = mesh->CreateGroupFromGEOM( elemType, name.toLatin1().data(), geom );
00351       if( !group->_is_nil() )
00352         if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( group ) )
00353           anEntryList.append( aSObject->GetID().c_str() );
00354     }
00355   }
00356   update( UF_ObjBrowser | UF_Model );
00357 
00358   SMESHGUI::Modified();
00359 
00360   // Re-init controls to create the next group
00361   myElemGeoIDs.clear();
00362   myNodeGeoIDs.clear();
00363   removeCustomFilters();
00364   myDlg->myNodeGeomList->clear();
00365   myDlg->myElemGeomList->clear();
00366   myDlg->myElemGeomBtn->setChecked(false); 
00367   myDlg->myNodeGeomBtn->setChecked(false);
00368   myDlg->updateButtons();
00369 
00370   update( UF_ObjBrowser | UF_Model );
00371 
00372   if( LightApp_Application* anApp =
00373       dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
00374     anApp->browseObjects( anEntryList, isApplyAndClose() );
00375 
00376   return !group->_is_nil();
00377 }
00378 
00379 //================================================================================
00383 //================================================================================
00384 
00385 void SMESHGUI_GroupOnShapeOp::onButtonClick()
00386 {
00387   removeCustomFilters();
00388 
00389   if ( sender() == myDlg->myMeshBtn && myDlg->myMeshBtn->isChecked() )
00390   {
00391     myDlg->myElemGeomBtn->setChecked(false);
00392     myDlg->myNodeGeomBtn->setChecked(false);
00393     onActivateObject( _MESH ); // install filter
00394   }
00395   else if ( sender() == myDlg->myElemGeomBtn && myDlg->myElemGeomBtn->isChecked() )
00396   {
00397     myDlg->myMeshBtn->setChecked(false);
00398     myDlg->myNodeGeomBtn->setChecked(false);
00399     onActivateObject( _ELEM_GEOM ); // install filter
00400   }
00401   else if ( sender() == myDlg->myNodeGeomBtn && myDlg->myNodeGeomBtn->isChecked() )
00402   {
00403     myDlg->myMeshBtn->setChecked(false);
00404     myDlg->myElemGeomBtn->setChecked(false);
00405     onActivateObject( _NODE_GEOM ); // install filter
00406   }
00407   //selectionDone();
00408 }
00409 
00410 //================================================================================
00418 //================================================================================
00419 SUIT_SelectionFilter* SMESHGUI_GroupOnShapeOp::createFilter( const int theId ) const
00420 {
00421   if ( theId == _ELEM_GEOM || theId == _NODE_GEOM )
00422     return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
00423   else if ( theId == _MESH )
00424     return new SMESH_TypeFilter( MESH );
00425   else
00426     return ( SUIT_SelectionFilter*) 0;
00427 }
00428 //================================================================================
00434 //================================================================================
00435 void SMESHGUI_GroupOnShapeOp::selectionDone()
00436 {
00437   QStringList names, ids;
00438   LightApp_Dialog::TypesList types;
00439   selected( names, types, ids );
00440   int nbSelected = names.size();
00441 
00442   if ( myDlg->myMeshBtn->isChecked() ) // mesh selected
00443   {
00444     myDlg->myMeshLine->setText("");
00445     myMeshID = "";
00446     if ( nbSelected == 1 ) {
00447       myDlg->myMeshLine->setText( names.front() );
00448       myMeshID = ids.front();
00449     }
00450     myDlg->myElemGeomList->clear();
00451     myDlg->myElemGeomBtn->setEnabled( nbSelected == 1 );
00452     myDlg->myNodeGeomList->clear();
00453     myDlg->myNodeGeomBtn->setEnabled( nbSelected == 1 );
00454     myDlg->myElemGeomBtn->click();
00455     return;
00456   }
00457 
00458   // Filter off inappropriate shapes
00459 
00460   QStringList goodNames, goodIds;
00461   if (nbSelected > 0) {
00462     // study
00463     if (_PTR(Study) aStudy = SMESH::GetActiveStudyDocument()) {
00464       // mesh
00465       if (_PTR(SObject)  meshSO = aStudy->FindObjectID( myMeshID.toLatin1().data() )) {
00466         // shape to mesh
00467         _PTR(SObject) anObj, shapeToMesh;
00468         if (meshSO->FindSubObject(1, anObj) && anObj->ReferencedObject(shapeToMesh)) {
00469           // loop on selected
00470           QStringList::iterator name = names.begin(), id = ids.begin(), idEnd = ids.end();
00471           for (; id != idEnd; ++id, ++name ) {
00472             // shape SO
00473             if (_PTR(SObject) shapeSO = aStudy->FindObjectID( id->toLatin1().data() )) {
00474             // check if shape SO is a child of shape to mesh 
00475               while ( shapeSO && shapeSO->GetID() != shapeToMesh->GetID() )
00476                 if  ( shapeSO->Depth() < 2 )
00477                   shapeSO.reset();
00478                 else
00479                   shapeSO = shapeSO->GetFather();
00480               if ( shapeSO ) {
00481                 //printf( "selectionDone() %s %s\n", (*id).latin1(), (*name).latin1() );
00482                 if ( !goodIds.contains( *id )) {
00483                   goodIds.append( *id );
00484                   goodNames.append( *name );
00485                 }
00486               }
00487             }
00488           }
00489         }
00490       }
00491     }
00492   }
00493 
00494   if ( myDlg->myElemGeomBtn->isChecked() ) // elem geomerty selection
00495   {
00496     myDlg->myElemGeomList->clear();
00497     myDlg->myElemGeomList->addItems( goodNames );
00498     myElemGeoIDs = goodIds;
00499   }
00500   else if ( myDlg->myNodeGeomBtn->isChecked() ) // Node geomerty selection
00501   {
00502     myDlg->myNodeGeomList->clear();
00503     myDlg->myNodeGeomList->addItems( goodNames );
00504     myNodeGeoIDs = goodIds;
00505   }
00506 
00507   // enable/diable Apply, which can change at selection
00508   myDlg->updateButtons();
00509 }