Version: 6.3.1

src/SMESHGUI/SMESHGUI_Make2DFrom3DOp.cxx

Go to the documentation of this file.
00001 // Copyright (C) 2007-2011  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 // File   : SMESHGUI_Make2DFrom3D.cxx
00020 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00021 
00022 #include "SMESHGUI_Make2DFrom3DOp.h"
00023 
00024 #include "SMESHGUI.h"
00025 #include "SMESHGUI_Utils.h"
00026 #include "SMESHGUI_MeshUtils.h"
00027 #include "SMESH_TypeFilter.hxx"
00028 #include "SMESH_LogicalFilter.hxx"
00029 #include "SMESHGUI_VTKUtils.h"
00030 #include "SMESH_Actor.h"
00031 
00032 // SALOME GUI includes
00033 #include <LightApp_Application.h>
00034 #include <LightApp_SelectionMgr.h>
00035 #include <LightApp_UpdateFlags.h>
00036 #include <SALOME_ListIO.hxx>
00037 #include <SUIT_Desktop.h>
00038 #include <SUIT_MessageBox.h>
00039 #include <SUIT_OverrideCursor.h>
00040 #include <SUIT_Session.h>
00041 #include <SVTK_ViewModel.h>
00042 //#include <SVTK_ViewWindow.h>
00043 #include <SalomeApp_Study.h>
00044 #include <SalomeApp_Tools.h>
00045 
00046 // IDL includes
00047 #include <SALOMEconfig.h>
00048 #include CORBA_SERVER_HEADER(SMESH_Group)
00049 
00050 // Qt includes
00051 #include <QGroupBox>
00052 #include <QRadioButton>
00053 #include <QLineEdit>
00054 #include <QCheckBox>
00055 #include <QHBoxLayout>
00056 #include <QGridLayout>
00057 #include <QToolButton>
00058 
00059 #include <Standard_ErrorHandler.hxx>
00060 
00061 #define SPACING 6
00062 #define MARGIN  11
00063 
00069 SMESHGUI_Make2DFrom3DDlg::SMESHGUI_Make2DFrom3DDlg( QWidget* parent )
00070   : SMESHGUI_Dialog( parent, false, true, OK | Apply | Close | Help )
00071 {
00072   // title
00073   setWindowTitle( tr("CAPTION") );
00074 
00075   // mode
00076   QGroupBox* aModeGrp = new QGroupBox( tr( "MODE" ), mainFrame() );
00077   QHBoxLayout* aModeGrpLayout = new QHBoxLayout( aModeGrp );
00078   aModeGrpLayout->setMargin( MARGIN );
00079   aModeGrpLayout->setSpacing( SPACING );
00080   my2dFrom3dRB = new QRadioButton( tr( "2D_FROM_3D" ), aModeGrp );
00081   my1dFrom2dRB = new QRadioButton( tr( "1D_FROM_2D" ), aModeGrp );
00082   //my1dFrom3dRB = new QRadioButton( tr( "1D_FROM_3D" ), aModeGrp );
00083   aModeGrpLayout->addWidget( my2dFrom3dRB );
00084   aModeGrpLayout->addWidget( my1dFrom2dRB );
00085   //aModeGrpLayout->addWidget( my1dFrom3dRB );
00086 
00087 //   // Groups of mesh faces
00088 //   setObjectPixmap( "SMESH", tr( "ICON_SELECT" ) );
00089 //   createObject( tr( "Groups" ), mainFrame(), Groups );
00090 //   setNameIndication( Groups, ListOfNames );
00091 //   objectWg( Groups, Btn )->hide();
00092 
00093   // Mesh or Groups
00094   //setObjectPixmap( "SMESH", tr( "ICON_SELECT" ) );
00095   createObject( tr( "Groups" ), mainFrame(), MeshOrGroups );
00096   setNameIndication( MeshOrGroups, ListOfNames );
00097   objectWg( MeshOrGroups, Btn )->hide();
00098 
00099   // target
00100   QGroupBox* aTargetGrp = new QGroupBox( tr( "TARGET" ), mainFrame() );
00101   QGridLayout* aTargetGrpLayout = new QGridLayout( aTargetGrp );
00102   aTargetGrpLayout->setMargin( MARGIN );
00103   aTargetGrpLayout->setSpacing( SPACING );
00104   myThisMeshRB   = new QRadioButton( tr( "THIS_MESH" ), aTargetGrp );
00105   myNewMeshRB    = new QRadioButton( tr( "NEW_MESH" ),  aTargetGrp );
00106   myMeshName     = new QLineEdit( aTargetGrp );
00107   myCopyCheck    = new QCheckBox( tr( "COPY_SRC" ),     aTargetGrp );
00108   aTargetGrpLayout->addWidget( myThisMeshRB,    0, 0 );
00109   aTargetGrpLayout->addWidget( myNewMeshRB,     1, 0 );
00110   aTargetGrpLayout->addWidget( myMeshName,     1, 1 );
00111   aTargetGrpLayout->addWidget( myCopyCheck,    2, 0 );
00112   myGroupCheck = new QCheckBox( tr( "CREATE_GROUP" ), mainFrame() );
00113   myGroupName  = new QLineEdit( mainFrame() );
00114 
00115   // layout
00116   QGridLayout* aDlgLay = new QGridLayout( mainFrame() );
00117   aDlgLay->setMargin( 0 );
00118   aDlgLay->setSpacing( SPACING );
00119   aDlgLay->addWidget( aModeGrp,     0, 0, 1, 3 );
00120   aDlgLay->addWidget( objectWg( MeshOrGroups,  Label ),   1, 0 );
00121   aDlgLay->addWidget( objectWg( MeshOrGroups,  Control ), 1, 1 );
00122   aDlgLay->addWidget( aTargetGrp,   2, 0, 1, 3 );
00123   aDlgLay->addWidget( myGroupCheck, 3, 0 );
00124   aDlgLay->addWidget( myGroupName,  3, 1, 1, 2 );
00125 
00126   // connect signals  
00127   connect( myThisMeshRB, SIGNAL( clicked() ), this, SLOT( onTargetChanged() ) );
00128   connect( myNewMeshRB,  SIGNAL( clicked() ), this, SLOT( onTargetChanged() ) );
00129   connect( myGroupCheck, SIGNAL( clicked() ), this, SLOT( onGroupChecked() ) );
00130 
00131   // init dlg
00132   my2dFrom3dRB->setChecked( true );
00133   myThisMeshRB->setChecked( true );
00134   onTargetChanged();
00135   onGroupChecked();
00136 }
00137 
00138 SMESHGUI_Make2DFrom3DDlg::~SMESHGUI_Make2DFrom3DDlg()
00139 {
00140 }
00141 
00142 SMESH::Bnd_Dimension SMESHGUI_Make2DFrom3DDlg::mode() const
00143 {
00144   if ( my2dFrom3dRB->isChecked() )
00145     return SMESH::BND_2DFROM3D;
00146   else if ( my1dFrom2dRB->isChecked() )
00147     return SMESH::BND_1DFROM2D;
00148   else
00149     return SMESH::BND_1DFROM3D;
00150 }
00151 
00152 bool SMESHGUI_Make2DFrom3DDlg::needNewMesh() const
00153 {
00154   return myNewMeshRB->isChecked();
00155 }
00156 
00157 QString SMESHGUI_Make2DFrom3DDlg::getNewMeshName() const
00158 {
00159   return myMeshName->text().trimmed();
00160 }
00161 
00162 void SMESHGUI_Make2DFrom3DDlg::setNewMeshName( const QString& name )
00163 {
00164   myMeshName->setText( name );
00165 }
00166 
00167 void SMESHGUI_Make2DFrom3DDlg::setNewMeshEnabled( bool enable )
00168 {
00169   if ( !enable )
00170     myThisMeshRB->setChecked( true );
00171 
00172   myNewMeshRB->setEnabled( enable );
00173 
00174   onTargetChanged();
00175 }
00176 
00177 bool SMESHGUI_Make2DFrom3DDlg::getNewMeshEnabled() const
00178 {
00179   return myNewMeshRB->isEnabled();
00180 }
00181 
00182 bool SMESHGUI_Make2DFrom3DDlg::needGroup() const
00183 {
00184   return myGroupCheck->isChecked();
00185 }
00186 
00187 QString SMESHGUI_Make2DFrom3DDlg::getGroupName() const
00188 {
00189   return myGroupName->text().trimmed();
00190 }
00191 
00192 void SMESHGUI_Make2DFrom3DDlg::setGroupName( const QString& name )
00193 {
00194   myGroupName->setText( name );
00195 }
00196 
00197 bool SMESHGUI_Make2DFrom3DDlg::copySource() const
00198 {
00199   return myCopyCheck->isChecked();
00200 }
00201 
00202 void SMESHGUI_Make2DFrom3DDlg::onTargetChanged()
00203 {
00204   myMeshName->setEnabled( myNewMeshRB->isChecked() );
00205   myCopyCheck->setEnabled( myNewMeshRB->isChecked() );
00206 }
00207 
00208 void SMESHGUI_Make2DFrom3DDlg::onGroupChecked()
00209 {
00210   myGroupName->setEnabled( myGroupCheck->isChecked() );
00211 }
00212 
00218 SMESHGUI_Make2DFrom3DOp::SMESHGUI_Make2DFrom3DOp()
00219   : SMESHGUI_SelectionOp(),
00220     myMeshFilter(MESH),
00221     myGroupFilter(GROUP)
00222 {
00223 }
00224 
00225 SMESHGUI_Make2DFrom3DOp::~SMESHGUI_Make2DFrom3DOp()
00226 {
00227   if ( myDlg )
00228     delete myDlg;
00229 }
00230 
00231 LightApp_Dialog* SMESHGUI_Make2DFrom3DOp::dlg() const
00232 {
00233   return myDlg;
00234 }
00235 
00236 void SMESHGUI_Make2DFrom3DOp::startOperation()
00237 {
00238   if( !myDlg )
00239     myDlg = new SMESHGUI_Make2DFrom3DDlg( desktop() );
00240 
00241   myHelpFileName = "make_2dmesh_from_3d_page.html";
00242 
00243   SMESHGUI_SelectionOp::startOperation();
00244 
00245   myDlg->setNewMeshName( SMESH::UniqueName( "Mesh_1" ) );
00246   myDlg->setGroupName( SMESH::UniqueName( "Group" ) );
00247   myDlg->show();
00248 
00249   connect( myDlg->my2dFrom3dRB, SIGNAL( toggled(bool) ), this, SLOT( onModeChanged() ) );
00250   connect( myDlg->my1dFrom2dRB, SIGNAL( toggled(bool) ), this, SLOT( onModeChanged() ) );
00251   //connect( myDlg->my1dFrom3dRB, SIGNAL( toggled(bool) ), this, SLOT( onModeChanged() ) );
00252 
00253   //onModeChanged();
00254 
00255   myDlg->activateObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
00256   selectionDone();
00257 }
00258 
00259 //================================================================================
00263 //================================================================================
00264 
00265 void SMESHGUI_Make2DFrom3DOp::onModeChanged()
00266 {
00267 //   QRadioButton* b = dynamic_cast<QRadioButton*>( sender());
00268 //   if ( b && !b->isChecked() )
00269 //     return;
00270 
00271 //   // enable "2D groups" field
00272 //   bool enableGroups = ( myDlg->mode() == SMESH::BND_1DFROM3D );
00273 //   myDlg->setObjectEnabled( SMESHGUI_Make2DFrom3DDlg::Groups, enableGroups );
00274 //   ((QToolButton*) myDlg->objectWg( SMESHGUI_Make2DFrom3DDlg::Groups,
00275 //                                    SMESHGUI_Make2DFrom3DDlg::Btn ))->setChecked( enableGroups );
00276   
00277 //   // install filter
00278 //   int id =  enableGroups ? SMESHGUI_Make2DFrom3DDlg::Groups : SMESHGUI_Make2DFrom3DDlg::Mesh;
00279 //   onDeactivateObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
00280 //   onActivateObject  ( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
00281 //   selectionDone();
00282 }
00283 
00284 void SMESHGUI_Make2DFrom3DOp::selectionDone()
00285 {
00286   myDlg->clearSelection( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
00287   mySrcMesh = SMESH::SMESH_Mesh::_nil();
00288 
00289   if ( !dlg() ) return;
00290 
00291   
00292   if ( dlg()->isVisible() ) {
00293     try {
00294       QStringList names, ids;
00295       LightApp_Dialog::TypesList types;
00296       selected( names, types, ids );
00297       myDlg->selectObject( names, types, ids );
00298 
00299       // enable/desable "new mesh" button
00300       bool isMesh = true;
00301       for ( int i = 0; i < ids.count() && isMesh; ++i )
00302       {
00303         _PTR(SObject) sobj =
00304           SMESHGUI::activeStudy()->studyDS()->FindObjectID( ids[i].toLatin1().constData() );
00305         mySrcMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( sobj );  
00306         isMesh = !mySrcMesh->_is_nil();
00307       }
00308       myDlg->setNewMeshEnabled( isMesh );
00309     }
00310     catch ( const SALOME::SALOME_Exception& S_ex ) {
00311       SalomeApp_Tools::QtCatchCorbaException( S_ex );
00312     }
00313     catch ( ... ) {
00314     }
00315   }
00316 }
00317 
00318 SUIT_SelectionFilter* SMESHGUI_Make2DFrom3DOp::createFilter( const int theId ) const
00319 {
00320   SMESHGUI_Make2DFrom3DOp* me = (SMESHGUI_Make2DFrom3DOp*) this;
00321 
00322   QList<SUIT_SelectionFilter*> subFilters;
00323   subFilters.append( & me->myMeshFilter );
00324   subFilters.append( & me->myGroupFilter );
00325 
00326   SUIT_SelectionFilter* f = new SMESH_LogicalFilter( subFilters, SMESH_LogicalFilter::LO_OR );
00327   return f;
00328 }
00329 
00330 bool SMESHGUI_Make2DFrom3DOp::isValid( QString& msg ) const
00331 {
00332   if ( !dlg() ) return false;
00333 
00334   // check if a mesh is selected
00335   if ( !myDlg->hasSelection( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups ))
00336   {
00337     msg = tr( "SMESH_ERR_NO_INPUT_MESH" );
00338     return false;
00339   }
00340   QStringList entries;
00341   dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
00342   const bool isMeshSelected = ( !mySrcMesh->_is_nil() );
00343   if ( isMeshSelected )
00344   {
00345     // only one mesh is allowed
00346     if ( entries.size() > 1 ) {
00347       msg = tr( "SMESH_TOO_MANY_MESHES" );
00348       return false;
00349     }
00350   }
00351   else
00352   {
00353     // check if only groups are selected
00354     for ( int i = 0; i < entries.count(); ++i )
00355     {
00356       SMESH::SMESH_GroupBase_var grp;
00357       if ( _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() ))
00358         grp = SMESH::SObjectToInterface<SMESH::SMESH_GroupBase>( sobj );
00359       if ( grp->_is_nil() ) {
00360         msg = tr( "SMESH_NOT_ONLY_GROUPS" );
00361         return false;
00362       }
00363     }
00364   }
00365   // check if the selected objects contains elements of required type
00366   bool hasFaces = false, hasVolumes = false;
00367   SMESH::Bnd_Dimension mode = myDlg->mode();
00368   for ( int i = 0; i < entries.count(); ++i )
00369   {
00370     SMESH::SMESH_IDSource_var idSource;
00371     if ( _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() ))
00372       idSource = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );
00373     if ( !idSource->_is_nil() ) {
00374       SMESH::array_of_ElementType_var types = idSource->GetTypes();
00375       for ( int j = 0; j < types->length(); ++j )
00376         if ( types[j] == SMESH::VOLUME )
00377           hasVolumes = true;
00378         else if ( types[j] == SMESH::FACE )
00379           hasFaces = true;
00380     }
00381   }
00382   if ( mode == SMESH::BND_2DFROM3D && !hasVolumes ) {
00383     msg = tr( "SMESH_ERR_NO_3D_ELEMENTS" );
00384     return false;
00385   }
00386   else if ( mode == SMESH::BND_1DFROM2D && !hasFaces  ) {
00387     msg = tr( "SMESH_ERR_NO_2D_ELEMENTS" );
00388     return false;
00389   }
00390 
00391   // check if new mesh name is specified
00392   if ( myDlg->needNewMesh() && myDlg->getNewMeshName().isEmpty() ) {
00393     msg = tr( "SMESH_ERR_MESH_NAME_NOT_SPECIFIED" );
00394     return false;
00395   }
00396 
00397   // check if group name is specified
00398   if ( myDlg->needGroup() && myDlg->getGroupName().isEmpty() ) {
00399     msg = tr( "SMESH_ERR_GRP_NAME_NOT_SPECIFIED" );
00400     return false;
00401   }
00402 
00403   return true;
00404 }
00405 
00406 bool SMESHGUI_Make2DFrom3DOp::compute2DMesh( QStringList& theEntryList )
00407 {
00408   SUIT_OverrideCursor wc;
00409 
00410   bool ok = false;
00411   try {
00412     SMESH::Bnd_Dimension mode = myDlg->mode();
00413     QString meshName          = myDlg->needNewMesh() ? myDlg->getNewMeshName() : QString();
00414     QString groupName         = myDlg->needGroup()   ? myDlg->getGroupName()   : QString();
00415     bool copyAll              = myDlg->copySource();
00416 
00417     QStringList entries;
00418     dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
00419     SMESH::ListOfIDSources_var groups = new SMESH::ListOfIDSources;
00420     QString wrongGroups = "";
00421 
00422     if ( mySrcMesh->_is_nil() ) // get selected groups, find groups of wrong type
00423     {
00424       int nbGroups = 0;
00425       int goodType = ( mode == SMESH::BND_2DFROM3D ? SMESH::VOLUME : SMESH::FACE );
00426       groups->length( entries.count() );
00427       for ( int i = 0; i < entries.count(); ++i )
00428       {
00429         _PTR(SObject) sobj =
00430           SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() );
00431         SMESH::SMESH_IDSource_var grp = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );  
00432         SMESH::array_of_ElementType_var types = grp->GetTypes();
00433         if ( types->length() < 1 || types[0] != goodType )
00434         {
00435           if ( !wrongGroups.isEmpty() )
00436             wrongGroups += ", ";
00437           wrongGroups += sobj->GetName().c_str();
00438         }
00439         else
00440         {
00441           groups[ nbGroups++ ] = grp;
00442         }
00443       }
00444       groups->length( nbGroups );
00445       mySrcMesh = groups[0]->GetMesh();
00446     }
00447 
00448     if ( !CORBA::is_nil( mySrcMesh ) ) {
00449       SMESH::SMESH_MeshEditor_var aMeshEditor = mySrcMesh->GetMeshEditor();
00450       SMESH::SMESH_Group_var newGrp;
00451       SMESH::SMESH_Mesh_var newMesh;
00452       CORBA::Long nbAdded = aMeshEditor->MakeBoundaryElements( mode,
00453                                                                groupName.toLatin1().constData(),
00454                                                                meshName.toLatin1().constData(),
00455                                                                copyAll,
00456                                                                groups,
00457                                                                newMesh.out(),
00458                                                                newGrp.out() );
00459       QString msg = tr("NB_ADDED").arg( nbAdded );
00460       if ( !wrongGroups.isEmpty() )
00461         msg += ".\n" + tr("WRONG_GROUPS").arg( wrongGroups );
00462       SUIT_MessageBox::information( myDlg, tr("SMESH_INFORMATION"), msg);
00463 
00464       if ( !newMesh->_is_nil() ) {
00465         if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
00466           theEntryList.append( aSObject->GetID().c_str() );
00467 #ifdef WITHGENERICOBJ
00468         newMesh->UnRegister();
00469 #endif
00470       }
00471       if ( !newGrp->_is_nil() ) {
00472 #ifdef WITHGENERICOBJ
00473         newGrp->UnRegister();
00474 #endif
00475       }
00476       ok = true;
00477 
00478       for ( int i = 0; i < entries.count(); ++i )
00479         if ( SMESH_Actor* actor = SMESH::FindActorByEntry( entries[i].toLatin1().constData() ))
00480           SMESH::Update(actor->getIO(),actor->GetVisibility());
00481       SMESH::RepaintCurrentView();
00482     }
00483   }
00484   catch ( ... ) {
00485   }
00486   return ok;
00487 }
00488 
00489 bool SMESHGUI_Make2DFrom3DOp::onApply()
00490 {
00491   if ( isStudyLocked() )
00492     return false;
00493 
00494   QString msg;
00495   if ( !isValid( msg ) ) {
00496     dlg()->show();
00497     if ( msg != "" )
00498       SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), msg );
00499     return false;
00500   }
00501 
00502   QStringList anEntryList;
00503   bool res = false;
00504   try {
00505     res = compute2DMesh( anEntryList );
00506   }
00507   catch ( const SALOME::SALOME_Exception& S_ex ) {
00508     SalomeApp_Tools::QtCatchCorbaException( S_ex );
00509   }
00510   catch ( ... ) {
00511   }
00512 
00513   if ( res ) {
00514     SMESHGUI::Modified();
00515     update( UF_ObjBrowser | UF_Model );
00516     if( LightApp_Application* anApp =
00517         dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
00518       anApp->browseObjects( anEntryList, isApplyAndClose() );
00519     myDlg->setNewMeshName( SMESH::UniqueName( "Mesh_1" ) );
00520     myDlg->setGroupName( SMESH::UniqueName( "Group" ) );
00521   }
00522   else {
00523     SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), tr( "SMESH_OPERATION_FAILED" ) );
00524   }
00525 
00526   return res;
00527 }
Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS