1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File : SMESHGUI_Add0DElemsOnAllNodesDlg.cxx
25 // Created : Fri Oct 19 15:51:24 2012
26 // Author : Edward AGAPOV (eap)
28 #include "SMESHGUI_Add0DElemsOnAllNodesDlg.h"
31 #include "SMESHGUI_FilterDlg.h"
32 #include "SMESHGUI_MeshUtils.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35 #include "SMESH_LogicalFilter.hxx"
36 #include "SMESH_TypeFilter.hxx"
38 #include <LightApp_SelectionMgr.h>
39 #include <SALOME_ListIO.hxx>
40 #include <SUIT_MessageBox.h>
41 #include <SVTK_Selector.h>
42 #include <SalomeApp_Tools.h>
45 #include <QButtonGroup>
49 #include <QGridLayout>
51 #include <QHBoxLayout>
54 #include <QPushButton>
55 #include <QRadioButton>
57 #include <SALOMEconfig.h>
58 #include CORBA_SERVER_HEADER(SMESH_Mesh)
59 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
64 enum { SEL_OBJECT, SEL_ELEMENTS, SEL_NODES };
66 //================================================================================
68 * \brief Dialog Constructor
70 //================================================================================
72 SMESHGUI_Add0DElemsOnAllNodesDlg::SMESHGUI_Add0DElemsOnAllNodesDlg()
73 : SMESHGUI_Dialog( 0, false, true ),
76 setWindowTitle( tr( "CAPTION" ) );
78 // Seletction type radio buttons
80 QGroupBox* selTypeGrBox = new QGroupBox( mainFrame() );
82 QRadioButton* objBtn = new QRadioButton( tr( "SMESH_SUBMESH_GROUP"),selTypeGrBox );
83 QRadioButton* elemBtn = new QRadioButton( tr( "SMESH_ELEMENTS" ),selTypeGrBox );
84 QRadioButton* nodeBtn = new QRadioButton( tr( "SMESH_NODES" ),selTypeGrBox );
86 QHBoxLayout* selTypeLay = new QHBoxLayout( selTypeGrBox );
87 selTypeLay->setMargin(MARGIN);
88 selTypeLay->setSpacing(SPACING);
89 selTypeLay->addWidget( objBtn );
90 selTypeLay->addWidget( elemBtn );
91 selTypeLay->addWidget( nodeBtn );
92 objBtn->setChecked(true);
94 mySelTypeBtnGrp = new QButtonGroup( mainFrame() );
95 mySelTypeBtnGrp->addButton( objBtn , SEL_OBJECT );
96 mySelTypeBtnGrp->addButton( elemBtn, SEL_ELEMENTS );
97 mySelTypeBtnGrp->addButton( nodeBtn, SEL_NODES );
99 // Label, Select Btn, LineEdit, Filter Btn
101 setObjectPixmap( "SMESH", tr( "ICON_SELECT" ) );
102 createObject( tr( "SMESH_NAME" ), mainFrame(), 0 );
104 myFilterBtn = new QPushButton( tr( "SMESH_BUT_FILTER" ), mainFrame() );
108 myGroupBox = new QGroupBox( tr( "SMESH_ADD_TO_GROUP" ), mainFrame() );
109 myGroupBox->setCheckable( true );
111 myGroupLabel = new QLabel( tr( "SMESH_GROUP" ), myGroupBox );
112 myGroupListCmBox = new QComboBox( myGroupBox );
113 myGroupListCmBox->setEditable( true );
114 myGroupListCmBox->setInsertPolicy( QComboBox::NoInsert );
116 QHBoxLayout* groupsLayout = new QHBoxLayout( myGroupBox );
117 groupsLayout->setSpacing(SPACING);
118 groupsLayout->setMargin(MARGIN);
119 groupsLayout->addWidget( myGroupLabel );
120 groupsLayout->addWidget( myGroupListCmBox, 1 );
124 QGridLayout* aLay = new QGridLayout( mainFrame() );
125 aLay->setMargin(MARGIN);
126 aLay->setSpacing(SPACING);
128 aLay->addWidget( selTypeGrBox, 0, 0, 1, 5 );
130 aLay->addWidget( objectWg( 0, Label ), 1, 0 );
131 aLay->addWidget( objectWg( 0, Btn ), 1, 1 );
132 aLay->addWidget( objectWg( 0, Control), 1, 2, 1, 2 );
133 aLay->addWidget( myFilterBtn, 1, 4 );
135 aLay->addWidget( myGroupBox, 2, 0, 1, 5 );
139 connect( myGroupBox, SIGNAL( toggled( bool )), SLOT( onGroupChecked() ));
140 connect( mySelTypeBtnGrp, SIGNAL( buttonClicked(int) ), SLOT( onSelTypeChange(int)));
142 onSelTypeChange( SEL_OBJECT );
145 //================================================================================
147 * \brief SLOT to enable/disable groups
149 //================================================================================
151 void SMESHGUI_Add0DElemsOnAllNodesDlg::onGroupChecked( bool on )
153 myGroupLabel->setEnabled( on );
154 myGroupListCmBox->setEnabled( on );
157 //================================================================================
159 * \brief SLOT to enable/disable groups
161 //================================================================================
163 void SMESHGUI_Add0DElemsOnAllNodesDlg::onSelTypeChange( int selType )
165 setNameIndication( 0, selType == SEL_OBJECT ? OneName : ListOfNames );
166 setReadOnly ( 0, selType == SEL_OBJECT );
168 QLabel* label = qobject_cast< QLabel* >( objectWg(0, Label ));
170 case SEL_OBJECT: label->setText( tr("SMESH_NAME")); break;
171 case SEL_ELEMENTS: label->setText( tr("ELEMENT_IDS")); break;
172 case SEL_NODES: label->setText( tr("NODE_IDS")); break;
175 QLineEdit* lineEdit = qobject_cast< QLineEdit* >( objectWg(0, Control ));
176 lineEdit->setText("");
177 lineEdit->setValidator( selType == SEL_OBJECT ? 0 : & myIDValidator );
179 myFilterBtn->setEnabled( selType != SEL_OBJECT );
181 emit selTypeChanged( selType );
184 //================================================================================
186 * \brief Return type of selected object: [SEL_OBJECT, SEL_ELEMENTS, SEL_NODES]
188 //================================================================================
190 int SMESHGUI_Add0DElemsOnAllNodesDlg::getSelectionType() const
192 return mySelTypeBtnGrp->checkedId();
195 //================================================================================
197 * \brief Checks consistency of data
199 //================================================================================
201 bool SMESHGUI_Add0DElemsOnAllNodesDlg::isValid()
203 if( myGroupBox->isChecked() && myGroupListCmBox->currentText().isEmpty() ) {
204 SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ), tr( "GROUP_NAME_IS_EMPTY" ) );
210 //================================================================================
212 * \brief Operation Constructor
214 //================================================================================
216 SMESHGUI_Add0DElemsOnAllNodesOp::SMESHGUI_Add0DElemsOnAllNodesOp()
217 :SMESHGUI_SelectionOp(),
218 myDlg( new SMESHGUI_Add0DElemsOnAllNodesDlg ),
221 myHelpFileName = "adding_nodes_and_elements_page.html#adding_0delems_on_all_nodes_anchor";
223 connect( myDlg, SIGNAL( selTypeChanged(int) ), SLOT( onSelTypeChange(int)));
224 connect( myDlg->myFilterBtn, SIGNAL( clicked()), SLOT( onSetFilter() ));
227 //================================================================================
231 //================================================================================
233 SMESHGUI_Add0DElemsOnAllNodesOp::~SMESHGUI_Add0DElemsOnAllNodesOp()
236 myFilterDlg->setParent( 0 );
242 //================================================================================
246 //================================================================================
248 void SMESHGUI_Add0DElemsOnAllNodesOp::startOperation()
250 SMESHGUI_SelectionOp::startOperation();
252 myDlg->myGroupBox->setChecked( false );
253 myDlg->activateObject( 0 );
259 //================================================================================
261 * \brief Treat changed selection
263 //================================================================================
265 void SMESHGUI_Add0DElemsOnAllNodesOp::selectionDone()
267 if (myFilterDlg && myFilterDlg->isVisible()) return; // filter dgl active
268 if (!myDlg->myGroupBox->isEnabled()) return; // inactive
271 myDlg->setObjectText( 0, "");
274 selectionMgr()->selectedObjects( aList );
275 if ( aList.Extent() == 1 )
276 myIO = aList.First();
281 switch ( myDlg->getSelectionType() ) {
283 SMESHGUI_SelectionOp::selectionDone();
286 SMESH::GetNameOfSelectedElements( selector(), myIO, ids );
287 myDlg->setObjectText( 0, ids );
290 SMESH::GetNameOfSelectedNodes( selector(), myIO, ids );
291 myDlg->setObjectText( 0, ids );
296 // fill the list of existing groups
297 myDlg->myGroupListCmBox->clear();
298 myDlg->myGroupListCmBox->addItem( QString() );
299 if ( !myIO.IsNull() && myIO->hasEntry()) {
300 _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
301 _PTR(SObject) meshSO = aStudy->FindObjectID( myIO->getEntry() );
302 _PTR(SObject) group0DRoot;
303 if ( meshSO->FindSubObject( SMESH::Tag_0DElementsGroups, group0DRoot ))
305 _PTR(ChildIterator) group0DIter = aStudy->NewChildIterator( group0DRoot );
306 for ( ; group0DIter->More(); group0DIter->Next() )
308 _PTR(SObject) groupSO = group0DIter->Value();
309 std::string groupName = groupSO->GetName();
310 if ( !groupName.empty() )
311 myDlg->myGroupListCmBox->addItem( groupName.c_str() );
317 //================================================================================
319 * \brief Return a filter of objects
321 //================================================================================
323 SUIT_SelectionFilter* SMESHGUI_Add0DElemsOnAllNodesOp::createFilter( const int ) const
325 if ( myDlg->getSelectionType() == SEL_OBJECT )
327 // Create a filter of objects: any IDSource except the group of 0D elements
329 QList<SUIT_SelectionFilter*> filters;
330 filters.push_back( new SMESH_TypeFilter( SMESH::GROUP_0D ));
331 SMESH_LogicalFilter* not0DGroup = new SMESH_LogicalFilter( filters,
332 SMESH_LogicalFilter::LO_NOT,
333 /*takeOwnership=*/true);
334 filters[0] = not0DGroup;
335 filters.push_back( new SMESH_TypeFilter( SMESH::IDSOURCE ));
336 return new SMESH_LogicalFilter( filters,
337 SMESH_LogicalFilter::LO_AND,
338 /*takeOwnership=*/true);
343 //================================================================================
345 * \brief Makes its main job
347 //================================================================================
349 bool SMESHGUI_Add0DElemsOnAllNodesOp::onApply()
351 if ( !myDlg->isValid() )
355 SMESH::SMESH_IDSource_var meshObject;
356 SMESH::SMESH_Mesh_var mesh;
357 if ( !myIO.IsNull() )
359 CORBA::Object_var obj = SMESH::IObjectToObject( myIO );
360 meshObject = SMESH::SMESH_IDSource::_narrow( obj );
361 if ( !meshObject->_is_nil() )
362 mesh = meshObject->GetMesh();
364 if ( mesh->_is_nil() )
366 SUIT_MessageBox::warning( myDlg, tr( "SMESH_WRN_WARNING" ), tr( "SMESH_BAD_SELECTION" ) );
371 SMESH::SMESH_MeshEditor_var editor = mesh->GetMeshEditor();
373 // make SMESH_IDSource holding IDs of selected elements
374 if ( myDlg->getSelectionType() != SEL_OBJECT )
376 QString elemIDs = myDlg->objectText( 0 );
377 QStringList idList = elemIDs.split( " ", QString::SkipEmptyParts );
378 if ( idList.count() == 0 )
380 SUIT_MessageBox::warning( myDlg, tr( "SMESH_WRN_WARNING" ), tr( "SMESH_BAD_SELECTION" ) );
383 SMESH::long_array_var idArray = new SMESH::long_array;
384 idArray->length( idList.count() );
385 QStringList::iterator idIt = idList.begin();
386 for ( int i = 0; idIt != idList.end(); ++idIt, ++i )
387 idArray[i] = idIt->toLong();
388 SMESH::ElementType elemType =
389 myDlg->getSelectionType() == SEL_NODES ? SMESH::NODE : SMESH::ALL;
390 meshObject = editor->MakeIDSource( idArray, elemType );
393 // Create 0D elements
395 int prevNb0D = mesh->Nb0DElements();
397 QString groupName = myDlg->myGroupListCmBox->currentText();
398 SMESH::SMESH_IDSource_var newObj =
399 editor->Create0DElementsOnAllNodes( meshObject, groupName.toLatin1().data() );
401 int newNb0D = mesh->Nb0DElements() - prevNb0D;
402 SUIT_MessageBox::information( myDlg, tr( "SMESH_INFORMATION" ),
403 tr( "NB_NEW_0D" ).arg( newNb0D ),
404 SUIT_MessageBox::Ok, SUIT_MessageBox::Ok);
406 catch ( const SALOME::SALOME_Exception& S_ex ) {
407 SalomeApp_Tools::QtCatchCorbaException( S_ex );
416 if ( myDlg->getSelectionType() == SEL_OBJECT )
419 selectionMgr()->setSelectedObjects( aList, /*append=*/false );
423 selector()->ClearIndex();
428 SMESHGUI::Modified();
430 if ( myDlg->myGroupBox->isChecked() )
431 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
436 //================================================================================
438 * \brief Sets selection mode
440 //================================================================================
442 void SMESHGUI_Add0DElemsOnAllNodesOp::onSelTypeChange(int selType)
445 case SEL_OBJECT: setSelectionMode( ActorSelection ); break;
446 case SEL_ELEMENTS: setSelectionMode( CellSelection ); break;
447 case SEL_NODES: setSelectionMode( NodeSelection ); break;
450 if ( selType != SEL_OBJECT )
451 connect( myDlg, SIGNAL( objectChanged( int, const QStringList& )),
452 this, SLOT ( onTextChanged( int, const QStringList& )));
454 disconnect( myDlg, SIGNAL( objectChanged( int, const QStringList& )),
455 this, SLOT ( onTextChanged( int, const QStringList& )));
460 //================================================================================
466 //================================================================================
468 void SMESHGUI_Add0DElemsOnAllNodesOp::onSetFilter()
470 SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO( myIO );
471 if ( mesh->_is_nil()) {
472 SUIT_MessageBox::critical( myDlg, tr("SMESH_ERROR"), tr("NO_MESH_SELECTED"));
476 if ( myDlg->getSelectionType() == SEL_NODES ) {
477 types.push_back( SMESH::NODE );
479 else if ( myDlg->getSelectionType() == SEL_ELEMENTS ) {
480 types.push_back( SMESH::EDGE );
481 types.push_back( SMESH::FACE );
482 types.push_back( SMESH::VOLUME );
487 myFilterDlg = new SMESHGUI_FilterDlg( getSMESHGUI(), SMESH::ALL );
489 myFilterDlg->Init( types );
490 myFilterDlg->SetSelection();
491 myFilterDlg->SetMesh( mesh );
492 myFilterDlg->SetSourceWg( myDlg->objectWg( 0, LightApp_Dialog::Control ));