1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 CEA
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SMESHGUI_GroupOnShapeDlg.cxx
24 // Created : Wed Sep 17 18:36:51 2008
25 // Author : Edward AGAPOV (eap)
30 #include "SMESHGUI_GroupOnShapeDlg.h"
32 #include "SMESH_TypeFilter.hxx"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_GEOMGenUtils.h"
36 #include <GeometryGUI.h>
37 #include <GEOM_SelectionFilter.h>
39 #include <SUIT_Session.h>
40 #include <SUIT_OverrideCursor.h>
41 #include <LightApp_UpdateFlags.h>
43 #include <SALOMEconfig.h>
44 #include CORBA_SERVER_HEADER(SMESH_Mesh)
45 #include CORBA_SERVER_HEADER(SMESH_Group)
47 #include <qgroupbox.h>
51 #include <qpushbutton.h>
53 #include <qlineedit.h>
55 enum { _MESH, _ELEM_GEOM, _NODE_GEOM };
57 SMESHGUI_GroupOnShapeDlg::SMESHGUI_GroupOnShapeDlg()
58 : SMESHGUI_Dialog( 0, false, true )
60 QPixmap image (resourceMgr()->loadPixmap("SMESH", tr("ICON_SELECT")));
62 QGroupBox* aGrp = new QGroupBox( 3, Qt::Horizontal, mainFrame() );
63 aGrp->setFrameStyle( QFrame::NoFrame );
64 aGrp->setInsideMargin( 0 );
67 //new QLabel( tr( "SMESH_NAME" ), aGrp ); new QLabel( "", aGrp );
68 //myGrpNameLine = new QLineEdit( aGrp, "myGrpNameLine" );
71 new QLabel( tr( "SMESH_OBJECT_MESH" ), aGrp );
72 myMeshBtn = new QPushButton( aGrp, "myMeshBtn");
73 myMeshBtn->setToggleButton(true);
74 myMeshBtn->setPixmap(image);
75 myMeshLine = new QLineEdit( aGrp, "myMeshLine" );
76 myMeshLine->setReadOnly(true);
80 QGroupBox* elemsGrp = new QGroupBox( 1, Qt::Horizontal, tr( "SMESH_ELEMENTS" ), mainFrame() );
81 QFrame* elemFr = new QFrame(elemsGrp, "elemFr");
83 QLabel* label = new QLabel( tr( "SMESH_OBJECT_GEOM" ), elemFr );
84 myElemGeomBtn = new QPushButton( elemFr, "myElemGeomBtn");
85 myElemGeomBtn->setToggleButton(true);
86 myElemGeomBtn->setPixmap(image);
87 myElemGeomList = new QListBox( elemFr, "myElemGeomList" );
88 myElemGeomList->setSelectionMode(QListBox::NoSelection);
90 QGridLayout* elemLay = new QGridLayout(elemFr, 2, 3);
91 elemLay->setSpacing(6);
92 elemLay->setAutoAdd(false);
93 elemLay->setRowStretch( 1, 1 );
94 elemLay->addWidget( label, 0, 0 );
95 elemLay->addWidget( myElemGeomBtn, 0, 1 );
96 elemLay->addMultiCellWidget(myElemGeomList, 0, 1, 2, 2);
100 QGroupBox* nodesGrp = new QGroupBox( 1, Qt::Horizontal, tr( "SMESH_NODES" ), mainFrame() );
101 QFrame* nodeFr = new QFrame(nodesGrp, "nodeFr");
103 label = new QLabel( tr( "SMESH_OBJECT_GEOM" ), nodeFr );
104 myNodeGeomBtn = new QPushButton( nodeFr, "myNodeGeomBtn");
105 myNodeGeomBtn->setToggleButton(true);
106 myNodeGeomBtn->setPixmap(image);
107 myNodeGeomList = new QListBox( nodeFr, "myNodeGeomList" );
108 myNodeGeomList->setSelectionMode(QListBox::NoSelection);
110 QGridLayout* nodeLay = new QGridLayout(nodeFr, 2, 3);
111 nodeLay->setSpacing(6);
112 nodeLay->setAutoAdd(false);
113 nodeLay->setRowStretch( 1, 1 );
114 nodeLay->addWidget( label, 0, 0 );
115 nodeLay->addWidget( myNodeGeomBtn, 0, 1 );
116 nodeLay->addMultiCellWidget(myNodeGeomList, 0, 1, 2, 2);
119 QVBoxLayout* aLay = new QVBoxLayout( mainFrame(), 0, 5 );
120 aLay->addWidget( aGrp );
121 aLay->addWidget( elemsGrp );
122 aLay->addWidget( nodesGrp );
124 setCaption( tr( "SMESH_CREATE_GROUP_FROM_GEOM" ) );
127 //================================================================================
129 * \brief slot to enable/diable [Apply]
131 //================================================================================
133 void SMESHGUI_GroupOnShapeDlg::updateButtons()
136 /*!myGrpNameLine->text().isEmpty() &&*/ myElemGeomList->count() + myNodeGeomList->count();
138 button(Apply)->setEnabled( enable );
139 button(OK)->setEnabled( enable );
142 //================================================================================
144 * \brief initialization
146 //================================================================================
148 void SMESHGUI_GroupOnShapeDlg::init()
150 //myGrpNameLine->setText("");
152 myMeshBtn->setOn( true );
153 myMeshLine->setText("");
155 myElemGeomBtn->setOn(false);
156 myElemGeomBtn->setEnabled(false);
157 myElemGeomList->clear();
158 myNodeGeomBtn->setOn(false);
159 myNodeGeomBtn->setEnabled(false);
160 myNodeGeomList->clear();
165 //================================================================================
167 * \brief operation constructor
169 //================================================================================
171 SMESHGUI_GroupOnShapeOp::SMESHGUI_GroupOnShapeOp()
172 : SMESHGUI_SelectionOp(ActorSelection),
175 myHelpFileName = "creating_groups_page.html";
178 SMESHGUI_GroupOnShapeOp::~SMESHGUI_GroupOnShapeOp()
183 //================================================================================
185 * \brief Gets dialog of this operation
186 * \retval LightApp_Dialog* - pointer to dialog of this operation
188 //================================================================================
190 LightApp_Dialog* SMESHGUI_GroupOnShapeOp::dlg() const
195 //================================================================================
197 * \brief return type of mesh group by geom object
199 //================================================================================
201 static SMESH::ElementType elementType(GEOM::GEOM_Object_var& geom)
203 if ( !geom->_is_nil() ) {
204 switch ( geom->GetShapeType() ) {
205 case GEOM::VERTEX: return SMESH::NODE;
206 case GEOM::EDGE: return SMESH::EDGE;
207 case GEOM::WIRE: return SMESH::EDGE;
208 case GEOM::FACE: return SMESH::FACE;
209 case GEOM::SHELL: return SMESH::FACE;
210 case GEOM::SOLID: return SMESH::VOLUME;
211 case GEOM::COMPSOLID:return SMESH::VOLUME;
212 case GEOM::COMPOUND: break;
213 default: return SMESH::ALL;
215 _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
216 GEOM::GEOM_IShapesOperations_var aShapeOp =
217 SMESH::GetGEOMGen()->GetIShapesOperations(aStudy->StudyId());
219 if ( geom->GetType() == 37 ) { // geom group
220 GEOM::GEOM_IGroupOperations_var aGroupOp =
221 SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
222 if ( !aGroupOp->_is_nil() ) {
223 GEOM::GEOM_Object_var mainShape = aGroupOp->GetMainShape( geom );
224 GEOM::ListOfLong_var ids = aGroupOp->GetObjects( geom );
225 if ( ids->length() && !mainShape->_is_nil() && !aShapeOp->_is_nil() ) {
226 GEOM::GEOM_Object_var member = aShapeOp->GetSubShape( mainShape, ids[0] );
227 return elementType( member );
231 else if ( !aShapeOp->_is_nil() ) { // just a compoud shape
232 GEOM::ListOfLong_var ids = aShapeOp->SubShapeAllIDs( geom, GEOM::SHAPE, false );
233 if ( ids->length() ) {
234 GEOM::GEOM_Object_var member = aShapeOp->GetSubShape( geom, ids[0] );
235 return elementType( member );
242 //================================================================================
244 * \brief initialization
246 //================================================================================
248 void SMESHGUI_GroupOnShapeOp::init()
251 myElemGeoIDs.clear();
252 myNodeGeoIDs.clear();
255 removeCustomFilters();
256 onActivateObject( _MESH ); // install filter
259 //================================================================================
261 * \brief start operation
263 //================================================================================
265 void SMESHGUI_GroupOnShapeOp::startOperation()
269 myDlg = new SMESHGUI_GroupOnShapeDlg();
270 connect(myDlg->myMeshBtn, SIGNAL(clicked()), this, SLOT(onButtonClick()));
271 connect(myDlg->myElemGeomBtn, SIGNAL(clicked()), this, SLOT(onButtonClick()));
272 connect(myDlg->myNodeGeomBtn, SIGNAL(clicked()), this, SLOT(onButtonClick()));
273 //connect(myDlg->myGrpNameLine, SIGNAL(textChanged(const QString&)),myDlg,SLOT(updateButtons()));
275 SMESHGUI_SelectionOp::startOperation();
281 //================================================================================
283 * \brief create groups
285 //================================================================================
287 bool SMESHGUI_GroupOnShapeOp::onApply()
289 SUIT_OverrideCursor aWaitCursor;
295 _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
296 if ( !aStudy ) return false;
299 _PTR(SObject) meshSO = aStudy->FindObjectID( myMeshID );
300 SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( meshSO );
301 if ( mesh->_is_nil() ) return false;
303 // names of all existing groups
304 // QStringList groupNames;
305 // QString givenName = myDlg->myGrpNameLine->text();
306 // if ( !givenName.isEmpty() ) {
307 // SMESH::ListOfGroups_var groups = mesh->GetGroups();
308 // for ( int i = 0; i < groups->length(); ++i ) {
309 // CORBA::String_var name = groups[i]->GetName();
310 // groupNames.append( name.in() );
315 SMESH::SMESH_GroupOnGeom_var group;
316 for ( int isNode = 0; isNode < 2; ++isNode ) // elems and then nodes
318 QStringList::iterator geomID = isNode ? myNodeGeoIDs.begin() : myElemGeoIDs.begin();
319 QStringList::iterator geomEnd = isNode ? myNodeGeoIDs.end() : myElemGeoIDs.end();
321 for (int i = 0 ; geomID != geomEnd; ++geomID, ++i )
324 _PTR(SObject) geomSO = aStudy->FindObjectID( *geomID );
325 GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>(geomSO);
326 if ( geom->_is_nil() ) continue;
329 SMESH::ElementType elemType = isNode ? SMESH::NODE : elementType( geom );
330 if ( elemType == SMESH::ALL )
335 //if ( givenName.isEmpty() ) { // use shape name
336 name = isNode ? myDlg->myNodeGeomList->text(i) : myDlg->myElemGeomList->text(i);
338 // else { // make a unique name
341 // if ( myNodeGeoIDs.size() + myElemGeoIDs.size() > 1 )
342 // name += "_" + QString::number(++nb);
343 // while ( groupNames.contains( name ))
344 // name = givenName + "_" + QString::number(++nb);
345 // groupNames.append( name );
347 //printf( "apply() %s %s\n", (*geomID).latin1(), name.latin1() );
348 group = mesh->CreateGroupFromGEOM( elemType, name, geom );
351 update( UF_ObjBrowser | UF_Model );
355 return !group->_is_nil();
358 //================================================================================
360 * \brief slot connected to selection buttons
362 //================================================================================
364 void SMESHGUI_GroupOnShapeOp::onButtonClick()
366 removeCustomFilters();
368 if ( sender() == myDlg->myMeshBtn && myDlg->myMeshBtn->isOn() )
370 myDlg->myElemGeomBtn->setOn(false);
371 myDlg->myNodeGeomBtn->setOn(false);
372 onActivateObject( _MESH ); // install filter
374 else if ( sender() == myDlg->myElemGeomBtn && myDlg->myElemGeomBtn->isOn() )
376 myDlg->myMeshBtn->setOn(false);
377 myDlg->myNodeGeomBtn->setOn(false);
378 onActivateObject( _ELEM_GEOM ); // install filter
380 else if ( sender() == myDlg->myNodeGeomBtn && myDlg->myNodeGeomBtn->isOn() )
382 myDlg->myMeshBtn->setOn(false);
383 myDlg->myElemGeomBtn->setOn(false);
384 onActivateObject( _NODE_GEOM ); // install filter
389 //================================================================================
391 * \brief Creates selection filter
392 * \param theId - identifier of current selection widget
393 * \retval SUIT_SelectionFilter* - pointer to the created filter or null
395 * Creates selection filter in accordance with identifier of current selection widget
397 //================================================================================
398 SUIT_SelectionFilter* SMESHGUI_GroupOnShapeOp::createFilter( const int theId ) const
400 if ( theId == _ELEM_GEOM || theId == _NODE_GEOM )
401 return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
402 else if ( theId == _MESH )
403 return new SMESH_TypeFilter( MESH );
405 return ( SUIT_SelectionFilter*) 0;
407 //================================================================================
409 * \brief Updates dialog's look and feel
411 * Virtual method redefined from the base class updates dialog's look and feel
413 //================================================================================
414 void SMESHGUI_GroupOnShapeOp::selectionDone()
416 QStringList names, ids;
417 LightApp_Dialog::TypesList types;
418 selected( names, types, ids );
419 int nbSelected = names.size();
421 if ( myDlg->myMeshBtn->isOn() ) // mesh selected
423 myDlg->myMeshLine->setText("");
425 if ( nbSelected == 1 ) {
426 myDlg->myMeshLine->setText( names.front() );
427 myMeshID = ids.front();
429 myDlg->myElemGeomList->clear();
430 myDlg->myElemGeomBtn->setEnabled( nbSelected == 1 );
431 myDlg->myNodeGeomList->clear();
432 myDlg->myNodeGeomBtn->setEnabled( nbSelected == 1 );
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 )) {
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 )) {
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->isOn() ) // elem geomerty selection
474 myDlg->myElemGeomList->clear();
475 myDlg->myElemGeomList->insertStringList( goodNames );
476 myElemGeoIDs = goodIds;
478 else if ( myDlg->myNodeGeomBtn->isOn() ) // Node geomerty selection
480 myDlg->myNodeGeomList->clear();
481 myDlg->myNodeGeomList->insertStringList( goodNames );
482 myNodeGeoIDs = goodIds;
485 // enable/diable Apply, which can change at selection
486 myDlg->updateButtons();