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