1 // Copyright (C) 2007-2012 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"
29 #include "SMESHGUI_VTKUtils.h"
30 #include "SMESH_Actor.h"
32 // SALOME GUI includes
33 #include <LightApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <LightApp_UpdateFlags.h>
36 #include <SALOME_ListIO.hxx>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_OverrideCursor.h>
40 #include <SUIT_Session.h>
41 #include <SVTK_ViewModel.h>
42 //#include <SVTK_ViewWindow.h>
43 #include <SalomeApp_Study.h>
44 #include <SalomeApp_Tools.h>
47 #include <SALOMEconfig.h>
48 #include CORBA_SERVER_HEADER(SMESH_Group)
52 #include <QRadioButton>
55 #include <QHBoxLayout>
56 #include <QGridLayout>
57 #include <QToolButton>
59 #include <Standard_ErrorHandler.hxx>
65 \class SMESHGUI_Make2DFrom3DDlg
66 \brief Copy Mesh dialog box
69 SMESHGUI_Make2DFrom3DDlg::SMESHGUI_Make2DFrom3DDlg( QWidget* parent )
70 : SMESHGUI_Dialog( parent, false, true, OK | Apply | Close | Help )
73 setWindowTitle( tr("CAPTION") );
76 QGroupBox* aModeGrp = new QGroupBox( tr( "MODE" ), mainFrame() );
77 QHBoxLayout* aModeGrpLayout = new QHBoxLayout( aModeGrp );
78 aModeGrpLayout->setMargin( MARGIN );
79 aModeGrpLayout->setSpacing( SPACING );
80 my2dFrom3dRB = new QRadioButton( tr( "2D_FROM_3D" ), aModeGrp );
81 my1dFrom2dRB = new QRadioButton( tr( "1D_FROM_2D" ), aModeGrp );
82 //my1dFrom3dRB = new QRadioButton( tr( "1D_FROM_3D" ), aModeGrp );
83 aModeGrpLayout->addWidget( my2dFrom3dRB );
84 aModeGrpLayout->addWidget( my1dFrom2dRB );
85 //aModeGrpLayout->addWidget( my1dFrom3dRB );
87 // // Groups of mesh faces
88 // setObjectPixmap( "SMESH", tr( "ICON_SELECT" ) );
89 // createObject( tr( "Groups" ), mainFrame(), Groups );
90 // setNameIndication( Groups, ListOfNames );
91 // objectWg( Groups, Btn )->hide();
94 //setObjectPixmap( "SMESH", tr( "ICON_SELECT" ) );
95 createObject( tr( "Groups" ), mainFrame(), MeshOrGroups );
96 setNameIndication( MeshOrGroups, ListOfNames );
97 objectWg( MeshOrGroups, Btn )->hide();
100 QGroupBox* aTargetGrp = new QGroupBox( tr( "TARGET" ), mainFrame() );
101 QGridLayout* aTargetGrpLayout = new QGridLayout( aTargetGrp );
102 aTargetGrpLayout->setMargin( MARGIN );
103 aTargetGrpLayout->setSpacing( SPACING );
104 myThisMeshRB = new QRadioButton( tr( "THIS_MESH" ), aTargetGrp );
105 myNewMeshRB = new QRadioButton( tr( "NEW_MESH" ), aTargetGrp );
106 myMeshName = new QLineEdit( aTargetGrp );
107 myCopyCheck = new QCheckBox( tr( "COPY_SRC" ), aTargetGrp );
108 aTargetGrpLayout->addWidget( myThisMeshRB, 0, 0 );
109 aTargetGrpLayout->addWidget( myNewMeshRB, 1, 0 );
110 aTargetGrpLayout->addWidget( myMeshName, 1, 1 );
111 aTargetGrpLayout->addWidget( myCopyCheck, 2, 0 );
112 myGroupCheck = new QCheckBox( tr( "CREATE_GROUP" ), mainFrame() );
113 myGroupName = new QLineEdit( mainFrame() );
116 QGridLayout* aDlgLay = new QGridLayout( mainFrame() );
117 aDlgLay->setMargin( 0 );
118 aDlgLay->setSpacing( SPACING );
119 aDlgLay->addWidget( aModeGrp, 0, 0, 1, 3 );
120 aDlgLay->addWidget( objectWg( MeshOrGroups, Label ), 1, 0 );
121 aDlgLay->addWidget( objectWg( MeshOrGroups, Control ), 1, 1 );
122 aDlgLay->addWidget( aTargetGrp, 2, 0, 1, 3 );
123 aDlgLay->addWidget( myGroupCheck, 3, 0 );
124 aDlgLay->addWidget( myGroupName, 3, 1, 1, 2 );
127 connect( myThisMeshRB, SIGNAL( clicked() ), this, SLOT( onTargetChanged() ) );
128 connect( myNewMeshRB, SIGNAL( clicked() ), this, SLOT( onTargetChanged() ) );
129 connect( myGroupCheck, SIGNAL( clicked() ), this, SLOT( onGroupChecked() ) );
132 my2dFrom3dRB->setChecked( true );
133 myThisMeshRB->setChecked( true );
138 SMESHGUI_Make2DFrom3DDlg::~SMESHGUI_Make2DFrom3DDlg()
142 SMESH::Bnd_Dimension SMESHGUI_Make2DFrom3DDlg::mode() const
144 if ( my2dFrom3dRB->isChecked() )
145 return SMESH::BND_2DFROM3D;
146 else if ( my1dFrom2dRB->isChecked() )
147 return SMESH::BND_1DFROM2D;
149 return SMESH::BND_1DFROM3D;
152 bool SMESHGUI_Make2DFrom3DDlg::needNewMesh() const
154 return myNewMeshRB->isChecked();
157 QString SMESHGUI_Make2DFrom3DDlg::getNewMeshName() const
159 return myMeshName->text().trimmed();
162 void SMESHGUI_Make2DFrom3DDlg::setNewMeshName( const QString& name )
164 myMeshName->setText( name );
167 void SMESHGUI_Make2DFrom3DDlg::setNewMeshEnabled( bool enable )
170 myThisMeshRB->setChecked( true );
172 myNewMeshRB->setEnabled( enable );
177 bool SMESHGUI_Make2DFrom3DDlg::getNewMeshEnabled() const
179 return myNewMeshRB->isEnabled();
182 bool SMESHGUI_Make2DFrom3DDlg::needGroup() const
184 return myGroupCheck->isChecked();
187 QString SMESHGUI_Make2DFrom3DDlg::getGroupName() const
189 return myGroupName->text().trimmed();
192 void SMESHGUI_Make2DFrom3DDlg::setGroupName( const QString& name )
194 myGroupName->setText( name );
197 bool SMESHGUI_Make2DFrom3DDlg::copySource() const
199 return myCopyCheck->isChecked();
202 void SMESHGUI_Make2DFrom3DDlg::onTargetChanged()
204 myMeshName->setEnabled( myNewMeshRB->isChecked() );
205 myCopyCheck->setEnabled( myNewMeshRB->isChecked() );
208 void SMESHGUI_Make2DFrom3DDlg::onGroupChecked()
210 myGroupName->setEnabled( myGroupCheck->isChecked() );
214 \class SMESHGUI_Make2DFrom3DOp
215 \brief Copy Mesh operation class
218 SMESHGUI_Make2DFrom3DOp::SMESHGUI_Make2DFrom3DOp()
219 : SMESHGUI_SelectionOp(),
220 myMeshFilter(SMESH::MESH),
221 myGroupFilter(SMESH::GROUP)
225 SMESHGUI_Make2DFrom3DOp::~SMESHGUI_Make2DFrom3DOp()
231 LightApp_Dialog* SMESHGUI_Make2DFrom3DOp::dlg() const
236 void SMESHGUI_Make2DFrom3DOp::startOperation()
239 myDlg = new SMESHGUI_Make2DFrom3DDlg( desktop() );
241 myHelpFileName = "make_2dmesh_from_3d_page.html";
243 SMESHGUI_SelectionOp::startOperation();
245 myDlg->setNewMeshName( SMESH::UniqueName( "Mesh_1" ) );
246 myDlg->setGroupName( SMESH::UniqueName( "Group" ) );
249 connect( myDlg->my2dFrom3dRB, SIGNAL( toggled(bool) ), this, SLOT( onModeChanged() ) );
250 connect( myDlg->my1dFrom2dRB, SIGNAL( toggled(bool) ), this, SLOT( onModeChanged() ) );
251 //connect( myDlg->my1dFrom3dRB, SIGNAL( toggled(bool) ), this, SLOT( onModeChanged() ) );
255 myDlg->activateObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
259 //================================================================================
261 * \brief Set filter corresponding to dimension
263 //================================================================================
265 void SMESHGUI_Make2DFrom3DOp::onModeChanged()
267 // QRadioButton* b = dynamic_cast<QRadioButton*>( sender());
268 // if ( b && !b->isChecked() )
271 // // enable "2D groups" field
272 // bool enableGroups = ( myDlg->mode() == SMESH::BND_1DFROM3D );
273 // myDlg->setObjectEnabled( SMESHGUI_Make2DFrom3DDlg::Groups, enableGroups );
274 // ((QToolButton*) myDlg->objectWg( SMESHGUI_Make2DFrom3DDlg::Groups,
275 // SMESHGUI_Make2DFrom3DDlg::Btn ))->setChecked( enableGroups );
278 // int id = enableGroups ? SMESHGUI_Make2DFrom3DDlg::Groups : SMESHGUI_Make2DFrom3DDlg::Mesh;
279 // onDeactivateObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
280 // onActivateObject ( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
284 void SMESHGUI_Make2DFrom3DOp::selectionDone()
286 myDlg->clearSelection( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
287 mySrcMesh = SMESH::SMESH_Mesh::_nil();
289 if ( !dlg() ) return;
292 if ( dlg()->isVisible() ) {
294 QStringList names, ids;
295 LightApp_Dialog::TypesList types;
296 selected( names, types, ids );
297 for ( int i = 0; i < names.count(); ++i )
298 names[i] = names[i].trimmed();
299 myDlg->selectObject( names, types, ids );
301 // enable/desable "new mesh" button
303 for ( int i = 0; i < ids.count() && isMesh; ++i )
306 SMESHGUI::activeStudy()->studyDS()->FindObjectID( ids[i].toLatin1().constData() );
307 mySrcMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( sobj );
308 isMesh = !mySrcMesh->_is_nil();
310 myDlg->setNewMeshEnabled( isMesh );
312 catch ( const SALOME::SALOME_Exception& S_ex ) {
313 SalomeApp_Tools::QtCatchCorbaException( S_ex );
320 SUIT_SelectionFilter* SMESHGUI_Make2DFrom3DOp::createFilter( const int theId ) const
322 SMESHGUI_Make2DFrom3DOp* me = (SMESHGUI_Make2DFrom3DOp*) this;
324 QList<SUIT_SelectionFilter*> subFilters;
325 subFilters.append( & me->myMeshFilter );
326 subFilters.append( & me->myGroupFilter );
328 SUIT_SelectionFilter* f = new SMESH_LogicalFilter( subFilters, SMESH_LogicalFilter::LO_OR );
332 bool SMESHGUI_Make2DFrom3DOp::isValid( QString& msg ) const
334 if ( !dlg() ) return false;
336 // check if a mesh is selected
337 if ( !myDlg->hasSelection( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups ))
339 msg = tr( "SMESH_ERR_NO_INPUT_MESH" );
343 dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
344 const bool isMeshSelected = ( !mySrcMesh->_is_nil() );
345 if ( isMeshSelected )
347 // only one mesh is allowed
348 if ( entries.size() > 1 ) {
349 msg = tr( "SMESH_TOO_MANY_MESHES" );
355 // check if only groups are selected
356 for ( int i = 0; i < entries.count(); ++i )
358 SMESH::SMESH_GroupBase_var grp;
359 if ( _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() ))
360 grp = SMESH::SObjectToInterface<SMESH::SMESH_GroupBase>( sobj );
361 if ( grp->_is_nil() ) {
362 msg = tr( "SMESH_NOT_ONLY_GROUPS" );
367 // check if the selected objects contains elements of required type
368 bool hasFaces = false, hasVolumes = false;
369 SMESH::Bnd_Dimension mode = myDlg->mode();
370 for ( int i = 0; i < entries.count(); ++i )
372 SMESH::SMESH_IDSource_var idSource;
373 if ( _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() ))
374 idSource = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );
375 if ( !idSource->_is_nil() ) {
376 SMESH::array_of_ElementType_var types = idSource->GetTypes();
377 for ( int j = 0; j < types->length(); ++j )
378 if ( types[j] == SMESH::VOLUME )
380 else if ( types[j] == SMESH::FACE )
384 if ( mode == SMESH::BND_2DFROM3D && !hasVolumes ) {
385 msg = tr( "SMESH_ERR_NO_3D_ELEMENTS" );
388 else if ( mode == SMESH::BND_1DFROM2D && !hasFaces ) {
389 msg = tr( "SMESH_ERR_NO_2D_ELEMENTS" );
393 // check if new mesh name is specified
394 if ( myDlg->needNewMesh() && myDlg->getNewMeshName().isEmpty() ) {
395 msg = tr( "SMESH_ERR_MESH_NAME_NOT_SPECIFIED" );
399 // check if group name is specified
400 if ( myDlg->needGroup() && myDlg->getGroupName().isEmpty() ) {
401 msg = tr( "SMESH_ERR_GRP_NAME_NOT_SPECIFIED" );
408 bool SMESHGUI_Make2DFrom3DOp::compute2DMesh( QStringList& theEntryList )
410 SUIT_OverrideCursor wc;
414 SMESH::Bnd_Dimension mode = myDlg->mode();
415 QString meshName = myDlg->needNewMesh() ? myDlg->getNewMeshName() : QString();
416 QString groupName = myDlg->needGroup() ? myDlg->getGroupName() : QString();
417 bool copyAll = myDlg->copySource();
420 dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
421 SMESH::ListOfIDSources_var groups = new SMESH::ListOfIDSources;
422 QString wrongGroups = "";
424 if ( mySrcMesh->_is_nil() ) // get selected groups, find groups of wrong type
427 int goodType = ( mode == SMESH::BND_2DFROM3D ? SMESH::VOLUME : SMESH::FACE );
428 groups->length( entries.count() );
429 for ( int i = 0; i < entries.count(); ++i )
432 SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() );
433 SMESH::SMESH_IDSource_var grp = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );
434 SMESH::array_of_ElementType_var types = grp->GetTypes();
435 if ( types->length() < 1 || types[0] != goodType )
437 if ( !wrongGroups.isEmpty() )
439 wrongGroups += sobj->GetName().c_str();
443 groups[ nbGroups++ ] = grp;
446 groups->length( nbGroups );
447 mySrcMesh = groups[0]->GetMesh();
450 if ( !CORBA::is_nil( mySrcMesh ) ) {
451 SMESH::SMESH_MeshEditor_var aMeshEditor = mySrcMesh->GetMeshEditor();
452 SMESH::SMESH_Group_var newGrp;
453 SMESH::SMESH_Mesh_var newMesh;
454 CORBA::Long nbAdded = aMeshEditor->MakeBoundaryElements( mode,
455 groupName.toLatin1().constData(),
456 meshName.toLatin1().constData(),
461 QString msg = tr("NB_ADDED").arg( nbAdded );
462 if ( !wrongGroups.isEmpty() )
463 msg += ".\n" + tr("WRONG_GROUPS").arg( wrongGroups );
464 SUIT_MessageBox::information( myDlg, tr("SMESH_INFORMATION"), msg);
466 if ( !newMesh->_is_nil() ) {
467 if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
468 theEntryList.append( aSObject->GetID().c_str() );
469 #ifdef WITHGENERICOBJ
470 newMesh->UnRegister();
473 if ( !newGrp->_is_nil() ) {
474 #ifdef WITHGENERICOBJ
475 newGrp->UnRegister();
480 for ( int i = 0; i < entries.count(); ++i )
481 if ( SMESH_Actor* actor = SMESH::FindActorByEntry( entries[i].toLatin1().constData() ))
482 SMESH::Update(actor->getIO(),actor->GetVisibility());
483 SMESH::RepaintCurrentView();
491 bool SMESHGUI_Make2DFrom3DOp::onApply()
493 if ( isStudyLocked() )
497 if ( !isValid( msg ) ) {
500 SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), msg );
504 QStringList anEntryList;
507 res = compute2DMesh( anEntryList );
509 catch ( const SALOME::SALOME_Exception& S_ex ) {
510 SalomeApp_Tools::QtCatchCorbaException( S_ex );
516 SMESHGUI::Modified();
517 update( UF_ObjBrowser | UF_Model );
518 if( LightApp_Application* anApp =
519 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
520 anApp->browseObjects( anEntryList, isApplyAndClose() );
521 myDlg->setNewMeshName( SMESH::UniqueName( "Mesh_1" ) );
522 myDlg->setGroupName( SMESH::UniqueName( "Group" ) );
525 SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), tr( "SMESH_OPERATION_FAILED" ) );