Salome HOME
INT PAL 0052683: Parameter "Color group" in the "Create Group" dialog box is empty
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Make2DFrom3DOp.cxx
1 // Copyright (C) 2007-2015  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 // File   : SMESHGUI_Make2DFrom3D.cxx
20 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
21
22 #include "SMESHGUI_Make2DFrom3DOp.h"
23
24 #include "SMESHGUI.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"
31
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>
45
46 // IDL includes
47 #include <SALOMEconfig.h>
48 #include CORBA_SERVER_HEADER(SMESH_Group)
49
50 // Qt includes
51 #include <QGroupBox>
52 #include <QRadioButton>
53 #include <QLineEdit>
54 #include <QCheckBox>
55 #include <QHBoxLayout>
56 #include <QGridLayout>
57 #include <QToolButton>
58
59 #include <Standard_ErrorHandler.hxx>
60
61 #define SPACING 6
62 #define MARGIN  11
63
64 /*!
65   \class SMESHGUI_Make2DFrom3DDlg
66   \brief Copy Mesh dialog box
67 */
68
69 SMESHGUI_Make2DFrom3DDlg::SMESHGUI_Make2DFrom3DDlg( QWidget* parent )
70   : SMESHGUI_Dialog( parent, false, true, OK | Apply | Close | Help )
71 {
72   // title
73   setWindowTitle( tr("CAPTION") );
74
75   // mode
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 );
86
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();
92
93   // Mesh or Groups
94   //setObjectPixmap( "SMESH", tr( "ICON_SELECT" ) );
95   createObject( tr( "Groups" ), mainFrame(), MeshOrGroups );
96   setNameIndication( MeshOrGroups, ListOfNames );
97   objectWg( MeshOrGroups, Btn )->hide();
98
99   // target
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() );
114
115   // layout
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 );
125
126   // connect signals  
127   connect( myThisMeshRB, SIGNAL( clicked() ), this, SLOT( onTargetChanged() ) );
128   connect( myNewMeshRB,  SIGNAL( clicked() ), this, SLOT( onTargetChanged() ) );
129   connect( myGroupCheck, SIGNAL( clicked() ), this, SLOT( onGroupChecked() ) );
130
131   // init dlg
132   my2dFrom3dRB->setChecked( true );
133   myThisMeshRB->setChecked( true );
134   onTargetChanged();
135   onGroupChecked();
136 }
137
138 SMESHGUI_Make2DFrom3DDlg::~SMESHGUI_Make2DFrom3DDlg()
139 {
140 }
141
142 SMESH::Bnd_Dimension SMESHGUI_Make2DFrom3DDlg::mode() const
143 {
144   if ( my2dFrom3dRB->isChecked() )
145     return SMESH::BND_2DFROM3D;
146   else if ( my1dFrom2dRB->isChecked() )
147     return SMESH::BND_1DFROM2D;
148   else
149     return SMESH::BND_1DFROM3D;
150 }
151
152 bool SMESHGUI_Make2DFrom3DDlg::needNewMesh() const
153 {
154   return myNewMeshRB->isChecked();
155 }
156
157 QString SMESHGUI_Make2DFrom3DDlg::getNewMeshName() const
158 {
159   return myMeshName->text().trimmed();
160 }
161
162 void SMESHGUI_Make2DFrom3DDlg::setNewMeshName( const QString& name )
163 {
164   myMeshName->setText( name );
165 }
166
167 void SMESHGUI_Make2DFrom3DDlg::setNewMeshEnabled( bool enable )
168 {
169   if ( !enable )
170     myThisMeshRB->setChecked( true );
171
172   myNewMeshRB->setEnabled( enable );
173
174   onTargetChanged();
175 }
176
177 bool SMESHGUI_Make2DFrom3DDlg::getNewMeshEnabled() const
178 {
179   return myNewMeshRB->isEnabled();
180 }
181
182 bool SMESHGUI_Make2DFrom3DDlg::needGroup() const
183 {
184   return myGroupCheck->isChecked();
185 }
186
187 QString SMESHGUI_Make2DFrom3DDlg::getGroupName() const
188 {
189   return myGroupName->text().trimmed();
190 }
191
192 void SMESHGUI_Make2DFrom3DDlg::setGroupName( const QString& name )
193 {
194   myGroupName->setText( name );
195 }
196
197 bool SMESHGUI_Make2DFrom3DDlg::copySource() const
198 {
199   return myCopyCheck->isChecked();
200 }
201
202 void SMESHGUI_Make2DFrom3DDlg::onTargetChanged()
203 {
204   myMeshName->setEnabled( myNewMeshRB->isChecked() );
205   myCopyCheck->setEnabled( myNewMeshRB->isChecked() );
206 }
207
208 void SMESHGUI_Make2DFrom3DDlg::onGroupChecked()
209 {
210   myGroupName->setEnabled( myGroupCheck->isChecked() );
211 }
212
213 /*!
214   \class SMESHGUI_Make2DFrom3DOp
215   \brief Copy Mesh operation class
216 */
217
218 SMESHGUI_Make2DFrom3DOp::SMESHGUI_Make2DFrom3DOp()
219   : SMESHGUI_SelectionOp(),
220     myMeshFilter(SMESH::MESH),
221     myGroupFilter(SMESH::GROUP)
222 {
223 }
224
225 SMESHGUI_Make2DFrom3DOp::~SMESHGUI_Make2DFrom3DOp()
226 {
227   if ( myDlg )
228     delete myDlg;
229 }
230
231 LightApp_Dialog* SMESHGUI_Make2DFrom3DOp::dlg() const
232 {
233   return myDlg;
234 }
235
236 void SMESHGUI_Make2DFrom3DOp::startOperation()
237 {
238   if( !myDlg )
239     myDlg = new SMESHGUI_Make2DFrom3DDlg( desktop() );
240
241   myHelpFileName = "make_2dmesh_from_3d_page.html";
242
243   SMESHGUI_SelectionOp::startOperation();
244
245   myDlg->setNewMeshName( SMESH::UniqueName( "Mesh_1" ) );
246   myDlg->setGroupName( SMESH::UniqueName( "Group" ) );
247   myDlg->show();
248
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() ) );
252
253   //onModeChanged();
254
255   myDlg->activateObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
256   selectionDone();
257 }
258
259 //================================================================================
260 /*!
261  * \brief Set filter corresponding to dimension
262  */
263 //================================================================================
264
265 void SMESHGUI_Make2DFrom3DOp::onModeChanged()
266 {
267 //   QRadioButton* b = dynamic_cast<QRadioButton*>( sender());
268 //   if ( b && !b->isChecked() )
269 //     return;
270
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 );
276   
277 //   // install filter
278 //   int id =  enableGroups ? SMESHGUI_Make2DFrom3DDlg::Groups : SMESHGUI_Make2DFrom3DDlg::Mesh;
279 //   onDeactivateObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
280 //   onActivateObject  ( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
281 //   selectionDone();
282 }
283
284 void SMESHGUI_Make2DFrom3DOp::selectionDone()
285 {
286   myDlg->clearSelection( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups );
287   mySrcMesh = SMESH::SMESH_Mesh::_nil();
288
289   if ( !dlg() ) return;
290
291   
292   if ( dlg()->isVisible() ) {
293     try {
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 );
300
301       // enable/desable "new mesh" button
302       bool isMesh = true;
303       for ( int i = 0; i < ids.count() && isMesh; ++i )
304       {
305         _PTR(SObject) sobj =
306           SMESHGUI::activeStudy()->studyDS()->FindObjectID( ids[i].toLatin1().constData() );
307         mySrcMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( sobj );  
308         isMesh = !mySrcMesh->_is_nil();
309       }
310       myDlg->setNewMeshEnabled( isMesh );
311     }
312     catch ( const SALOME::SALOME_Exception& S_ex ) {
313       SalomeApp_Tools::QtCatchCorbaException( S_ex );
314     }
315     catch ( ... ) {
316     }
317   }
318 }
319
320 SUIT_SelectionFilter* SMESHGUI_Make2DFrom3DOp::createFilter( const int theId ) const
321 {
322   SMESHGUI_Make2DFrom3DOp* me = (SMESHGUI_Make2DFrom3DOp*) this;
323
324   QList<SUIT_SelectionFilter*> subFilters;
325   subFilters.append( & me->myMeshFilter );
326   subFilters.append( & me->myGroupFilter );
327
328   SUIT_SelectionFilter* f = new SMESH_LogicalFilter( subFilters, SMESH_LogicalFilter::LO_OR );
329   return f;
330 }
331
332 bool SMESHGUI_Make2DFrom3DOp::isValid( QString& msg ) const
333 {
334   if ( !dlg() ) return false;
335
336   // check if a mesh is selected
337   if ( !myDlg->hasSelection( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups ))
338   {
339     msg = tr( "SMESH_ERR_NO_INPUT_MESH" );
340     return false;
341   }
342   QStringList entries;
343   dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
344   const bool isMeshSelected = ( !mySrcMesh->_is_nil() );
345   if ( isMeshSelected )
346   {
347     // only one mesh is allowed
348     if ( entries.size() > 1 ) {
349       msg = tr( "SMESH_TOO_MANY_MESHES" );
350       return false;
351     }
352   }
353   else
354   {
355     // check if only groups are selected
356     for ( int i = 0; i < entries.count(); ++i )
357     {
358       SMESH::SMESH_GroupBase_var grp;
359       if ( _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() ))
360         grp = SMESH::SObjectToInterface<SMESH::SMESH_GroupBase>( sobj );
361       if ( grp->_is_nil() ) {
362         msg = tr( "SMESH_NOT_ONLY_GROUPS" );
363         return false;
364       }
365     }
366   }
367   // check if the selected objects contains elements of required type
368   bool hasFaces = false, hasVolumes = false;
369   SMESH::Bnd_Dimension mode = myDlg->mode();
370   for ( int i = 0; i < entries.count(); ++i )
371   {
372     SMESH::SMESH_IDSource_var idSource;
373     if ( _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() ))
374       idSource = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );
375     if ( !idSource->_is_nil() ) {
376       SMESH::array_of_ElementType_var types = idSource->GetTypes();
377       for ( int j = 0; j < types->length(); ++j )
378         if ( types[j] == SMESH::VOLUME )
379           hasVolumes = true;
380         else if ( types[j] == SMESH::FACE )
381           hasFaces = true;
382     }
383   }
384   if ( mode == SMESH::BND_2DFROM3D && !hasVolumes ) {
385     msg = tr( "SMESH_ERR_NO_3D_ELEMENTS" );
386     return false;
387   }
388   else if ( mode == SMESH::BND_1DFROM2D && !hasFaces  ) {
389     msg = tr( "SMESH_ERR_NO_2D_ELEMENTS" );
390     return false;
391   }
392
393   // check if new mesh name is specified
394   if ( myDlg->needNewMesh() && myDlg->getNewMeshName().isEmpty() ) {
395     msg = tr( "SMESH_ERR_MESH_NAME_NOT_SPECIFIED" );
396     return false;
397   }
398
399   // check if group name is specified
400   if ( myDlg->needGroup() && myDlg->getGroupName().isEmpty() ) {
401     msg = tr( "SMESH_ERR_GRP_NAME_NOT_SPECIFIED" );
402     return false;
403   }
404
405   return true;
406 }
407
408 bool SMESHGUI_Make2DFrom3DOp::compute2DMesh( QStringList& theEntryList )
409 {
410   SUIT_OverrideCursor wc;
411
412   bool ok = false;
413   try {
414     SMESH::Bnd_Dimension mode = myDlg->mode();
415     QString meshName          = myDlg->needNewMesh() ? myDlg->getNewMeshName() : QString();
416     QString groupName         = myDlg->needGroup()   ? myDlg->getGroupName()   : QString();
417     bool copyAll              = myDlg->copySource();
418
419     QStringList entries;
420     dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
421     SMESH::ListOfIDSources_var groups = new SMESH::ListOfIDSources;
422     QString wrongGroups = "";
423
424     if ( mySrcMesh->_is_nil() ) // get selected groups, find groups of wrong type
425     {
426       int nbGroups = 0;
427       int goodType = ( mode == SMESH::BND_2DFROM3D ? SMESH::VOLUME : SMESH::FACE );
428       groups->length( entries.count() );
429       for ( int i = 0; i < entries.count(); ++i )
430       {
431         _PTR(SObject) sobj =
432           SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() );
433         SMESH::SMESH_IDSource_var grp = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );  
434         SMESH::array_of_ElementType_var types = grp->GetTypes();
435         if ( types->length() < 1 || types[0] != goodType )
436         {
437           if ( !wrongGroups.isEmpty() )
438             wrongGroups += ", ";
439           wrongGroups += sobj->GetName().c_str();
440         }
441         else
442         {
443           groups[ nbGroups++ ] = grp;
444         }
445       }
446       groups->length( nbGroups );
447       mySrcMesh = groups[0]->GetMesh();
448     }
449
450     if ( !CORBA::is_nil( mySrcMesh ) ) {
451       SMESH::SMESH_MeshEditor_var aMeshEditor = mySrcMesh->GetMeshEditor();
452       SMESH::SMESH_Group_var newGrp;
453       SMESH::SMESH_Mesh_var newMesh;
454       CORBA::Long nbAdded = aMeshEditor->MakeBoundaryElements( mode,
455                                                                groupName.toLatin1().constData(),
456                                                                meshName.toLatin1().constData(),
457                                                                copyAll,
458                                                                groups,
459                                                                newMesh.out(),
460                                                                newGrp.out() );
461       QString msg = tr("NB_ADDED").arg( nbAdded );
462       if ( !wrongGroups.isEmpty() )
463         msg += ".\n" + tr("WRONG_GROUPS").arg( wrongGroups );
464       SUIT_MessageBox::information( myDlg, tr("SMESH_INFORMATION"), msg);
465
466       if ( !newMesh->_is_nil() ) {
467         if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
468           theEntryList.append( aSObject->GetID().c_str() );
469       }
470       ok = true;
471
472       for ( int i = 0; i < entries.count(); ++i )
473         if ( SMESH_Actor* actor = SMESH::FindActorByEntry( entries[i].toLatin1().constData() ))
474           SMESH::Update(actor->getIO(),actor->GetVisibility());
475       SMESH::RepaintCurrentView();
476     }
477   }
478   catch ( ... ) {
479   }
480   return ok;
481 }
482
483 bool SMESHGUI_Make2DFrom3DOp::onApply()
484 {
485   if ( isStudyLocked() )
486     return false;
487
488   QString msg;
489   if ( !isValid( msg ) ) {
490     dlg()->show();
491     if ( msg != "" )
492       SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), msg );
493     return false;
494   }
495
496   QStringList anEntryList;
497   bool res = false;
498   try {
499     res = compute2DMesh( anEntryList );
500   }
501   catch ( const SALOME::SALOME_Exception& S_ex ) {
502     SalomeApp_Tools::QtCatchCorbaException( S_ex );
503   }
504   catch ( ... ) {
505   }
506
507   if ( res ) {
508     SMESHGUI::Modified();
509     update( UF_ObjBrowser | UF_Model );
510     if( LightApp_Application* anApp =
511         dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
512       anApp->browseObjects( anEntryList, isApplyAndClose() );
513     myDlg->setNewMeshName( SMESH::UniqueName( "Mesh_1" ) );
514     myDlg->setGroupName( SMESH::UniqueName( "Group" ) );
515   }
516   else {
517     SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), tr( "SMESH_OPERATION_FAILED" ) );
518   }
519
520   return res;
521 }