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
20 // SMESH SMESHGUI : GUI for SMESH component
21 // File : SMESHGUI_GroupOnShapeDlg.cxx
22 // Created : Wed Sep 17 18:36:51 2008
23 // Author : Edward AGAPOV (eap)
27 #include "SMESHGUI_GroupOnShapeDlg.h"
29 #include "SMESH_TypeFilter.hxx"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_GEOMGenUtils.h"
33 #include <GeometryGUI.h>
34 #include <GEOM_SelectionFilter.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_OverrideCursor.h>
38 #include <LightApp_UpdateFlags.h>
39 #include <SUIT_ResourceMgr.h>
41 #include <SALOMEconfig.h>
42 #include CORBA_SERVER_HEADER(SMESH_Mesh)
43 #include CORBA_SERVER_HEADER(SMESH_Group)
49 #include <QPushButton>
50 #include <QListWidget>
56 enum { _MESH, _ELEM_GEOM, _NODE_GEOM };
58 SMESHGUI_GroupOnShapeDlg::SMESHGUI_GroupOnShapeDlg()
59 : SMESHGUI_Dialog( 0, false, true )
61 QPixmap image (resourceMgr()->loadPixmap("SMESH", tr("ICON_SELECT")));
64 //QLabel* nameLabel = new QLabel( tr( "SMESH_NAME" ), mainFrame() );
65 //myGrpNameLine = new QLineEdit( mainFrame() );
68 QLabel* meshLabel = new QLabel( tr( "SMESH_OBJECT_MESH" ), mainFrame() );
69 myMeshBtn = new QPushButton( mainFrame() );
70 myMeshBtn->setCheckable(true);
71 myMeshBtn->setIcon(image);
72 myMeshLine = new QLineEdit( mainFrame() );
73 myMeshLine->setReadOnly(true);
77 QGroupBox* elemsGrp = new QGroupBox( tr( "SMESH_ELEMENTS" ), mainFrame() );
78 QLabel* label = new QLabel( tr( "SMESH_GEOM" ), elemsGrp );
79 myElemGeomBtn = new QPushButton( elemsGrp );
80 myElemGeomBtn->setCheckable(true);
81 myElemGeomBtn->setIcon(image);
82 myElemGeomList = new QListWidget( elemsGrp );
83 myElemGeomList->setSelectionMode(QListWidget::NoSelection);
85 QGridLayout* elemLay = new QGridLayout(elemsGrp);
86 elemLay->setSpacing( SPACING );
87 elemLay->setMargin( MARGIN );
88 elemLay->setRowStretch( 1, 1 );
89 elemLay->addWidget( label, 0, 0 );
90 elemLay->addWidget( myElemGeomBtn, 0, 1 );
91 elemLay->addWidget( myElemGeomList, 0, 2, 2, 1);
95 QGroupBox* nodesGrp = new QGroupBox( tr( "SMESH_NODES" ), mainFrame() );
97 label = new QLabel( tr( "SMESH_GEOM" ), nodesGrp );
98 myNodeGeomBtn = new QPushButton( nodesGrp );
99 myNodeGeomBtn->setCheckable(true);
100 myNodeGeomBtn->setIcon(image);
101 myNodeGeomList = new QListWidget( nodesGrp );
102 myNodeGeomList->setSelectionMode(QListWidget::NoSelection);
104 QGridLayout* nodeLay = new QGridLayout(nodesGrp);
105 nodeLay->setSpacing( SPACING );
106 nodeLay->setMargin( MARGIN );
107 nodeLay->setRowStretch( 1, 1 );
108 nodeLay->addWidget( label, 0, 0 );
109 nodeLay->addWidget( myNodeGeomBtn, 0, 1 );
110 nodeLay->addWidget(myNodeGeomList, 0, 2, 2, 1);
113 QGridLayout* aLay = new QGridLayout( mainFrame());
114 aLay->setSpacing( SPACING );
115 aLay->setMargin( MARGIN );
116 //aLay->addWidget( nameLabel, 0, 0 );
117 //aLay->addWidget( myGrpNameLine, 0, 2 );
118 aLay->addWidget( meshLabel, 1, 0 );
119 aLay->addWidget( myMeshBtn, 1, 1 );
120 aLay->addWidget( myMeshLine,1, 2 );
121 aLay->addWidget( elemsGrp, 2, 1, 1, 3 );
122 aLay->addWidget( nodesGrp, 3, 1, 1, 3 );
124 setWindowTitle( tr( "SMESH_CREATE_GROUP_FROM_GEOM" ) );
127 SMESHGUI_GroupOnShapeDlg::~SMESHGUI_GroupOnShapeDlg()
131 //================================================================================
133 * \brief slot to enable/diable [Apply]
135 //================================================================================
137 void SMESHGUI_GroupOnShapeDlg::updateButtons()
140 /*!myGrpNameLine->text().isEmpty() &&*/ myElemGeomList->count() + myNodeGeomList->count();
142 button(Apply)->setEnabled( enable );
143 button(OK)->setEnabled( enable );
146 //================================================================================
148 * \brief initialization
150 //================================================================================
152 void SMESHGUI_GroupOnShapeDlg::init()
154 //myGrpNameLine->setText("");
156 myMeshBtn->setChecked( true );
157 myMeshLine->setText("");
159 myElemGeomBtn->setChecked(false);
160 myElemGeomBtn->setEnabled(false);
161 myElemGeomList->clear();
162 myNodeGeomBtn->setChecked(false);
163 myNodeGeomBtn->setEnabled(false);
164 myNodeGeomList->clear();
169 //================================================================================
171 * \brief operation constructor
173 //================================================================================
175 SMESHGUI_GroupOnShapeOp::SMESHGUI_GroupOnShapeOp()
176 : SMESHGUI_SelectionOp(ActorSelection),
179 myHelpFileName = "create_groups_from_geometry_page.html";
182 SMESHGUI_GroupOnShapeOp::~SMESHGUI_GroupOnShapeOp()
187 //================================================================================
189 * \brief Gets dialog of this operation
190 * \retval LightApp_Dialog* - pointer to dialog of this operation
192 //================================================================================
194 LightApp_Dialog* SMESHGUI_GroupOnShapeOp::dlg() const
199 //================================================================================
201 * \brief return type of mesh group by geom object
203 //================================================================================
205 static SMESH::ElementType elementType(GEOM::GEOM_Object_var& geom)
207 if ( !geom->_is_nil() ) {
208 switch ( geom->GetShapeType() ) {
209 case GEOM::VERTEX: return SMESH::NODE;
210 case GEOM::EDGE: return SMESH::EDGE;
211 case GEOM::WIRE: return SMESH::EDGE;
212 case GEOM::FACE: return SMESH::FACE;
213 case GEOM::SHELL: return SMESH::FACE;
214 case GEOM::SOLID: return SMESH::VOLUME;
215 case GEOM::COMPSOLID:return SMESH::VOLUME;
216 case GEOM::COMPOUND: break;
217 default: return SMESH::ALL;
219 _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
220 GEOM::GEOM_IShapesOperations_var aShapeOp =
221 SMESH::GetGEOMGen()->GetIShapesOperations(aStudy->StudyId());
223 if ( geom->GetType() == 37 ) { // geom group
224 GEOM::GEOM_IGroupOperations_var aGroupOp =
225 SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
226 if ( !aGroupOp->_is_nil() ) {
227 GEOM::GEOM_Object_var mainShape = aGroupOp->GetMainShape( geom );
228 GEOM::ListOfLong_var ids = aGroupOp->GetObjects( geom );
229 if ( ids->length() && !mainShape->_is_nil() && !aShapeOp->_is_nil() ) {
230 GEOM::GEOM_Object_var member = aShapeOp->GetSubShape( mainShape, ids[0] );
231 return elementType( member );
235 else if ( !aShapeOp->_is_nil() ) { // just a compoud shape
236 GEOM::ListOfLong_var ids = aShapeOp->SubShapeAllIDs( geom, GEOM::SHAPE, false );
237 if ( ids->length() ) {
238 GEOM::GEOM_Object_var member = aShapeOp->GetSubShape( geom, ids[0] );
239 return elementType( member );
246 //================================================================================
248 * \brief initialization
250 //================================================================================
252 void SMESHGUI_GroupOnShapeOp::init()
255 myElemGeoIDs.clear();
256 myNodeGeoIDs.clear();
259 removeCustomFilters();
260 onActivateObject( _MESH ); // install filter
263 //================================================================================
265 * \brief start operation
267 //================================================================================
269 void SMESHGUI_GroupOnShapeOp::startOperation()
273 myDlg = new SMESHGUI_GroupOnShapeDlg();
274 connect(myDlg->myMeshBtn, SIGNAL(clicked()), this, SLOT(onButtonClick()));
275 connect(myDlg->myElemGeomBtn, SIGNAL(clicked()), this, SLOT(onButtonClick()));
276 connect(myDlg->myNodeGeomBtn, SIGNAL(clicked()), this, SLOT(onButtonClick()));
277 //connect(myDlg->myGrpNameLine, SIGNAL(textChanged(const QString&)),myDlg,SLOT(updateButtons()));
279 SMESHGUI_SelectionOp::startOperation();
285 //================================================================================
287 * \brief create groups
289 //================================================================================
291 bool SMESHGUI_GroupOnShapeOp::onApply()
293 SUIT_OverrideCursor aWaitCursor;
299 _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
300 if ( !aStudy ) return false;
303 _PTR(SObject) meshSO = aStudy->FindObjectID( myMeshID.toLatin1().data() );
304 SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( meshSO );
305 if ( mesh->_is_nil() ) return false;
307 // names of all existing groups
308 // QStringList groupNames;
309 // QString givenName = myDlg->myGrpNameLine->text();
310 // if ( !givenName.isEmpty() ) {
311 // SMESH::ListOfGroups_var groups = mesh->GetGroups();
312 // for ( int i = 0; i < groups->length(); ++i ) {
313 // CORBA::String_var name = groups[i]->GetName();
314 // groupNames.append( name.in() );
319 SMESH::SMESH_GroupOnGeom_var group;
320 for ( int isNode = 0; isNode < 2; ++isNode ) // elems and then nodes
322 QStringList::iterator geomID = isNode ? myNodeGeoIDs.begin() : myElemGeoIDs.begin();
323 QStringList::iterator geomEnd = isNode ? myNodeGeoIDs.end() : myElemGeoIDs.end();
325 for ( int i = 0; geomID != geomEnd; ++geomID, ++i )
328 _PTR(SObject) geomSO = aStudy->FindObjectID( geomID->toLatin1().data() );
329 GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>(geomSO);
330 if ( geom->_is_nil() ) continue;
333 SMESH::ElementType elemType = isNode ? SMESH::NODE : elementType( geom );
334 if ( elemType == SMESH::ALL )
337 // make a unique name
339 isNode ? myDlg->myNodeGeomList->item(i)->text() : myDlg->myElemGeomList->item(i)->text();
341 // QString name = myDlg->myGrpNameLine->text() + "_" + QString::number(nb);
342 // while ( groupNames.contains( name ))
343 // name = myDlg->myGrpNameLine->text() + "_" + QString::number(++nb);
344 // groupNames.append( name );
346 //printf( "apply() %s %s\n", (*geomID).latin1(), name.latin1() );
347 group = mesh->CreateGroupFromGEOM( elemType, name.toLatin1().data(), geom );
350 update( UF_ObjBrowser | UF_Model );
354 return !group->_is_nil();
357 //================================================================================
359 * \brief slot connected to selection buttons
361 //================================================================================
363 void SMESHGUI_GroupOnShapeOp::onButtonClick()
365 removeCustomFilters();
367 if ( sender() == myDlg->myMeshBtn && myDlg->myMeshBtn->isChecked() )
369 myDlg->myElemGeomBtn->setChecked(false);
370 myDlg->myNodeGeomBtn->setChecked(false);
371 onActivateObject( _MESH ); // install filter
373 else if ( sender() == myDlg->myElemGeomBtn && myDlg->myElemGeomBtn->isChecked() )
375 myDlg->myMeshBtn->setChecked(false);
376 myDlg->myNodeGeomBtn->setChecked(false);
377 onActivateObject( _ELEM_GEOM ); // install filter
379 else if ( sender() == myDlg->myNodeGeomBtn && myDlg->myNodeGeomBtn->isChecked() )
381 myDlg->myMeshBtn->setChecked(false);
382 myDlg->myElemGeomBtn->setChecked(false);
383 onActivateObject( _NODE_GEOM ); // install filter
388 //================================================================================
390 * \brief Creates selection filter
391 * \param theId - identifier of current selection widget
392 * \retval SUIT_SelectionFilter* - pointer to the created filter or null
394 * Creates selection filter in accordance with identifier of current selection widget
396 //================================================================================
397 SUIT_SelectionFilter* SMESHGUI_GroupOnShapeOp::createFilter( const int theId ) const
399 if ( theId == _ELEM_GEOM || theId == _NODE_GEOM )
400 return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
401 else if ( theId == _MESH )
402 return new SMESH_TypeFilter( MESH );
404 return ( SUIT_SelectionFilter*) 0;
406 //================================================================================
408 * \brief Updates dialog's look and feel
410 * Virtual method redefined from the base class updates dialog's look and feel
412 //================================================================================
413 void SMESHGUI_GroupOnShapeOp::selectionDone()
415 QStringList names, ids;
416 LightApp_Dialog::TypesList types;
417 selected( names, types, ids );
418 int nbSelected = names.size();
420 if ( myDlg->myMeshBtn->isChecked() ) // mesh selected
422 myDlg->myMeshLine->setText("");
424 if ( nbSelected == 1 ) {
425 myDlg->myMeshLine->setText( names.front() );
426 myMeshID = ids.front();
428 myDlg->myElemGeomList->clear();
429 myDlg->myElemGeomBtn->setEnabled( nbSelected == 1 );
430 myDlg->myNodeGeomList->clear();
431 myDlg->myNodeGeomBtn->setEnabled( nbSelected == 1 );
432 myDlg->myElemGeomBtn->click();
436 // Filter off inappropriate shapes
438 QStringList goodNames, goodIds;
439 if (nbSelected > 0) {
441 if (_PTR(Study) aStudy = SMESH::GetActiveStudyDocument()) {
443 if (_PTR(SObject) meshSO = aStudy->FindObjectID( myMeshID.toLatin1().data() )) {
445 _PTR(SObject) anObj, shapeToMesh;
446 if (meshSO->FindSubObject(1, anObj) && anObj->ReferencedObject(shapeToMesh)) {
448 QStringList::iterator name = names.begin(), id = ids.begin(), idEnd = ids.end();
449 for (; id != idEnd; ++id, ++name ) {
451 if (_PTR(SObject) shapeSO = aStudy->FindObjectID( id->toLatin1().data() )) {
452 // check if shape SO is a child of shape to mesh
453 while ( shapeSO && shapeSO->GetID() != shapeToMesh->GetID() )
454 if ( shapeSO->Depth() < 2 )
457 shapeSO = shapeSO->GetFather();
459 //printf( "selectionDone() %s %s\n", (*id).latin1(), (*name).latin1() );
460 if ( !goodIds.contains( *id )) {
461 goodIds.append( *id );
462 goodNames.append( *name );
472 if ( myDlg->myElemGeomBtn->isChecked() ) // elem geomerty selection
474 myDlg->myElemGeomList->clear();
475 myDlg->myElemGeomList->addItems( goodNames );
476 myElemGeoIDs = goodIds;
478 else if ( myDlg->myNodeGeomBtn->isChecked() ) // Node geomerty selection
480 myDlg->myNodeGeomList->clear();
481 myDlg->myNodeGeomList->addItems( goodNames );
482 myNodeGeoIDs = goodIds;
485 // enable/diable Apply, which can change at selection
486 myDlg->updateButtons();