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