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