Salome HOME
53225: Viscous Layers hyp: problem with selection of input/output faces
[modules/smesh.git] / src / SMESHGUI / SMESHGUI.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SMESHGUI.cxx
23 //  Author : Nicolas REJNERI, Open CASCADE S.A.S.
24
25 #include <Standard_math.hxx>  // E.A. must be included before Python.h to fix compilation on windows
26 #ifdef HAVE_FINITE
27 #undef HAVE_FINITE            // VSR: avoid compilation warning on Linux : "HAVE_FINITE" redefined
28 #endif
29 #include "Python.h"
30
31 //  SMESH includes
32 #include "SMESHGUI.h"
33 #include "SMESHGUI_Add0DElemsOnAllNodesDlg.h"
34 #include "SMESHGUI_AddMeshElementDlg.h"
35 #include "SMESHGUI_AddQuadraticElementDlg.h"
36 #include "SMESHGUI_BuildCompoundDlg.h"
37 #include "SMESHGUI_ClippingDlg.h"
38 #include "SMESHGUI_ComputeDlg.h"
39 #include "SMESHGUI_ConvToQuadOp.h"
40 #include "SMESHGUI_CopyMeshDlg.h"
41 #include "SMESHGUI_CreatePolyhedralVolumeDlg.h"
42 #include "SMESHGUI_DeleteGroupDlg.h"
43 #include "SMESHGUI_Displayer.h"
44 #include "SMESHGUI_DuplicateNodesDlg.h"
45 #include "SMESHGUI_ExtrusionAlongPathDlg.h"
46 #include "SMESHGUI_ExtrusionDlg.h"
47 #include "SMESHGUI_FieldSelectorWdg.h"
48 #include "SMESHGUI_FileInfoDlg.h"
49 #include "SMESHGUI_FileValidator.h"
50 #include "SMESHGUI_FilterDlg.h"
51 #include "SMESHGUI_FilterLibraryDlg.h"
52 #include "SMESHGUI_FindElemByPointDlg.h"
53 #include "SMESHGUI_GroupDlg.h"
54 #include "SMESHGUI_GroupOnShapeDlg.h"
55 #include "SMESHGUI_GroupOpDlg.h"
56 #include "SMESHGUI_Hypotheses.h"
57 #include "SMESHGUI_Make2DFrom3DOp.h"
58 #include "SMESHGUI_MakeNodeAtPointDlg.h"
59 #include "SMESHGUI_Measurements.h"
60 #include "SMESHGUI_MergeDlg.h"
61 #include "SMESHGUI_MeshInfo.h"
62 #include "SMESHGUI_MeshOp.h"
63 #include "SMESHGUI_MeshOrderOp.h"
64 #include "SMESHGUI_MeshPatternDlg.h"
65 #include "SMESHGUI_MultiEditDlg.h"
66 #include "SMESHGUI_NodesDlg.h"
67 #include "SMESHGUI_Operations.h"
68 #include "SMESHGUI_Preferences_ScalarBarDlg.h"
69 #include "SMESHGUI_PropertiesDlg.h"
70 #include "SMESHGUI_RemoveElementsDlg.h"
71 #include "SMESHGUI_RemoveNodesDlg.h"
72 #include "SMESHGUI_RenumberingDlg.h"
73 #include "SMESHGUI_ReorientFacesDlg.h"
74 #include "SMESHGUI_RevolutionDlg.h"
75 #include "SMESHGUI_RotationDlg.h"
76 #include "SMESHGUI_ScaleDlg.h"
77 #include "SMESHGUI_Selection.h"
78 #include "SMESHGUI_SewingDlg.h"
79 #include "SMESHGUI_SingleEditDlg.h"
80 #include "SMESHGUI_SmoothingDlg.h"
81 #include "SMESHGUI_SymmetryDlg.h"
82 #include "SMESHGUI_TranslationDlg.h"
83 #include "SMESHGUI_TransparencyDlg.h"
84 #include "SMESHGUI_DisplayEntitiesDlg.h"
85 #include "SMESHGUI_SplitBiQuad.h"
86
87 #include "SMESHGUI_FilterUtils.h"
88 #include "SMESHGUI_GEOMGenUtils.h"
89 #include "SMESHGUI_GroupUtils.h"
90 #include "SMESHGUI_HypothesesUtils.h"
91 #include "SMESHGUI_MeshUtils.h"
92 #include "SMESHGUI_PatternUtils.h"
93 #include "SMESHGUI_Utils.h"
94 #include "SMESHGUI_VTKUtils.h"
95
96 #include "SMESH_version.h"
97
98 #include "SMESH_ControlsDef.hxx"
99 #include "SMESH_Actor.h"
100 #include "SMESH_ActorUtils.h"
101 #include "SMESH_Client.hxx"
102 #include "SMESH_ScalarBarActor.h"
103 #include "SMESH_TypeFilter.hxx"
104
105 // SALOME GUI includes
106 #include <SalomeApp_Application.h>
107 #include <SalomeApp_CheckFileDlg.h>
108 #include <SalomeApp_DataObject.h>
109 #include <SalomeApp_Study.h>
110 #include <SalomeApp_Tools.h>
111
112 #include <LightApp_DataOwner.h>
113 #include <LightApp_NameDlg.h>
114 #include <LightApp_Preferences.h>
115 #include <LightApp_SelectionMgr.h>
116 #include <LightApp_UpdateFlags.h>
117
118 #include <SVTK_ViewManager.h>
119 #include <SVTK_ViewModel.h>
120 #include <SVTK_ViewWindow.h>
121
122 #include <VTKViewer_Algorithm.h>
123
124 #include <SUIT_Desktop.h>
125 #include <SUIT_FileDlg.h>
126 #include <SUIT_MessageBox.h>
127 #include <SUIT_OverrideCursor.h>
128 #include <SUIT_ResourceMgr.h>
129 #include <SUIT_Session.h>
130
131 #include <QtxPopupMgr.h>
132 #include <QtxFontEdit.h>
133
134 #include <SALOME_ListIO.hxx>
135
136 #ifndef DISABLE_PLOT2DVIEWER
137 #include <SPlot2d_ViewModel.h>
138 #include <SPlot2d_Histogram.h>
139 #endif
140
141 // IDL includes
142 #include <SALOMEconfig.h>
143 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
144 #include CORBA_CLIENT_HEADER(SMESH_MeshEditor)
145 #include CORBA_CLIENT_HEADER(SMESH_Measurements)
146
147 // Qt includes
148 // #define       INCLUDE_MENUITEM_DEF // VSR commented ????????
149 #include <QApplication>
150 #include <QMenu>
151 #include <QTextStream>
152 #include <QListView>
153 #include <QTreeView>
154
155 // BOOST includes
156 #include <boost/shared_ptr.hpp>
157
158 // VTK includes
159 #include <vtkCallbackCommand.h>
160 #include <vtkCamera.h>
161 #include <vtkLookupTable.h>
162 #include <vtkPlane.h>
163 #include <vtkRenderer.h>
164
165 // SALOME KERNEL includes
166 #include <SALOMEDSClient_ClientFactory.hxx>
167 #include <SALOMEDSClient_IParameters.hxx>
168 #include <SALOMEDSClient_SComponent.hxx>
169 #include <SALOMEDSClient_StudyBuilder.hxx>
170 #include <SALOMEDS_Study.hxx>
171 #include <SALOMEDS_SObject.hxx>
172 #include "utilities.h"
173
174 // OCCT includes
175 #include <Standard_ErrorHandler.hxx>
176 #include <NCollection_DataMap.hxx>
177 #include <NCollection_DoubleMap.hxx>
178
179 #include <Basics_Utils.hxx>
180
181 // Below macro, when uncommented, switches on simplified (more performant) algorithm
182 // of auto-color picking up
183 #define SIMPLE_AUTOCOLOR
184
185 namespace
186 {
187   // Declarations
188   //=============================================================
189   void ImportMeshesFromFile(SMESH::SMESH_Gen_ptr theComponentMesh,
190                             int                  theCommandID);
191
192   void ExportMeshToFile(int theCommandID);
193
194   void SetDisplayMode(int theCommandID, SMESHGUI_StudyId2MarkerMap& theMarkerMap);
195
196   void SetDisplayEntity(int theCommandID);
197
198   int ActionToControl( int theID, bool theReversed = false );
199
200   void Control( int theCommandID );
201
202   // Definitions
203   //================================================================================
204   /*!
205    * \brief Reads meshes from file
206    */
207   //================================================================================
208
209   void ImportMeshesFromFile( SMESH::SMESH_Gen_ptr theComponentMesh,
210                              int                  theCommandID )
211   {
212     QStringList filter;
213     std::string myExtension;
214
215     if ( theCommandID == SMESHOp::OpImportMED ) {
216       filter.append( QObject::tr( "MED_FILES_FILTER" ) + " (*.*med)" );
217       filter.append( QObject::tr( "ALL_FILES_FILTER" ) + " (*)" );
218     }
219     else if ( theCommandID == SMESHOp::OpImportUNV ) {
220       filter.append( QObject::tr( "IDEAS_FILES_FILTER" ) + " (*.unv)" );
221     }
222     else if ( theCommandID == SMESHOp::OpImportDAT ) {
223       filter.append( QObject::tr( "DAT_FILES_FILTER" ) + " (*.dat)" );
224     }
225     else if ( theCommandID == SMESHOp::OpImportSTL ) {
226       filter.append( QObject::tr( "STL_FILES_FILTER" ) + " (*.stl)" );
227     }
228   #ifdef WITH_CGNS
229     else if ( theCommandID == SMESHOp::OpImportCGNS ) {
230       filter.append( QObject::tr( "CGNS_FILES_FILTER" ) + " (*.cgns)" );
231     }
232   #endif
233     else if ( theCommandID == SMESHOp::OpImportSAUV ) {
234       filter.append( QObject::tr( "SAUV files (*.sauv*)" ) );
235       filter.append( QObject::tr( "All files (*)" ) );
236     }
237     else if ( theCommandID == SMESHOp::OpImportGMF ) {
238       filter.append( QObject::tr( "GMF_ASCII_FILES_FILTER" ) + " (*.mesh)"  );
239       filter.append( QObject::tr( "GMF_BINARY_FILES_FILTER") + " (*.meshb)" );
240     }
241
242     QString anInitialPath = "";
243     if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
244       anInitialPath = QDir::currentPath();
245
246     QStringList filenames;
247     bool toCreateGroups = true;
248
249     // if ( theCommandID == SMESHOp::OpImportGMF ) { // GMF
250     //   SalomeApp_CheckFileDlg* fd = new SalomeApp_CheckFileDlg
251     //     ( SMESHGUI::desktop(), true, QObject::tr("SMESH_REQUIRED_GROUPS"), true, true );
252     //   fd->setWindowTitle( QObject::tr( "SMESH_IMPORT_MESH" ) );
253     //   fd->setNameFilters( filter );
254     //   fd->SetChecked( true );
255     //   if ( fd->exec() )
256     //     filenames << fd->selectedFile();
257     //   toCreateGroups = fd->IsChecked();
258
259     //   delete fd;
260     // }
261     // else
262     {
263       filenames = SUIT_FileDlg::getOpenFileNames( SMESHGUI::desktop(),
264                                                   anInitialPath,
265                                                   filter,
266                                                   QObject::tr( "SMESH_IMPORT_MESH" ) );
267     }
268     if ( filenames.count() > 0 ) {
269       SUIT_OverrideCursor wc;
270       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
271
272       QStringList errors;
273       QStringList anEntryList;
274       bool isEmpty = false;
275       for ( QStringList::ConstIterator it = filenames.begin(); it != filenames.end(); ++it ) {
276         QString filename = *it;
277         SMESH::mesh_array_var aMeshes = new SMESH::mesh_array;
278         try {
279           switch ( theCommandID ) {
280           case SMESHOp::OpImportDAT:
281             {
282               // DAT format (currently unsupported)
283               errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
284                              arg( QObject::tr( "SMESH_ERR_NOT_SUPPORTED_FORMAT" ) ) );
285               break;
286             }
287           case SMESHOp::OpImportUNV:
288             {
289               // UNV format
290               aMeshes->length( 1 );
291               aMeshes[0] = theComponentMesh->CreateMeshesFromUNV( filename.toUtf8().constData() );
292               if ( aMeshes[0]->_is_nil() )
293                 errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
294                                arg( QObject::tr( "SMESH_ERR_UNKNOWN_IMPORT_ERROR" ) ) );
295               break;
296             }
297           case SMESHOp::OpImportMED:
298             {
299               // MED format
300               SMESH::DriverMED_ReadStatus res;
301               aMeshes = theComponentMesh->CreateMeshesFromMED( filename.toUtf8().constData(), res );
302               if ( res != SMESH::DRS_OK ) {
303                 errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
304                                arg( QObject::tr( QString( "SMESH_DRS_%1" ).arg( res ).toLatin1().data() ) ) );
305               }
306               break;
307             }
308           case SMESHOp::OpImportSTL:
309             {
310               // STL format
311               aMeshes->length( 1 );
312               aMeshes[0] = theComponentMesh->CreateMeshesFromSTL( filename.toUtf8().constData() );
313               if ( aMeshes[0]->_is_nil() ) {
314                 errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
315                                arg( QObject::tr( "SMESH_ERR_UNKNOWN_IMPORT_ERROR" ) ) );
316               }
317               break;
318             }
319         #ifdef WITH_CGNS
320           case SMESHOp::OpImportCGNS:
321             {
322               // CGNS format
323               SMESH::DriverMED_ReadStatus res;
324               aMeshes = theComponentMesh->CreateMeshesFromCGNS( filename.toUtf8().constData(), res );
325               if ( res != SMESH::DRS_OK ) {
326                 errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
327                                arg( QObject::tr( QString( "SMESH_DRS_%1" ).arg( res ).toLatin1().data() ) ) );
328               }
329               break;
330             }
331         #endif
332           case SMESHOp::OpImportSAUV:
333             {
334               // SAUV format
335               SMESH::DriverMED_ReadStatus res;
336               aMeshes = theComponentMesh->CreateMeshesFromSAUV( filename.toUtf8().constData(), res );
337               if ( res != SMESH::DRS_OK ) {
338                 errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
339                                arg( QObject::tr( QString( "SMESH_DRS_%1" ).arg( res ).toLatin1().data() ) ) );
340               }
341               break;
342             }
343           case SMESHOp::OpImportGMF:
344             {
345               // GMF format
346               SMESH::ComputeError_var res;
347               aMeshes->length( 1 );
348               aMeshes[0] = theComponentMesh->CreateMeshesFromGMF( filename.toUtf8().constData(),
349                                                                   toCreateGroups,
350                                                                   res.out() );
351               if ( res->code != SMESH::DRS_OK ) {
352                 errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
353                                arg( QObject::tr( QString( "SMESH_DRS_%1" ).arg( res->code ).toLatin1().data() ) ) );
354                 if ( strlen( res->comment.in() ) > 0 ) {
355                   errors.back() += ": ";
356                   errors.back() += res->comment.in();
357                 }
358               }
359               break;
360             }
361           }
362         }
363         catch ( const SALOME::SALOME_Exception& S_ex ) {
364           errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
365                          arg( QObject::tr( "SMESH_ERR_UNKNOWN_IMPORT_ERROR" ) ) );
366         }
367
368         for ( int i = 0, iEnd = aMeshes->length(); i < iEnd; i++ ) {
369           _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshes[i] );
370           if ( aMeshSO ) {
371             _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
372             _PTR(AttributePixMap) aPixmap = aBuilder->FindOrCreateAttribute( aMeshSO, "AttributePixMap" );
373             aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH_IMPORTED" );
374             if ( theCommandID == SMESHOp::OpImportUNV ) // mesh names aren't taken from the file for UNV import
375               SMESH::SetName( aMeshSO, QFileInfo(filename).fileName() );
376
377             anEntryList.append( aMeshSO->GetID().c_str() );
378           }
379           else {
380             isEmpty = true;
381           }
382         }
383       }
384
385       // update Object browser
386       SMESHGUI::GetSMESHGUI()->updateObjBrowser();
387
388       // browse to the published meshes
389       if( LightApp_Application* anApp =
390           dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
391         anApp->browseObjects( anEntryList );
392
393       // show Error message box if there were errors
394       if ( errors.count() > 0 ) {
395         SUIT_MessageBox::critical( SMESHGUI::desktop(),
396                                    QObject::tr( "SMESH_ERROR" ),
397                                    QObject::tr( "SMESH_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
398       }
399
400       // show warning message box, if some imported mesh is empty
401       if ( isEmpty ) {
402           SUIT_MessageBox::warning( SMESHGUI::desktop(),
403                                     QObject::tr( "SMESH_WRN_WARNING" ),
404                                     QObject::tr( "SMESH_DRS_SOME_EMPTY" ) );
405       }
406     }
407   }
408
409   //================================================================================
410   /*!
411    * \brief Export selected meshes or groups into a file
412    */
413   //================================================================================
414
415   void ExportMeshToFile( int theCommandID )
416   {
417     LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
418     SALOME_ListIO selected;
419     if( aSel )
420       aSel->selectedObjects( selected );
421
422     const bool isDAT = ( theCommandID == SMESHOp::OpExportDAT ||
423                          theCommandID == SMESHOp::OpPopupExportDAT );
424     const bool isMED = ( theCommandID == SMESHOp::OpExportMED ||
425                          theCommandID == SMESHOp::OpPopupExportMED );
426     const bool isUNV = ( theCommandID == SMESHOp::OpExportUNV ||
427                          theCommandID == SMESHOp::OpPopupExportUNV );
428     const bool isSTL = ( theCommandID == SMESHOp::OpExportSTL ||
429                          theCommandID == SMESHOp::OpPopupExportSTL );
430 #ifdef WITH_CGNS
431     const bool isCGNS= ( theCommandID == SMESHOp::OpExportCGNS ||
432                          theCommandID == SMESHOp::OpPopupExportCGNS );
433 #else
434     const bool isCGNS= false;
435 #endif
436     const bool isSAUV= ( theCommandID == SMESHOp::OpExportSAUV ||
437                          theCommandID == SMESHOp::OpPopupExportSAUV );
438     const bool isGMF = ( theCommandID == SMESHOp::OpExportGMF ||
439                          theCommandID == SMESHOp::OpPopupExportGMF );
440
441     const bool multiMeshSupported = ( isMED || isCGNS ); // file can hold several meshes
442     if ( selected.Extent() == 0 || ( selected.Extent() > 1 && !multiMeshSupported ))
443       return;
444
445     // get mesh object from selection and check duplication of their names
446     bool hasDuplicatedMeshNames = false;
447     QList< QPair< SMESH::SMESH_IDSource_var, QString > >           aMeshList;
448     QList< QPair< SMESH::SMESH_IDSource_var, QString > >::iterator aMeshIter;
449     SALOME_ListIteratorOfListIO It( selected );
450     for( ; It.More(); It.Next() )
451     {
452       Handle(SALOME_InteractiveObject) anIObject = It.Value();
453       SMESH::SMESH_IDSource_var aMeshItem =
454         SMESH::IObjectToInterface<SMESH::SMESH_IDSource>(anIObject);
455       if ( aMeshItem->_is_nil() ) {
456         SUIT_MessageBox::warning( SMESHGUI::desktop(),
457                                   QObject::tr( "SMESH_WRN_WARNING" ),
458                                   QObject::tr( "SMESH_BAD_MESH_SELECTION" ));
459         return;
460       }
461
462       QString aMeshName = anIObject->getName();
463
464       // check for name duplications
465       if ( !hasDuplicatedMeshNames )
466         for( aMeshIter = aMeshList.begin(); aMeshIter != aMeshList.end(); aMeshIter++ ) {
467           if( aMeshName == (*aMeshIter).second ) {
468             hasDuplicatedMeshNames = true;
469             break;
470           }
471         }
472
473       aMeshList.append( QPair< SMESH::SMESH_IDSource_var, QString >( aMeshItem, aMeshName ) );
474     }
475
476     if( hasDuplicatedMeshNames && isMED ) {
477       int aRet = SUIT_MessageBox::warning(SMESHGUI::desktop(),
478                                           QObject::tr("SMESH_WRN_WARNING"),
479                                           QObject::tr("SMESH_EXPORT_MED_DUPLICATED_MESH_NAMES"),
480                                           QObject::tr("SMESH_BUT_YES"),
481                                           QObject::tr("SMESH_BUT_NO"), 0, 1);
482       if (aRet != 0)
483         return;
484     }
485
486     aMeshIter = aMeshList.begin();
487     SMESH::SMESH_IDSource_var aMeshOrGroup = (*aMeshIter).first;
488     SMESH::SMESH_Mesh_var            aMesh = aMeshOrGroup->GetMesh();
489     QString                      aMeshName = (*aMeshIter).second;
490
491     if ( isMED || isCGNS || isSAUV ) // formats where group names must be unique
492     {
493       // check for equal group names within each mesh
494       for( aMeshIter = aMeshList.begin(); aMeshIter != aMeshList.end(); aMeshIter++ ) {
495         SMESH::SMESH_Mesh_var aMeshItem = SMESH::SMESH_Mesh::_narrow( (*aMeshIter).first );
496         if ( !aMeshItem->_is_nil() && aMeshItem->HasDuplicatedGroupNamesMED()) {
497           int aRet = SUIT_MessageBox::warning
498             (SMESHGUI::desktop(),
499              QObject::tr("SMESH_WRN_WARNING"),
500              QObject::tr("SMESH_EXPORT_MED_DUPLICATED_GRP").arg((*aMeshIter).second),
501              QObject::tr("SMESH_BUT_YES"),
502              QObject::tr("SMESH_BUT_NO"), 0, 1);
503           if (aRet != 0)
504             return;
505         }
506       }
507     }
508     
509     // Warn the user about presence of not supported elements
510     QString format;
511     std::vector< SMESH::EntityType > notSupportedElemTypes, presentNotSupported;
512     if ( isDAT )
513     {
514       format = "DAT";
515       notSupportedElemTypes.push_back( SMESH::Entity_0D );
516       notSupportedElemTypes.push_back( SMESH::Entity_Ball );
517     }
518     else if ( isUNV )
519     {
520       format = "UNV";
521       notSupportedElemTypes.push_back( SMESH::Entity_Polygon );
522       notSupportedElemTypes.push_back( SMESH::Entity_Quad_Polygon );
523       notSupportedElemTypes.push_back( SMESH::Entity_Polyhedra );
524       notSupportedElemTypes.push_back( SMESH::Entity_Quad_Polyhedra );
525       notSupportedElemTypes.push_back( SMESH::Entity_Pyramid );
526       notSupportedElemTypes.push_back( SMESH::Entity_Hexagonal_Prism );
527       notSupportedElemTypes.push_back( SMESH::Entity_0D );
528       notSupportedElemTypes.push_back( SMESH::Entity_Ball );
529     }
530     else if ( isSTL )
531     {
532       format = "STL";
533       notSupportedElemTypes.push_back( SMESH::Entity_Edge );
534       notSupportedElemTypes.push_back( SMESH::Entity_Quad_Edge );
535       notSupportedElemTypes.push_back( SMESH::Entity_0D );
536       notSupportedElemTypes.push_back( SMESH::Entity_Ball );
537     }
538     else if ( isCGNS )
539     {
540       format = "CGNS";
541       notSupportedElemTypes.push_back( SMESH::Entity_Ball );
542     }
543     else if ( isSAUV )
544     {
545       format = "SAUV";
546       notSupportedElemTypes.push_back( SMESH::Entity_Ball );
547       notSupportedElemTypes.push_back( SMESH::Entity_BiQuad_Triangle );
548       notSupportedElemTypes.push_back( SMESH::Entity_BiQuad_Quadrangle );
549       notSupportedElemTypes.push_back( SMESH::Entity_TriQuad_Hexa );
550       notSupportedElemTypes.push_back( SMESH::Entity_Hexagonal_Prism );
551       notSupportedElemTypes.push_back( SMESH::Entity_Polygon );
552       notSupportedElemTypes.push_back( SMESH::Entity_Quad_Polygon );
553       notSupportedElemTypes.push_back( SMESH::Entity_Polyhedra );
554     }
555     else if ( isGMF )
556     {
557       format = "GMF";
558       notSupportedElemTypes.push_back( SMESH::Entity_0D );
559       notSupportedElemTypes.push_back( SMESH::Entity_Polygon );
560       notSupportedElemTypes.push_back( SMESH::Entity_Quad_Polygon );
561       notSupportedElemTypes.push_back( SMESH::Entity_Quad_Pyramid );
562       notSupportedElemTypes.push_back( SMESH::Entity_Quad_Penta );
563       notSupportedElemTypes.push_back( SMESH::Entity_Hexagonal_Prism );
564       notSupportedElemTypes.push_back( SMESH::Entity_Polyhedra );
565       notSupportedElemTypes.push_back( SMESH::Entity_Quad_Polyhedra );
566       notSupportedElemTypes.push_back( SMESH::Entity_Ball );
567     }
568     if ( ! notSupportedElemTypes.empty() )
569     {
570       SMESH::long_array_var nbElems = aMeshOrGroup->GetMeshInfo();
571       for ( size_t iType = 0; iType < notSupportedElemTypes.size(); ++iType )
572         if ( nbElems[ notSupportedElemTypes[ iType ]] > 0 )
573           presentNotSupported.push_back( notSupportedElemTypes[ iType ]);
574     }
575     if ( !presentNotSupported.empty() )
576     {
577       QString typeNames;
578       const char* typeMsg[] = {
579         "SMESH_NODES", "SMESH_ELEMS0D","SMESH_EDGES","SMESH_QUADRATIC_EDGES",
580         "SMESH_TRIANGLES", "SMESH_QUADRATIC_TRIANGLES", "SMESH_BIQUADRATIC_TRIANGLES",
581         "SMESH_QUADRANGLES","SMESH_QUADRATIC_QUADRANGLES", "SMESH_BIQUADRATIC_QUADRANGLES",
582         "SMESH_POLYGONS","SMESH_QUADRATIC_POLYGONS",
583         "SMESH_TETRAHEDRA","SMESH_QUADRATIC_TETRAHEDRONS","SMESH_PYRAMIDS",
584         "SMESH_QUADRATIC_PYRAMIDS","SMESH_HEXAHEDRA","SMESH_QUADRATIC_HEXAHEDRONS",
585         "SMESH_TRIQUADRATIC_HEXAHEDRONS","SMESH_PENTAHEDRA","SMESH_QUADRATIC_PENTAHEDRONS",
586         "SMESH_OCTAHEDRA","SMESH_POLYEDRONS","SMESH_QUADRATIC_POLYEDRONS","SMESH_BALLS"
587       };
588       // is typeMsg complete? (compilation failure mains that enum SMDSAbs_EntityType changed)
589       const int nbTypes = sizeof( typeMsg ) / sizeof( const char* );
590       int _assert[( nbTypes == SMESH::Entity_Last ) ? 1 : -1 ]; _assert[0]=1;
591
592       QString andStr = " " + QObject::tr("SMESH_AND") + " ", comma(", ");
593       for ( size_t iType = 0; iType < presentNotSupported.size(); ++iType ) {
594         typeNames += QObject::tr( typeMsg[ presentNotSupported[ iType ]]);
595         if ( iType != presentNotSupported.size() - 1 )
596           typeNames += ( iType == presentNotSupported.size() - 2 ) ? andStr : comma;
597       }
598       int aRet = SUIT_MessageBox::warning
599         (SMESHGUI::desktop(),
600          QObject::tr("SMESH_WRN_WARNING"),
601          QObject::tr("EXPORT_NOT_SUPPORTED").arg(aMeshName).arg(format).arg(typeNames),
602          QObject::tr("SMESH_BUT_YES"),
603          QObject::tr("SMESH_BUT_NO"), 0, 1);
604       if (aRet != 0)
605         return;
606     }
607
608     // Get parameters of export operation
609
610     QString            aFilename;
611     SMESH::MED_VERSION aFormat;
612     // Init the parameters with the default values
613     bool aIsASCII_STL   = true;
614     bool toCreateGroups = false;
615     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
616     if ( resMgr )
617       toCreateGroups = resMgr->booleanValue( "SMESH", "auto_groups", false );
618     bool toOverwrite  = true;
619     bool toFindOutDim = true;
620
621     QString aFilter, aTitle = QObject::tr("SMESH_EXPORT_MESH");
622     QString anInitialPath = "";
623     if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
624       anInitialPath = QDir::currentPath();
625
626     QList< QPair< GEOM::ListOfFields_var, QString > > aFieldList;
627
628     // Get a file name to write in and additional otions
629     if ( isUNV || isDAT || isGMF ) // Export w/o options
630     {
631       if ( isUNV )
632         aFilter = QObject::tr( "IDEAS_FILES_FILTER" ) + " (*.unv)";
633       else if ( isDAT )
634         aFilter = QObject::tr( "DAT_FILES_FILTER" ) + " (*.dat)";
635       else if ( isGMF )
636         aFilter = QObject::tr( "GMF_ASCII_FILES_FILTER" ) + " (*.mesh)" +
637           ";;" +  QObject::tr( "GMF_BINARY_FILES_FILTER" )  + " (*.meshb)";
638      if ( anInitialPath.isEmpty() ) anInitialPath = SUIT_FileDlg::getLastVisitedPath();
639       aFilename = SUIT_FileDlg::getFileName(SMESHGUI::desktop(),
640                                             anInitialPath + QString("/") + aMeshName,
641                                             aFilter, aTitle, false);
642     }
643     else if ( isCGNS )// Export to CGNS
644     {
645       SUIT_FileDlg* fd = new SUIT_FileDlg( SMESHGUI::desktop(), false, true, true );
646       fd->setWindowTitle( aTitle );
647       fd->setNameFilter( QObject::tr( "CGNS_FILES_FILTER" ) + " (*.cgns)" );
648       if ( !anInitialPath.isEmpty() )
649         fd->setDirectory( anInitialPath );
650       fd->selectFile(aMeshName);
651       SMESHGUI_FileValidator* fv = new SMESHGUI_FileValidator( fd );
652       fd->setValidator( fv );
653
654       if ( fd->exec() )
655         aFilename = fd->selectedFile();
656       toOverwrite = fv->isOverwrite();
657
658       delete fd;
659     }
660     else if ( isSTL ) // Export to STL
661     {
662       QMap<QString, int> aFilterMap;
663       aFilterMap.insert( QObject::tr( "STL_ASCII_FILES_FILTER" ) + " (*.stl)", 1 );
664       aFilterMap.insert( QObject::tr( "STL_BIN_FILES_FILTER" )   + " (*.stl)", 0 );
665
666       QStringList filters;
667       QMap<QString, int>::const_iterator it = aFilterMap.begin();
668       for ( ; it != aFilterMap.end(); ++it )
669         filters.push_back( it.key() );
670
671       SUIT_FileDlg* fd = new SUIT_FileDlg( SMESHGUI::desktop(), false, true, true );
672       fd->setWindowTitle( aTitle );
673       fd->setNameFilters( filters );
674       fd->selectNameFilter( QObject::tr( "STL_ASCII_FILES_FILTER" ) + " (*.stl)" );
675       if ( !anInitialPath.isEmpty() )
676         fd->setDirectory( anInitialPath );
677       fd->selectFile(aMeshName);
678       bool is_ok = false;
679       while (!is_ok) {
680         if ( fd->exec() )
681           aFilename = fd->selectedFile();
682         aIsASCII_STL = (aFilterMap[fd->selectedNameFilter()]) == 1 ? true: false;
683         is_ok = true;
684       }
685       delete fd;
686     }
687     else if ( isMED || isSAUV ) // Export to MED or SAUV
688     {
689       QMap<QString, SMESH::MED_VERSION> aFilterMap;
690       //QString v21 (aMesh->GetVersionString(SMESH::MED_V2_1, 2));
691       if ( isMED ) {
692         QString v22 (aMesh->GetVersionString(SMESH::MED_V2_2, 2));
693         //aFilterMap.insert( QObject::tr( "MED_VX_FILES_FILTER" ).arg( v21 ) + " (*.med)", SMESH::MED_V2_1 );
694         aFilterMap.insert( QObject::tr( "MED_VX_FILES_FILTER" ).arg( v22 ) + " (*.med)", SMESH::MED_V2_2 );
695       }
696       else { // isSAUV
697         aFilterMap.insert("All files (*)", SMESH::MED_V2_1 );
698         aFilterMap.insert("SAUV files (*.sauv)", SMESH::MED_V2_2 );
699         aFilterMap.insert("SAUV files (*.sauve)", SMESH::MED_V2_1 );
700       }
701
702       QStringList filters;
703       QString aDefaultFilter;
704       QMap<QString, SMESH::MED_VERSION>::const_iterator it = aFilterMap.begin();
705       for ( ; it != aFilterMap.end(); ++it ) {
706         filters.push_back( it.key() );
707         if (it.value() == SMESH::MED_V2_2)
708           aDefaultFilter = it.key();
709       }
710       QStringList checkBoxes;
711       checkBoxes << QObject::tr("SMESH_AUTO_GROUPS") << QObject::tr("SMESH_AUTO_DIM");
712
713       SMESHGUI_FieldSelectorWdg* fieldSelWdg = new SMESHGUI_FieldSelectorWdg();
714       QList< QWidget* > wdgList;
715       if ( fieldSelWdg->GetAllFeilds( aMeshList, aFieldList ))
716         wdgList.append( fieldSelWdg );
717
718       SalomeApp_CheckFileDlg* fd =
719         new SalomeApp_CheckFileDlg ( SMESHGUI::desktop(), false, checkBoxes, true, true, wdgList );
720       fd->setWindowTitle( aTitle );
721       fd->setNameFilters( filters );
722       fd->selectNameFilter( aDefaultFilter );
723       fd->SetChecked( toCreateGroups, 0 );
724       fd->SetChecked( toFindOutDim,   1 );
725       if ( !anInitialPath.isEmpty() )
726         fd->setDirectory( anInitialPath );
727       fd->selectFile(aMeshName);
728       
729       
730       QListView *lview = fd->findChild<QListView*>("listView");
731       if( lview ) {
732         lview->setMinimumHeight(200);
733       }
734       QTreeView *tview = fd->findChild<QTreeView*>("treeView");
735       if( tview ) {
736         tview->setMinimumHeight(200);
737       }
738
739       SMESHGUI_FileValidator* fv = new SMESHGUI_FileValidator( fd );
740       fd->setValidator( fv );
741
742       bool is_ok = false;
743       while (!is_ok) {
744         if ( fd->exec() )
745           aFilename = fd->selectedFile();
746         else {
747           aFilename = QString::null;
748           break;
749         }
750         aFormat = aFilterMap[fd->selectedNameFilter()];
751         toOverwrite = fv->isOverwrite();
752         is_ok = true;
753         if ( !aFilename.isEmpty() ) {
754           // med-2.1 does not support poly elements
755           if ( aFormat==SMESH::MED_V2_1 )
756             for( aMeshIter = aMeshList.begin(); aMeshIter != aMeshList.end(); aMeshIter++ ) {
757               SMESH::SMESH_IDSource_var aMeshItem = (*aMeshIter).first;
758               SMESH::long_array_var nbElems = aMeshItem->GetMeshInfo();
759               if ( nbElems[ SMESH::Entity_Polygon   ] + nbElems[ SMESH::Entity_Quad_Polygon   ] +
760                    nbElems[ SMESH::Entity_Polyhedra ] + nbElems[ SMESH::Entity_Quad_Polyhedra ])
761               {
762                 int aRet = SUIT_MessageBox::warning(SMESHGUI::desktop(),
763                                                     QObject::tr("SMESH_WRN_WARNING"),
764                                                     QObject::tr("SMESH_EXPORT_MED_V2_1").arg((*aMeshIter).second),
765                                                     QObject::tr("SMESH_BUT_YES"),
766                                                     QObject::tr("SMESH_BUT_NO"), 0, 1);
767                 if (aRet != 0) {
768                   is_ok = false;
769                   break;
770                 }
771               }
772             }
773           if( !toOverwrite ) {
774             // can't append to an existing using other format
775             SMESH::MED_VERSION aVersion = SMESH::MED_V2_1;
776             bool isVersionOk = SMESHGUI::GetSMESHGen()->GetMEDVersion( aFilename.toUtf8().constData(), aVersion );
777             if( !isVersionOk || aVersion != aFormat ) {
778               int aRet = SUIT_MessageBox::warning(SMESHGUI::desktop(),
779                                                   QObject::tr("SMESH_WRN_WARNING"),
780                                                   QObject::tr("SMESH_EXPORT_MED_VERSION_COLLISION").arg(aFilename),
781                                                   QObject::tr("SMESH_BUT_YES"),
782                                                   QObject::tr("SMESH_BUT_NO"), 0, 1);
783               if (aRet == 0)
784                 toOverwrite = true;
785               else
786                 is_ok = false;
787             }
788
789             QStringList aMeshNamesCollisionList;
790             SMESH::string_array_var aMeshNames = SMESHGUI::GetSMESHGen()->GetMeshNames( aFilename.toUtf8().constData() );
791             for( int i = 0, n = aMeshNames->length(); i < n; i++ ) {
792               QString anExistingMeshName( aMeshNames[ i ] );
793               for( aMeshIter = aMeshList.begin(); aMeshIter != aMeshList.end(); aMeshIter++ ) {
794                 QString anExportMeshName = (*aMeshIter).second;
795                 if( anExportMeshName == anExistingMeshName ) {
796                   aMeshNamesCollisionList.append( anExportMeshName );
797                   break;
798                 }
799               }
800             }
801             if( !aMeshNamesCollisionList.isEmpty() ) {
802               QString aMeshNamesCollisionString = aMeshNamesCollisionList.join( ", " );
803               int aRet = SUIT_MessageBox::warning(SMESHGUI::desktop(),
804                                                   QObject::tr("SMESH_WRN_WARNING"),
805                                                   QObject::tr("SMESH_EXPORT_MED_MESH_NAMES_COLLISION").arg(aMeshNamesCollisionString),
806                                                   QObject::tr("SMESH_BUT_YES"),
807                                                   QObject::tr("SMESH_BUT_NO"),
808                                                   QObject::tr("SMESH_BUT_CANCEL"), 0, 2);
809               if (aRet == 0)
810                 toOverwrite = true;
811               else if (aRet == 2)
812                 is_ok = false;
813             }
814           }
815         }
816       }
817       toCreateGroups = fd->IsChecked(0);
818       toFindOutDim   = fd->IsChecked(1);
819       fieldSelWdg->GetSelectedFeilds();
820       if ( !fieldSelWdg->parent() )
821         delete fieldSelWdg;
822       delete fd;
823     }
824     else
825     {
826       return;
827     }
828
829     // Perform export
830
831     if ( !aFilename.isEmpty() ) {
832       // Check whether the file already exists and delete it if yes
833       QFile aFile( aFilename );
834       if ( aFile.exists() && toOverwrite )
835         aFile.remove();
836       SUIT_OverrideCursor wc;
837
838       try {
839         // Renumbering is not needed since SMDS redesign in V6.2.0 (Nov 2010)
840 //         bool Renumber = false;
841 //         // PAL 14172  : Check of we have to renumber or not from the preferences before export
842 //         if (resMgr)
843 //           Renumber= resMgr->booleanValue("renumbering");
844 //         if (Renumber){
845 //           SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
846 //           aMeshEditor->RenumberNodes();
847 //           aMeshEditor->RenumberElements();
848 //           if ( SMESHGUI::automaticUpdate() )
849 //             SMESH::UpdateView();
850 //         }
851         if ( isMED )
852         {
853           aMeshIter = aMeshList.begin();
854           for( int aMeshIndex = 0; aMeshIter != aMeshList.end(); aMeshIter++, aMeshIndex++ )
855           {
856             SMESH::SMESH_IDSource_var aMeshOrGroup = (*aMeshIter).first;
857             SMESH::SMESH_Mesh_var        aMeshItem = aMeshOrGroup->GetMesh();
858             const GEOM::ListOfFields&       fields = aFieldList[ aMeshIndex ].first.in();
859             const QString&            geoAssFields = aFieldList[ aMeshIndex ].second;
860             const bool                   hasFields = ( fields.length() || !geoAssFields.isEmpty() );
861             if ( !hasFields && aMeshOrGroup->_is_equivalent( aMeshItem ))
862               aMeshItem->ExportToMEDX( aFilename.toUtf8().data(), toCreateGroups,
863                                        aFormat, toOverwrite && aMeshIndex == 0, toFindOutDim );
864             else
865               aMeshItem->ExportPartToMED( aMeshOrGroup, aFilename.toUtf8().data(), toCreateGroups,
866                                           aFormat, toOverwrite && aMeshIndex == 0, toFindOutDim,
867                                           fields, geoAssFields.toLatin1().data() );
868           }
869         }
870         else if ( isSAUV )
871         {
872           for( aMeshIter = aMeshList.begin(); aMeshIter != aMeshList.end(); aMeshIter++ )
873           {
874             SMESH::SMESH_Mesh_var aMeshItem = SMESH::SMESH_Mesh::_narrow( (*aMeshIter).first );
875             if( !aMeshItem->_is_nil() )
876               aMeshItem->ExportSAUV( aFilename.toUtf8().data(), toCreateGroups );
877           }
878         }
879         else if ( isDAT )
880         {
881           if ( aMeshOrGroup->_is_equivalent( aMesh ))
882             aMesh->ExportDAT( aFilename.toUtf8().data() );
883           else
884             aMesh->ExportPartToDAT( aMeshOrGroup, aFilename.toUtf8().data() );
885         }
886         else if ( isUNV )
887         {
888           if ( aMeshOrGroup->_is_equivalent( aMesh ))
889             aMesh->ExportUNV( aFilename.toUtf8().data() );
890           else
891             aMesh->ExportPartToUNV( aMeshOrGroup, aFilename.toUtf8().data() );
892         }
893         else if ( isSTL )
894         {
895           if ( aMeshOrGroup->_is_equivalent( aMesh ))
896             aMesh->ExportSTL( aFilename.toUtf8().data(), aIsASCII_STL );
897           else
898             aMesh->ExportPartToSTL( aMeshOrGroup, aFilename.toUtf8().data(), aIsASCII_STL );
899         }
900         else if ( isCGNS )
901         {
902           aMeshIter = aMeshList.begin();
903           for( int aMeshIndex = 0; aMeshIter != aMeshList.end(); aMeshIter++, aMeshIndex++ )
904           {
905             SMESH::SMESH_IDSource_var aMeshOrGroup = (*aMeshIter).first;
906             SMESH::SMESH_Mesh_var        aMeshItem = aMeshOrGroup->GetMesh();
907             aMeshItem->ExportCGNS( aMeshOrGroup,
908                                    aFilename.toUtf8().data(),
909                                    toOverwrite && aMeshIndex == 0 );
910           }
911         }
912         else if ( isGMF )
913         {
914           toCreateGroups = true;
915           aMesh->ExportGMF( aMeshOrGroup, aFilename.toUtf8().data(), toCreateGroups );
916         }
917       }
918       catch (const SALOME::SALOME_Exception& S_ex){
919         wc.suspend();
920         SUIT_MessageBox::warning(SMESHGUI::desktop(),
921                                  QObject::tr("SMESH_WRN_WARNING"),
922                                  QObject::tr("SMESH_EXPORT_FAILED"));
923         wc.resume();
924       }
925     }
926   }
927
928   inline void InverseEntityMode(unsigned int& theOutputMode,
929                                 unsigned int  theMode)
930   {
931     bool anIsNotPresent = ~theOutputMode & theMode;
932     if(anIsNotPresent)
933       theOutputMode |= theMode;
934     else
935       theOutputMode &= ~theMode;
936   }
937
938   void SetDisplayEntity(int theCommandID)
939   {
940     LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
941     SALOME_ListIO selected;
942     if ( aSel )
943       aSel->selectedObjects( selected );
944
945     if ( selected.Extent() >= 1 ) {
946       SALOME_ListIteratorOfListIO It( selected );
947       for( ; It.More(); It.Next()){
948         Handle(SALOME_InteractiveObject) IObject = It.Value();
949         if(IObject->hasEntry()){
950           if(SMESH_Actor *anActor = SMESH::FindActorByEntry(IObject->getEntry())){
951             unsigned int aMode = anActor->GetEntityMode();
952             switch(theCommandID){
953             case SMESHOp::OpDE0DElements: InverseEntityMode(aMode,SMESH_Actor::e0DElements); break;
954             case SMESHOp::OpDEEdges:      InverseEntityMode(aMode,SMESH_Actor::eEdges); break;
955             case SMESHOp::OpDEFaces:      InverseEntityMode(aMode,SMESH_Actor::eFaces); break;
956             case SMESHOp::OpDEVolumes:    InverseEntityMode(aMode,SMESH_Actor::eVolumes); break;
957             case SMESHOp::OpDEBalls:      InverseEntityMode(aMode,SMESH_Actor::eBallElem); break;
958             case SMESHOp::OpDEAllEntity:  aMode = SMESH_Actor::eAllEntity; break;
959             }
960             if(aMode)
961               anActor->SetEntityMode(aMode);
962           }
963         }
964       }
965     }
966   }
967
968   void AutoColor()
969   {
970     SalomeApp_Application* app =
971       dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
972     if ( !app )
973       return;
974
975     LightApp_SelectionMgr* aSel = app->selectionMgr();
976     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
977     if ( !aSel || !appStudy )
978       return;
979
980     SALOME_ListIO selected;
981     aSel->selectedObjects( selected );
982     if ( selected.IsEmpty() )
983       return;
984
985     Handle(SALOME_InteractiveObject) anIObject = selected.First();
986
987     _PTR(Study)         aStudy = appStudy->studyDS();
988     _PTR(SObject) aMainSObject = aStudy->FindObjectID( anIObject->getEntry() );
989     SMESH::SMESH_Mesh_var aMainObject = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIObject);
990     if ( aMainObject->_is_nil() )
991       return;
992
993     SUIT_OverrideCursor wc;
994
995     aMainObject->SetAutoColor( true ); // mesh groups are re-colored here
996
997     QList<SALOMEDS::Color> aReservedColors;
998
999     SMESH::ListOfGroups aListOfGroups = *aMainObject->GetGroups();
1000     for ( int i = 0, n = aListOfGroups.length(); i < n; i++ )
1001     {
1002       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
1003
1004 #ifdef SIMPLE_AUTOCOLOR   // simplified algorithm for auto-colors
1005       SALOMEDS::Color aColor = SMESHGUI::getPredefinedUniqueColor();
1006 #else                     // old algorithm  for auto-colors
1007       SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
1008       aReservedColors.append( aColor );
1009 #endif                    // SIMPLE_AUTOCOLOR
1010       aGroupObject->SetColor( aColor );
1011
1012       _PTR(SObject) aGroupSObject = SMESH::FindSObject(aGroupObject);
1013       if ( aGroupSObject ) {
1014         QColor c;
1015         int delta;
1016         if ( SMESH_Actor *anActor = SMESH::FindActorByEntry(aGroupSObject->GetID().c_str())) {
1017           switch ( aGroupObject->GetType ()) {
1018           case SMESH::NODE:
1019             anActor->SetNodeColor( aColor.R, aColor.G, aColor.B ); break;
1020           case SMESH::EDGE:
1021             anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B ); break;
1022           case SMESH::ELEM0D:
1023             anActor->Set0DColor( aColor.R, aColor.G, aColor.B ); break;
1024           case SMESH::BALL:
1025             anActor->SetBallColor( aColor.R, aColor.G, aColor.B ); break;
1026           case SMESH::VOLUME:
1027             SMESH::GetColor("SMESH", "volume_color", c, delta, "255,0,170|-100");
1028             anActor->SetVolumeColor( aColor.R, aColor.G, aColor.B, delta ); break;
1029           case SMESH::FACE:
1030           default:
1031             SMESH::GetColor("SMESH", "fill_color", c, delta, "0,170,255|-100");
1032             anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B, delta );
1033           }
1034         }
1035       }
1036     }
1037
1038     SMESH::RepaintCurrentView();
1039   }
1040
1041   void OverallMeshQuality()
1042   {
1043     SMESHGUI::GetSMESHGUI()->EmitSignalDeactivateDialog();
1044     LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
1045     SALOME_ListIO selected;
1046     if( aSel )
1047       aSel->selectedObjects( selected );
1048
1049     if ( selected.IsEmpty() ) return;
1050     SALOME_ListIteratorOfListIO It( selected );
1051     for ( ; It.More(); It.Next() ) {
1052       SMESHGUI_CtrlInfoDlg* ctrlDlg = new SMESHGUI_CtrlInfoDlg( SMESHGUI::desktop() );
1053       ctrlDlg->showInfo( It.Value() );
1054       ctrlDlg->show();
1055     }
1056   }
1057
1058   QString functorToString( SMESH::Controls::FunctorPtr f )
1059   {
1060     QString type = QObject::tr( "UNKNOWN_CONTROL" );
1061     if ( dynamic_cast< SMESH::Controls::Volume* >( f.get() ) )
1062       type = QObject::tr( "VOLUME_3D_ELEMENTS" );
1063     else if ( dynamic_cast< SMESH::Controls::MaxElementLength2D* >( f.get() ) )
1064       type = QObject::tr( "MAX_ELEMENT_LENGTH_2D" );
1065     else if ( dynamic_cast< SMESH::Controls::MaxElementLength3D* >( f.get() ) )
1066       type = QObject::tr( "MAX_ELEMENT_LENGTH_3D" );
1067     else if ( dynamic_cast< SMESH::Controls::MinimumAngle* >( f.get() ) )
1068       type = QObject::tr( "MINIMUMANGLE_ELEMENTS" );
1069     else if ( dynamic_cast< SMESH::Controls::AspectRatio* >( f.get() ) )
1070       type = QObject::tr( "ASPECTRATIO_ELEMENTS" );
1071     else if ( dynamic_cast< SMESH::Controls::AspectRatio3D* >( f.get() ) )
1072       type = QObject::tr( "ASPECTRATIO_3D_ELEMENTS" );
1073     else if ( dynamic_cast< SMESH::Controls::Warping* >( f.get() ) )
1074       type = QObject::tr( "WARP_ELEMENTS" );
1075     else if ( dynamic_cast< SMESH::Controls::Taper* >( f.get() ) )
1076       type = QObject::tr( "TAPER_ELEMENTS" );
1077     else if ( dynamic_cast< SMESH::Controls::Skew* >( f.get() ) )
1078       type = QObject::tr( "SKEW_ELEMENTS" );
1079     else if ( dynamic_cast< SMESH::Controls::Area* >( f.get() ) )
1080       type = QObject::tr( "AREA_ELEMENTS" );
1081     else if ( dynamic_cast< SMESH::Controls::Length* >( f.get() ) )
1082       type = QObject::tr( "LENGTH_EDGES" );
1083     else if ( dynamic_cast< SMESH::Controls::Length2D* >( f.get() ) )
1084       type = QObject::tr( "LENGTH2D_EDGES" );
1085     else if ( dynamic_cast< SMESH::Controls::MultiConnection* >( f.get() ) )
1086       type = QObject::tr( "MULTI_BORDERS" );
1087     else if ( dynamic_cast< SMESH::Controls::MultiConnection2D* >( f.get() ) )
1088       type = QObject::tr( "MULTI2D_BORDERS" );
1089     else if ( dynamic_cast< SMESH::Controls::FreeNodes* >( f.get() ) )
1090       type = QObject::tr( "FREE_NODES" );
1091     else if ( dynamic_cast< SMESH::Controls::FreeEdges* >( f.get() ) )
1092       type = QObject::tr( "FREE_EDGES" );
1093     else if ( dynamic_cast< SMESH::Controls::FreeBorders* >( f.get() ) )
1094       type = QObject::tr( "FREE_BORDERS" );
1095     else if ( dynamic_cast< SMESH::Controls::FreeFaces* >( f.get() ) )
1096       type = QObject::tr( "FREE_FACES" );
1097     else if ( dynamic_cast< SMESH::Controls::BareBorderVolume* >( f.get() ) )
1098       type = QObject::tr( "BARE_BORDER_VOLUME" );
1099     else if ( dynamic_cast< SMESH::Controls::BareBorderFace* >( f.get() ) )
1100       type = QObject::tr( "BARE_BORDER_FACE" );
1101     else if ( dynamic_cast< SMESH::Controls::OverConstrainedVolume* >( f.get() ) )
1102       type = QObject::tr( "OVER_CONSTRAINED_VOLUME" );
1103     else if ( dynamic_cast< SMESH::Controls::OverConstrainedFace* >( f.get() ) )
1104       type = QObject::tr( "OVER_CONSTRAINED_FACE" );
1105     else if ( dynamic_cast< SMESH::Controls::CoincidentNodes* >( f.get() ) )
1106       type = QObject::tr( "EQUAL_NODE" );
1107     else if ( dynamic_cast< SMESH::Controls::CoincidentElements1D* >( f.get() ) )
1108       type = QObject::tr( "EQUAL_EDGE" );
1109     else if ( dynamic_cast< SMESH::Controls::CoincidentElements2D* >( f.get() ) )
1110       type = QObject::tr( "EQUAL_FACE" );
1111     else if ( dynamic_cast< SMESH::Controls::CoincidentElements3D* >( f.get() ) )
1112       type = QObject::tr( "EQUAL_VOLUME" );
1113     return type;
1114   }
1115
1116   void SaveDistribution()
1117   {
1118     LightApp_SelectionMgr* aSel = SMESHGUI::selectionMgr();
1119     SALOME_ListIO selected;
1120     if ( aSel )
1121       aSel->selectedObjects( selected );
1122
1123     if ( selected.Extent() == 1 ) {
1124       Handle(SALOME_InteractiveObject) anIO = selected.First();
1125       if ( anIO->hasEntry() ) {
1126         SMESH_Actor* anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1127         if ( anActor &&
1128              anActor->GetScalarBarActor() &&
1129              anActor->GetControlMode() != SMESH_Actor::eNone )
1130         {
1131           SMESH_ScalarBarActor* aScalarBarActor = anActor->GetScalarBarActor();
1132           SMESH::Controls::FunctorPtr aFunctor = anActor->GetFunctor();
1133           if ( aScalarBarActor && aFunctor ) {
1134             SMESH::Controls::NumericalFunctor* aNumFun =
1135               dynamic_cast<SMESH::Controls::NumericalFunctor*>( aFunctor.get() );
1136             if ( aNumFun ) {
1137               std::vector<int> elements;
1138               SMESH::SMESH_Mesh_var mesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIO);
1139               if ( mesh->_is_nil() ) {
1140                 SMESH::SMESH_IDSource_var idSource =
1141                   SMESH::IObjectToInterface<SMESH::SMESH_IDSource>(anIO);
1142                 if ( !idSource->_is_nil() )
1143                 {
1144                   SMESH::long_array_var ids = idSource->GetIDs();
1145                   elements.resize( ids->length() );
1146                   for ( unsigned i = 0; i < elements.size(); ++i )
1147                     elements[i] = ids[i];
1148                 }
1149               }
1150               int nbIntervals = aScalarBarActor->GetMaximumNumberOfColors();
1151               vtkLookupTable* lookupTable =
1152                 static_cast<vtkLookupTable*>(aScalarBarActor->GetLookupTable());
1153               double * minmax = lookupTable->GetRange();
1154               bool isLogarithmic = lookupTable->GetScale() == VTK_SCALE_LOG10;
1155               std::vector<int>    nbEvents;
1156               std::vector<double> funValues;
1157               aNumFun->GetHistogram( nbIntervals, nbEvents, funValues,
1158                                      elements, minmax, isLogarithmic );
1159               QString anInitialPath = "";
1160               if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
1161                 anInitialPath = QDir::currentPath();
1162               QString aMeshName = anIO->getName();
1163               QStringList filter;
1164               filter.append( QObject::tr( "TEXT_FILES_FILTER" ) + " (*.txt)" );
1165               filter.append( QObject::tr( "ALL_FILES_FILTER" ) + " (*)" );
1166               QString aFilename = anInitialPath + "/" + aMeshName + "_" +
1167                 functorToString( aFunctor ).toLower().simplified().replace( QRegExp( " |-" ), "_" ) + ".txt";
1168               aFilename = SUIT_FileDlg::getFileName( SMESHGUI::desktop(),
1169                                                      aFilename,
1170                                                      filter,
1171                                                      QObject::tr( "SMESH_SAVE_DISTRIBUTION" ),
1172                                                      false );
1173               if ( !aFilename.isEmpty() ) {
1174                 QFile f( aFilename );
1175                 if ( f.open( QFile::WriteOnly | QFile::Truncate ) ) {
1176                   QTextStream out( &f );
1177                   out << "# Mesh: " << aMeshName << endl;
1178                   out << "# Control: " << functorToString( aFunctor ) << endl;
1179                   out << "#" << endl;
1180                   out.setFieldWidth( 10 );
1181                   for ( int i = 0; i < (int)qMin( nbEvents.size(), funValues.size()-1 ); i++ )
1182                     out << funValues[i] << "\t" << funValues[i+1] << "\t" << nbEvents[i] << endl;
1183                   f.close();
1184                 }
1185               }
1186             }
1187           }
1188         }
1189       }
1190     }
1191   }
1192
1193   void ShowElement( int theCommandID )
1194   {
1195     LightApp_SelectionMgr* aSel = SMESHGUI::selectionMgr();
1196     SALOME_ListIO selected;
1197     if ( aSel )
1198       aSel->selectedObjects( selected );
1199
1200     if ( selected.Extent() == 1 ) {
1201       Handle(SALOME_InteractiveObject) anIO = selected.First();
1202       if ( anIO->hasEntry() ) {
1203         SMESH_Actor* anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1204         if ( anActor &&
1205              anActor->GetScalarBarActor() &&
1206              anActor->GetControlMode() != SMESH_Actor::eNone )
1207         {
1208           SMESH_ScalarBarActor *aScalarBarActor = anActor->GetScalarBarActor();
1209           if ( theCommandID == SMESHOp::OpShowDistribution ) {
1210             aScalarBarActor->SetDistributionVisibility(!aScalarBarActor->GetDistributionVisibility());
1211           }
1212           else if ( theCommandID == SMESHOp::OpShowScalarBar ) {
1213             aScalarBarActor->SetVisibility( !aScalarBarActor->GetVisibility());
1214           }
1215         }
1216       }
1217     }
1218   }
1219
1220 #ifndef DISABLE_PLOT2DVIEWER
1221   void PlotDistribution()
1222   {
1223     SalomeApp_Application* app =
1224       dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
1225     if( !app )
1226       return;
1227
1228     LightApp_SelectionMgr* aSel = SMESHGUI::selectionMgr();
1229     SALOME_ListIO selected;
1230     if ( aSel )
1231       aSel->selectedObjects( selected );
1232
1233     if ( selected.Extent() == 1 ) {
1234       Handle(SALOME_InteractiveObject) anIO = selected.First();
1235       if ( anIO->hasEntry() ) {
1236         //Find Actor by entry before getting Plot2d viewer,
1237         //because after call getViewManager( Plot2d_Viewer::Type(), true ) active window is Plot2d Viewer
1238         SMESH_Actor* anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1239
1240         SUIT_ViewManager* aViewManager =
1241           app->getViewManager( Plot2d_Viewer::Type(), true ); // create if necessary
1242         if( !aViewManager )
1243           return;
1244
1245         SPlot2d_Viewer* aView = dynamic_cast<SPlot2d_Viewer*>(aViewManager->getViewModel());
1246         if ( !aView )
1247           return;
1248
1249         Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
1250         if ( !aPlot )
1251           return;
1252
1253         if ( anActor && anActor->GetControlMode() != SMESH_Actor::eNone )
1254         {
1255           SPlot2d_Histogram* aHistogram = anActor->UpdatePlot2Histogram();
1256           QString functorName = functorToString( anActor->GetFunctor());
1257           QString aHistogramName("%1 : %2");
1258           aHistogramName = aHistogramName.arg(anIO->getName()).arg(functorName);
1259           aHistogram->setName(aHistogramName);
1260           aHistogram->setHorTitle(functorName);
1261           aHistogram->setVerTitle(QObject::tr("DISTRIBUTION_NB_ENT"));
1262           aPlot->displayObject(aHistogram, true);
1263         }
1264       }
1265     }
1266   }
1267 #endif //DISABLE_PLOT2DVIEWER
1268
1269   void DisableAutoColor()
1270   {
1271     LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
1272     SALOME_ListIO selected;
1273     if ( aSel )
1274       aSel->selectedObjects( selected );
1275
1276     if ( selected.Extent() ) {
1277       Handle(SALOME_InteractiveObject) anIObject = selected.First();
1278       SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIObject);
1279       if ( !aMesh->_is_nil() ) {
1280         aMesh->SetAutoColor( false );
1281       }
1282     }
1283   }
1284
1285   void sortChildren()
1286   {
1287     LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
1288     SALOME_ListIO selected;
1289     if ( aSel ) {
1290       aSel->selectedObjects( selected );
1291       if ( selected.Extent() )
1292       {
1293         Handle(SALOME_InteractiveObject) anIObject = selected.First();
1294         _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1295         _PTR(SObject) aSObj = aStudy->FindObjectID(anIObject->getEntry());
1296         if (aSObj) {
1297           if ( aStudy->GetUseCaseBuilder()->SortChildren( aSObj, true/*AscendingOrder*/ ) ) {
1298             SMESHGUI::GetSMESHGUI()->updateObjBrowser();
1299           }
1300         }
1301       }
1302     }
1303   }
1304
1305   void SetDisplayMode(int theCommandID, SMESHGUI_StudyId2MarkerMap& theMarkerMap)
1306   {
1307     SALOME_ListIO selected;
1308     SalomeApp_Application* app =
1309       dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
1310     if ( !app )
1311       return;
1312
1313     LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
1314     SalomeApp_Study*   appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1315     if ( !aSel || !appStudy )
1316       return;
1317
1318     if ( theCommandID == SMESHOp::OpClipping ) { // Clipping dialog can be activated without selection
1319       if ( SMESHGUI* aModule = SMESHGUI::GetSMESHGUI() ) {
1320         aModule->EmitSignalDeactivateDialog();
1321         if( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( aModule ) )
1322           (new SMESHGUI_ClippingDlg( aModule, aViewWindow ))->show();
1323       }
1324       return;
1325     }
1326
1327     _PTR(Study) aStudy = appStudy->studyDS();
1328
1329     aSel->selectedObjects( selected );
1330
1331     if ( selected.Extent() >= 1 )
1332     {
1333       switch ( theCommandID ) {
1334       case SMESHOp::OpTransparency:
1335       {
1336         SMESHGUI::GetSMESHGUI()->EmitSignalDeactivateDialog();
1337         (new SMESHGUI_TransparencyDlg( SMESHGUI::GetSMESHGUI() ))->show();
1338         return;
1339       }
1340       case SMESHOp::OpProperties:
1341       {
1342         double color[3];
1343         QColor faceColor, edgeColor, nodeColor, elem0dColor, ballColor;
1344         QColor orientationColor, outlineColor, volumeColor;
1345         int deltaF = 0, deltaV = 0;
1346         int elem0dSize   = 1;
1347         //int ballSize     = 1;
1348         double ballScale = 1.0;
1349         int edgeWidth    = 1;
1350         int outlineWidth = 1;
1351         double shrinkCoef = 0.0;
1352         double orientationScale = 0.0;
1353         bool orientation3d = false;
1354         VTK::MarkerType markerType = VTK::MT_NONE;
1355         VTK::MarkerScale markerScale = VTK::MS_NONE;
1356         int markerId = 0;
1357         bool hasNodes = false;
1358         int presentEntities = 0;
1359         bool firstTime  = true;
1360
1361         SALOME_ListIteratorOfListIO It( selected );
1362         for ( ; It.More(); It.Next() ) {
1363           Handle(SALOME_InteractiveObject) IObject = It.Value();
1364           if ( !IObject->hasEntry() ) continue;
1365           SMESH_Actor* anActor = SMESH::FindActorByEntry( IObject->getEntry() );
1366           if ( !anActor || !anActor->GetObject() ) continue;
1367
1368           if ( firstTime ) {
1369             // nodes: color, marker
1370             anActor->GetNodeColor( color[0], color[1], color[2] );
1371             nodeColor.setRgbF( color[0], color[1], color[2] );
1372             markerType  = anActor->GetMarkerType();
1373             markerScale = anActor->GetMarkerScale();
1374             markerId    = anActor->GetMarkerTexture();
1375             // edges: color, width
1376             anActor->GetEdgeColor( color[0], color[1], color[2] );
1377             edgeColor.setRgbF( color[0], color[1], color[2] );
1378             edgeWidth = qMax( (int)anActor->GetLineWidth(), 1 ); // minimum allowed width is 1
1379             // faces: front color, back color (delta)
1380             anActor->GetSufaceColor( color[0], color[1], color[2], deltaF );
1381             faceColor.setRgbF( color[0], color[1], color[2] );
1382             // faces: front color, back color (delta)
1383             anActor->GetVolumeColor( color[0], color[1], color[2], deltaV );
1384             volumeColor.setRgbF( color[0], color[1], color[2] );
1385             // 0d elements: color, size
1386             anActor->Get0DColor( color[0], color[1], color[2] );
1387             elem0dColor.setRgbF( color[0], color[1], color[2] );
1388             elem0dSize = qMax( (int)anActor->Get0DSize(), 1 ); // minimum allowed size is 1
1389             // balls: color, size
1390             anActor->GetBallColor( color[0], color[1], color[2] );
1391             ballColor.setRgbF( color[0], color[1], color[2] );
1392             //ballSize = qMax( (int)anActor->GetBallSize(), 1 ); // minimum allowed size is 1
1393             ballScale = qMax( (double)anActor->GetBallScale(), 1e-2 ); // minimum allowed scale is 1e-2
1394             // outlines: color
1395             anActor->GetOutlineColor( color[0], color[1], color[2] );
1396             outlineColor.setRgbF( color[0], color[1], color[2] );
1397             outlineWidth = qMax( (int)anActor->GetOutlineWidth(), 1 ); // minimum allowed width is 1
1398             // orientation vectors: color, scale, 3d flag
1399             anActor->GetFacesOrientationColor( color[0], color[1], color[2] );
1400             orientationColor.setRgbF( color[0], color[1], color[2] );
1401             orientationScale = anActor->GetFacesOrientationScale();
1402             orientation3d = anActor->GetFacesOrientation3DVectors();
1403             // shrink factor
1404             shrinkCoef = anActor->GetShrinkFactor();
1405           }
1406
1407           firstTime = false; // we only take properties from first object (for performance reasons)
1408
1409           if ( !hasNodes )
1410             hasNodes = anActor->GetObject()->GetNbEntities( SMDSAbs_Node );
1411           if ( !(presentEntities & SMESH_Actor::eEdges) && anActor->GetObject()->GetNbEntities( SMDSAbs_Edge ) )
1412             presentEntities = presentEntities | SMESH_Actor::eEdges;
1413           if ( !(presentEntities & SMESH_Actor::eFaces) && anActor->GetObject()->GetNbEntities( SMDSAbs_Face ) )
1414             presentEntities = presentEntities | SMESH_Actor::eFaces;
1415           if ( !(presentEntities & SMESH_Actor::eVolumes) && anActor->GetObject()->GetNbEntities( SMDSAbs_Volume ) )
1416             presentEntities = presentEntities | SMESH_Actor::eVolumes;
1417           if ( !(presentEntities & SMESH_Actor::e0DElements) && anActor->GetObject()->GetNbEntities( SMDSAbs_0DElement ) )
1418             presentEntities = presentEntities | SMESH_Actor::e0DElements;
1419           if ( !(presentEntities & SMESH_Actor::eBallElem) && anActor->GetObject()->GetNbEntities( SMDSAbs_Ball ) )
1420             presentEntities = presentEntities | SMESH_Actor::eBallElem;
1421           
1422           // as we know that all types of elements are present, we can exit the loop
1423           if ( presentEntities == SMESH_Actor::eAllEntity )
1424             break;
1425         }
1426
1427         SMESHGUI_PropertiesDlg dlg( theMarkerMap[ aStudy->StudyId() ], SMESHGUI::desktop() );
1428         // nodes: color, marker
1429         dlg.setNodeColor( nodeColor );
1430         if( markerType != VTK::MT_USER )
1431           dlg.setNodeMarker( markerType, markerScale );
1432         else
1433           dlg.setNodeCustomMarker( markerId );
1434         // edges: color, line width
1435         dlg.setEdgeColor( edgeColor );
1436         dlg.setEdgeWidth( edgeWidth );
1437         // faces: front color, back color
1438         dlg.setFaceColor( faceColor, deltaF );
1439         // volumes: normal color, reversed color
1440         dlg.setVolumeColor( volumeColor, deltaV );
1441         // outlines: color, line width
1442         dlg.setOutlineColor( outlineColor );
1443         dlg.setOutlineWidth( outlineWidth );
1444         // 0d elements: color, size
1445         dlg.setElem0dColor( elem0dColor );
1446         dlg.setElem0dSize( elem0dSize );
1447         // balls: color, size
1448         dlg.setBallColor( ballColor );
1449         //dlg.setBallSize( ballSize );
1450         dlg.setBallScale( ballScale );
1451         // orientation: color, scale, 3d flag
1452         dlg.setOrientationColor( orientationColor );
1453         dlg.setOrientationSize( int( orientationScale * 100. ) );
1454         dlg.setOrientation3d( orientation3d );
1455         // shrink: scale factor
1456         dlg.setShrinkCoef( int( shrinkCoef * 100. ) );
1457         // hide unused controls
1458         dlg.showControls( presentEntities, hasNodes );
1459         
1460         if ( dlg.exec() ) {
1461           nodeColor        = dlg.nodeColor();
1462           markerType       = dlg.nodeMarkerType();
1463           markerScale      = dlg.nodeMarkerScale();
1464           markerId         = dlg.nodeMarkerId();
1465           edgeColor        = dlg.edgeColor();
1466           edgeWidth        = dlg.edgeWidth();
1467           faceColor        = dlg.faceColor();
1468           deltaF           = dlg.faceColorDelta();
1469           volumeColor      = dlg.volumeColor();
1470           deltaV           = dlg.volumeColorDelta();
1471           outlineColor     = dlg.outlineColor();
1472           outlineWidth     = dlg.outlineWidth();
1473           elem0dColor      = dlg.elem0dColor();
1474           elem0dSize       = dlg.elem0dSize();
1475           ballColor        = dlg.ballColor();
1476          // ballSize         = dlg.ballSize();
1477           ballScale        = dlg.ballScale();
1478           orientationColor = dlg.orientationColor();
1479           orientationScale = dlg.orientationSize() / 100.;
1480           orientation3d    = dlg.orientation3d();
1481           shrinkCoef       = dlg.shrinkCoef() / 100.;
1482
1483           // store point markers map that might be changed by the user
1484           theMarkerMap[ aStudy->StudyId() ] = dlg.customMarkers();
1485
1486           // set properties from dialog box to the presentations
1487           SALOME_ListIteratorOfListIO It( selected );
1488           for ( ; It.More(); It.Next() ) {
1489             Handle(SALOME_InteractiveObject) IObject = It.Value();
1490             if ( !IObject->hasEntry() ) continue;
1491             SMESH_Actor* anActor = SMESH::FindActorByEntry( IObject->getEntry() );
1492             if ( !anActor ) continue;
1493             
1494             // nodes: color, marker
1495             anActor->SetNodeColor( nodeColor.redF(), nodeColor.greenF(), nodeColor.blueF() );
1496             if ( markerType != VTK::MT_USER ) {
1497               anActor->SetMarkerStd( markerType, markerScale );
1498             }
1499             else {
1500               const VTK::MarkerMap& markerMap = theMarkerMap[ aStudy->StudyId() ];
1501               VTK::MarkerMap::const_iterator iter = markerMap.find( markerId );
1502               if ( iter != markerMap.end() )
1503                 anActor->SetMarkerTexture( markerId, iter->second.second );
1504             }
1505             // volumes: normal color, reversed color (delta)
1506             anActor->SetVolumeColor( volumeColor.redF(), volumeColor.greenF(), volumeColor.blueF(), deltaV );
1507             // faces: front color, back color (delta)
1508             anActor->SetSufaceColor( faceColor.redF(), faceColor.greenF(), faceColor.blueF(), deltaF );
1509             // edges: color, width
1510             anActor->SetEdgeColor( edgeColor.redF(), edgeColor.greenF(), edgeColor.blueF() );
1511             anActor->SetLineWidth( edgeWidth );
1512             // outlines: color
1513             anActor->SetOutlineColor( outlineColor.redF(), outlineColor.greenF(), outlineColor.blueF() );
1514             anActor->SetOutlineWidth( outlineWidth );
1515             // 0D elements: color, size
1516             anActor->Set0DColor( elem0dColor.redF(), elem0dColor.greenF(), elem0dColor.blueF() );
1517             anActor->Set0DSize( elem0dSize );
1518             // balls: color, size
1519             anActor->SetBallColor( ballColor.redF(), ballColor.greenF(), ballColor.blueF() );
1520             // anActor->SetBallSize( ballSize );
1521             anActor->SetBallScale( ballScale );
1522             // orientation: color, scale, 3d flag
1523             anActor->SetFacesOrientationColor( orientationColor.redF(), orientationColor.greenF(), orientationColor.blueF() );
1524             anActor->SetFacesOrientationScale( orientationScale );
1525             anActor->SetFacesOrientation3DVectors( orientation3d );
1526             // shrink factor
1527             anActor->SetShrinkFactor( shrinkCoef );
1528
1529             // for groups, set also proper color
1530             SMESH::SMESH_GroupBase_var aGroupObject = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(IObject);
1531             if ( !aGroupObject->_is_nil() ) {
1532               SMESH::ElementType anElementType = aGroupObject->GetType();
1533               QColor aColor;
1534               switch( anElementType ) {
1535               case SMESH::NODE:
1536                 aColor = nodeColor; break;
1537               case SMESH::EDGE:
1538                 aColor = edgeColor; break;
1539               case SMESH::FACE: 
1540                 aColor = faceColor; break;
1541               case SMESH::VOLUME:
1542                 aColor = volumeColor; break;
1543               case SMESH::ELEM0D: 
1544                 aColor = elem0dColor; break;
1545               case SMESH::BALL: 
1546                 aColor = ballColor; break;
1547               default: break;
1548               }
1549               
1550               if ( aColor.isValid() ) {
1551                 SALOMEDS::Color aGroupColor;
1552                 aGroupColor.R = aColor.redF();
1553                 aGroupColor.G = aColor.greenF();
1554                 aGroupColor.B = aColor.blueF();
1555                 aGroupObject->SetColor( aGroupColor );
1556               }
1557             } // if ( !aGroupObject->_is_nil() )
1558           } // for ( ; It.More(); It.Next() )
1559           SMESH::RepaintCurrentView();
1560         } // if ( dlg.exec() )
1561         return;
1562       } // case SMESHOp::OpProperties:
1563       } // switch(theCommandID)
1564       SALOME_ListIteratorOfListIO It( selected );
1565       for( ; It.More(); It.Next()){
1566         Handle(SALOME_InteractiveObject) IObject = It.Value();
1567         if(IObject->hasEntry()){
1568           if(SMESH_Actor *anActor = SMESH::FindActorByEntry(IObject->getEntry())){
1569             switch(theCommandID){
1570             case SMESHOp::OpDMWireframe:
1571               anActor->SetRepresentation(SMESH_Actor::eEdge);
1572               break;
1573             case SMESHOp::OpDMShading:
1574               anActor->SetRepresentation(SMESH_Actor::eSurface);
1575               break;
1576             case SMESHOp::OpDMShrink:
1577               if(anActor->IsShrunk())
1578                 anActor->UnShrink();
1579               else
1580                 anActor->SetShrink();
1581               break;
1582             case SMESHOp::OpDMNodes:
1583               anActor->SetRepresentation(SMESH_Actor::ePoint);
1584               break;
1585             case SMESHOp::OpRepresentationLines:
1586               if(anActor->GetQuadratic2DRepresentation() != SMESH_Actor::eLines)
1587                 anActor->SetQuadratic2DRepresentation(SMESH_Actor::eLines);
1588               break;
1589             case SMESHOp::OpRepresentationArcs:
1590               if(anActor->GetQuadratic2DRepresentation() != SMESH_Actor::eArcs)
1591                 anActor->SetQuadratic2DRepresentation(SMESH_Actor::eArcs);
1592               break;
1593             }
1594           }
1595         }
1596       }
1597       SMESH::RepaintCurrentView();
1598     }
1599   }
1600
1601   int ActionToControl( int theID, bool theReversed )
1602   {
1603     NCollection_DoubleMap<int,int> ActionControl;
1604     ActionControl.Bind( 0,                                SMESH_Actor::eNone );
1605     ActionControl.Bind( SMESHOp::OpFreeNode,              SMESH_Actor::eFreeNodes );
1606     ActionControl.Bind( SMESHOp::OpEqualNode,             SMESH_Actor::eCoincidentNodes );
1607     ActionControl.Bind( SMESHOp::OpFreeEdge,              SMESH_Actor::eFreeEdges );
1608     ActionControl.Bind( SMESHOp::OpFreeBorder,            SMESH_Actor::eFreeBorders );
1609     ActionControl.Bind( SMESHOp::OpLength,                SMESH_Actor::eLength );
1610     ActionControl.Bind( SMESHOp::OpConnection,            SMESH_Actor::eMultiConnection );
1611     ActionControl.Bind( SMESHOp::OpEqualEdge,             SMESH_Actor::eCoincidentElems1D );
1612     ActionControl.Bind( SMESHOp::OpFreeFace,              SMESH_Actor::eFreeFaces );
1613     ActionControl.Bind( SMESHOp::OpBareBorderFace,        SMESH_Actor::eBareBorderFace );
1614     ActionControl.Bind( SMESHOp::OpOverConstrainedFace,   SMESH_Actor::eOverConstrainedFace );
1615     ActionControl.Bind( SMESHOp::OpLength2D,              SMESH_Actor::eLength2D );
1616     ActionControl.Bind( SMESHOp::OpConnection2D,          SMESH_Actor::eMultiConnection2D );
1617     ActionControl.Bind( SMESHOp::OpArea,                  SMESH_Actor::eArea );
1618     ActionControl.Bind( SMESHOp::OpTaper,                 SMESH_Actor::eTaper );
1619     ActionControl.Bind( SMESHOp::OpAspectRatio,           SMESH_Actor::eAspectRatio );
1620     ActionControl.Bind( SMESHOp::OpMinimumAngle,          SMESH_Actor::eMinimumAngle );
1621     ActionControl.Bind( SMESHOp::OpWarpingAngle,          SMESH_Actor::eWarping );
1622     ActionControl.Bind( SMESHOp::OpSkew,                  SMESH_Actor::eSkew );
1623     ActionControl.Bind( SMESHOp::OpMaxElementLength2D,    SMESH_Actor::eMaxElementLength2D );
1624     ActionControl.Bind( SMESHOp::OpEqualFace,             SMESH_Actor::eCoincidentElems2D );
1625     ActionControl.Bind( SMESHOp::OpAspectRatio3D,         SMESH_Actor::eAspectRatio3D );
1626     ActionControl.Bind( SMESHOp::OpVolume,                SMESH_Actor::eVolume3D );
1627     ActionControl.Bind( SMESHOp::OpMaxElementLength3D,    SMESH_Actor::eMaxElementLength3D );
1628     ActionControl.Bind( SMESHOp::OpBareBorderVolume,      SMESH_Actor::eBareBorderVolume );
1629     ActionControl.Bind( SMESHOp::OpOverConstrainedVolume, SMESH_Actor::eOverConstrainedVolume );
1630     ActionControl.Bind( SMESHOp::OpEqualVolume,           SMESH_Actor::eCoincidentElems3D );
1631
1632     return theReversed ? ActionControl.Find2( theID ) : ActionControl.Find1( theID );
1633   }
1634
1635   void Control( int theCommandID )
1636   {
1637     SMESH_Actor::eControl aControl = SMESH_Actor::eControl( ActionToControl( theCommandID ) );
1638     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1639     LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
1640     SALOME_ListIO selected;
1641     if ( aSel )
1642       aSel->selectedObjects( selected );
1643
1644     if ( !selected.IsEmpty() ) {
1645       SALOME_ListIteratorOfListIO It(selected);
1646       for ( ; It.More(); It.Next())
1647       {
1648         Handle(SALOME_InteractiveObject) anIO = It.Value();
1649         if(!anIO.IsNull()){
1650           _PTR(SObject) SO = aStudy->FindObjectID( It.Value()->getEntry() );
1651           if ( SO ) {
1652             CORBA::Object_var         aObject = SMESH::SObjectToObject( SO );
1653             SMESH::SMESH_IDSource_var anIDSrc = SMESH::SMESH_IDSource::_narrow( aObject );
1654             if ( !anIDSrc->_is_nil() ) {
1655               if ( SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry()) ) {
1656                 QString functorName = functorToString( anActor->GetFunctor() );
1657                 anActor->SetControlMode( aControl );
1658                 anActor->GetScalarBarActor()->SetTitle( functorName.toLatin1().constData() );
1659                 SMESH::RepaintCurrentView();
1660 #ifndef DISABLE_PLOT2DVIEWER
1661                 if ( anActor->GetPlot2Histogram() ) {
1662                   SPlot2d_Histogram* aHistogram = anActor->UpdatePlot2Histogram();
1663                   QString aHistogramName("%1 : %2");
1664                   aHistogramName = aHistogramName.arg( anIO->getName() ).arg( functorName );
1665                   aHistogram->setName( aHistogramName );
1666                   aHistogram->setHorTitle( functorName );
1667                   SMESH::ProcessIn2DViewers( anActor );
1668                 }
1669 #endif
1670               }
1671             }
1672           }
1673         }
1674       }
1675     }
1676   }
1677
1678
1679   bool CheckOIType(const Handle(SALOME_InteractiveObject) & theIO,
1680                    SMESH::MeshObjectType                    theType,
1681                    const QString                            theInTypeName,
1682                    QString &                                theOutTypeName)
1683   {
1684     SMESH_TypeFilter aTypeFilter( theType );
1685     QString entry;
1686     if ( !theIO.IsNull() )
1687     {
1688       entry = theIO->getEntry();
1689       LightApp_DataOwner owner( entry );
1690       if ( aTypeFilter.isOk( &owner )) {
1691         theOutTypeName = theInTypeName;
1692         return true;
1693       }
1694     }
1695     return false;
1696   }
1697
1698
1699   QString CheckTypeObject(const Handle(SALOME_InteractiveObject) & theIO)
1700   {
1701     _PTR(Study)  aStudy = SMESH::GetActiveStudyDocument();
1702     _PTR(SObject) aSObj = aStudy->FindObjectID(theIO->getEntry());
1703     if (aSObj) {
1704       _PTR(SComponent) aSComp = aSObj->GetFatherComponent();
1705       CORBA::String_var  anID = aSComp->GetID().c_str();
1706       if ( !strcmp(anID.in(),theIO->getEntry()) )
1707         return "Component";
1708     }
1709
1710     QString aTypeName;
1711     if (
1712         CheckOIType ( theIO, SMESH::HYPOTHESIS,    "Hypothesis", aTypeName ) ||
1713         CheckOIType ( theIO, SMESH::ALGORITHM,     "Algorithm",  aTypeName ) ||
1714         CheckOIType ( theIO, SMESH::MESH,          "Mesh",       aTypeName ) ||
1715         CheckOIType ( theIO, SMESH::SUBMESH,       "SubMesh",    aTypeName ) ||
1716         CheckOIType ( theIO, SMESH::GROUP,         "Group",      aTypeName )
1717         )
1718       return aTypeName;
1719
1720     return "NoType";
1721   }
1722
1723
1724   QString CheckHomogeneousSelection()
1725   {
1726     LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
1727     SALOME_ListIO selected;
1728     if ( aSel )
1729       aSel->selectedObjects( selected );
1730
1731     QString RefType = CheckTypeObject(selected.First());
1732     SALOME_ListIteratorOfListIO It(selected);
1733     for ( ; It.More(); It.Next())
1734     {
1735       Handle(SALOME_InteractiveObject) IObject = It.Value();
1736       QString Type = CheckTypeObject(IObject);
1737       if ( Type.compare(RefType) != 0 )
1738         return "Heterogeneous Selection";
1739     }
1740
1741     return RefType;
1742   }
1743
1744   uint randomize( uint size )
1745   {
1746     static bool initialized = false;
1747     if ( !initialized ) {
1748       qsrand( QDateTime::currentDateTime().toTime_t() );
1749       initialized = true;
1750     }
1751     uint v = qrand();
1752     v = uint( (double)( v ) / RAND_MAX * size );
1753     v = qMax( uint(0), qMin ( v, size-1 ) );
1754     return v;
1755   }
1756   
1757 } //namespace
1758
1759 void SMESHGUI::OnEditDelete()
1760 {
1761   // VSR 17/11/04: check if all objects selected belong to SMESH component --> start
1762   LightApp_SelectionMgr* aSel = SMESHGUI::selectionMgr();
1763   SALOME_ListIO selected; aSel->selectedObjects( selected, QString::null, false );
1764
1765   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1766   _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
1767   _PTR(GenericAttribute) anAttr;
1768   _PTR(AttributeIOR) anIOR;
1769
1770   int objectCount = 0;
1771   QString aNameList;
1772   QString aParentComponent = QString::null;
1773   Handle(SALOME_InteractiveObject) anIO;
1774   for( SALOME_ListIteratorOfListIO anIt( selected ); anIt.More(); anIt.Next() )
1775   {
1776     anIO = anIt.Value();
1777     QString cur = anIO->getComponentDataType();
1778     _PTR(SObject) aSO = aStudy->FindObjectID(anIO->getEntry());
1779     if (aSO) {
1780       // check if object is reference
1781       _PTR(SObject) aRefSObj;
1782       aNameList.append("\n    - ");
1783       if ( aSO->ReferencedObject( aRefSObj ) ) {
1784         QString aRefName = QString::fromStdString ( aRefSObj->GetName() );
1785         aNameList.append( aRefName );
1786         cur = QString::fromStdString ( aRefSObj->GetFatherComponent()->ComponentDataType() );
1787       }
1788       else
1789         aNameList.append(anIO->getName());
1790       objectCount++;
1791     }
1792
1793     if( aParentComponent.isNull() )
1794       aParentComponent = cur;
1795     else if( !aParentComponent.isEmpty() && aParentComponent!=cur )
1796       aParentComponent = "";
1797   }
1798
1799   if ( objectCount == 0 )
1800     return; // No Valid Objects Selected
1801
1802   if ( aParentComponent != SMESHGUI::GetSMESHGUI()->name() )  {
1803     SUIT_MessageBox::warning( SMESHGUI::desktop(),
1804                               QObject::tr("ERR_ERROR"),
1805                               QObject::tr("NON_SMESH_OBJECTS_SELECTED").arg( SMESHGUI::GetSMESHGUI()->moduleName() ) );
1806     return;
1807   }
1808   // VSR 17/11/04: check if all objects selected belong to SMESH component <-- finish
1809   if (SUIT_MessageBox::warning
1810       (SMESHGUI::desktop(),
1811        QObject::tr("SMESH_WRN_WARNING"),
1812        QObject::tr("SMESH_REALLY_DELETE").arg( objectCount ).arg( aNameList ),
1813        SUIT_MessageBox::Yes | SUIT_MessageBox::No,
1814        SUIT_MessageBox::Yes) != SUIT_MessageBox::Yes)
1815     return;
1816
1817   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
1818
1819   // Put the whole hierarchy of sub-objects of the selected SO's into a list and
1820   // then treat them all starting from the deepest objects (at list back)
1821   std::list< _PTR(SObject) > listSO;
1822   SALOME_ListIteratorOfListIO It(selected);
1823   for( ; It.More(); It.Next()) // loop on selected IO's
1824   {
1825     Handle(SALOME_InteractiveObject) IObject = It.Value();
1826     if(IObject->hasEntry()) {
1827       _PTR(SObject) aSO = aStudy->FindObjectID(IObject->getEntry());
1828
1829       // disable removal of "SMESH" component object
1830       if(aSO->FindAttribute(anAttr, "AttributeIOR")){
1831         anIOR = anAttr;
1832         if ( engineIOR() == anIOR->Value().c_str() )
1833           continue;
1834       }
1835       //Check the referenced object
1836       _PTR(SObject) aRefSObject;
1837       if ( aSO && aSO->ReferencedObject( aRefSObject ) )
1838         aSO = aRefSObject; // Delete main Object instead of reference
1839
1840       listSO.push_back( aSO );
1841       std::list< _PTR(SObject) >::iterator itSO = --listSO.end();
1842       for ( ; itSO != listSO.end(); ++itSO ) {
1843         _PTR(ChildIterator) it = aStudy->NewChildIterator( *itSO );
1844         for (it->InitEx(false); it->More(); it->Next())
1845           listSO.push_back( it->Value() );
1846       }
1847     }
1848   }
1849   // Check if none of objects to delete is referred from outside
1850   std::list< _PTR(SObject) >::reverse_iterator ritSO;
1851   for ( ritSO = listSO.rbegin(); ritSO != listSO.rend(); ++ritSO )
1852   {
1853     _PTR(SObject) SO = *ritSO;
1854     if ( !SO ) continue;
1855     std::vector<_PTR(SObject)> aReferences = aStudy->FindDependances( *ritSO  );
1856     for (size_t i = 0; i < aReferences.size(); i++) {
1857       _PTR(SComponent) aComponent = aReferences[i]->GetFatherComponent();
1858       std::string type = aComponent->ComponentDataType();
1859       if ( type != "SMESH" )
1860       {
1861         SUIT_MessageBox::warning( anApp->desktop(),
1862                                   QObject::tr("WRN_WARNING"),
1863                                   QObject::tr("DEP_OBJECT") );
1864         return; // outside SMESH, there is an object depending on a SMESH object
1865       }
1866     }
1867   }
1868
1869   // Call mesh->Clear() to prevent loading mesh from file caused by hypotheses removal
1870   for( It.Initialize( selected ); It.More(); It.Next()) // loop on selected IO's
1871   {
1872     Handle(SALOME_InteractiveObject) IObject = It.Value();
1873     SMESH::SMESH_Mesh_var mesh = SMESH::IObjectToInterface< SMESH::SMESH_Mesh >( IObject );
1874     if ( !mesh->_is_nil() )
1875       mesh->Clear();
1876   }
1877
1878   // Treat SO's in the list starting from the back
1879   aStudyBuilder->NewCommand();  // There is a transaction
1880   for ( ritSO = listSO.rbegin(); ritSO != listSO.rend(); ++ritSO )
1881   {
1882     _PTR(SObject) SO = *ritSO;
1883     if ( !SO ) continue;
1884     std::string anEntry = SO->GetID();
1885
1886     /** Erase graphical object and remove all its data **/
1887     if(SO->FindAttribute(anAttr, "AttributeIOR")) {
1888       SMESH::RemoveVisualObjectWithActors( anEntry.c_str(), true);
1889     }
1890     /** Remove an object from data structures **/
1891     SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow( SMESH::SObjectToObject( SO ));
1892     SMESH::SMESH_subMesh_var   aSubMesh = SMESH::SMESH_subMesh::_narrow( SMESH::SObjectToObject( SO ));
1893     if ( !aGroup->_is_nil() ) {                          // DELETE GROUP
1894       SMESH::SMESH_Mesh_var aMesh = aGroup->GetMesh();
1895       aMesh->RemoveGroup( aGroup );
1896     }
1897     else if ( !aSubMesh->_is_nil() ) {                   // DELETE SUBMESH
1898       SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
1899       aMesh->RemoveSubMesh( aSubMesh );
1900
1901       _PTR(SObject) aMeshSO = SMESH::FindSObject(aMesh);
1902       if (aMeshSO)
1903         SMESH::ModifiedMesh(aMeshSO, false, aMesh->NbNodes()==0);
1904     }
1905     else {
1906       Handle(SALOME_InteractiveObject) IObject = new SALOME_InteractiveObject
1907         ( anEntry.c_str(), engineIOR().toLatin1().data(), SO->GetName().c_str() );
1908       QString objType = CheckTypeObject(IObject);
1909       if ( objType == "Hypothesis" || objType == "Algorithm" ) {// DELETE HYPOTHESIS
1910         SMESH::RemoveHypothesisOrAlgorithmOnMesh(IObject);
1911         aStudyBuilder->RemoveObjectWithChildren( SO );
1912       }
1913       else {// default action: remove SObject from the study
1914         // san - it's no use opening a transaction here until UNDO/REDO is provided in SMESH
1915         //SUIT_Operation *op = new SALOMEGUI_ImportOperation(myActiveStudy);
1916         //op->start();
1917         aStudyBuilder->RemoveObjectWithChildren( SO );
1918         //op->finish();
1919       }
1920     }
1921   } /* listSO back loop */
1922
1923   aStudyBuilder->CommitCommand();
1924
1925   /* Clear any previous selection */
1926   SALOME_ListIO l1;
1927   aSel->setSelectedObjects( l1 );
1928
1929   SMESHGUI::GetSMESHGUI()->updateObjBrowser();
1930 }
1931
1932 extern "C" {
1933   SMESHGUI_EXPORT CAM_Module* createModule()
1934   {
1935     return new SMESHGUI();
1936   }
1937
1938   SMESHGUI_EXPORT  char* getModuleVersion() {
1939     return (char*)SMESH_VERSION_STR;
1940   }
1941 }
1942
1943 SMESH::SMESH_Gen_var SMESHGUI::myComponentSMESH = SMESH::SMESH_Gen::_nil();
1944
1945 //=============================================================================
1946 /*!
1947  *
1948  */
1949 //=============================================================================
1950 SMESHGUI::SMESHGUI() :
1951 SalomeApp_Module( "SMESH" )
1952 {
1953   if ( CORBA::is_nil( myComponentSMESH ) )
1954   {
1955     CORBA::Boolean anIsEmbeddedMode;
1956     myComponentSMESH = SMESH_Client::GetSMESHGen(getApp()->orb(),anIsEmbeddedMode);
1957     //MESSAGE("-------------------------------> anIsEmbeddedMode=" << anIsEmbeddedMode);
1958
1959     //  0019923: EDF 765 SMESH : default values of hypothesis
1960     SUIT_ResourceMgr* aResourceMgr = SMESH::GetResourceMgr(this);
1961     int nbSeg = aResourceMgr->integerValue( "SMESH", "segmentation", 10 );
1962     myComponentSMESH->SetBoundaryBoxSegmentation( nbSeg );
1963     nbSeg = aResourceMgr->integerValue( "SMESH", "nb_segments_per_edge", 15 );
1964     myComponentSMESH->SetDefaultNbSegments( nbSeg );
1965
1966     const char* options[] = { "historical_python_dump", "forget_mesh_on_hyp_modif", "default_grp_color" };
1967     for ( size_t i = 0; i < sizeof(options)/sizeof(char*); ++i )
1968       if ( aResourceMgr->hasValue( "SMESH", options[i] ))
1969       {
1970         QString val = aResourceMgr->stringValue( "SMESH", options[i] );
1971         myComponentSMESH->SetOption( options[i], val.toLatin1().constData() );
1972       }
1973   }
1974
1975   myActiveDialogBox = 0;
1976   myFilterLibraryDlg = 0;
1977   myState = -1;
1978   myDisplayer = 0;
1979
1980   myEventCallbackCommand = vtkCallbackCommand::New();
1981   myEventCallbackCommand->Delete();
1982   myEventCallbackCommand->SetClientData( this );
1983   myEventCallbackCommand->SetCallback( SMESHGUI::ProcessEvents );
1984   myPriority = 0.0;
1985
1986   /* load resources for all available meshers */
1987   SMESH::InitAvailableHypotheses();
1988 }
1989
1990 //=============================================================================
1991 /*!
1992  *
1993  */
1994 //=============================================================================
1995 SMESHGUI::~SMESHGUI()
1996 {
1997 }
1998
1999 //=============================================================================
2000 /*!
2001  *
2002  */
2003 //=============================================================================
2004 LightApp_SelectionMgr* SMESHGUI::selectionMgr()
2005 {
2006   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
2007   if( anApp )
2008     return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
2009   else
2010     return 0;
2011 }
2012
2013 //=============================================================================
2014 /*!
2015  *
2016  */
2017 //=============================================================================
2018 bool SMESHGUI::automaticUpdate(unsigned int requestedSize, bool* limitExceeded)
2019 {
2020   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
2021   if ( !resMgr )
2022     return false;
2023
2024   bool autoUpdate  = resMgr->booleanValue( "SMESH", "auto_update",  false );
2025   long updateLimit = resMgr->integerValue( "SMESH", "update_limit", 500000 );
2026   bool exceeded = updateLimit > 0 && requestedSize > updateLimit;
2027   if ( limitExceeded ) *limitExceeded = autoUpdate && exceeded;
2028   return autoUpdate && !exceeded;
2029 }
2030
2031 //=============================================================================
2032 /*!
2033  *
2034  */
2035 //=============================================================================
2036 bool SMESHGUI::automaticUpdate( SMESH::SMESH_IDSource_ptr theMesh,
2037                                 int* entities, bool* limitExceeded, int* hidden, long* nbElements )
2038 {
2039   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
2040   if ( !resMgr )
2041     return false;
2042
2043   bool autoUpdate  = resMgr->booleanValue( "SMESH", "auto_update", false );
2044   long updateLimit = resMgr->integerValue( "SMESH", "update_limit", 500000 );
2045   bool incrementalLimit = resMgr->booleanValue( "SMESH", "incremental_limit", false );
2046
2047   SMESH::long_array_var info = theMesh->GetMeshInfo();
2048   long nbOdElems = info[SMDSEntity_0D];
2049   long nbEdges   = info[SMDSEntity_Edge] + info[SMDSEntity_Quad_Edge];
2050   long nbFaces   = info[SMDSEntity_Triangle]   + info[SMDSEntity_Quad_Triangle]   + info[SMDSEntity_BiQuad_Triangle] + 
2051                    info[SMDSEntity_Quadrangle] + info[SMDSEntity_Quad_Quadrangle] + info[SMDSEntity_BiQuad_Quadrangle] + 
2052                    info[SMDSEntity_Polygon] + info[SMDSEntity_Quad_Polygon];
2053   long nbVolumes = info[SMDSEntity_Tetra]   + info[SMDSEntity_Quad_Tetra] + 
2054                    info[SMDSEntity_Hexa]    + info[SMDSEntity_Quad_Hexa] + info[SMDSEntity_TriQuad_Hexa] + 
2055                    info[SMDSEntity_Pyramid] + info[SMDSEntity_Quad_Pyramid] + 
2056                    info[SMDSEntity_Penta]   + info[SMDSEntity_Quad_Penta] + 
2057                    info[SMDSEntity_Polyhedra] + 
2058                    info[SMDSEntity_Hexagonal_Prism];
2059   long nbBalls   = info[SMDSEntity_Ball];
2060
2061   long requestedSize = nbOdElems + nbBalls + nbEdges + nbFaces + nbVolumes;
2062   *nbElements = requestedSize;
2063   
2064   *entities = SMESH_Actor::eAllEntity;
2065   *hidden   = 0;
2066
2067   bool exceeded = updateLimit > 0 && requestedSize > updateLimit;
2068
2069   if ( limitExceeded ) *limitExceeded = autoUpdate && exceeded;
2070
2071   if ( incrementalLimit ) {
2072     long total     = 0;
2073
2074     if ( nbOdElems > 0 ) {
2075       if ( total + nbOdElems > updateLimit ) {
2076         *entities = *entities & ~SMESH_Actor::e0DElements;
2077         *hidden = *hidden | SMESH_Actor::e0DElements;
2078       }
2079       else
2080         exceeded = false;
2081     }
2082     total += nbOdElems;
2083
2084     if ( nbEdges > 0 ) {
2085       if ( total + nbEdges > updateLimit ) {
2086         *entities = *entities & ~SMESH_Actor::eEdges;
2087         *hidden = *hidden | SMESH_Actor::eEdges;
2088       }
2089       else
2090         exceeded = false;
2091     }
2092     total += nbEdges;
2093
2094     if ( nbFaces > 0 ) {
2095       if ( total + nbFaces > updateLimit ) {
2096         *entities = *entities & ~SMESH_Actor::eFaces;
2097         *hidden = *hidden | SMESH_Actor::eFaces;
2098       }
2099       else
2100         exceeded = false;
2101     }
2102     total += nbFaces;
2103
2104     if ( nbVolumes > 0 ) {
2105       if ( total + nbVolumes > updateLimit ) {
2106         *entities = *entities & ~SMESH_Actor::eVolumes;
2107         *hidden = *hidden | SMESH_Actor::eVolumes;
2108       }
2109       else
2110         exceeded = false;
2111     }
2112     total += nbVolumes;
2113
2114     if ( nbBalls > 0 ) {
2115       if ( total + nbBalls > updateLimit ) {
2116         *entities = *entities & ~SMESH_Actor::eBallElem;
2117         *hidden = *hidden | SMESH_Actor::eBallElem;
2118       }
2119       else
2120         exceeded = false;
2121     }
2122     total += nbBalls;
2123   }
2124
2125   return autoUpdate && !exceeded;
2126 }
2127
2128 //=============================================================================
2129 /*!
2130  *
2131  */
2132 //=============================================================================
2133 SUIT_ResourceMgr* SMESHGUI::resourceMgr()
2134 {
2135   return dynamic_cast<SUIT_ResourceMgr*>( SUIT_Session::session()->resourceMgr() );
2136 }
2137
2138 //=============================================================================
2139 /*!
2140  *
2141  */
2142 //=============================================================================
2143 SMESHGUI* SMESHGUI::GetSMESHGUI()
2144 {
2145   SMESHGUI* smeshMod = 0;
2146   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
2147   if ( app )
2148   {
2149     CAM_Module* module = app->module( "Mesh" );
2150     smeshMod = dynamic_cast<SMESHGUI*>( module );
2151   }
2152
2153   if ( smeshMod && smeshMod->application() && smeshMod->application()->activeStudy() )
2154   {
2155     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( smeshMod->application()->activeStudy() );
2156     if ( study )
2157     {
2158       _PTR(Study) aStudy = study->studyDS();
2159       if ( aStudy )
2160         GetSMESHGen()->SetCurrentStudy( _CAST(Study,aStudy)->GetStudy() );
2161     }
2162   }
2163
2164   return smeshMod;
2165 }
2166
2167 extern "C"
2168 {
2169   Standard_EXPORT SMESHGUI* GetComponentGUI()
2170   {
2171     return SMESHGUI::GetSMESHGUI();
2172   }
2173 }
2174
2175 //=============================================================================
2176 /*!
2177  *
2178  */
2179 //=============================================================================
2180 void SMESHGUI::SetState(int aState)
2181 {
2182   myState = aState;
2183 }
2184
2185 //=============================================================================
2186 /*!
2187  *
2188  */
2189 //=============================================================================
2190 void SMESHGUI::ResetState()
2191 {
2192   myState = -1;
2193 }
2194
2195 //=============================================================================
2196 /*!
2197  *
2198  */
2199 //=============================================================================
2200 void SMESHGUI::EmitSignalDeactivateDialog()
2201 {
2202   emit SignalDeactivateActiveDialog();
2203 }
2204
2205 //=============================================================================
2206 /*!
2207  *
2208  */
2209 //=============================================================================
2210 void SMESHGUI::EmitSignalStudyFrameChanged()
2211 {
2212   emit SignalStudyFrameChanged();
2213 }
2214
2215 //=============================================================================
2216 /*!
2217  *
2218  */
2219 //=============================================================================
2220 void SMESHGUI::EmitSignalCloseAllDialogs()
2221 {
2222   emit SignalCloseAllDialogs();
2223 }
2224
2225 //=============================================================================
2226 /*!
2227  *
2228  */
2229 //=============================================================================
2230 void SMESHGUI::EmitSignalVisibilityChanged()
2231 {
2232   emit SignalVisibilityChanged();
2233 }
2234
2235 //=============================================================================
2236 /*!
2237  *
2238  */
2239 //=============================================================================
2240 void SMESHGUI::EmitSignalCloseView()
2241 {
2242   emit SignalCloseView();
2243 }
2244
2245 //=============================================================================
2246 /*!
2247  *
2248  */
2249 //=============================================================================
2250 void SMESHGUI::EmitSignalActivatedViewManager()
2251 {
2252   emit SignalActivatedViewManager();
2253 }
2254
2255 //=============================================================================
2256 /*!
2257  *
2258  */
2259 //=============================================================================
2260 QDialog *SMESHGUI::GetActiveDialogBox()
2261 {
2262   return myActiveDialogBox;
2263 }
2264
2265 //=============================================================================
2266 /*!
2267  *
2268  */
2269 //=============================================================================
2270 void SMESHGUI::SetActiveDialogBox(QDialog * aDlg)
2271 {
2272   myActiveDialogBox = (QDialog *) aDlg;
2273   return;
2274 }
2275
2276 //=============================================================================
2277 /*!
2278  *
2279  */
2280 //=============================================================================
2281 SUIT_Desktop* SMESHGUI::desktop()
2282 {
2283   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
2284   if( app )
2285     return app->desktop();
2286   else
2287     return 0;
2288 }
2289
2290 //=============================================================================
2291 /*!
2292  *
2293  */
2294 //=============================================================================
2295 SalomeApp_Study* SMESHGUI::activeStudy()
2296 {
2297   SUIT_Application* app = SUIT_Session::session()->activeApplication();
2298   if( app )
2299     return dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
2300   else
2301     return NULL;
2302 }
2303
2304 //=============================================================================
2305 /*!
2306  *
2307  */
2308 //=============================================================================
2309 void SMESHGUI::Modified( bool theIsUpdateActions )
2310 {
2311   if( SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() ) ) {
2312     if( SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() ) ) {
2313       appStudy->Modified();
2314       if( theIsUpdateActions )
2315         app->updateActions();
2316     }
2317   }
2318 }
2319
2320 //=============================================================================
2321 /*!
2322  *
2323  */
2324 //=============================================================================
2325 bool SMESHGUI::DefineDlgPosition(QWidget * aDlg, int &x, int &y)
2326 {
2327   /* Here the position is on the bottom right corner - 10 */
2328   // aDlg->resize(QSize().expandedTo(aDlg->minimumSizeHint()));
2329   aDlg->adjustSize();
2330   SUIT_Desktop *PP = desktop();
2331   x = abs(PP->x() + PP->size().width() - aDlg->size().width() - 10);
2332   y = abs(PP->y() + PP->size().height() - aDlg->size().height() - 10);
2333   return true;
2334 }
2335
2336 //=============================================================================
2337 /*!
2338  *
2339  */
2340 //=============================================================================
2341 static int isStudyLocked(_PTR(Study) theStudy){
2342   return theStudy->GetProperties()->IsLocked();
2343 }
2344
2345 static bool checkLock(_PTR(Study) theStudy) {
2346   if (isStudyLocked(theStudy)) {
2347     SUIT_MessageBox::warning( SMESHGUI::desktop(),
2348                               QObject::tr("WRN_WARNING"),
2349                               QObject::tr("WRN_STUDY_LOCKED") );
2350     return true;
2351   }
2352   return false;
2353 }
2354
2355 //=======================================================================
2356 //function : CheckActiveStudyLocked
2357 //purpose  :
2358 //=======================================================================
2359
2360 bool SMESHGUI::isActiveStudyLocked()
2361 {
2362   _PTR(Study) aStudy = activeStudy()->studyDS();
2363   return checkLock( aStudy );
2364 }
2365
2366 //=============================================================================
2367 /*!
2368  *
2369  */
2370 //=============================================================================
2371 bool SMESHGUI::OnGUIEvent( int theCommandID )
2372 {
2373   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( application() );
2374   if( !anApp )
2375     return false;
2376
2377   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument(); //Document OCAF de l'etude active
2378   SUIT_ResourceMgr* mgr = resourceMgr();
2379   if( !mgr )
2380     return false;
2381
2382   if (CORBA::is_nil(GetSMESHGen()->GetCurrentStudy())) {
2383     GetSMESHGen()->SetCurrentStudy(_CAST(Study,aStudy)->GetStudy());
2384   }
2385
2386   SUIT_ViewWindow* view = application()->desktop()->activeWindow();
2387   SVTK_ViewWindow* vtkwnd = dynamic_cast<SVTK_ViewWindow*>( view );
2388
2389   //QAction* act = action( theCommandID );
2390
2391   switch (theCommandID) {
2392   case SMESHOp::OpDelete:
2393     if(checkLock(aStudy)) break;
2394     OnEditDelete();
2395     break;
2396   case SMESHOp::OpImportDAT:
2397   case SMESHOp::OpImportUNV:
2398   case SMESHOp::OpImportMED:
2399   case SMESHOp::OpImportSTL:
2400 #ifdef WITH_CGNS
2401   case SMESHOp::OpImportCGNS:
2402 #endif
2403   case SMESHOp::OpImportSAUV:
2404   case SMESHOp::OpImportGMF:
2405     {
2406       if(checkLock(aStudy)) break;
2407       ::ImportMeshesFromFile(GetSMESHGen(),theCommandID);
2408       break;
2409     }
2410
2411   case SMESHOp::OpFileInformation:
2412     {
2413       SALOME_ListIO selected;
2414       LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
2415       if( aSel )
2416         aSel->selectedObjects( selected );
2417       if( selected.Extent() )
2418       {
2419         Handle(SALOME_InteractiveObject) anIObject = selected.First();
2420         SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIObject);
2421         if ( !aMesh->_is_nil() )
2422         {
2423           SMESHGUI_FileInfoDlg dlg( desktop(), aMesh->GetMEDFileInfo() );
2424           dlg.exec();
2425         }
2426       }
2427       break;
2428     }
2429   case SMESHOp::OpExportDAT:
2430   case SMESHOp::OpExportMED:
2431   case SMESHOp::OpExportUNV:
2432   case SMESHOp::OpExportSTL:
2433 #ifdef WITH_CGNS
2434   case SMESHOp::OpExportCGNS:
2435 #endif
2436   case SMESHOp::OpExportSAUV:
2437   case SMESHOp::OpExportGMF:
2438   case SMESHOp::OpPopupExportDAT:
2439   case SMESHOp::OpPopupExportMED:
2440   case SMESHOp::OpPopupExportUNV:
2441   case SMESHOp::OpPopupExportSTL:
2442 #ifdef WITH_CGNS
2443   case SMESHOp::OpPopupExportCGNS:
2444 #endif
2445   case SMESHOp::OpPopupExportSAUV:
2446   case SMESHOp::OpPopupExportGMF:
2447     {
2448       ::ExportMeshToFile(theCommandID);
2449       break;
2450     }
2451
2452   case SMESHOp::OpReset:                      // SCALAR BAR
2453     {
2454       LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
2455       SALOME_ListIO selected;
2456       if( aSel )
2457         aSel->selectedObjects( selected );
2458
2459       SALOME_ListIteratorOfListIO it(selected);
2460       for( ; it.More(); it.Next()) {
2461         Handle(SALOME_InteractiveObject) anIO = it.Value();
2462         if( anIO->hasEntry() ) {
2463           if( SMESH_Actor* anActor = SMESH::FindActorByEntry( anIO->getEntry() ) ) {
2464             anActor->SetControlMode( SMESH_Actor::eNone );
2465 #ifndef DISABLE_PLOT2DVIEWER
2466             SMESH::ProcessIn2DViewers(anActor,SMESH::RemoveFrom2dViewer);
2467 #endif
2468           }
2469         }
2470       }
2471       SMESH::UpdateView();
2472       break;
2473     }
2474   case SMESHOp::OpScalarBarProperties:
2475     {
2476       SMESHGUI_Preferences_ScalarBarDlg::ScalarBarProperties( this );
2477       break;
2478     }
2479   case SMESHOp::OpShowScalarBar:
2480     {
2481       // show/hide scalar bar
2482       ::ShowElement(theCommandID);
2483       break;
2484     }
2485   case SMESHOp::OpSaveDistribution:
2486     {
2487       // dump control distribution data to the text file
2488       ::SaveDistribution();
2489       break;
2490     }
2491
2492   case SMESHOp::OpShowDistribution:
2493     {
2494       // show/hide distribution
2495       ::ShowElement(theCommandID);
2496       break;
2497     }
2498
2499 #ifndef DISABLE_PLOT2DVIEWER
2500   case SMESHOp::OpPlotDistribution:
2501     {
2502       // plot distribution
2503       ::PlotDistribution();
2504       break;
2505     }
2506 #endif
2507
2508     // Auto-color
2509   case SMESHOp::OpAutoColor:
2510     ::AutoColor();
2511   break;
2512
2513   case SMESHOp::OpDisableAutoColor:
2514     ::DisableAutoColor();
2515   break;
2516
2517   case SMESHOp::OpClipping:
2518   case SMESHOp::OpTransparency:
2519   case SMESHOp::OpProperties: // Display preferences (colors, shrink size, line width, ...)
2520
2521     // Display Mode
2522   case SMESHOp::OpDMWireframe:
2523   case SMESHOp::OpDMShading:
2524   case SMESHOp::OpDMNodes:
2525   case SMESHOp::OpDMShrink:
2526     ::SetDisplayMode(theCommandID, myMarkerMap);
2527   break;
2528
2529   //2D quadratic representation
2530   case SMESHOp::OpRepresentationLines:
2531   case SMESHOp::OpRepresentationArcs:
2532     ::SetDisplayMode(theCommandID, myMarkerMap);
2533   break;
2534
2535   // Display Entity
2536   case SMESHOp::OpDE0DElements:
2537   case SMESHOp::OpDEEdges:
2538   case SMESHOp::OpDEFaces:
2539   case SMESHOp::OpDEVolumes:
2540   case SMESHOp::OpDEBalls:
2541   case SMESHOp::OpDEAllEntity:
2542     ::SetDisplayEntity(theCommandID);
2543   break;
2544
2545   // Choose entities to be displayed
2546   case SMESHOp::OpDEChoose:
2547     {
2548       ( new SMESHGUI_DisplayEntitiesDlg( SMESHGUI::desktop() ) )->exec();
2549       break;
2550     }
2551
2552   case SMESHOp::OpOrientationOnFaces:
2553     {
2554       LightApp_SelectionMgr* mgr = selectionMgr();
2555       SALOME_ListIO selected; mgr->selectedObjects( selected );
2556
2557       SALOME_ListIteratorOfListIO it(selected);
2558       for( ; it.More(); it.Next()) {
2559         Handle(SALOME_InteractiveObject) anIObject = it.Value();
2560         if(anIObject->hasEntry()) {
2561           if(SMESH_Actor *anActor = SMESH::FindActorByEntry(anIObject->getEntry())){
2562             anActor->SetFacesOriented( !anActor->GetFacesOriented() );
2563           }
2564         }
2565       }
2566       break;
2567     }
2568
2569   case SMESHOp::OpUpdate:
2570     {
2571       if(checkLock(aStudy)) break;
2572       SUIT_OverrideCursor wc;
2573       try {
2574 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
2575         OCC_CATCH_SIGNALS;
2576 #endif
2577         SMESH::UpdateView();
2578       }
2579       catch (std::bad_alloc) { // PAL16774 (Crash after display of many groups)
2580         SMESH::OnVisuException();
2581       }
2582       catch (...) { // PAL16774 (Crash after display of many groups)
2583         SMESH::OnVisuException();
2584       }
2585
2586       SALOME_ListIO l;
2587       LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
2588       aSel->selectedObjects( l );
2589       aSel->setSelectedObjects( l );
2590       break;
2591     }
2592
2593   case SMESHOp::OpHide:
2594   case SMESHOp::OpShow:
2595   case SMESHOp::OpShowOnly:
2596     {
2597       SUIT_OverrideCursor wc;
2598       SMESH::EDisplaing anAction;
2599       switch (theCommandID) {
2600       case SMESHOp::OpHide:     anAction = SMESH::eErase; break;
2601       case SMESHOp::OpShow:     anAction = SMESH::eDisplay; break;
2602       case SMESHOp::OpShowOnly: anAction = SMESH::eDisplayOnly; break;
2603       }
2604
2605       LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
2606       SALOME_ListIO sel_objects, to_process;
2607       if (aSel)
2608         aSel->selectedObjects( sel_objects );
2609
2610       if ( theCommandID==SMESHOp::OpShowOnly )
2611       {
2612         //MESSAGE("anAction = SMESH::eDisplayOnly");
2613         startOperation( myEraseAll );
2614       }
2615
2616       extractContainers( sel_objects, to_process );
2617
2618       try {
2619 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
2620         OCC_CATCH_SIGNALS;
2621 #endif
2622         if (vtkwnd) {
2623           SALOME_ListIteratorOfListIO It( to_process );
2624           for ( ; It.More(); It.Next())
2625           {
2626             Handle(SALOME_InteractiveObject) IOS = It.Value();
2627             if ( IOS->hasEntry() )
2628             {
2629               if ( !SMESH::UpdateView( anAction, IOS->getEntry() )) {
2630                 SMESHGUI::GetSMESHGUI()->EmitSignalVisibilityChanged();
2631                 break; // PAL16774 (Crash after display of many groups)
2632               }
2633               if (anAction == SMESH::eDisplayOnly)
2634                 anAction = SMESH::eDisplay;
2635             }
2636           }
2637         }
2638
2639         // PAL13338 + PAL15161 -->
2640         if ( ( theCommandID==SMESHOp::OpShow || theCommandID==SMESHOp::OpShowOnly ) && !checkLock(aStudy)) {
2641           SMESH::UpdateView();
2642           SMESHGUI::GetSMESHGUI()->EmitSignalVisibilityChanged();
2643         }
2644         // PAL13338 + PAL15161 <--
2645       }
2646       catch (...) { // PAL16774 (Crash after display of many groups)
2647         SMESH::OnVisuException();
2648       }
2649
2650       if (anAction == SMESH::eErase) {
2651         SALOME_ListIO l1;
2652         aSel->setSelectedObjects( l1 );
2653       }
2654       else
2655         aSel->setSelectedObjects( to_process );
2656
2657       break;
2658     }
2659
2660   case SMESHOp::OpNode:
2661     {
2662       if(checkLock(aStudy)) break;
2663
2664       if ( vtkwnd ) {
2665         EmitSignalDeactivateDialog();
2666
2667         ( new SMESHGUI_NodesDlg( this ) )->show();
2668       }
2669       else {
2670         SUIT_MessageBox::warning(desktop(),tr("SMESH_WRN_WARNING"),tr("SMESH_WRN_VIEWER_VTK"));
2671       }
2672       break;
2673     }
2674
2675   case SMESHOp::OpCreateMesh:
2676   case SMESHOp::OpCreateSubMesh:
2677   case SMESHOp::OpEditMeshOrSubMesh:
2678   case SMESHOp::OpCompute:
2679   case SMESHOp::OpPreCompute:
2680   case SMESHOp::OpEvaluate:
2681   case SMESHOp::OpMeshOrder:
2682     startOperation( theCommandID );
2683     break;
2684   case SMESHOp::OpCopyMesh:
2685     {
2686       if (checkLock(aStudy)) break;
2687       EmitSignalDeactivateDialog();
2688       ( new SMESHGUI_CopyMeshDlg( this ) )->show();
2689     }
2690     break;
2691   case SMESHOp::OpBuildCompoundMesh:
2692     {
2693       if (checkLock(aStudy)) break;
2694       EmitSignalDeactivateDialog();
2695       ( new SMESHGUI_BuildCompoundDlg( this ) )->show();
2696     }
2697     break;
2698
2699   case SMESHOp::OpDiagonalInversion:
2700   case SMESHOp::OpUnionOfTwoTriangle:
2701     {
2702       if ( !vtkwnd )
2703       {
2704         SUIT_MessageBox::warning( desktop(), tr( "SMESH_WRN_WARNING" ), tr( "NOT_A_VTK_VIEWER" ) );
2705         break;
2706       }
2707
2708       if ( checkLock( aStudy ) )
2709         break;
2710
2711       /*Standard_Boolean aRes;
2712       SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IObject);
2713       if ( aMesh->_is_nil() )
2714       {
2715         SUIT_MessageBox::warning(GetDesktop(), tr( "SMESH_WRN_WARNING" ),
2716           tr( "SMESH_BAD_SELECTION" ) );
2717         break;
2718       }
2719       */
2720       EmitSignalDeactivateDialog();
2721       if ( theCommandID == SMESHOp::OpDiagonalInversion )
2722         ( new SMESHGUI_TrianglesInversionDlg( this ) )->show();
2723       else
2724         ( new SMESHGUI_UnionOfTwoTrianglesDlg( this ) )->show();
2725       break;
2726     }
2727   case SMESHOp::OpOrientation:
2728   case SMESHOp::OpUnionOfTriangles:
2729   case SMESHOp::OpCuttingOfQuadrangles:
2730   case SMESHOp::OpSplitVolumes:
2731     {
2732       if ( !vtkwnd )
2733       {
2734         SUIT_MessageBox::warning( desktop(), tr( "SMESH_WRN_WARNING" ), tr( "NOT_A_VTK_VIEWER" ) );
2735         break;
2736       }
2737
2738       if ( checkLock( aStudy ) )
2739         break;
2740
2741       EmitSignalDeactivateDialog();
2742       SMESHGUI_MultiEditDlg* aDlg = NULL;
2743       if ( theCommandID == SMESHOp::OpOrientation )
2744         aDlg = new SMESHGUI_ChangeOrientationDlg(this);
2745       else if ( theCommandID == SMESHOp::OpUnionOfTriangles )
2746         aDlg = new SMESHGUI_UnionOfTrianglesDlg(this);
2747       else if ( theCommandID == SMESHOp::OpSplitVolumes )
2748         aDlg = new SMESHGUI_SplitVolumesDlg(this);
2749       else
2750         aDlg = new SMESHGUI_CuttingOfQuadsDlg(this);
2751
2752       aDlg->show();
2753       break;
2754     }
2755   case SMESHOp::OpSmoothing:
2756     {
2757       if(checkLock(aStudy)) break;
2758       if( vtkwnd ) {
2759         EmitSignalDeactivateDialog();
2760         ( new SMESHGUI_SmoothingDlg( this ) )->show();
2761       }
2762       else {
2763         SUIT_MessageBox::warning(desktop(), tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"));
2764       }
2765       break;
2766     }
2767   case SMESHOp::OpExtrusion:
2768     {
2769       if (checkLock(aStudy)) break;
2770       if (vtkwnd) {
2771         EmitSignalDeactivateDialog();
2772         ( new SMESHGUI_ExtrusionDlg ( this ) )->show();
2773       } else {
2774         SUIT_MessageBox::warning(desktop(),tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"));
2775       }
2776       break;
2777     }
2778   case SMESHOp::OpExtrusionAlongAPath:
2779     {
2780       if (checkLock(aStudy)) break;
2781       if (vtkwnd) {
2782         EmitSignalDeactivateDialog();
2783         ( new SMESHGUI_ExtrusionAlongPathDlg( this ) )->show();
2784       } else {
2785         SUIT_MessageBox::warning(desktop(),tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"));
2786       }
2787       break;
2788     }
2789   case SMESHOp::OpRevolution:
2790     {
2791       if(checkLock(aStudy)) break;
2792       if( vtkwnd ) {
2793         EmitSignalDeactivateDialog();
2794         ( new SMESHGUI_RevolutionDlg( this ) )->show();
2795       }
2796       else {
2797         SUIT_MessageBox::warning(desktop(),tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"));
2798       }
2799       break;
2800     }
2801   case SMESHOp::OpPatternMapping:
2802     {
2803       if ( checkLock( aStudy ) )
2804         break;
2805       if ( vtkwnd )
2806       {
2807         EmitSignalDeactivateDialog();
2808         ( new SMESHGUI_MeshPatternDlg( this ) )->show();
2809       }
2810       else {
2811         SUIT_MessageBox::warning(desktop(),tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"));
2812       }
2813       break;
2814     }
2815   case SMESHOp::OpSplitBiQuadratic:
2816   case SMESHOp::OpConvertMeshToQuadratic:
2817   case SMESHOp::OpCreateBoundaryElements: // create 2D mesh from 3D
2818   case SMESHOp::OpReorientFaces:
2819   case SMESHOp::OpCreateGeometryGroup:
2820     {
2821       startOperation( theCommandID );
2822       break;
2823     }
2824   case SMESHOp::OpCreateGroup:
2825     {
2826       if ( !vtkwnd )
2827       {
2828         SUIT_MessageBox::warning( desktop(), tr( "SMESH_WRN_WARNING" ),tr( "NOT_A_VTK_VIEWER" ) );
2829         break;
2830       }
2831
2832       if(checkLock(aStudy)) break;
2833       EmitSignalDeactivateDialog();
2834       SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_nil();
2835
2836       LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
2837       SALOME_ListIO selected;
2838       if( aSel )
2839         aSel->selectedObjects( selected );
2840
2841       int nbSel = selected.Extent();
2842       if (nbSel == 1) {
2843         // check if mesh is selected
2844         aMesh = SMESH::GetMeshByIO( selected.First() );
2845       }
2846       SMESHGUI_GroupDlg *aDlg = new SMESHGUI_GroupDlg( this, aMesh);
2847       aDlg->show();
2848       break;
2849     }
2850
2851   case SMESHOp::OpConstructGroup:
2852     {
2853       if ( !vtkwnd )
2854       {
2855         SUIT_MessageBox::warning( desktop(), tr( "SMESH_WRN_WARNING" ),tr( "NOT_A_VTK_VIEWER" ) );
2856         break;
2857       }
2858
2859       if(checkLock(aStudy)) break;
2860       EmitSignalDeactivateDialog();
2861
2862       LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
2863       SALOME_ListIO selected;
2864       if( aSel )
2865         aSel->selectedObjects( selected );
2866
2867       int nbSel = selected.Extent();
2868       if (nbSel == 1) {
2869         // check if submesh is selected
2870         Handle(SALOME_InteractiveObject) IObject = selected.First();
2871         if (IObject->hasEntry()) {
2872           _PTR(SObject) aSObj = aStudy->FindObjectID(IObject->getEntry());
2873           if( aSObj ) {
2874             SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( SMESH::SObjectToObject( aSObj ) );
2875             if (!aSubMesh->_is_nil()) {
2876               try {
2877                 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
2878                 // get submesh elements list by types
2879                 SMESH::long_array_var aNodes = aSubMesh->GetElementsByType(SMESH::NODE);
2880                 SMESH::long_array_var aEdges = aSubMesh->GetElementsByType(SMESH::EDGE);
2881                 SMESH::long_array_var aFaces = aSubMesh->GetElementsByType(SMESH::FACE);
2882                 SMESH::long_array_var aVolumes = aSubMesh->GetElementsByType(SMESH::VOLUME);
2883                 // create group for each type o elements
2884                 QString aName = IObject->getName();
2885                 QStringList anEntryList;
2886                 if (aNodes->length() > 0) {
2887                   SMESH::SMESH_Group_var aGroup = SMESH::AddGroup(aMesh, SMESH::NODE, aName + "_Nodes");
2888                   aGroup->Add(aNodes.inout());
2889                   if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( aGroup ) )
2890                     anEntryList.append( aSObject->GetID().c_str() );
2891                 }
2892                 if (aEdges->length() > 0) {
2893                   SMESH::SMESH_Group_var aGroup = SMESH::AddGroup(aMesh, SMESH::EDGE, aName + "_Edges");
2894                   aGroup->Add(aEdges.inout());
2895                   if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( aGroup ) )
2896                     anEntryList.append( aSObject->GetID().c_str() );
2897                 }
2898                 if (aFaces->length() > 0) {
2899                   SMESH::SMESH_Group_var aGroup = SMESH::AddGroup(aMesh, SMESH::FACE, aName + "_Faces");
2900                   aGroup->Add(aFaces.inout());
2901                   if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( aGroup ) )
2902                     anEntryList.append( aSObject->GetID().c_str() );
2903                 }
2904                 if (aVolumes->length() > 0) {
2905                   SMESH::SMESH_Group_var aGroup = SMESH::AddGroup(aMesh, SMESH::VOLUME, aName + "_Volumes");
2906                   aGroup->Add(aVolumes.inout());
2907                   if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( aGroup ) )
2908                     anEntryList.append( aSObject->GetID().c_str() );
2909                 }
2910                 updateObjBrowser();
2911                 anApp->browseObjects( anEntryList );
2912               }
2913               catch(const SALOME::SALOME_Exception & S_ex){
2914                 SalomeApp_Tools::QtCatchCorbaException(S_ex);
2915               }
2916             }
2917           }
2918         }
2919       }
2920       else if(nbSel==0) {
2921         SUIT_MessageBox::warning(desktop(),
2922                                  tr("SMESH_WRN_WARNING"),
2923                                  tr("SMESH_WRN_NO_AVAILABLE_DATA"));
2924       }
2925       break;
2926     }
2927
2928   case SMESHOp::OpEditGroup:
2929     {
2930       if ( !vtkwnd )
2931       {
2932         SUIT_MessageBox::warning( desktop(), tr( "SMESH_WRN_WARNING" ),tr( "NOT_A_VTK_VIEWER" ) );
2933         break;
2934       }
2935
2936       if(checkLock(aStudy)) break;
2937       EmitSignalDeactivateDialog();
2938
2939       LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
2940       SALOME_ListIO selected;
2941       if( aSel )
2942         aSel->selectedObjects( selected );
2943
2944       SALOME_ListIteratorOfListIO It (selected);
2945       int nbSelectedGroups = 0;
2946       for ( ; It.More(); It.Next() )
2947       {
2948         SMESH::SMESH_GroupBase_var aGroup =
2949           SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(It.Value());
2950         if (!aGroup->_is_nil()) {
2951           nbSelectedGroups++;
2952           SMESHGUI_GroupDlg *aDlg = new SMESHGUI_GroupDlg( this, aGroup);
2953           aDlg->show();
2954         }
2955       }
2956       if (nbSelectedGroups == 0)
2957         {
2958           SMESHGUI_GroupDlg *aDlg = new SMESHGUI_GroupDlg( this, SMESH::SMESH_GroupBase::_nil());
2959           aDlg->show();
2960         }
2961       break;
2962     }
2963
2964   case SMESHOp::OpAddElemGroupPopup:     // Add elements to group
2965     {
2966       if(checkLock(aStudy)) break;
2967       if (myState == 800) {
2968         SMESHGUI_GroupDlg *aDlg = (SMESHGUI_GroupDlg*) myActiveDialogBox;
2969         if (aDlg) aDlg->onAdd();
2970       }
2971       break;
2972     }
2973
2974   case SMESHOp::OpRemoveElemGroupPopup:  // Remove elements from group
2975     {
2976       if(checkLock(aStudy)) break;
2977       if (myState == 800) {
2978         SMESHGUI_GroupDlg *aDlg = (SMESHGUI_GroupDlg*) myActiveDialogBox;
2979         if (aDlg) aDlg->onRemove();
2980       }
2981       break;
2982     }
2983
2984   case SMESHOp::OpEditGeomGroupAsGroup:
2985     {
2986       if ( !vtkwnd )
2987       {
2988         SUIT_MessageBox::warning( desktop(), tr( "SMESH_WRN_WARNING" ),tr( "NOT_A_VTK_VIEWER" ) );
2989         break;
2990       }
2991
2992       if(checkLock(aStudy)) break;
2993       EmitSignalDeactivateDialog();
2994
2995       LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
2996       SALOME_ListIO selected;
2997       if( aSel )
2998         aSel->selectedObjects( selected );
2999
3000       SALOME_ListIteratorOfListIO It (selected);
3001       for ( ; It.More(); It.Next() )
3002       {
3003         SMESH::SMESH_GroupOnGeom_var aGroup =
3004           SMESH::IObjectToInterface<SMESH::SMESH_GroupOnGeom>(It.Value());
3005         if (!aGroup->_is_nil()) {
3006           SMESHGUI_GroupDlg *aDlg = new SMESHGUI_GroupDlg( this, aGroup, true );
3007           aDlg->show();
3008         }
3009         else
3010         {
3011           SMESH::SMESH_GroupOnFilter_var aGroup =
3012             SMESH::IObjectToInterface<SMESH::SMESH_GroupOnFilter>(It.Value());
3013           if (!aGroup->_is_nil()) {
3014             SMESHGUI_GroupDlg *aDlg = new SMESHGUI_GroupDlg( this, aGroup, true );
3015             aDlg->show();
3016           }
3017         }
3018       }
3019       break;
3020     }
3021
3022     case SMESHOp::OpUnionGroups:
3023     case SMESHOp::OpIntersectGroups:
3024     case SMESHOp::OpCutGroups:
3025     {
3026       if ( !vtkwnd )
3027       {
3028         SUIT_MessageBox::warning( desktop(), tr( "SMESH_WRN_WARNING" ),tr( "NOT_A_VTK_VIEWER" ) );
3029         break;
3030       }
3031
3032       if ( checkLock( aStudy ) )
3033         break;
3034
3035       EmitSignalDeactivateDialog();
3036
3037       SMESHGUI_GroupOpDlg* aDlg = 0;
3038       if ( theCommandID == SMESHOp::OpUnionGroups )
3039         aDlg = new SMESHGUI_UnionGroupsDlg( this );
3040       else if ( theCommandID == SMESHOp::OpIntersectGroups )
3041         aDlg = new SMESHGUI_IntersectGroupsDlg( this );
3042       else
3043         aDlg = new SMESHGUI_CutGroupsDlg( this );
3044
3045       aDlg->show();
3046
3047       break;
3048     }
3049
3050     case SMESHOp::OpGroupUnderlyingElem: // Create groups of entities from existing groups of superior dimensions
3051     {
3052       if ( checkLock( aStudy ) )
3053         break;
3054
3055       EmitSignalDeactivateDialog();
3056       SMESHGUI_GroupOpDlg* aDlg = new SMESHGUI_DimGroupDlg( this );
3057       aDlg->show();
3058
3059       break;
3060     }
3061
3062     case SMESHOp::OpDeleteGroup: // Delete groups with their contents
3063     {
3064       if ( !vtkwnd )
3065       {
3066         SUIT_MessageBox::warning( desktop(), tr( "SMESH_WRN_WARNING" ),tr( "NOT_A_VTK_VIEWER" ) );
3067         break;
3068       }
3069
3070       if ( checkLock( aStudy ) )
3071         break;
3072
3073       EmitSignalDeactivateDialog();
3074