1 // Copyright (C) 2007-2021 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, or (at your option) any later version.
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.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 )
305 _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( ids[i].toUtf8().constData() );
306 mySrcMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( sobj );
307 //isMesh = !mySrcMesh->_is_nil(); // EAP - it's sometimes necessary to copy to a new mesh
309 myDlg->setNewMeshEnabled( isMesh );
311 catch ( const SALOME::SALOME_Exception& S_ex ) {
312 SalomeApp_Tools::QtCatchCorbaException( S_ex );
319 SUIT_SelectionFilter* SMESHGUI_Make2DFrom3DOp::createFilter( const int /*theId*/ ) const
321 SMESHGUI_Make2DFrom3DOp* me = (SMESHGUI_Make2DFrom3DOp*) this;
323 QList<SUIT_SelectionFilter*> subFilters;
324 subFilters.append( & me->myMeshFilter );
325 subFilters.append( & me->myGroupFilter );
327 SUIT_SelectionFilter* f = new SMESH_LogicalFilter( subFilters, SMESH_LogicalFilter::LO_OR );
331 bool SMESHGUI_Make2DFrom3DOp::isValid( QString& msg ) const
333 if ( !dlg() ) return false;
335 // check if a mesh is selected
336 if ( !myDlg->hasSelection( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups ))
338 msg = tr( "SMESH_ERR_NO_INPUT_MESH" );
342 dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
343 const bool isMeshSelected = ( !mySrcMesh->_is_nil() );
344 if ( isMeshSelected )
346 // only one mesh is allowed
347 if ( entries.size() > 1 ) {
348 msg = tr( "SMESH_TOO_MANY_MESHES" );
354 // check if only groups are selected
355 for ( int i = 0; i < entries.count(); ++i )
357 SMESH::SMESH_GroupBase_var grp;
358 if ( _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entries[i].toUtf8().constData() ))
359 grp = SMESH::SObjectToInterface<SMESH::SMESH_GroupBase>( sobj );
360 if ( grp->_is_nil() ) {
361 msg = tr( "SMESH_NOT_ONLY_GROUPS" );
366 // check if the selected objects contains elements of required type
367 bool hasFaces = false, hasVolumes = false;
368 SMESH::Bnd_Dimension mode = myDlg->mode();
369 for ( int i = 0; i < entries.count(); ++i )
371 SMESH::SMESH_IDSource_var idSource;
372 if ( _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entries[i].toUtf8().constData() ))
373 idSource = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );
374 if ( !idSource->_is_nil() ) {
375 SMESH::array_of_ElementType_var types = idSource->GetTypes();
376 for ( int j = 0; j < (int) types->length(); ++j )
377 if ( types[j] == SMESH::VOLUME )
379 else if ( types[j] == SMESH::FACE )
383 if ( mode == SMESH::BND_2DFROM3D && !hasVolumes ) {
384 msg = tr( "SMESH_ERR_NO_3D_ELEMENTS" );
387 else if ( mode == SMESH::BND_1DFROM2D && !hasFaces ) {
388 msg = tr( "SMESH_ERR_NO_2D_ELEMENTS" );
392 // check if new mesh name is specified
393 if ( myDlg->needNewMesh() && myDlg->getNewMeshName().isEmpty() ) {
394 msg = tr( "SMESH_ERR_MESH_NAME_NOT_SPECIFIED" );
398 // check if group name is specified
399 if ( myDlg->needGroup() && myDlg->getGroupName().isEmpty() ) {
400 msg = tr( "SMESH_ERR_GRP_NAME_NOT_SPECIFIED" );
407 bool SMESHGUI_Make2DFrom3DOp::compute2DMesh( QStringList& theEntryList )
409 SUIT_OverrideCursor wc;
413 SMESH::Bnd_Dimension mode = myDlg->mode();
414 QString meshName = myDlg->needNewMesh() ? myDlg->getNewMeshName() : QString();
415 QString groupName = myDlg->needGroup() ? myDlg->getGroupName() : QString();
416 bool copyAll = myDlg->copySource();
419 dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
420 SMESH::ListOfIDSources_var groups = new SMESH::ListOfIDSources;
421 QString wrongGroups = "";
423 if ( mySrcMesh->_is_nil() ) // get selected groups, find groups of wrong type
426 int goodType = ( mode == SMESH::BND_2DFROM3D ? SMESH::VOLUME : SMESH::FACE );
427 groups->length( entries.count() );
428 for ( int i = 0; i < entries.count(); ++i )
430 _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entries[i].toUtf8().constData() );
431 SMESH::SMESH_IDSource_var grp = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );
432 SMESH::array_of_ElementType_var types = grp->GetTypes();
433 if ( types->length() < 1 || types[0] != goodType )
435 if ( !wrongGroups.isEmpty() )
437 wrongGroups += sobj->GetName().c_str();
441 groups[ nbGroups++ ] = grp;
444 groups->length( nbGroups );
445 mySrcMesh = groups[0]->GetMesh();
448 if ( !CORBA::is_nil( mySrcMesh ) ) {
449 SMESH::SMESH_MeshEditor_var aMeshEditor = mySrcMesh->GetMeshEditor();
450 SMESH::SMESH_Group_var newGrp;
451 SMESH::SMESH_Mesh_var newMesh;
452 CORBA::Long nbAdded = aMeshEditor->MakeBoundaryElements( mode,
453 groupName.toUtf8().constData(),
454 meshName.toUtf8().constData(),
459 QString msg = tr("NB_ADDED").arg( nbAdded );
460 if ( !wrongGroups.isEmpty() )
461 msg += ".\n" + tr("WRONG_GROUPS").arg( wrongGroups );
462 SUIT_MessageBox::information( myDlg, tr("SMESH_INFORMATION"), msg);
464 if ( !newMesh->_is_nil() ) {
465 if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
466 theEntryList.append( aSObject->GetID().c_str() );
470 for ( int i = 0; i < entries.count(); ++i )
471 if ( SMESH_Actor* actor = SMESH::FindActorByEntry( entries[i].toUtf8().constData() ))
473 actor->SetEntityMode( actor->GetEntityMode() | SMESH_Actor::eFaces );
474 SMESH::Update( actor->getIO(), actor->GetVisibility() );
476 SMESH::RepaintCurrentView();
484 bool SMESHGUI_Make2DFrom3DOp::onApply()
486 if ( SMESHGUI::isStudyLocked() )
490 if ( !isValid( msg ) ) {
493 SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), msg );
497 QStringList anEntryList;
500 res = compute2DMesh( anEntryList );
502 catch ( const SALOME::SALOME_Exception& S_ex ) {
503 SalomeApp_Tools::QtCatchCorbaException( S_ex );
509 SMESHGUI::Modified();
510 update( UF_ObjBrowser | UF_Model );
511 if( LightApp_Application* anApp =
512 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
513 anApp->browseObjects( anEntryList, isApplyAndClose() );
514 myDlg->setNewMeshName( SMESH::UniqueName( "Mesh_1" ) );
515 myDlg->setGroupName( SMESH::UniqueName( "Group" ) );
518 SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), tr( "SMESH_OPERATION_FAILED" ) );