Salome HOME
IPAL21957: Max Element Area does not influence on resulting mesh for Triangle Mefisto
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Make2DFrom3DOp.cxx
1 // Copyright (C) 2007-2011  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.
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(MESH),
221     myGroupFilter(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       myDlg->selectObject( names, types, ids );
298
299       // enable/desable "new mesh" button
300       bool isMesh = true;
301       for ( int i = 0; i < ids.count() && isMesh; ++i )
302       {
303         _PTR(SObject) sobj =
304           SMESHGUI::activeStudy()->studyDS()->FindObjectID( ids[i].toLatin1().constData() );
305         mySrcMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( sobj );  
306         isMesh = !mySrcMesh->_is_nil();
307       }
308       myDlg->setNewMeshEnabled( isMesh );
309     }
310     catch ( const SALOME::SALOME_Exception& S_ex ) {
311       SalomeApp_Tools::QtCatchCorbaException( S_ex );
312     }
313     catch ( ... ) {
314     }
315   }
316 }
317
318 SUIT_SelectionFilter* SMESHGUI_Make2DFrom3DOp::createFilter( const int theId ) const
319 {
320   SMESHGUI_Make2DFrom3DOp* me = (SMESHGUI_Make2DFrom3DOp*) this;
321
322   QList<SUIT_SelectionFilter*> subFilters;
323   subFilters.append( & me->myMeshFilter );
324   subFilters.append( & me->myGroupFilter );
325
326   SUIT_SelectionFilter* f = new SMESH_LogicalFilter( subFilters, SMESH_LogicalFilter::LO_OR );
327   return f;
328 }
329
330 bool SMESHGUI_Make2DFrom3DOp::isValid( QString& msg ) const
331 {
332   if ( !dlg() ) return false;
333
334   // check if a mesh is selected
335   if ( !myDlg->hasSelection( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups ))
336   {
337     msg = tr( "SMESH_ERR_NO_INPUT_MESH" );
338     return false;
339   }
340   QStringList entries;
341   dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
342   const bool isMeshSelected = ( !mySrcMesh->_is_nil() );
343   if ( isMeshSelected )
344   {
345     // only one mesh is allowed
346     if ( entries.size() > 1 ) {
347       msg = tr( "SMESH_TOO_MANY_MESHES" );
348       return false;
349     }
350   }
351   else
352   {
353     // check if only groups are selected
354     for ( int i = 0; i < entries.count(); ++i )
355     {
356       SMESH::SMESH_GroupBase_var grp;
357       if ( _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() ))
358         grp = SMESH::SObjectToInterface<SMESH::SMESH_GroupBase>( sobj );
359       if ( grp->_is_nil() ) {
360         msg = tr( "SMESH_NOT_ONLY_GROUPS" );
361         return false;
362       }
363     }
364   }
365   // check if the selected objects contains elements of required type
366   bool hasFaces = false, hasVolumes = false;
367   SMESH::Bnd_Dimension mode = myDlg->mode();
368   for ( int i = 0; i < entries.count(); ++i )
369   {
370     SMESH::SMESH_IDSource_var idSource;
371     if ( _PTR(SObject) sobj = SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() ))
372       idSource = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );
373     if ( !idSource->_is_nil() ) {
374       SMESH::array_of_ElementType_var types = idSource->GetTypes();
375       for ( int j = 0; j < types->length(); ++j )
376         if ( types[j] == SMESH::VOLUME )
377           hasVolumes = true;
378         else if ( types[j] == SMESH::FACE )
379           hasFaces = true;
380     }
381   }
382   if ( mode == SMESH::BND_2DFROM3D && !hasVolumes ) {
383     msg = tr( "SMESH_ERR_NO_3D_ELEMENTS" );
384     return false;
385   }
386   else if ( mode == SMESH::BND_1DFROM2D && !hasFaces  ) {
387     msg = tr( "SMESH_ERR_NO_2D_ELEMENTS" );
388     return false;
389   }
390
391   // check if new mesh name is specified
392   if ( myDlg->needNewMesh() && myDlg->getNewMeshName().isEmpty() ) {
393     msg = tr( "SMESH_ERR_MESH_NAME_NOT_SPECIFIED" );
394     return false;
395   }
396
397   // check if group name is specified
398   if ( myDlg->needGroup() && myDlg->getGroupName().isEmpty() ) {
399     msg = tr( "SMESH_ERR_GRP_NAME_NOT_SPECIFIED" );
400     return false;
401   }
402
403   return true;
404 }
405
406 bool SMESHGUI_Make2DFrom3DOp::compute2DMesh( QStringList& theEntryList )
407 {
408   SUIT_OverrideCursor wc;
409
410   bool ok = false;
411   try {
412     SMESH::Bnd_Dimension mode = myDlg->mode();
413     QString meshName          = myDlg->needNewMesh() ? myDlg->getNewMeshName() : QString();
414     QString groupName         = myDlg->needGroup()   ? myDlg->getGroupName()   : QString();
415     bool copyAll              = myDlg->copySource();
416
417     QStringList entries;
418     dlg()->selectedObject( SMESHGUI_Make2DFrom3DDlg::MeshOrGroups, entries );
419     SMESH::ListOfIDSources_var groups = new SMESH::ListOfIDSources;
420     QString wrongGroups = "";
421
422     if ( mySrcMesh->_is_nil() ) // get selected groups, find groups of wrong type
423     {
424       int nbGroups = 0;
425       int goodType = ( mode == SMESH::BND_2DFROM3D ? SMESH::VOLUME : SMESH::FACE );
426       groups->length( entries.count() );
427       for ( int i = 0; i < entries.count(); ++i )
428       {
429         _PTR(SObject) sobj =
430           SMESHGUI::activeStudy()->studyDS()->FindObjectID( entries[i].toLatin1().constData() );
431         SMESH::SMESH_IDSource_var grp = SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( sobj );  
432         SMESH::array_of_ElementType_var types = grp->GetTypes();
433         if ( types->length() < 1 || types[0] != goodType )
434         {
435           if ( !wrongGroups.isEmpty() )
436             wrongGroups += ", ";
437           wrongGroups += sobj->GetName().c_str();
438         }
439         else
440         {
441           groups[ nbGroups++ ] = grp;
442         }
443       }
444       groups->length( nbGroups );
445       mySrcMesh = groups[0]->GetMesh();
446     }
447
448     if ( !CORBA::is_nil( mySrcMesh ) ) {
449       SMESH::SMESH_MeshEditor_var aMeshEditor = mySrcMesh->GetMeshEditor();
450       SMESH::SMESH_Group_var newGrp;
451       SMESH::SMESH_Mesh_var newMesh;
452       CORBA::Long nbAdded = aMeshEditor->MakeBoundaryElements( mode,
453                                                                groupName.toLatin1().constData(),
454                                                                meshName.toLatin1().constData(),
455                                                                copyAll,
456                                                                groups,
457                                                                newMesh.out(),
458                                                                newGrp.out() );
459       QString msg = tr("NB_ADDED").arg( nbAdded );
460       if ( !wrongGroups.isEmpty() )
461         msg += ".\n" + tr("WRONG_GROUPS").arg( wrongGroups );
462       SUIT_MessageBox::information( myDlg, tr("SMESH_INFORMATION"), msg);
463
464       if ( !newMesh->_is_nil() ) {
465         if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( newMesh ) )
466           theEntryList.append( aSObject->GetID().c_str() );
467 #ifdef WITHGENERICOBJ
468         newMesh->UnRegister();
469 #endif
470       }
471       if ( !newGrp->_is_nil() ) {
472 #ifdef WITHGENERICOBJ
473         newGrp->UnRegister();
474 #endif
475       }
476       ok = true;
477
478       for ( int i = 0; i < entries.count(); ++i )
479         if ( SMESH_Actor* actor = SMESH::FindActorByEntry( entries[i].toLatin1().constData() ))
480           SMESH::Update(actor->getIO(),actor->GetVisibility());
481       SMESH::RepaintCurrentView();
482     }
483   }
484   catch ( ... ) {
485   }
486   return ok;
487 }
488
489 bool SMESHGUI_Make2DFrom3DOp::onApply()
490 {
491   if ( isStudyLocked() )
492     return false;
493
494   QString msg;
495   if ( !isValid( msg ) ) {
496     dlg()->show();
497     if ( msg != "" )
498       SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), msg );
499     return false;
500   }
501
502   QStringList anEntryList;
503   bool res = false;
504   try {
505     res = compute2DMesh( anEntryList );
506   }
507   catch ( const SALOME::SALOME_Exception& S_ex ) {
508     SalomeApp_Tools::QtCatchCorbaException( S_ex );
509   }
510   catch ( ... ) {
511   }
512
513   if ( res ) {
514     SMESHGUI::Modified();
515     update( UF_ObjBrowser | UF_Model );
516     if( LightApp_Application* anApp =
517         dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
518       anApp->browseObjects( anEntryList, isApplyAndClose() );
519     myDlg->setNewMeshName( SMESH::UniqueName( "Mesh_1" ) );
520     myDlg->setGroupName( SMESH::UniqueName( "Group" ) );
521   }
522   else {
523     SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), tr( "SMESH_OPERATION_FAILED" ) );
524   }
525
526   return res;
527 }