Salome HOME
Merge multi-study removal branch.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GroupOnShapeDlg.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
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)
24 // Module    : SMESH
25 // IDL Headers
26 //
27 #include "SMESHGUI_GroupOnShapeDlg.h"
28
29 #include "SMESH_TypeFilter.hxx"
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_GEOMGenUtils.h"
33
34 #include <GeometryGUI.h>
35 #include <GEOM_SelectionFilter.h>
36 #include <GEOM_wrap.hxx>
37
38 #include <SUIT_Session.h>
39 #include <SUIT_OverrideCursor.h>
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
42 #include <SUIT_ResourceMgr.h>
43
44 #include <SALOMEconfig.h>
45 #include CORBA_SERVER_HEADER(SMESH_Mesh)
46 #include CORBA_SERVER_HEADER(SMESH_Group)
47
48 #include <QGroupBox>
49 #include <QLayout>
50 #include <QFrame>
51 #include <QLabel>
52 #include <QPushButton>
53 #include <QListWidget>
54 #include <QLineEdit>
55
56 #define SPACING 6
57 #define MARGIN  11
58
59 enum { _MESH, _ELEM_GEOM, _NODE_GEOM };
60
61 SMESHGUI_GroupOnShapeDlg::SMESHGUI_GroupOnShapeDlg()
62   : SMESHGUI_Dialog( 0, false, true )
63 {
64   QPixmap image (resourceMgr()->loadPixmap("SMESH", tr("ICON_SELECT")));
65
66   // Name
67   //QLabel* nameLabel = new QLabel( tr( "SMESH_NAME" ), mainFrame() );
68   //myGrpNameLine = new QLineEdit( mainFrame() );
69
70   // Mesh
71   QLabel* meshLabel = new QLabel( tr( "SMESH_OBJECT_MESH" ), mainFrame() );
72   myMeshBtn = new QPushButton( mainFrame() );
73   myMeshBtn->setCheckable(true);
74   myMeshBtn->setIcon(image);
75   myMeshLine = new QLineEdit( mainFrame() );
76   myMeshLine->setReadOnly(true);
77
78   // Elem geom
79
80   QGroupBox* elemsGrp = new QGroupBox( tr( "SMESH_ELEMENTS" ), mainFrame() );
81   QLabel* label = new QLabel( tr( "SMESH_GEOM" ), elemsGrp );
82   myElemGeomBtn = new QPushButton( elemsGrp );
83   myElemGeomBtn->setCheckable(true);
84   myElemGeomBtn->setIcon(image);
85   myElemGeomList = new QListWidget( elemsGrp );
86   myElemGeomList->setSelectionMode(QListWidget::NoSelection);
87
88   QGridLayout* elemLay = new QGridLayout(elemsGrp);
89   elemLay->setSpacing( SPACING );
90   elemLay->setMargin( MARGIN );
91   elemLay->setRowStretch( 1, 1 );
92   elemLay->addWidget( label, 0, 0 );
93   elemLay->addWidget( myElemGeomBtn, 0, 1 );
94   elemLay->addWidget( myElemGeomList, 0, 2, 2, 1);
95
96   // Node geom
97
98   QGroupBox* nodesGrp = new QGroupBox( tr( "SMESH_NODES" ), mainFrame() );
99
100   label = new QLabel( tr( "SMESH_GEOM" ), nodesGrp );
101   myNodeGeomBtn = new QPushButton( nodesGrp );
102   myNodeGeomBtn->setCheckable(true);
103   myNodeGeomBtn->setIcon(image);
104   myNodeGeomList = new QListWidget( nodesGrp );
105   myNodeGeomList->setSelectionMode(QListWidget::NoSelection);
106
107   QGridLayout* nodeLay = new QGridLayout(nodesGrp);
108   nodeLay->setSpacing( SPACING );
109   nodeLay->setMargin( MARGIN );
110   nodeLay->setRowStretch( 1, 1 );
111   nodeLay->addWidget( label, 0, 0 );
112   nodeLay->addWidget( myNodeGeomBtn, 0, 1 );
113   nodeLay->addWidget(myNodeGeomList, 0, 2, 2, 1);
114
115   // Fill layout
116   QGridLayout* aLay = new QGridLayout( mainFrame());
117   aLay->setSpacing( SPACING );
118   aLay->setMargin( MARGIN );
119   //aLay->addWidget( nameLabel, 0, 0 );
120   //aLay->addWidget( myGrpNameLine, 0, 2 );
121   aLay->addWidget( meshLabel, 1, 0 );
122   aLay->addWidget( myMeshBtn, 1, 1 );
123   aLay->addWidget( myMeshLine,1, 2 );
124   aLay->addWidget( elemsGrp, 2, 1, 1, 3 );
125   aLay->addWidget( nodesGrp, 3, 1, 1, 3 );
126
127   setWindowTitle( tr( "SMESH_CREATE_GROUP_FROM_GEOM" ) );
128 }
129
130 SMESHGUI_GroupOnShapeDlg::~SMESHGUI_GroupOnShapeDlg()
131 {
132 }
133
134 //================================================================================
135 /*!
136  * \brief slot to enable/diable [Apply]
137  */
138 //================================================================================
139
140 void SMESHGUI_GroupOnShapeDlg::updateButtons()
141 {
142   bool enable =
143     /*!myGrpNameLine->text().isEmpty() &&*/ myElemGeomList->count() + myNodeGeomList->count();
144
145   button(Apply)->setEnabled( enable );
146   button(OK)->setEnabled( enable );
147 }
148
149 //================================================================================
150 /*!
151  * \brief initialization
152  */
153 //================================================================================
154
155 void SMESHGUI_GroupOnShapeDlg::init()
156 {
157   //myGrpNameLine->setText("");
158
159   myMeshBtn->setChecked( true );
160   myMeshLine->setText("");
161
162   myElemGeomBtn->setChecked(false);
163   myElemGeomBtn->setEnabled(false);
164   myElemGeomList->clear();
165   myNodeGeomBtn->setChecked(false);
166   myNodeGeomBtn->setEnabled(false);
167   myNodeGeomList->clear();
168
169   updateButtons();
170 }
171
172 //================================================================================
173 /*!
174  * \brief operation constructor
175  */
176 //================================================================================
177
178 SMESHGUI_GroupOnShapeOp::SMESHGUI_GroupOnShapeOp()
179   : SMESHGUI_SelectionOp(ActorSelection),
180     myDlg( 0 )
181 {
182   myHelpFileName = "create_groups_from_geometry_page.html";
183 }
184
185 SMESHGUI_GroupOnShapeOp::~SMESHGUI_GroupOnShapeOp()
186 {
187   if ( myDlg )
188     delete myDlg;
189 }
190 //================================================================================
191 /*!
192  * \brief Gets dialog of this operation
193   * \retval LightApp_Dialog* - pointer to dialog of this operation
194 */
195 //================================================================================
196
197 LightApp_Dialog* SMESHGUI_GroupOnShapeOp::dlg() const
198 {
199   return myDlg;
200 }
201
202 //================================================================================
203 /*!
204  * \brief return type of mesh group by geom object
205  */
206 //================================================================================
207
208 static SMESH::ElementType elementType(GEOM::GEOM_Object_var geom)
209 {
210   if ( !geom->_is_nil() ) {
211     switch ( geom->GetShapeType() ) {
212     case GEOM::VERTEX:   return SMESH::NODE;
213     case GEOM::EDGE:     return SMESH::EDGE;
214     case GEOM::WIRE:     return SMESH::EDGE;
215     case GEOM::FACE:     return SMESH::FACE;
216     case GEOM::SHELL:    return SMESH::FACE;
217     case GEOM::SOLID:    return SMESH::VOLUME;
218     case GEOM::COMPSOLID:return SMESH::VOLUME;
219     case GEOM::COMPOUND: break;
220     default:             return SMESH::ALL;
221     }
222     GEOM::GEOM_IShapesOperations_wrap aShapeOp =
223       SMESH::GetGEOMGen()->GetIShapesOperations();
224
225     if ( geom->GetType() == 37 ) { // geom group
226       GEOM::GEOM_IGroupOperations_wrap aGroupOp =
227         SMESH::GetGEOMGen()->GetIGroupOperations();
228       if ( !aGroupOp->_is_nil() ) {
229         // mainShape is an existing servant => GEOM_Object_var not GEOM_Object_wrap
230         GEOM::GEOM_Object_var mainShape = aGroupOp->GetMainShape( geom );
231         GEOM::ListOfLong_var        ids = aGroupOp->GetObjects( geom );
232         if ( ids->length() && !mainShape->_is_nil() && !aShapeOp->_is_nil() ) {
233           GEOM::GEOM_Object_wrap member = aShapeOp->GetSubShape( mainShape, ids[0] );
234           return elementType( member );
235         }
236       }
237     }
238     else if ( !aShapeOp->_is_nil() ) { // just a compoud shape
239       GEOM::ListOfLong_var ids = aShapeOp->SubShapeAllIDs( geom, GEOM::SHAPE, false );
240       if ( ids->length() ) {
241         GEOM::GEOM_Object_wrap member = aShapeOp->GetSubShape( geom, ids[0] );
242         return elementType( member );
243       }
244     }
245   }
246   return SMESH::ALL;
247 }
248
249 //================================================================================
250 /*!
251  * \brief initialization
252  */
253 //================================================================================
254
255 void SMESHGUI_GroupOnShapeOp::init()
256 {
257   myMeshID="";
258   myElemGeoIDs.clear();
259   myNodeGeoIDs.clear();
260
261   myDlg->init();
262   removeCustomFilters();
263   onActivateObject( _MESH ); // install filter
264 }
265
266 //================================================================================
267 /*!
268  * \brief start operation
269  */
270 //================================================================================
271
272 void SMESHGUI_GroupOnShapeOp::startOperation()
273 {
274   if (!myDlg)
275   {
276     myDlg = new SMESHGUI_GroupOnShapeDlg();
277     connect(myDlg->myMeshBtn,     SIGNAL(clicked()), this, SLOT(onButtonClick()));
278     connect(myDlg->myElemGeomBtn, SIGNAL(clicked()), this, SLOT(onButtonClick()));
279     connect(myDlg->myNodeGeomBtn, SIGNAL(clicked()), this, SLOT(onButtonClick()));
280     //connect(myDlg->myGrpNameLine, SIGNAL(textChanged(const QString&)),myDlg,SLOT(updateButtons()));
281   }
282   SMESHGUI_SelectionOp::startOperation();
283
284   init();
285   myDlg->show();
286 }
287
288 //================================================================================
289 /*!
290  * \brief create groups
291  */
292 //================================================================================
293
294 bool SMESHGUI_GroupOnShapeOp::onApply()
295 {
296   SUIT_OverrideCursor aWaitCursor;
297
298   if (SMESHGUI::isStudyLocked())
299     return false;
300
301   // study
302   _PTR(Study) aStudy = SMESH::getStudy();
303   if ( !aStudy ) return false;
304
305   // mesh
306   _PTR(SObject) meshSO = aStudy->FindObjectID( myMeshID.toLatin1().data() );
307   SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( meshSO );
308   if ( mesh->_is_nil() ) return false;
309
310   // names of all existing groups
311 //   QStringList groupNames;
312 //   QString givenName = myDlg->myGrpNameLine->text();
313 //   if ( !givenName.isEmpty() ) {
314 //     SMESH::ListOfGroups_var groups = mesh->GetGroups();
315 //     for ( int i = 0; i < groups->length(); ++i ) {
316 //       CORBA::String_var name = groups[i]->GetName();
317 //       groupNames.append( name.in() );
318 //     }
319 //   }
320
321   // create groups
322   SMESH::SMESH_GroupOnGeom_var group;
323   QStringList anEntryList;
324   for ( int isNode = 0; isNode < 2; ++isNode ) // elems and then nodes
325   {
326     QStringList::iterator geomID = isNode ? myNodeGeoIDs.begin() : myElemGeoIDs.begin();
327     QStringList::iterator geomEnd = isNode ? myNodeGeoIDs.end() : myElemGeoIDs.end();
328
329     for ( int i = 0; geomID != geomEnd; ++geomID, ++i )
330     {
331       // selected geom
332       _PTR(SObject) geomSO = aStudy->FindObjectID( geomID->toLatin1().data() );
333       GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>(geomSO);
334       if ( geom->_is_nil() ) continue;
335
336       // group type
337       SMESH::ElementType elemType = isNode ? SMESH::NODE : elementType( geom );
338       if ( elemType == SMESH::ALL )
339         continue;
340
341       // make a unique name
342       QString name =
343         isNode ? myDlg->myNodeGeomList->item(i)->text() : myDlg->myElemGeomList->item(i)->text();
344 //       int nb = 1;
345 //       QString name = myDlg->myGrpNameLine->text() + "_" + QString::number(nb);
346 //       while ( groupNames.contains( name ))
347 //         name = myDlg->myGrpNameLine->text() + "_" + QString::number(++nb);
348 //       groupNames.append( name );
349
350       //printf( "apply() %s %s\n", (*geomID).latin1(), name.latin1() );
351       group = mesh->CreateGroupFromGEOM( elemType, name.toLatin1().data(), geom );
352       if( !group->_is_nil() )
353         if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( group ) )
354           anEntryList.append( aSObject->GetID().c_str() );
355     }
356   }
357   SMESHGUI::Modified();
358
359   update( UF_ObjBrowser | UF_Model );
360
361   // Re-init controls to create the next group
362   myElemGeoIDs.clear();
363   myNodeGeoIDs.clear();
364   removeCustomFilters();
365   myDlg->myNodeGeomList->clear();
366   myDlg->myElemGeomList->clear();
367   myDlg->myElemGeomBtn->setChecked(false); 
368   myDlg->myNodeGeomBtn->setChecked(false);
369   myDlg->updateButtons();
370
371   if( LightApp_Application* anApp =
372       dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
373     anApp->browseObjects( anEntryList, isApplyAndClose() );
374
375   return !group->_is_nil();
376 }
377
378 //================================================================================
379 /*!
380  * \brief slot connected to selection buttons
381  */
382 //================================================================================
383
384 void SMESHGUI_GroupOnShapeOp::onButtonClick()
385 {
386   removeCustomFilters();
387
388   if ( sender() == myDlg->myMeshBtn && myDlg->myMeshBtn->isChecked() )
389   {
390     myDlg->myElemGeomBtn->setChecked(false);
391     myDlg->myNodeGeomBtn->setChecked(false);
392     onActivateObject( _MESH ); // install filter
393   }
394   else if ( sender() == myDlg->myElemGeomBtn && myDlg->myElemGeomBtn->isChecked() )
395   {
396     myDlg->myMeshBtn->setChecked(false);
397     myDlg->myNodeGeomBtn->setChecked(false);
398     onActivateObject( _ELEM_GEOM ); // install filter
399   }
400   else if ( sender() == myDlg->myNodeGeomBtn && myDlg->myNodeGeomBtn->isChecked() )
401   {
402     myDlg->myMeshBtn->setChecked(false);
403     myDlg->myElemGeomBtn->setChecked(false);
404     onActivateObject( _NODE_GEOM ); // install filter
405   }
406   //selectionDone();
407 }
408
409 //================================================================================
410 /*!
411  * \brief Creates selection filter
412   * \param theId - identifier of current selection widget
413   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
414  *
415  * Creates selection filter in accordance with identifier of current selection widget
416  */
417 //================================================================================
418 SUIT_SelectionFilter* SMESHGUI_GroupOnShapeOp::createFilter( const int theId ) const
419 {
420   if ( theId == _ELEM_GEOM || theId == _NODE_GEOM )
421     return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
422   else if ( theId == _MESH )
423     return new SMESH_TypeFilter( SMESH::MESH );
424   else
425     return ( SUIT_SelectionFilter*) 0;
426 }
427 //================================================================================
428 /*!
429  * \brief Updates dialog's look and feel
430  *
431  * Virtual method redefined from the base class updates dialog's look and feel
432  */
433 //================================================================================
434 void SMESHGUI_GroupOnShapeOp::selectionDone()
435 {
436   QStringList names, ids;
437   LightApp_Dialog::TypesList types;
438   selected( names, types, ids );
439   int nbSelected = names.size();
440
441   if ( myDlg->myMeshBtn->isChecked() ) // mesh selected
442   {
443     myDlg->myMeshLine->setText("");
444     myMeshID = "";
445     if ( nbSelected == 1 ) {
446       myDlg->myMeshLine->setText( names.front() );
447       myMeshID = ids.front();
448     }
449     myDlg->myElemGeomList->clear();
450     myDlg->myElemGeomBtn->setEnabled( nbSelected == 1 );
451     myDlg->myNodeGeomList->clear();
452     myDlg->myNodeGeomBtn->setEnabled( nbSelected == 1 );
453     myDlg->myElemGeomBtn->click();
454     return;
455   }
456
457   // Filter off inappropriate shapes
458
459   QStringList goodNames, goodIds;
460   if (nbSelected > 0) {
461     // study
462     if (_PTR(Study) aStudy = SMESH::getStudy()) {
463       // mesh
464       if (_PTR(SObject)  meshSO = aStudy->FindObjectID( myMeshID.toLatin1().data() )) {
465         // shape to mesh
466         _PTR(SObject) anObj, shapeToMesh;
467         if (meshSO->FindSubObject(1, anObj) && anObj->ReferencedObject(shapeToMesh)) {
468           // loop on selected
469           QStringList::iterator name = names.begin(), id = ids.begin(), idEnd = ids.end();
470           for (; id != idEnd; ++id, ++name ) {
471             // shape SO
472             if (_PTR(SObject) shapeSO = aStudy->FindObjectID( id->toLatin1().data() )) {
473             // check if shape SO is a child of shape to mesh 
474               while ( shapeSO && shapeSO->GetID() != shapeToMesh->GetID() )
475                 if  ( shapeSO->Depth() < 2 )
476                   shapeSO.reset();
477                 else
478                   shapeSO = shapeSO->GetFather();
479               if ( shapeSO ) {
480                 //printf( "selectionDone() %s %s\n", (*id).latin1(), (*name).latin1() );
481                 if ( !goodIds.contains( *id )) {
482                   goodIds.append( *id );
483                   goodNames.append( *name );
484                 }
485               }
486             }
487           }
488         }
489       }
490     }
491   }
492
493   if ( myDlg->myElemGeomBtn->isChecked() ) // elem geometry selection
494   {
495     myDlg->myElemGeomList->clear();
496     myDlg->myElemGeomList->addItems( goodNames );
497     myElemGeoIDs = goodIds;
498   }
499   else if ( myDlg->myNodeGeomBtn->isChecked() ) // Node geometry selection
500   {
501     myDlg->myNodeGeomList->clear();
502     myDlg->myNodeGeomList->addItems( goodNames );
503     myNodeGeoIDs = goodIds;
504   }
505
506   // enable/diable Apply, which can change at selection
507   myDlg->updateButtons();
508 }