Back to index

salome-smesh  6.5.0
SMESHGUI_Make2DFrom3DOp.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 // 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       for ( int i = 0; i < names.count(); ++i )
00298         names[i] = names[i].trimmed();
00299       myDlg->selectObject( names, types, ids );
00300 
00301       // enable/desable "new mesh" button
00302       bool isMesh = true;
00303       for ( int i = 0; i < ids.count() && isMesh; ++i )
00304       {
00305         _PTR(SObject) sobj =
00306           SMESHGUI::activeStudy()->studyDS()->FindObjectID( ids[i].toLatin1().constData() );
00307         mySrcMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( sobj );  
00308         isMesh = !mySrcMesh->_is_nil();
00309       }
00310       myDlg->setNewMeshEnabled( isMesh );
00311     }
00312     catch ( const SALOME::SALOME_Exception& S_ex ) {
00313       SalomeApp_Tools::QtCatchCorbaException( S_ex );
00314     }
00315     catch ( ... ) {
00316     }
00317   }
00318 }
00319 
00320 SUIT_SelectionFilter* SMESHGUI_Make2DFrom3DOp::createFilter( const int theId ) const
00321 {
00322   SMESHGUI_Make2DFrom3DOp* me = (SMESHGUI_Make2DFrom3DOp*) this;
00323 
00324   QList<SUIT_SelectionFilter*> subFilters;
00325   subFilters.append( & me->myMeshFilter );
00326   subFilters.append( & me->myGroupFilter );
00327 
00328   SUIT_SelectionFilter* f = new SMESH_LogicalFilter( subFilters, SMESH_LogicalFilter::LO_OR );
00329   return f;
00330 }
00331 
00332 bool SMESHGUI_Make2DFrom3DOp::isValid( QString& msg ) const
00333 {
00334   if ( !dlg() ) return false;
00335 
00336   // check if a mesh is selected
00337   if ( !myDlg->hasSelection( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups ))
00338   {
00339     msg = tr( "SMESH_ERR_NO_INPUT_MESH" );
00340     return false;
00341   }
00342   QStringList entries;
00343   dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
00344   const bool isMeshSelected = ( !mySrcMesh->_is_nil() );
00345   if ( isMeshSelected )
00346   {
00347     // only one mesh is allowed
00348     if ( entries.size() > 1 ) {
00349       msg = tr( "SMESH_TOO_MANY_MESHES" );
00350       return false;
00351     }
00352   }
00353   else
00354   {
00355     // check if only groups are selected
00356     for ( int i = 0; i < entries.count(); ++i )
00357     {
00358       SMESH::SMESH_GroupBase_var grp;
00359       if ( _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() ))
00360         grp = SMESH::SObjectToInterface<SMESH::SMESH_GroupBase>( sobj );
00361       if ( grp->_is_nil() ) {
00362         msg = tr( "SMESH_NOT_ONLY_GROUPS" );
00363         return false;
00364       }
00365     }
00366   }
00367   // check if the selected objects contains elements of required type
00368   bool hasFaces = false, hasVolumes = false;
00369   SMESH::Bnd_Dimension mode = myDlg->mode();
00370   for ( int i = 0; i < entries.count(); ++i )
00371   {
00372     SMESH::SMESH_IDSource_var idSource;
00373     if ( _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() ))
00374       idSource = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );
00375     if ( !idSource->_is_nil() ) {
00376       SMESH::array_of_ElementType_var types = idSource->GetTypes();
00377       for ( int j = 0; j < types->length(); ++j )
00378         if ( types[j] == SMESH::VOLUME )
00379           hasVolumes = true;
00380         else if ( types[j] == SMESH::FACE )
00381           hasFaces = true;
00382     }
00383   }
00384   if ( mode == SMESH::BND_2DFROM3D && !hasVolumes ) {
00385     msg = tr( "SMESH_ERR_NO_3D_ELEMENTS" );
00386     return false;
00387   }
00388   else if ( mode == SMESH::BND_1DFROM2D && !hasFaces  ) {
00389     msg = tr( "SMESH_ERR_NO_2D_ELEMENTS" );
00390     return false;
00391   }
00392 
00393   // check if new mesh name is specified
00394   if ( myDlg->needNewMesh() && myDlg->getNewMeshName().isEmpty() ) {
00395     msg = tr( "SMESH_ERR_MESH_NAME_NOT_SPECIFIED" );
00396     return false;
00397   }
00398 
00399   // check if group name is specified
00400   if ( myDlg->needGroup() && myDlg->getGroupName().isEmpty() ) {
00401     msg = tr( "SMESH_ERR_GRP_NAME_NOT_SPECIFIED" );
00402     return false;
00403   }
00404 
00405   return true;
00406 }
00407 
00408 bool SMESHGUI_Make2DFrom3DOp::compute2DMesh( QStringList& theEntryList )
00409 {
00410   SUIT_OverrideCursor wc;
00411 
00412   bool ok = false;
00413   try {
00414     SMESH::Bnd_Dimension mode = myDlg->mode();
00415     QString meshName          = myDlg->needNewMesh() ? myDlg->getNewMeshName() : QString();
00416     QString groupName         = myDlg->needGroup()   ? myDlg->getGroupName()   : QString();
00417     bool copyAll              = myDlg->copySource();
00418 
00419     QStringList entries;
00420     dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
00421     SMESH::ListOfIDSources_var groups = new SMESH::ListOfIDSources;
00422     QString wrongGroups = "";
00423 
00424     if ( mySrcMesh->_is_nil() ) // get selected groups, find groups of wrong type
00425     {
00426       int nbGroups = 0;
00427       int goodType = ( mode == SMESH::BND_2DFROM3D ? SMESH::VOLUME : SMESH::FACE );
00428       groups->length( entries.count() );
00429       for ( int i = 0; i < entries.count(); ++i )
00430       {
00431         _PTR(SObject) sobj =
00432           SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() );
00433         SMESH::SMESH_IDSource_var grp = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );  
00434         SMESH::array_of_ElementType_var types = grp->GetTypes();
00435         if ( types->length() < 1 || types[0] != goodType )
00436         {
00437           if ( !wrongGroups.isEmpty() )
00438             wrongGroups += ", ";
00439           wrongGroups += sobj->GetName().c_str();
00440         }
00441         else
00442         {
00443           groups[ nbGroups++ ] = grp;
00444         }
00445       }
00446       groups->length( nbGroups );
00447       mySrcMesh = groups[0]->GetMesh();
00448     }
00449 
00450     if ( !CORBA::is_nil( mySrcMesh ) ) {
00451       SMESH::SMESH_MeshEditor_var aMeshEditor = mySrcMesh->GetMeshEditor();
00452       SMESH::SMESH_Group_var newGrp;
00453       SMESH::SMESH_Mesh_var newMesh;
00454       CORBA::Long nbAdded = aMeshEditor->MakeBoundaryElements( mode,
00455                                                                groupName.toLatin1().constData(),
00456                                                                meshName.toLatin1().constData(),
00457                                                                copyAll,
00458                                                                groups,
00459                                                                newMesh.out(),
00460                                                                newGrp.out() );
00461       QString msg = tr("NB_ADDED").arg( nbAdded );
00462       if ( !wrongGroups.isEmpty() )
00463         msg += ".\n" + tr("WRONG_GROUPS").arg( wrongGroups );
00464       SUIT_MessageBox::information( myDlg, tr("SMESH_INFORMATION"), msg);
00465 
00466       if ( !newMesh->_is_nil() ) {
00467         if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
00468           theEntryList.append( aSObject->GetID().c_str() );
00469 #ifdef WITHGENERICOBJ
00470         newMesh->UnRegister();
00471 #endif
00472       }
00473       if ( !newGrp->_is_nil() ) {
00474 #ifdef WITHGENERICOBJ
00475         newGrp->UnRegister();
00476 #endif
00477       }
00478       ok = true;
00479 
00480       for ( int i = 0; i < entries.count(); ++i )
00481         if ( SMESH_Actor* actor = SMESH::FindActorByEntry( entries[i].toLatin1().constData() ))
00482           SMESH::Update(actor->getIO(),actor->GetVisibility());
00483       SMESH::RepaintCurrentView();
00484     }
00485   }
00486   catch ( ... ) {
00487   }
00488   return ok;
00489 }
00490 
00491 bool SMESHGUI_Make2DFrom3DOp::onApply()
00492 {
00493   if ( isStudyLocked() )
00494     return false;
00495 
00496   QString msg;
00497   if ( !isValid( msg ) ) {
00498     dlg()->show();
00499     if ( msg != "" )
00500       SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), msg );
00501     return false;
00502   }
00503 
00504   QStringList anEntryList;
00505   bool res = false;
00506   try {
00507     res = compute2DMesh( anEntryList );
00508   }
00509   catch ( const SALOME::SALOME_Exception& S_ex ) {
00510     SalomeApp_Tools::QtCatchCorbaException( S_ex );
00511   }
00512   catch ( ... ) {
00513   }
00514 
00515   if ( res ) {
00516     SMESHGUI::Modified();
00517     update( UF_ObjBrowser | UF_Model );
00518     if( LightApp_Application* anApp =
00519         dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
00520       anApp->browseObjects( anEntryList, isApplyAndClose() );
00521     myDlg->setNewMeshName( SMESH::UniqueName( "Mesh_1" ) );
00522     myDlg->setGroupName( SMESH::UniqueName( "Group" ) );
00523   }
00524   else {
00525     SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), tr( "SMESH_OPERATION_FAILED" ) );
00526   }
00527 
00528   return res;
00529 }