1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File : SMESHGUI_Make2DFrom3D.cxx
20 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
22 #include "SMESHGUI_Make2DFrom3DOp.h"
25 #include "SMESHGUI_Utils.h"
26 #include "SMESHGUI_MeshUtils.h"
27 #include "SMESH_TypeFilter.hxx"
28 #include "SMESH_LogicalFilter.hxx"
30 // SALOME GUI includes
31 #include <LightApp_SelectionMgr.h>
32 #include <LightApp_UpdateFlags.h>
33 #include <SALOME_ListIO.hxx>
34 #include <SUIT_Desktop.h>
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_OverrideCursor.h>
37 #include <SVTK_ViewModel.h>
38 #include <SalomeApp_Study.h>
39 #include <SalomeApp_Tools.h>
42 #include <SALOMEconfig.h>
43 #include CORBA_SERVER_HEADER(SMESH_Group)
47 #include <QRadioButton>
50 #include <QHBoxLayout>
51 #include <QGridLayout>
52 #include <QToolButton>
58 \class SMESHGUI_Make2DFrom3DDlg
59 \brief Copy Mesh dialog box
62 SMESHGUI_Make2DFrom3DDlg::SMESHGUI_Make2DFrom3DDlg( QWidget* parent )
63 : SMESHGUI_Dialog( parent, false, true, OK | Apply | Close | Help )
66 setWindowTitle( tr("CAPTION") );
69 QGroupBox* aModeGrp = new QGroupBox( tr( "MODE" ), mainFrame() );
70 QHBoxLayout* aModeGrpLayout = new QHBoxLayout( aModeGrp );
71 aModeGrpLayout->setMargin( MARGIN );
72 aModeGrpLayout->setSpacing( SPACING );
73 my2dFrom3dRB = new QRadioButton( tr( "2D_FROM_3D" ), aModeGrp );
74 my1dFrom2dRB = new QRadioButton( tr( "1D_FROM_2D" ), aModeGrp );
75 my1dFrom3dRB = new QRadioButton( tr( "1D_FROM_3D" ), aModeGrp );
76 aModeGrpLayout->addWidget( my2dFrom3dRB );
77 aModeGrpLayout->addWidget( my1dFrom2dRB );
78 aModeGrpLayout->addWidget( my1dFrom3dRB );
80 // Groups of mesh faces
81 setObjectPixmap( "SMESH", tr( "ICON_SELECT" ) );
82 createObject( tr( "Groups" ), mainFrame(), Groups );
83 setNameIndication( Groups, ListOfNames );
84 objectWg( Groups, Btn )->hide();
87 QGroupBox* aTargetGrp = new QGroupBox( tr( "TARGET" ), mainFrame() );
88 QGridLayout* aTargetGrpLayout = new QGridLayout( aTargetGrp );
89 aTargetGrpLayout->setMargin( MARGIN );
90 aTargetGrpLayout->setSpacing( SPACING );
91 myThisMeshRB = new QRadioButton( tr( "THIS_MESH" ), aTargetGrp );
92 myNewMeshRB = new QRadioButton( tr( "NEW_MESH" ), aTargetGrp );
93 myMeshName = new QLineEdit( aTargetGrp );
94 myCopyCheck = new QCheckBox( tr( "COPY_SRC" ), aTargetGrp );
95 aTargetGrpLayout->addWidget( myThisMeshRB, 0, 0 );
96 aTargetGrpLayout->addWidget( myNewMeshRB, 1, 0 );
97 aTargetGrpLayout->addWidget( myMeshName, 1, 1 );
98 aTargetGrpLayout->addWidget( myCopyCheck, 2, 0 );
99 myGroupCheck = new QCheckBox( tr( "CREATE_GROUP" ), mainFrame() );
100 myGroupName = new QLineEdit( mainFrame() );
103 QGridLayout* aDlgLay = new QGridLayout( mainFrame() );
104 aDlgLay->setMargin( 0 );
105 aDlgLay->setSpacing( SPACING );
106 aDlgLay->addWidget( aModeGrp, 0, 0, 1, 3 );
107 aDlgLay->addWidget( objectWg( Groups, Label ), 1, 0 );
108 aDlgLay->addWidget( objectWg( Groups, Control ), 1, 1 );
109 aDlgLay->addWidget( aTargetGrp, 2, 0, 1, 3 );
110 aDlgLay->addWidget( myGroupCheck, 3, 0 );
111 aDlgLay->addWidget( myGroupName, 3, 1, 1, 2 );
114 connect( myThisMeshRB, SIGNAL( clicked() ), this, SLOT( onTargetChanged() ) );
115 connect( myNewMeshRB, SIGNAL( clicked() ), this, SLOT( onTargetChanged() ) );
116 connect( myGroupCheck, SIGNAL( clicked() ), this, SLOT( onGroupChecked() ) );
119 my2dFrom3dRB->setChecked( true );
120 myThisMeshRB->setChecked( true );
125 SMESHGUI_Make2DFrom3DDlg::~SMESHGUI_Make2DFrom3DDlg()
129 SMESH::Bnd_Dimension SMESHGUI_Make2DFrom3DDlg::mode() const
131 if ( my2dFrom3dRB->isChecked() )
132 return SMESH::BND_2DFROM3D;
133 else if ( my1dFrom2dRB->isChecked() )
134 return SMESH::BND_1DFROM2D;
136 return SMESH::BND_1DFROM3D;
139 bool SMESHGUI_Make2DFrom3DDlg::needNewMesh() const
141 return myNewMeshRB->isChecked();
144 QString SMESHGUI_Make2DFrom3DDlg::getNewMeshName() const
146 return myMeshName->text().trimmed();
149 void SMESHGUI_Make2DFrom3DDlg::setNewMeshName( const QString& name )
151 myMeshName->setText( name );
154 bool SMESHGUI_Make2DFrom3DDlg::needGroup() const
156 return myGroupCheck->isChecked();
159 QString SMESHGUI_Make2DFrom3DDlg::getGroupName() const
161 return myGroupName->text().trimmed();
164 void SMESHGUI_Make2DFrom3DDlg::setGroupName( const QString& name )
166 myGroupName->setText( name );
169 bool SMESHGUI_Make2DFrom3DDlg::copySource() const
171 return myCopyCheck->isChecked();
174 void SMESHGUI_Make2DFrom3DDlg::onTargetChanged()
176 myMeshName->setEnabled( myNewMeshRB->isChecked() );
177 myCopyCheck->setEnabled( myNewMeshRB->isChecked() );
180 void SMESHGUI_Make2DFrom3DDlg::onGroupChecked()
182 myGroupName->setEnabled( myGroupCheck->isChecked() );
186 \class SMESHGUI_Make2DFrom3DOp
187 \brief Copy Mesh operation class
190 SMESHGUI_Make2DFrom3DOp::SMESHGUI_Make2DFrom3DOp()
191 : SMESHGUI_SelectionOp()
195 SMESHGUI_Make2DFrom3DOp::~SMESHGUI_Make2DFrom3DOp()
201 LightApp_Dialog* SMESHGUI_Make2DFrom3DOp::dlg() const
206 void SMESHGUI_Make2DFrom3DOp::startOperation()
209 myDlg = new SMESHGUI_Make2DFrom3DDlg( desktop() );
211 myHelpFileName = "make_2dmesh_from_3d_page.html";
213 SMESHGUI_SelectionOp::startOperation();
215 myDlg->setNewMeshName( SMESH::UniqueName( "Mesh_1" ) );
216 myDlg->setGroupName( SMESH::UniqueName( "Group" ) );
219 connect( myDlg->my2dFrom3dRB, SIGNAL( toggled(bool) ), this, SLOT( onModeChanged() ) );
220 connect( myDlg->my1dFrom2dRB, SIGNAL( toggled(bool) ), this, SLOT( onModeChanged() ) );
221 connect( myDlg->my1dFrom3dRB, SIGNAL( toggled(bool) ), this, SLOT( onModeChanged() ) );
226 void SMESHGUI_Make2DFrom3DOp::onModeChanged()
228 QRadioButton* b = dynamic_cast<QRadioButton*>( sender());
229 if ( b && !b->isChecked() )
232 // enable "2D groups" field
233 bool enableGroups = ( myDlg->mode() == SMESH::BND_1DFROM3D );
234 myDlg->setObjectEnabled( SMESHGUI_Make2DFrom3DDlg::Groups, enableGroups );
235 ((QToolButton*) myDlg->objectWg( SMESHGUI_Make2DFrom3DDlg::Groups,
236 SMESHGUI_Make2DFrom3DDlg::Btn ))->setChecked( enableGroups );
239 int id = enableGroups ? SMESHGUI_Make2DFrom3DDlg::Groups : SMESHGUI_Make2DFrom3DDlg::Mesh;
240 onDeactivateObject( id );
241 onActivateObject( id );
244 void SMESHGUI_Make2DFrom3DOp::selectionDone()
246 mySrcMesh = SMESH::SMESH_Mesh::_nil();
247 myDlg->clearSelection( SMESHGUI_Make2DFrom3DDlg::Groups );
249 if ( !dlg() ) return;
252 if ( dlg()->isVisible() ) {
254 QStringList names, ids;
255 LightApp_Dialog::TypesList types;
256 selected( names, types, ids );
257 myDlg->selectObject( names, types, ids );
259 SALOME_ListIO sel; selectionMgr()->selectedObjects( sel, SVTK_Viewer::Type() );
260 if ( !sel.IsEmpty() )
262 SMESH::SMESH_IDSource_var IS = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>(sel.First());
263 if(!CORBA::is_nil(IS))
264 mySrcMesh = IS->GetMesh();
267 catch ( const SALOME::SALOME_Exception& S_ex ) {
268 SalomeApp_Tools::QtCatchCorbaException( S_ex );
275 SUIT_SelectionFilter* SMESHGUI_Make2DFrom3DOp::createFilter( const int theId ) const
277 MeshObjectType type = ( theId == SMESHGUI_Make2DFrom3DDlg::Groups ? GROUP_FACE : MESH );
278 SUIT_SelectionFilter* f = new SMESH_TypeFilter( type );
282 bool SMESHGUI_Make2DFrom3DOp::isValid( QString& msg ) const
284 if ( !dlg() ) return false;
286 // check if a mesh is selected
287 if ( mySrcMesh->_is_nil() )
289 msg = tr( "SMESH_ERR_NO_INPUT_MESH" );
292 // check if groups are selected
293 SMESH::Bnd_Dimension mode = myDlg->mode();
294 if ( mode == SMESH::BND_1DFROM3D )
296 SMESH::SMESH_GroupBase_var grp;
298 dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::Groups, entries );
299 if ( !entries.isEmpty() )
301 _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[0].toLatin1().constData() );
303 grp = SMESH::SObjectToInterface<SMESH::SMESH_GroupBase>( sobj );
305 if ( grp->_is_nil() ) {
306 msg = tr( "SMESH_ERR_NO_INPUT_GROUP" );
312 // check if mesh contains elements of required type
313 SMESH::Bnd_Dimension mode = myDlg->mode();
315 if ( mode == SMESH::BND_2DFROM3D && mySrcMesh->NbVolumes() == 0 ) {
316 msg = tr( "SMESH_ERR_NO_3D_ELEMENTS" );
319 else if ( mode == SMESH::BND_1DFROM2D && mySrcMesh->NbFaces() == 0 ) {
320 msg = tr( "SMESH_ERR_NO_2D_ELEMENTS" );
325 // check if new mesh name is specified
326 if ( myDlg->needNewMesh() && myDlg->getNewMeshName().isEmpty() ) {
327 msg = tr( "SMESH_ERR_MESH_NAME_NOT_SPECIFIED" );
331 // check if group name is specified
332 if ( myDlg->needGroup() && myDlg->getGroupName().isEmpty() ) {
333 msg = tr( "SMESH_ERR_GRP_NAME_NOT_SPECIFIED" );
340 bool SMESHGUI_Make2DFrom3DOp::compute2DMesh()
342 SUIT_OverrideCursor wc;
347 dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::Groups, entries );
348 SMESH::ListOfIDSources_var groups = new SMESH::ListOfIDSources;
349 groups->length( entries.count() );
350 for ( int i = 0; i < entries.count(); ++i )
352 _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() );
353 SMESH::SMESH_IDSource_var grp = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );
356 SMESH::Bnd_Dimension mode = myDlg->mode();
357 QString meshName = myDlg->needNewMesh() ? myDlg->getNewMeshName() : QString();
358 QString groupName = myDlg->needGroup() ? myDlg->getGroupName() : QString();
359 bool copyAll = myDlg->copySource();
361 if ( !CORBA::is_nil( mySrcMesh ) ) {
362 SMESH::SMESH_MeshEditor_var aMeshEditor = mySrcMesh->GetMeshEditor();
363 SMESH::SMESH_Group_var newGrp;
364 SMESH::SMESH_Mesh_var newMesh;
365 CORBA::Long nbAdded = aMeshEditor->MakeBoundaryElements( mode,
366 groupName.toLatin1().constData(),
367 meshName.toLatin1().constData(),
372 SUIT_MessageBox::information( myDlg,
373 tr("SMESH_INFORMATION"),
374 tr("NB_ADDED").arg( nbAdded ));
375 if ( !newMesh->_is_nil() ) {
376 #ifdef WITHGENERICOBJ
377 newMesh->UnRegister();
380 if ( !newGrp->_is_nil() ) {
381 #ifdef WITHGENERICOBJ
382 newGrp->UnRegister();
393 bool SMESHGUI_Make2DFrom3DOp::onApply()
395 if ( isStudyLocked() )
399 if ( !isValid( msg ) ) {
402 SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), msg );
408 res = compute2DMesh();
410 catch ( const SALOME::SALOME_Exception& S_ex ) {
411 SalomeApp_Tools::QtCatchCorbaException( S_ex );
417 SMESHGUI::Modified();
418 update( UF_ObjBrowser | UF_Model );
419 myDlg->setNewMeshName( SMESH::UniqueName( "Mesh_1" ) );
420 myDlg->setGroupName( SMESH::UniqueName( "Group" ) );
423 SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), tr( "SMESH_OPERATION_FAILED" ) );