Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //  File   : SMESHGUI.cxx
23 //  Author : Nicolas REJNERI
24 //  Module : SMESH
25 //  $Header$
26
27 #include "SMESHGUI.h"
28 #include "SMESHGUI_AddHypothesisDlg.h"
29 #include "SMESHGUI_AddAlgorithmDlg.h"
30 #include "SMESHGUI_InitMeshDlg.h"
31 #include "SMESHGUI_AddSubMeshDlg.h"
32 #include "SMESHGUI_NodesDlg.h"
33 #include "SMESHGUI_TransparencyDlg.h"
34 #include "SMESHGUI_GroupDlg.h"
35 #include "SMESHGUI_RemoveNodesDlg.h"
36 #include "SMESHGUI_RemoveElementsDlg.h"
37 #include "SMESHGUI_MeshInfosDlg.h"
38 #include "SMESHGUI_Preferences_ColorDlg.h"
39 #include "SMESHGUI_Preferences_ScalarBarDlg.h"
40 #include "SMESHGUI_Preferences_SelectionDlg.h"
41 #include "SMESHGUI_aParameterDlg.h"
42 #include "SMESHGUI_ComputeScalarValue.h"
43 #include "SMESHGUI_MoveNodesDlg.h"
44 #include "SMESHGUI_OrientationElementsDlg.h"
45 #include "SMESHGUI_DiagonalInversionDlg.h"
46 #include "SMESHGUI_AddFaceDlg.h"
47 #include "SMESHGUI_AddEdgeDlg.h"
48 #include "SMESHGUI_AddVolumeDlg.h"
49 #include "SMESHGUI_EditHypothesesDlg.h"
50 #include "SMESHGUI_CreateHypothesesDlg.h"
51 #include "SMESHGUI_FilterDlg.h"
52 #include "SMESHGUI_XmlHandler.h"
53
54 #include "SMESH_Actor.h"
55 #include "SMESH_Object.h"
56
57 // SALOME Includes
58 #include "Utils_ORB_INIT.hxx"
59 #include "Utils_SINGLETON.hxx"
60
61 #include "QAD.h"
62 #include "QAD_Tools.h"
63 #include "QAD_Config.h"
64 #include "QAD_Settings.h"
65 #include "QAD_RightFrame.h"
66 #include "QAD_MessageBox.h"
67 #include "QAD_Resource.h"
68 #include "QAD_FileDlg.h"
69 #include "QAD_Desktop.h"
70 #include "QAD_ResourceMgr.h"
71 #include "QAD_WaitCursor.h"
72
73 #include "OCCViewer_ViewPort.h"
74 #include "OCCViewer_ViewPort3d.h"
75 #include "OCCViewer_Viewer3d.h"
76
77 #include "GEOM_InteractiveObject.hxx"
78
79 #include "SALOME_NamingService.hxx"
80 #include "SALOME_ListIteratorOfListIO.hxx"
81 #include "SALOME_InteractiveObject.hxx"
82
83 #include "SALOMEGUI_Desktop.h"
84 #include "SALOMEGUI_NameDlg.h"
85 #include "SALOMEGUI_ImportOperation.h"
86 #include "SALOMEGUI_QtCatchCorbaException.hxx"
87
88 #include "utilities.h"
89 #include <dlfcn.h>
90
91 #include "SMDS_Mesh.hxx"
92 #include "SMESHDS_Document.hxx"
93 #include "Document_Reader.h"
94 #include "Document_Writer.h"
95 #include "Mesh_Reader.h"
96 #include "Mesh_Writer.h"
97
98 #include "DriverDAT_R_SMESHDS_Document.h"
99 #include "DriverMED_R_SMESHDS_Document.h"
100 #include "DriverUNV_R_SMESHDS_Document.h"
101 #include "DriverDAT_W_SMESHDS_Document.h"
102 #include "DriverMED_W_SMESHDS_Document.h"
103 #include "DriverUNV_W_SMESHDS_Document.h"
104 #include "DriverDAT_R_SMESHDS_Mesh.h"
105 #include "DriverMED_R_SMESHDS_Mesh.h"
106 #include "DriverUNV_R_SMESHDS_Mesh.h"
107 #include "DriverDAT_W_SMESHDS_Mesh.h"
108 #include "DriverMED_W_SMESHDS_Mesh.h"
109 #include "DriverUNV_W_SMESHDS_Mesh.h"
110
111 // QT Includes
112 #define  INCLUDE_MENUITEM_DEF
113 #include <qapplication.h>
114 #include <qlineedit.h>
115 #include <qmenudata.h>
116 #include <qmenubar.h>
117 #include <qpopupmenu.h>
118 #include <qfont.h>
119 #include <qstring.h>
120 #include <qcheckbox.h>
121 #include <qcolordialog.h>
122 #include <qspinbox.h>
123 #include <qlist.h>
124 #include <qwidget.h>
125 #include <qevent.h>
126 #include <qradiobutton.h>
127
128 // VTK Includes
129 #include "VTKViewer_Common.h"
130 #include "VTKViewer_ViewFrame.h"
131 #include "VTKViewer_InteractorStyleSALOME.h"
132
133 #include <vtkScalarBarActor.h>
134 #include <vtkLegendBoxActor.h>
135 #include <vtkFeatureEdges.h>
136 #include <vtkDoubleArray.h>
137 #include <vtkTextProperty.h>
138
139 // Open CASCADE Includes
140 #include <gp_Pnt.hxx>
141 #include <gp_Vec.hxx>
142
143 #include <boost/shared_ptr.hpp>
144
145 static SMESHGUI *smeshGUI = 0;
146 static CORBA::ORB_var _orb;
147
148 using namespace std;
149
150 typedef pair<int,string> TKeyOfVisualObj;
151 typedef boost::shared_ptr<SMESH_VisualObj> TVisualObjPtr;
152 typedef map<TKeyOfVisualObj,TVisualObjPtr> TVisualObjCont;
153 static TVisualObjCont VISUALOBJCONT;
154
155
156 TVisualObjPtr GetVisualObj(int theStudyId, const char* theEntry){
157   TVisualObjPtr aVisualObj;
158   try{
159     TVisualObjCont::key_type aKey(theStudyId,theEntry);
160     TVisualObjCont::iterator anIter = VISUALOBJCONT.find(aKey);
161     if(anIter != VISUALOBJCONT.end()){
162       aVisualObj = anIter->second;
163     }else{
164       SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
165       SALOMEDS::SObject_var aSObj = aStudy->FindObjectID(theEntry);
166       if(!CORBA::is_nil(aSObj)){
167         SALOMEDS::GenericAttribute_var anAttr;
168         if(aSObj->FindAttribute(anAttr,"AttributeIOR")){
169           SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
170           CORBA::String_var aVal = anIOR->Value();
171           CORBA::Object_var anObj =_orb->string_to_object(aVal.in());
172           if(!CORBA::is_nil(anObj)){
173             //Try narrow to SMESH_Mesh interafce
174             SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObj);
175             if(!aMesh->_is_nil()){
176               aVisualObj.reset(new SMESH_MeshObj(aMesh));
177               aVisualObj->Update();
178               TVisualObjCont::value_type aValue(aKey,aVisualObj); 
179               VISUALOBJCONT.insert(aValue);
180               return aVisualObj;
181             }
182             //Try narrow to SMESH_Group interafce
183             SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow(anObj);
184             if(!aGroup->_is_nil()){
185               SALOMEDS::SObject_var aFatherSObj = aSObj->GetFather();
186               if(aFatherSObj->_is_nil()) return aVisualObj;
187               aFatherSObj = aFatherSObj->GetFather();
188               if(aFatherSObj->_is_nil()) return aVisualObj;
189               CORBA::String_var anEntry = aFatherSObj->GetID();
190               TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
191               if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
192                 aVisualObj.reset(new SMESH_GroupObj(aGroup,aMeshObj));
193                 aVisualObj->Update();
194                 TVisualObjCont::value_type aValue(aKey,aVisualObj); 
195                 VISUALOBJCONT.insert(aValue);
196                 return aVisualObj;
197               }
198             }
199             //Try narrow to SMESH_subMesh interafce
200             SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(anObj);
201             if(!aSubMesh->_is_nil()){
202               SALOMEDS::SObject_var aFatherSObj = aSObj->GetFather();
203               if(aFatherSObj->_is_nil()) return aVisualObj;
204               aFatherSObj = aFatherSObj->GetFather();
205               if(aFatherSObj->_is_nil()) return aVisualObj;
206               CORBA::String_var anEntry = aFatherSObj->GetID();
207               TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
208               if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
209                 aVisualObj.reset(new SMESH_subMeshObj(aSubMesh,aMeshObj));
210                 aVisualObj->Update();
211                 TVisualObjCont::value_type aValue(aKey,aVisualObj); 
212                 VISUALOBJCONT.insert(aValue);
213                 return aVisualObj;
214               }
215             }
216           }
217         }
218       }
219     }
220   }catch(...){
221     INFOS("GetMeshObj - There is no SMESH_Mesh object for the SALOMEDS::Strudy and Entry!!!");
222   }
223   return aVisualObj;
224 }
225
226
227 VTKViewer_ViewFrame* GetVtkViewFrame(QAD_StudyFrame* theStudyFrame){
228   QAD_ViewFrame* aViewFrame = theStudyFrame->getRightFrame()->getViewFrame();
229   return dynamic_cast<VTKViewer_ViewFrame*>(aViewFrame);
230 }
231
232 void UpdateSelectionProp() {
233   QAD_Study* aStudy = SMESHGUI::GetSMESHGUI()->GetActiveStudy();
234   QList<QAD_StudyFrame> aFrameList = aStudy->getStudyFrames();
235
236   QString SCr, SCg, SCb;
237   SCr = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorRed");
238   SCg = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorGreen");
239   SCb = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorBlue");
240   QColor aHiColor = Qt::white;
241   if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
242     aHiColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
243
244   SCr = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorRed");
245   SCg = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorGreen");
246   SCb = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorBlue");
247   QColor aSelColor = Qt::yellow;
248   if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
249     aSelColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
250   QString SW = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectWidth");
251   if (SW.isEmpty()) SW = "5";
252
253   SCr = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorRed");
254   SCg = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorGreen");
255   SCb = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorBlue");
256   QColor aPreColor = Qt::cyan;
257   if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
258     aPreColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
259   QString PW = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectWidth");
260   if (PW.isEmpty()) PW = "5";
261
262   QString SP1 = QAD_CONFIG->getSetting("SMESH:SettingsNodeSelectTol");
263   if (SP1.isEmpty()) SP1 = "0.025";
264   QString SP2 = QAD_CONFIG->getSetting("SMESH:SettingsElementsSelectTol");
265   if (SP2.isEmpty()) SP2 = "0.001";
266
267   for (QAD_StudyFrame* aStudyFrame = aFrameList.first(); aStudyFrame; aStudyFrame = aFrameList.next()) {
268     if (aStudyFrame->getTypeView() == VIEW_VTK) {
269       VTKViewer_ViewFrame* aVtkViewFrame = GetVtkViewFrame(aStudyFrame);
270       if (!aVtkViewFrame) continue;
271       // update VTK viewer properties
272       VTKViewer_RenderWindowInteractor* anInteractor = aVtkViewFrame->getRWInteractor();
273       if (anInteractor) {
274         // mesh element selection
275         anInteractor->SetSelectionProp(aSelColor.red()/255., aSelColor.green()/255., 
276                                        aSelColor.blue()/255., SW.toInt());
277           
278         // tolerances
279         anInteractor->SetSelectionTolerance(SP1.toDouble(), SP2.toDouble());
280           
281         // pre-selection
282         VTKViewer_InteractorStyleSALOME* aStyle = anInteractor->GetInteractorStyleSALOME();
283         if (aStyle) {
284           aStyle->setPreselectionProp(aPreColor.red()/255., aPreColor.green()/255., 
285                                       aPreColor.blue()/255., PW.toInt());
286         }
287       }
288       // update actors
289       vtkRenderer* aRenderer = aVtkViewFrame->getRenderer();
290       vtkActorCollection *aCollection = aRenderer->GetActors();
291       aCollection->InitTraversal();
292       while(vtkActor *anAct = aCollection->GetNextActor()){
293         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
294           anActor->SetHighlightColor(aHiColor.red()/255., aHiColor.green()/255., 
295                                      aHiColor.blue()/255.);
296           anActor->SetPreHighlightColor(aPreColor.red()/255., aPreColor.green()/255., 
297                                         aPreColor.blue()/255.);
298         }
299       }
300     }
301   }
302 }
303
304 VTKViewer_ViewFrame* GetCurrentVtkView(){
305   QAD_Study* aStudy = SMESHGUI::GetSMESHGUI()->GetActiveStudy();
306   QAD_StudyFrame *aStudyFrame = aStudy->getActiveStudyFrame();
307   return GetVtkViewFrame(aStudyFrame);
308 }
309
310
311 SMESH_Actor* FindActorByEntry(QAD_StudyFrame *theStudyFrame, 
312                               const char* theEntry)
313 {
314   if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
315     vtkRenderer *aRenderer = aViewFrame->getRenderer();
316     vtkActorCollection *aCollection = aRenderer->GetActors();
317     aCollection->InitTraversal();
318     while(vtkActor *anAct = aCollection->GetNextActor()){
319       if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
320         if(anActor->hasIO()){
321           Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
322           if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
323             return anActor;
324           }
325         }
326       }
327     }
328   }
329   return NULL;
330 }
331
332
333 SMESH_Actor* FindActorByEntry(const char* theEntry){
334   QAD_Study* aStudy = SMESHGUI::GetSMESHGUI()->GetActiveStudy();
335   QAD_StudyFrame *aStudyFrame = aStudy->getActiveStudyFrame();
336   return FindActorByEntry(aStudyFrame,theEntry);
337 }
338
339
340 SMESH_Actor* CreateActor(SALOMEDS::Study_ptr theStudy,
341                          const char* theEntry,
342                          int theIsClear = false)
343 {
344   SMESH_Actor *anActor = NULL;
345   CORBA::Long anId = theStudy->StudyId();
346   if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
347     SALOMEDS::SObject_var aSObj = theStudy->FindObjectID(theEntry);
348     if(!aSObj->_is_nil()){
349       SALOMEDS::GenericAttribute_var anAttr;
350       if(aSObj->FindAttribute(anAttr,"AttributeName")){
351         SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
352         CORBA::String_var aVal = aName->Value();
353         string aNameVal = aVal.in();
354         SMESH::FilterManager_ptr aFilterMgr = smeshGUI->GetFilterMgr();
355         anActor = SMESH_Actor::New(aVisualObj,aFilterMgr,theEntry,aNameVal.c_str(),theIsClear);
356       }
357     }
358   }
359   return anActor;
360 }
361
362
363 void DisplayActor(QAD_StudyFrame *theStudyFrame, SMESH_Actor* theActor){
364   if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
365     aViewFrame->AddActor(theActor);
366     aViewFrame->Repaint();
367   }
368 }
369
370
371 void RemoveActor(QAD_StudyFrame *theStudyFrame, SMESH_Actor* theActor){
372   if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
373     aViewFrame->RemoveActor(theActor);
374     aViewFrame->Repaint();
375   }
376 }
377
378
379 void FitAll(){
380   if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
381     aViewFrame->onViewFitAll();
382     aViewFrame->Repaint();
383   }
384 }
385
386 vtkRenderer* GetCurrentRenderer(){
387   if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView())
388     return aViewFrame->getRenderer();
389   return NULL;
390 }
391
392 void RepaintCurrentView(){
393   if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView())
394     aViewFrame->Repaint();
395 }
396
397
398 enum EDisplaing {eDisplayAll, eDisplay, eDisplayOnly, eErase, eEraseAll};
399 void UpdateView(QAD_StudyFrame *theStudyFrame, EDisplaing theAction,
400                 const char* theEntry = "")
401 {
402   if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
403     vtkRenderer *aRenderer = aViewFrame->getRenderer();
404     vtkActorCollection *aCollection = aRenderer->GetActors();
405     aCollection->InitTraversal();
406     switch(theAction){
407       case eDisplayAll: {
408         while(vtkActor *anAct = aCollection->GetNextActor()){
409           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
410             anActor->SetVisibility(true);
411           }
412         }
413         break;
414       }
415       case eDisplayOnly:
416       case eEraseAll: {
417         while(vtkActor *anAct = aCollection->GetNextActor()){
418           if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
419             anActor->SetVisibility(false);
420           }
421         }
422       }
423       default: {
424         if(SMESH_Actor *anActor = FindActorByEntry(theStudyFrame,theEntry)){
425           switch(theAction) {
426             case eDisplay:
427             case eDisplayOnly:
428               anActor->SetVisibility(true);
429               break;
430             case eErase:
431               anActor->SetVisibility(false);
432               break;
433             }
434         } else {
435           switch(theAction){
436             case eDisplay:
437             case eDisplayOnly:{
438               QAD_Study* aStudy = theStudyFrame->getStudy();
439               SALOMEDS::Study_var aDocument = aStudy->getStudyDocument();
440               if(anActor = CreateActor(aDocument,theEntry,true)) {
441                 DisplayActor(theStudyFrame,anActor);
442                 FitAll();
443               }
444               break;
445             }
446           }
447         }
448       }
449     }
450   }
451 }
452
453
454 void UpdateView(EDisplaing theAction, const char* theEntry = ""){
455   QAD_Study* aStudy = SMESHGUI::GetSMESHGUI()->GetActiveStudy();
456   QAD_StudyFrame *aStudyFrame = aStudy->getActiveStudyFrame();
457   UpdateView(aStudyFrame,theAction,theEntry);
458 }
459
460 static bool IsReferencedObject( SALOMEDS::SObject_ptr SO ) 
461 {
462   if ( !SO->_is_nil() ) {
463     SALOMEDS::GenericAttribute_var anAttr;
464     if ( SO->FindAttribute( anAttr, "AttributeTarget" ) ) {
465       SALOMEDS::AttributeTarget_var aTarget = SALOMEDS::AttributeTarget::_narrow( anAttr );
466       SALOMEDS::Study::ListOfSObject_var aList = aTarget->Get();
467       if ( aList->length() > 0 ) {
468         return true;
469       }
470     }
471   }
472   return false;
473 }
474
475 void SetPointRepresentation(bool theIsVisible){
476   if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
477     vtkRenderer *aRenderer = aViewFrame->getRenderer();
478     vtkActorCollection *aCollection = aRenderer->GetActors();
479     aCollection->InitTraversal();
480     while(vtkActor *anAct = aCollection->GetNextActor()){
481       if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
482         if(anActor->GetVisibility()){
483           anActor->SetPointRepresentation(theIsVisible);
484         }
485       }
486     }
487     RepaintCurrentView();
488   }
489 }
490
491
492 //=============================================================================
493 /*!
494  *
495  */
496 //=============================================================================
497 class CustomItem:public QCustomMenuItem
498 {
499  public:
500   CustomItem(const QString & s, const QFont & f):string(s), font(f)
501   {
502   };
503   ~CustomItem()
504   {
505   }
506
507   void paint(QPainter * p, const QColorGroup & /*cg */ , bool /*act */ ,
508              bool /*enabled */ , int x, int y, int w, int h)
509   {
510     p->setFont(font);
511     p->drawText(x, y, w, h,
512                 AlignHCenter | AlignVCenter | ShowPrefix | DontClip, string);
513   }
514
515   QSize sizeHint()
516   {
517     return QFontMetrics(font).
518       size(AlignHCenter | AlignVCenter | ShowPrefix | DontClip, string);
519   }
520  private:
521   QString string;
522   QFont font;
523 };
524
525 //=============================================================================
526 /*!
527  *
528  */
529 //=============================================================================
530 SMESHGUI::SMESHGUI( const QString& theName, QObject* theParent ) :
531   SALOMEGUI( theName, theParent )
532 {
533 }
534
535 //=============================================================================
536 /*!
537  *
538  */
539 //=============================================================================
540 SMESHGUI::~SMESHGUI()
541 {
542 }
543
544 //=============================================================================
545 /*!
546  *
547  */
548 //=============================================================================
549 SMESHGUI* SMESHGUI::GetSMESHGUI()
550 {
551   if ( !smeshGUI )
552     GetOrCreateSMESHGUI( QAD_Application::getDesktop() );
553   return smeshGUI;
554 }
555
556 extern "C"
557 {
558   Standard_EXPORT SALOMEGUI* GetComponentGUI() {
559     return SMESHGUI::GetSMESHGUI();
560   }
561 }
562 //=============================================================================
563 /*!
564  *
565  */
566 //=============================================================================
567 SMESHGUI* SMESHGUI::GetOrCreateSMESHGUI( QAD_Desktop* desktop )
568 {
569   if( !smeshGUI ) {
570     setOrb();
571     smeshGUI = new SMESHGUI;
572     smeshGUI->myActiveDialogBox = 0 ;
573     smeshGUI->myState = -1 ;
574     smeshGUI->myDesktop = desktop ; 
575
576     /* get smesh engine */
577     Engines::Component_var comp = desktop->getEngine("FactoryServer", "SMESH");
578     smeshGUI->myComponentMesh = SMESH::SMESH_Gen::_narrow(comp);
579     
580     /* get geom engine */
581     Engines::Component_var comp1 = desktop->getEngine("FactoryServer", "GEOM");    
582     smeshGUI->myComponentGeom = GEOM::GEOM_Gen::_narrow(comp1);
583       
584     /* set current study */
585     smeshGUI->myActiveStudy = desktop->getActiveStudy();
586     smeshGUI->myStudyId     = smeshGUI->myActiveStudy->getStudyId();
587     smeshGUI->myStudy       = smeshGUI->myActiveStudy->getStudyDocument();
588     smeshGUI->myComponentGeom->GetCurrentStudy( smeshGUI->myStudyId );
589     smeshGUI->myComponentMesh->SetCurrentStudy( smeshGUI->myStudy );
590     //    smeshGUI->myComponentGeom->NbLabels();
591     
592     /* create service object */
593     smeshGUI->myStudyAPI = SMESHGUI_StudyAPI(smeshGUI->myStudy, smeshGUI->myComponentMesh);
594
595     smeshGUI->myDocument = new SMESHDS_Document(1);//NBU
596
597     smeshGUI->mySimulationActors = vtkActorCollection::New();
598     smeshGUI->mySimulationActors2D = vtkActor2DCollection::New();
599
600     /*filter manager*/
601     smeshGUI->myFilterMgr = smeshGUI->myComponentMesh->CreateFilterManager();
602
603     /* load resources for all available meshers */
604     smeshGUI->InitAvailableHypotheses();
605   } else {
606     /* study may have changed - set current study */
607     smeshGUI->myActiveStudy = desktop->getActiveStudy();
608     smeshGUI->myStudyId     = smeshGUI->myActiveStudy->getStudyId();
609     smeshGUI->myStudy       = smeshGUI->myActiveStudy->getStudyDocument();
610     smeshGUI->myComponentGeom->GetCurrentStudy( smeshGUI->myStudyId );
611     smeshGUI->myComponentMesh->SetCurrentStudy( smeshGUI->myStudy );
612     
613     // Set active study to Study API
614     smeshGUI->myStudyAPI.Update( smeshGUI->myStudy );
615   }
616   
617   /* Automatic Update flag */
618   smeshGUI->myAutomaticUpdate = ( QAD_CONFIG->getSetting( "SMESH:AutomaticUpdate" ).compare( "true" ) == 0 );
619
620   return smeshGUI;
621 }
622
623 //=============================================================================
624 /*!
625  *
626  */
627 //=============================================================================
628 void SMESHGUI::SetState(int aState)
629 {
630   this->myState = aState;
631   return;
632 }
633
634 //=============================================================================
635 /*!
636  *
637  */
638 //=============================================================================
639 void SMESHGUI::ResetState()
640 {
641   this->myState = -1;
642   return;
643 }
644
645 //=============================================================================
646 /*!
647  *
648  */
649 //=============================================================================
650 void SMESHGUI::EmitSignalDeactivateDialog()
651 {
652   emit this->SignalDeactivateActiveDialog();
653   return;
654 }
655
656 //=============================================================================
657 /*!
658  *
659  */
660 //=============================================================================
661 void SMESHGUI::EmitSignalCloseAllDialogs()
662 {
663   emit SignalCloseAllDialogs();
664   return;
665 }
666
667 //=============================================================================
668 /*!
669  *
670  */
671 //=============================================================================
672 QDialog *SMESHGUI::GetActiveDialogBox()
673 {
674   return this->myActiveDialogBox;
675 }
676
677 //=============================================================================
678 /*!
679  *
680  */
681 //=============================================================================
682 void SMESHGUI::SetActiveDialogBox(QDialog * aDlg)
683 {
684   this->myActiveDialogBox = (QDialog *) aDlg;
685   return;
686 }
687
688 //=============================================================================
689 /*!
690  *
691  */
692 //=============================================================================
693 QAD_Study *SMESHGUI::GetActiveStudy()
694 {
695   return this->myActiveStudy;
696 }
697
698 //=============================================================================
699 /*!
700  *
701  */
702 //=============================================================================
703 SALOMEDS::Study_ptr SMESHGUI::GetStudy()
704 {
705   return SALOMEDS::Study::_narrow(myStudy);
706 }
707
708 //=============================================================================
709 /*!
710  *
711  */
712 //=============================================================================
713 SMESHGUI_StudyAPI SMESHGUI::GetStudyAPI()
714 {
715   return myStudyAPI;
716 }
717
718 //=============================================================================
719 /*!
720  *
721  */
722 //=============================================================================
723 QAD_Desktop *SMESHGUI::GetDesktop()
724 {
725   return this->myDesktop;
726 }
727
728 //=============================================================================
729 /*!
730  *
731  */
732 //=============================================================================
733 bool SMESHGUI::ActiveStudyChanged(QAD_Desktop* parent)
734 {
735   MESSAGE("SMESHGUI::activeStudyChanged init.");
736   QAD_Study* prevStudy = 0;
737   if ( smeshGUI )
738     prevStudy = smeshGUI->myActiveStudy;
739   /* Create or retrieve an object SMESHGUI */
740   MESSAGE("Active study changed : prev study =" << prevStudy);
741   SMESHGUI::GetOrCreateSMESHGUI( parent );
742   MESSAGE("Active study changed : active study =" << smeshGUI->myActiveStudy);
743   if ( prevStudy != smeshGUI->myActiveStudy ) {
744     smeshGUI->EmitSignalCloseAllDialogs();
745     MESSAGE("Active study changed : SMESHGUI nullified" << endl);
746     //smeshGUI = 0;
747     ::UpdateSelectionProp();
748   }
749   MESSAGE("SMESHGUI::activeStudyChanged done.");
750   return true;
751 }
752
753 //=============================================================================
754 /*!
755  *
756  */
757 //=============================================================================
758 bool SMESHGUI::DefineDlgPosition(QWidget * aDlg, int &x, int &y)
759 {
760   /* Here the position is on the bottom right corner - 10 */
761   // aDlg->resize(QSize().expandedTo(aDlg->minimumSizeHint()));
762   aDlg->adjustSize();
763   QAD_Desktop *PP = QAD_Application::getDesktop();
764   x = abs(PP->x() + PP->size().width() - aDlg->size().width() - 10);
765   y = abs(PP->y() + PP->size().height() - aDlg->size().height() - 10);
766   return true;
767 }
768
769 //=============================================================================
770 /*!
771  *
772  */
773 //=============================================================================
774 void SMESHGUI::EraseSimulationActors()
775 {
776   if (myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK)
777   {                                                     //VTK
778     vtkRenderer *theRenderer =
779       ((VTKViewer_ViewFrame *) myActiveStudy->getActiveStudyFrame()->
780        getRightFrame()->getViewFrame())->getRenderer();
781     vtkRenderWindow *renWin = theRenderer->GetRenderWindow();
782
783     if (mySimulationActors != NULL)
784     {
785       mySimulationActors->InitTraversal();
786       vtkActor *ac = mySimulationActors->GetNextActor();
787       while (!(ac == NULL))
788       {
789         theRenderer->RemoveActor(ac);
790         ac = mySimulationActors->GetNextActor();
791       }
792       mySimulationActors->RemoveAllItems();
793     }
794     if (mySimulationActors2D != NULL)
795     {
796       mySimulationActors2D->InitTraversal();
797       vtkActor2D *ac = mySimulationActors2D->GetNextActor2D();
798       while (!(ac == NULL))
799       {
800         theRenderer->RemoveActor2D(ac);
801         ac = mySimulationActors2D->GetNextActor2D();
802       }
803       mySimulationActors2D->RemoveAllItems();
804     }
805     SetPointRepresentation(false);
806   }
807 }
808
809 //=============================================================================
810 /*!
811  *
812  */
813 //=============================================================================
814 SMESH::SMESH_Mesh_ptr SMESHGUI::InitMesh( GEOM::GEOM_Shape_ptr aShape,
815                                           QString              NameMesh )
816 {
817   try {
818     if ( !myComponentMesh->_is_nil() && !aShape->_is_nil() ) {
819       SMESH::SMESH_Mesh_var aMesh = myComponentMesh->CreateMesh( aShape );
820       if ( !aMesh->_is_nil() ) {
821         SALOMEDS::SObject_var SM = smeshGUI->myStudy->FindObjectIOR( _orb->object_to_string( aMesh ) );
822         myStudyAPI.SetName( SM, NameMesh );
823         return SMESH::SMESH_Mesh::_narrow(aMesh);
824       }
825     }
826   }
827   catch( const SALOME::SALOME_Exception& S_ex ) {
828     QtCatchCorbaException( S_ex );
829   }
830   return SMESH::SMESH_Mesh::_nil();
831 }
832
833 //=============================================================================
834 /*!
835  *
836  */
837 //=============================================================================
838 SMESH::SMESH_subMesh_ptr SMESHGUI::AddSubMesh( SMESH::SMESH_Mesh_ptr aMesh,
839                                                GEOM::GEOM_Shape_ptr  aShape,
840                                                QString               NameMesh )
841 {
842   SMESH::SMESH_subMesh_var aSubMesh;
843   try {
844     if ( !aMesh->_is_nil() && !aShape->_is_nil() )
845       // create sub mesh
846       aSubMesh = SMESH::SMESH_subMesh::_duplicate( aMesh->GetSubMesh( aShape, NameMesh.latin1() ) ); //VSR: published automatically
847   }
848   catch( const SALOME::SALOME_Exception& S_ex ) {
849     QtCatchCorbaException( S_ex );
850     aSubMesh = SMESH::SMESH_subMesh::_nil();
851   }
852   return aSubMesh._retn();
853 }
854
855 //=============================================================================
856 /*!
857  *
858  */
859 //=============================================================================
860 SMESH::SMESH_Group_ptr SMESHGUI::AddGroup( SMESH::SMESH_Mesh_ptr    aMesh,
861                                            SMESH::ElementType       aType,
862                                            QString                  aName )
863 {
864   SMESH::SMESH_Group_var aGroup;
865   try {
866     if ( !aMesh->_is_nil() )
867       // create group
868       aGroup = SMESH::SMESH_Group::_duplicate( aMesh->CreateGroup( aType, strdup(aName) ) ); //VSR: published automatically
869   }
870   catch( const SALOME::SALOME_Exception& S_ex ) {
871     QtCatchCorbaException( S_ex );
872     aGroup = SMESH::SMESH_Group::_nil();
873   }
874   myActiveStudy->updateObjBrowser( true );
875   return aGroup._retn();
876 }
877
878 //=============================================================================
879 /*!
880  *
881  */
882 //=============================================================================
883
884 static void addMap( const map<string, HypothesisData*>& theMap,
885                    map<string, HypothesisData*>&       toMap)
886 {
887   map<string, HypothesisData*>::const_iterator it;
888   for ( it = theMap.begin(); it != theMap.end(); it++ )
889     toMap.insert( *it );
890 }
891
892     
893 //=============================================================================
894 /*!
895  *  InitAvailableHypotheses (read from resource XML file)
896  */
897 //=============================================================================
898 void SMESHGUI::InitAvailableHypotheses ()
899 {
900   QAD_WaitCursor wc;
901   if ((myHypothesesMap.begin() == myHypothesesMap.end()) &&
902       (myAlgorithmsMap.begin() == myAlgorithmsMap.end()))
903   {
904     // Resource manager
905     QAD_ResourceMgr* resMgr = QAD_Desktop::createResourceManager();
906     if (!resMgr) return;
907
908     // Find name of a resource XML file ("SMESH_Meshers.xml");
909     QString HypsXml;
910     char* cenv = getenv("SMESH_MeshersList");
911     if (cenv)
912       HypsXml.sprintf("%s", cenv);
913
914     QStringList HypsXmlList = QStringList::split( ":", HypsXml, false );
915     if (HypsXmlList.count() == 0)
916     {
917       QAD_MessageBox::error1(QAD_Application::getDesktop(),
918                              tr("SMESH_WRN_WARNING"),
919                              tr("MESHERS_FILE_NO_VARIABLE"),
920                              tr("SMESH_BUT_OK"));
921       return;
922     }
923
924     // loop on files in HypsXml
925     QString aNoAccessFiles;
926     for ( int i = 0; i < HypsXmlList.count(); i++ ) {
927       QString HypsXml = HypsXmlList[ i ];
928
929       // Find full path to the resource XML file
930       QString xmlFile = HypsXml + ".xml";
931       xmlFile = QAD_Tools::addSlash(resMgr->findFile(xmlFile, HypsXml)) + xmlFile;
932
933       QFile file (QAD_Tools::unix2win(xmlFile));
934       if (file.exists() && file.open(IO_ReadOnly))
935       {
936         file.close();
937
938         SMESHGUI_XmlHandler* myXmlHandler = new SMESHGUI_XmlHandler();
939         ASSERT(myXmlHandler);
940
941         QXmlInputSource source (file);
942         QXmlSimpleReader reader;
943         reader.setContentHandler(myXmlHandler);
944         reader.setErrorHandler(myXmlHandler);
945         bool ok = reader.parse(source);
946         file.close();
947         if (ok)
948         {
949           addMap( myXmlHandler->myHypothesesMap, myHypothesesMap );
950           addMap( myXmlHandler->myAlgorithmsMap, myAlgorithmsMap );
951         }
952         else
953         {
954           QAD_MessageBox::error1(myDesktop, 
955                                  tr("INF_PARSE_ERROR"),
956                                  tr(myXmlHandler->errorProtocol()),
957                                  tr("SMESH_BUT_OK"));
958         }
959       }
960       else
961       {
962         if (aNoAccessFiles.isEmpty())
963           aNoAccessFiles = xmlFile;
964         else
965           aNoAccessFiles += ", " + xmlFile;
966       }
967     } // end loop
968
969
970     if (!aNoAccessFiles.isEmpty())
971     {
972       QString aMess = tr("MESHERS_FILE_CANT_OPEN") + " " + aNoAccessFiles + "\n";
973       aMess += tr("MESHERS_FILE_CHECK_VARIABLE");
974       wc.stop();
975       QAD_MessageBox::warn1(QAD_Application::getDesktop(),
976                             tr("SMESH_WRN_WARNING"),
977                             aMess,
978                             tr("SMESH_BUT_OK"));
979       wc.start();
980     }
981   }
982 }
983
984 //=============================================================================
985 /*!
986  *  GetAvailableHypotheses (read from resource XML file)
987  */
988 //=============================================================================
989 QStringList SMESHGUI::GetAvailableHypotheses (const bool isAlgo)
990 {
991   QStringList aHypList;
992
993   // Init list of available hypotheses, if needed
994   InitAvailableHypotheses();
995
996   // fill list of hypotheses/algorithms
997   map<string, HypothesisData*>::iterator anIter;
998   if (isAlgo)
999   {
1000     anIter = myAlgorithmsMap.begin();
1001     for (; anIter != myAlgorithmsMap.end(); anIter++)
1002     {
1003       aHypList.append(((*anIter).first).c_str());
1004     }
1005   }
1006   else
1007   {
1008     anIter = myHypothesesMap.begin();
1009     for (; anIter != myHypothesesMap.end(); anIter++)
1010     {
1011       aHypList.append(((*anIter).first).c_str());
1012     }
1013   }
1014
1015   return aHypList;
1016 }
1017
1018 //=============================================================================
1019 /*!
1020  *  GetHypothesisData
1021  */
1022 //=============================================================================
1023 HypothesisData* SMESHGUI::GetHypothesisData (const char* aHypType)
1024 {
1025   HypothesisData* aHypData = 0;
1026
1027   // Init list of available hypotheses, if needed
1028   InitAvailableHypotheses();
1029
1030   if (myHypothesesMap.find(aHypType) == myHypothesesMap.end())
1031   {
1032     if (myAlgorithmsMap.find(aHypType) != myAlgorithmsMap.end())
1033     {
1034       aHypData = myAlgorithmsMap[aHypType];
1035     }
1036   }
1037   else
1038   {
1039     aHypData = myHypothesesMap[aHypType];
1040   }
1041   return aHypData;
1042 }
1043
1044 //=============================================================================
1045 /*! 
1046  *  Get a Hypothesis Creator from a Plugin Library
1047  */
1048 //=============================================================================
1049 SMESHGUI_GenericHypothesisCreator* SMESHGUI::GetHypothesisCreator(const QString& aHypType)
1050 {
1051   char* sHypType = (char*)aHypType.latin1();
1052   MESSAGE("Get HypothesisCreator for " << sHypType);
1053
1054   SMESHGUI_GenericHypothesisCreator* aCreator = 0;
1055
1056   // check, if creator for this hypothesis type already exists
1057   if (myHypCreatorMap.find(sHypType) != myHypCreatorMap.end())
1058   {
1059     aCreator = myHypCreatorMap[sHypType];
1060   }
1061   else
1062   {
1063     // 1. Init list of available hypotheses, if needed
1064     InitAvailableHypotheses();
1065
1066     // 2. Get names of plugin libraries
1067     HypothesisData* aHypData = GetHypothesisData(sHypType);
1068     if (!aHypData)
1069     {
1070       return aCreator;
1071     }
1072     QString aClientLibName = aHypData->ClientLibName;
1073     QString aServerLibName = aHypData->ServerLibName;
1074
1075     // 3. Load Client Plugin Library
1076     try
1077     {
1078       // load plugin library
1079       MESSAGE("Loading client meshers plugin library ...");
1080       void* libHandle = dlopen (aClientLibName, RTLD_LAZY);
1081       if (!libHandle)
1082       {
1083         // report any error, if occured
1084         const char* anError = dlerror();
1085         MESSAGE(anError);
1086       }
1087       else
1088       {
1089         // get method, returning hypothesis creator
1090         MESSAGE("Find GetHypothesisCreator() method ...");
1091         typedef SMESHGUI_GenericHypothesisCreator* (*GetHypothesisCreator) \
1092           (QString aHypType, QString aServerLibName, SMESHGUI* aSMESHGUI);
1093         GetHypothesisCreator procHandle =
1094           (GetHypothesisCreator)dlsym( libHandle, "GetHypothesisCreator" );
1095         if (!procHandle)
1096         {
1097           MESSAGE("bad hypothesis client plugin library");
1098           dlclose(libHandle);
1099         }
1100         else
1101         {
1102           // get hypothesis creator
1103           MESSAGE("Get Hypothesis Creator for " << aHypType);
1104           aCreator = procHandle(aHypType, aServerLibName, smeshGUI);
1105           if (!aCreator)
1106           {
1107             MESSAGE("no such a hypothesis in this plugin");
1108           }
1109           else
1110           {
1111             // map hypothesis creator to a hypothesis name
1112             myHypCreatorMap[sHypType] = aCreator;
1113           }
1114         }
1115       }
1116     }
1117     catch (const SALOME::SALOME_Exception& S_ex)
1118     {
1119       QtCatchCorbaException(S_ex);
1120     }
1121   }
1122
1123   return aCreator;
1124 }
1125
1126 //=============================================================================
1127 /*!
1128  *
1129  */
1130 //=============================================================================
1131 SMESH::SMESH_Hypothesis_ptr SMESHGUI::CreateHypothesis (const QString& aHypType,
1132                                                         const QString& aHypName,
1133                                                         const bool     isAlgo)
1134 {
1135   MESSAGE("Create " << aHypType << " with name " << aHypName);
1136
1137   SMESH::SMESH_Hypothesis_var Hyp;
1138
1139   HypothesisData* aHypData = GetHypothesisData((char*)aHypType.latin1());
1140   QString aServLib = aHypData->ServerLibName;
1141
1142   try
1143   {
1144     Hyp = myComponentMesh->CreateHypothesis(aHypType, aServLib);
1145     if (!Hyp->_is_nil())
1146     {
1147       SALOMEDS::SObject_var SHyp =
1148         smeshGUI->myStudy->FindObjectIOR( _orb->object_to_string( Hyp ) );
1149       if (!SHyp->_is_nil())
1150       {
1151         if ( !aHypName.isEmpty() )
1152           myStudyAPI.SetName( SHyp, aHypName );
1153         myActiveStudy->updateObjBrowser(true);
1154         return Hyp._retn();
1155       }
1156     }
1157   }
1158   catch (const SALOME::SALOME_Exception & S_ex)
1159   {
1160     QtCatchCorbaException(S_ex);
1161   }
1162
1163   return SMESH::SMESH_Hypothesis::_nil();
1164 }
1165
1166 //=============================================================================
1167 /*!
1168  *
1169  */
1170 //=============================================================================
1171 bool SMESHGUI::AddHypothesisOnMesh(SMESH::SMESH_Mesh_ptr aMesh,
1172                                    SMESH::SMESH_Hypothesis_ptr aHyp)
1173 {
1174   int res = SMESH::HYP_UNKNOWN_FATAL; 
1175   QAD_WaitCursor wc;
1176
1177   if ( !aMesh->_is_nil() ) {
1178     SALOMEDS::SObject_var SM = myStudyAPI.FindObject( aMesh );
1179     GEOM::GEOM_Shape_var aShape = myStudyAPI.GetShapeOnMeshOrSubMesh(SM);
1180     try {
1181       res = aMesh->AddHypothesis( aShape, aHyp );
1182       if ( res < SMESH::HYP_UNKNOWN_FATAL ) {
1183         SALOMEDS::SObject_var SH = myStudyAPI.FindObject(aHyp);
1184         if ( !SM->_is_nil() && !SH->_is_nil() ) {
1185           //myStudyAPI.SetHypothesis(SM, SH); // VSR: published automatically by engine
1186           myStudyAPI.ModifiedMesh(SM, false);
1187         }
1188       }
1189       if ( res >= SMESH::HYP_UNKNOWN_FATAL ) {
1190         wc.stop();
1191         QAD_MessageBox::error1(QAD_Application::getDesktop(),
1192                               tr("SMESH_ERROR"),
1193                               tr(QString("SMESH_HYP_%1").arg(res)),
1194                               tr("SMESH_BUT_OK"));
1195         wc.start();
1196       }
1197       else if ( res > SMESH::HYP_OK ) {
1198         wc.stop();
1199         QAD_MessageBox::warn1(QAD_Application::getDesktop(),
1200                               tr("SMESH_WRN_WARNING"),
1201                               tr(QString("SMESH_HYP_%1").arg(res)),
1202                               tr("SMESH_BUT_OK"));
1203         wc.start();
1204       }
1205     }
1206     catch( const SALOME::SALOME_Exception& S_ex ) {
1207       wc.stop();
1208       QtCatchCorbaException( S_ex );
1209       res = SMESH::HYP_UNKNOWN_FATAL;
1210     }
1211   }
1212   return res < SMESH::HYP_UNKNOWN_FATAL;
1213 }
1214
1215 //=============================================================================
1216 /*!
1217  *
1218  */
1219 //=============================================================================
1220 bool SMESHGUI::RemoveHypothesisOrAlgorithmOnMesh
1221                (const Handle(SALOME_InteractiveObject)& IObject)
1222 {
1223   int res = SMESH::HYP_UNKNOWN_FATAL;
1224   QAD_WaitCursor wc;
1225
1226   if (IObject->hasReference())
1227   {
1228     try {
1229       SMESH::SMESH_Hypothesis_var anHyp;
1230       SALOMEDS::SObject_var SO_Hypothesis =
1231         smeshGUI->myStudy->FindObjectID(IObject->getEntry());
1232       SALOMEDS::GenericAttribute_var anAttr;
1233       SALOMEDS::AttributeIOR_var anIOR;
1234
1235       if (!SO_Hypothesis->_is_nil()) {
1236         if (SO_Hypothesis->FindAttribute(anAttr, "AttributeIOR")) {
1237           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1238           anHyp = SMESH::SMESH_Hypothesis::_narrow
1239             (_orb->string_to_object(anIOR->Value()));
1240         }
1241
1242         SALOMEDS::SObject_var SO_Applied_Hypothesis =
1243           smeshGUI->myStudy->FindObjectID(IObject->getReference());
1244         if (!SO_Applied_Hypothesis->_is_nil()) {
1245           SALOMEDS::SObject_var MorSM =
1246             smeshGUI->myStudyAPI.GetMeshOrSubmesh(SO_Applied_Hypothesis);
1247           if (!MorSM->_is_nil()) {
1248             GEOM::GEOM_Shape_var aShape =
1249               smeshGUI->myStudyAPI.GetShapeOnMeshOrSubMesh(MorSM);
1250             if (!aShape->_is_nil() && MorSM->FindAttribute(anAttr, "AttributeIOR")) {
1251               anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1252               SMESH::SMESH_Mesh_var aMesh =
1253                 SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(anIOR->Value()));
1254               SMESH::SMESH_subMesh_var aSubMesh =
1255                 SMESH::SMESH_subMesh::_narrow(_orb->string_to_object(anIOR->Value()));
1256
1257               if (!aSubMesh->_is_nil())
1258                 aMesh = aSubMesh->GetFather();
1259
1260               if (!aMesh->_is_nil())
1261               {
1262                 res = aMesh->RemoveHypothesis(aShape, anHyp);
1263                 if ( res < SMESH::HYP_UNKNOWN_FATAL )
1264                   smeshGUI->myStudyAPI.ModifiedMesh(MorSM, false);
1265                 if ( res > SMESH::HYP_OK ) {
1266                   wc.stop();
1267                   QAD_MessageBox::warn1(QAD_Application::getDesktop(),
1268                                         tr("SMESH_WRN_WARNING"),
1269                                         tr(QString("SMESH_HYP_%1").arg(res)),
1270                                         tr("SMESH_BUT_OK"));
1271                   wc.start();
1272                 }
1273               }
1274             }
1275           }
1276         }
1277       }
1278     }
1279     catch( const SALOME::SALOME_Exception& S_ex ) {
1280       wc.stop();
1281       QtCatchCorbaException( S_ex );
1282       res = SMESH::HYP_UNKNOWN_FATAL;
1283     }
1284   }
1285   else if (IObject->hasEntry())
1286   {
1287     MESSAGE("IObject entry " << IObject->getEntry());
1288   }
1289   return res < SMESH::HYP_UNKNOWN_FATAL;
1290 }
1291
1292 //=============================================================================
1293 /*!
1294  *
1295  */
1296 //=============================================================================
1297 bool SMESHGUI::RemoveHypothesisOrAlgorithmOnMesh(SALOMEDS::SObject_ptr MorSM,
1298         SMESH::SMESH_Hypothesis_ptr anHyp)
1299 {
1300   SALOMEDS::SObject_var AHR, aRef;
1301   SALOMEDS::GenericAttribute_var anAttr;
1302   SALOMEDS::AttributeIOR_var anIOR;
1303   int res = SMESH::HYP_UNKNOWN_FATAL;
1304   QAD_WaitCursor wc;
1305
1306   if (!MorSM->_is_nil()) {
1307     try {
1308       GEOM::GEOM_Shape_var aShape = smeshGUI->myStudyAPI.GetShapeOnMeshOrSubMesh(MorSM);
1309       if (!aShape->_is_nil()) {
1310         if (MorSM->FindAttribute(anAttr, "AttributeIOR")) {
1311           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1312           SMESH::SMESH_Mesh_var aMesh =
1313             SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(anIOR->Value()));
1314           SMESH::SMESH_subMesh_var aSubMesh =
1315             SMESH::SMESH_subMesh::_narrow(_orb->string_to_object(anIOR->Value()));
1316           
1317           if ( !aSubMesh->_is_nil() )
1318             aMesh = aSubMesh->GetFather();
1319           
1320           if (!aMesh->_is_nil()) {
1321             res = aMesh->RemoveHypothesis(aShape, anHyp);
1322             if ( res < SMESH::HYP_UNKNOWN_FATAL )
1323               smeshGUI->myStudyAPI.ModifiedMesh(MorSM, false);
1324             if ( res > SMESH::HYP_OK ) {
1325               wc.stop();
1326               QAD_MessageBox::warn1(QAD_Application::getDesktop(),
1327                                     tr("SMESH_WRN_WARNING"),
1328                                     tr(QString("SMESH_HYP_%1").arg(res)),
1329                                     tr("SMESH_BUT_OK"));
1330               wc.start();
1331             }
1332           }
1333         }
1334       }
1335     }
1336     catch( const SALOME::SALOME_Exception& S_ex ) {
1337       wc.stop();
1338       QtCatchCorbaException( S_ex );
1339       res = SMESH::HYP_UNKNOWN_FATAL;
1340     }
1341   }
1342   return res < SMESH::HYP_UNKNOWN_FATAL;
1343 }
1344
1345 //=============================================================================
1346 /*!
1347  *
1348  */
1349 //=============================================================================
1350 bool SMESHGUI::AddAlgorithmOnMesh( SMESH::SMESH_Mesh_ptr       aMesh,
1351                                    SMESH::SMESH_Hypothesis_ptr aHyp )
1352 {
1353   int res = SMESH::HYP_UNKNOWN_FATAL;
1354   QAD_WaitCursor wc;
1355
1356   if ( !aMesh->_is_nil() ) {
1357     SALOMEDS::SObject_var SM = myStudyAPI.FindObject( aMesh );
1358     GEOM::GEOM_Shape_var aShape = myStudyAPI.GetShapeOnMeshOrSubMesh( SM );
1359     try {
1360       res = aMesh->AddHypothesis( aShape, aHyp );
1361       if ( res < SMESH::HYP_UNKNOWN_FATAL ) {
1362         //SALOMEDS::SObject_var SH = myStudyAPI.FindObject( aHyp );
1363         //if ( !SM->_is_nil() && !SH->_is_nil() ) {
1364           //myStudyAPI.SetAlgorithms(SM, SH);
1365         myStudyAPI.ModifiedMesh( SM, false );
1366         //}
1367       }
1368       if ( res >= SMESH::HYP_UNKNOWN_FATAL ) {
1369         wc.stop();
1370         QAD_MessageBox::error1(QAD_Application::getDesktop(),
1371                               tr("SMESH_ERROR"),
1372                               tr(QString("SMESH_HYP_%1").arg(res)),
1373                               tr("SMESH_BUT_OK"));
1374         wc.start();
1375       }
1376       else if ( res > SMESH::HYP_OK ) {
1377         wc.stop();
1378         QAD_MessageBox::warn1( QAD_Application::getDesktop(),
1379                                tr( "SMESH_WRN_WARNING" ),
1380                                tr(QString("SMESH_HYP_%1").arg(res)),
1381                                tr( "SMESH_BUT_OK" ) );
1382         wc.start();
1383       }
1384     }
1385     catch( const SALOME::SALOME_Exception& S_ex ) {
1386       wc.stop();
1387       QtCatchCorbaException( S_ex );
1388       res = SMESH::HYP_UNKNOWN_FATAL;
1389     }
1390   }
1391   return res < SMESH::HYP_UNKNOWN_FATAL;
1392 }
1393
1394 //=============================================================================
1395 /*!
1396  *
1397  */
1398 //=============================================================================
1399 bool SMESHGUI::AddHypothesisOnSubMesh( SMESH::SMESH_subMesh_ptr    aSubMesh,
1400                                        SMESH::SMESH_Hypothesis_ptr aHyp )
1401 {
1402   int res = SMESH::HYP_UNKNOWN_FATAL;
1403   QAD_WaitCursor wc;
1404
1405   if ( !aSubMesh->_is_nil() ) {
1406     try {
1407       SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
1408       SALOMEDS::SObject_var SsubM = myStudyAPI.FindObject( aSubMesh );
1409       GEOM::GEOM_Shape_var aShape = myStudyAPI.GetShapeOnMeshOrSubMesh( SsubM );
1410       if ( !aMesh->_is_nil() && !SsubM->_is_nil() && !aShape->_is_nil() ) {
1411         res = aMesh->AddHypothesis( aShape, aHyp );
1412         if ( res < SMESH::HYP_UNKNOWN_FATAL )  {
1413           //SALOMEDS::SObject_var SH = myStudyAPI.FindObject(aHyp);
1414           //                    if (!SsubM->_is_nil() && !SH->_is_nil())
1415           //                    {
1416           //                            myStudyAPI.SetHypothesis(SsubM, SH);
1417           myStudyAPI.ModifiedMesh( SsubM, false );
1418           //                    }
1419         }
1420         if ( res >= SMESH::HYP_UNKNOWN_FATAL ) {
1421           wc.stop();
1422           QAD_MessageBox::error1(QAD_Application::getDesktop(),
1423                                  tr("SMESH_ERROR"),
1424                                  tr(QString("SMESH_HYP_%1").arg(res)),
1425                                  tr("SMESH_BUT_OK"));
1426           wc.start();
1427         }
1428         else if ( res > SMESH::HYP_OK ) {
1429           wc.stop();
1430           QAD_MessageBox::warn1(QAD_Application::getDesktop(),
1431                                 tr( "SMESH_WRN_WARNING" ),
1432                                 tr(QString("SMESH_HYP_%1").arg(res)),
1433                                 tr( "SMESH_BUT_OK" ) );
1434           wc.start();
1435         }
1436       }
1437     }
1438     catch( const SALOME::SALOME_Exception& S_ex ) {
1439       wc.stop();
1440       QtCatchCorbaException( S_ex );
1441       res = SMESH::HYP_UNKNOWN_FATAL;
1442     }
1443   }
1444   return res < SMESH::HYP_UNKNOWN_FATAL;
1445 }
1446
1447 //=============================================================================
1448 /*!
1449  *
1450  */
1451 //=============================================================================
1452 bool SMESHGUI::AddAlgorithmOnSubMesh( SMESH::SMESH_subMesh_ptr    aSubMesh,
1453                                       SMESH::SMESH_Hypothesis_ptr aHyp )
1454 {
1455   int res = SMESH::HYP_UNKNOWN_FATAL;
1456   QAD_WaitCursor wc;
1457
1458   if ( !aSubMesh->_is_nil() ) {
1459     try {
1460       SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
1461       SALOMEDS::SObject_var SsubM = myStudyAPI.FindObject(aSubMesh);
1462       GEOM::GEOM_Shape_var aShape = myStudyAPI.GetShapeOnMeshOrSubMesh(SsubM);
1463       if ( !aMesh->_is_nil() && !SsubM->_is_nil() && !aShape->_is_nil() ) {
1464         res = aMesh->AddHypothesis( aShape, aHyp );
1465         if ( res < SMESH::HYP_UNKNOWN_FATAL )  {
1466           //SALOMEDS::SObject_var SH = myStudyAPI.FindObject(aHyp);
1467           //if (!SsubM->_is_nil() && !SH->_is_nil()) {
1468           //myStudyAPI.SetAlgorithms(SsubM, SH);
1469           myStudyAPI.ModifiedMesh( SsubM, false );
1470           //}
1471         }
1472         if ( res >= SMESH::HYP_UNKNOWN_FATAL ) {
1473           wc.stop();
1474           QAD_MessageBox::error1(QAD_Application::getDesktop(),
1475                                  tr("SMESH_ERROR"),
1476                                  tr(QString("SMESH_HYP_%1").arg(res)),
1477                                  tr("SMESH_BUT_OK"));
1478           wc.start();
1479         }
1480         else if ( res > SMESH::HYP_OK ) {
1481           wc.stop();
1482           QAD_MessageBox::warn1(QAD_Application::getDesktop(),
1483                                 tr( "SMESH_WRN_WARNING" ),
1484                                 tr(QString("SMESH_HYP_%1").arg(res)),
1485                                 tr( "SMESH_BUT_OK" ) );
1486           wc.start();
1487         }
1488       }
1489     }
1490     catch( const SALOME::SALOME_Exception& S_ex ) {
1491       wc.stop();
1492       QtCatchCorbaException( S_ex );
1493       res = SMESH::HYP_UNKNOWN_FATAL;
1494     }
1495   }
1496   return res < SMESH::HYP_UNKNOWN_FATAL;
1497 }
1498
1499 //=============================================================================
1500 /*!
1501  *
1502  */
1503 //=============================================================================
1504 int SMESHGUI::GetNameOfSelectedIObjects(SALOME_Selection * Sel, QString & aName)
1505 {
1506         int nbSel = Sel->IObjectCount();
1507         if (nbSel == 1)
1508         {
1509                 Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
1510                 aName = IObject->getName();
1511         }
1512         else
1513         {
1514                 aName = tr("SMESH_OBJECTS_SELECTED").arg(nbSel);
1515         }
1516         return nbSel;
1517 }
1518
1519 //=============================================================================
1520 /*!
1521  *
1522  */
1523 //=============================================================================
1524 int SMESHGUI::GetNameOfSelectedNodes(SALOME_Selection * Sel, QString & aName){
1525   aName = "";
1526   if(Sel->IObjectCount() == 1){
1527     Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
1528     if(IObject->hasEntry()){
1529       if(SMESH_Actor *anActor = ::FindActorByEntry(IObject->getEntry())){
1530         TColStd_MapOfInteger MapIndex;
1531         Sel->GetIndex(IObject, MapIndex);
1532         TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
1533         for(; ite.More(); ite.Next()){
1534           aName += QString(" %1").arg(anActor->GetNodeObjId(ite.Key()));
1535         }
1536         return MapIndex.Extent();
1537       }
1538     }
1539   }
1540   return -1;
1541 }
1542
1543 //=============================================================================
1544 /*!
1545  *
1546  */
1547 //=============================================================================
1548 int SMESHGUI::GetNameOfSelectedElements(SALOME_Selection * Sel, QString & aName){
1549   aName = "";
1550   if(Sel->IObjectCount() == 1){
1551     Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
1552     if(IObject->hasEntry()){
1553       if(SMESH_Actor *anActor = ::FindActorByEntry(IObject->getEntry())){
1554         TColStd_MapOfInteger MapIndex;
1555         Sel->GetIndex(IObject, MapIndex);
1556         TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
1557         std::vector<int> aList;
1558         for(; ite.More(); ite.Next()){
1559           int aVtkId = ite.Key();
1560           int anObjId = anActor->GetElemObjId(aVtkId);
1561           std::vector<int>::iterator found = find(aList.begin(), aList.end(), anObjId);
1562           //      MESSAGE("GetNameOfSelectedElements(): VTK Id = " << aVtkId << ", OBJECT Id = " << anObjId);
1563           if (found == aList.end()) {
1564             aList.push_back(anObjId);
1565             aName += QString(" %1").arg(anObjId);
1566           }
1567         }
1568         return aList.size();
1569       }
1570     }
1571   }
1572   return -1;
1573 }
1574
1575
1576 //=============================================================================
1577 /*!
1578  *
1579  */
1580 //=============================================================================
1581 int SMESHGUI::GetNameOfSelectedEdges(SALOME_Selection * Sel, QString & aName){
1582   aName = "";
1583   if(Sel->IObjectCount() == 1){
1584     Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
1585     if(IObject->hasEntry()){
1586       if(SMESH_Actor *anActor = ::FindActorByEntry(IObject->getEntry())){
1587         TColStd_MapOfInteger MapIndex;
1588         Sel->GetIndex(IObject, MapIndex);
1589         TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
1590         for(; ite.More(); ite.Next()){
1591           aName += QString(" %1").arg(ite.Key());
1592         }
1593         return MapIndex.Extent();
1594       }
1595     }
1596   }
1597   return -1;
1598 }
1599
1600
1601 //=============================================================================
1602 /*!
1603  *
1604  */
1605 //=============================================================================
1606 SMESH_Actor *SMESHGUI::FindActorByEntry(QString theEntry,
1607                                         Standard_Boolean & theResult, 
1608                                         bool onlyInActiveView)
1609 {
1610   theResult = false;
1611   int aNbStudyFrames = myActiveStudy->getStudyFramesCount();
1612   for (int i = 0; i < aNbStudyFrames; i++){
1613     QAD_StudyFrame *aStudyFrame = myActiveStudy->getStudyFrame(i);
1614     if(SMESH_Actor *anActor = ::FindActorByEntry(aStudyFrame,theEntry.latin1())){
1615       theResult = true;
1616       return anActor;
1617     }
1618   }
1619   return NULL;
1620 }
1621
1622 //=============================================================================
1623 /*!
1624  *
1625  */
1626 //=============================================================================
1627 SMESH_Actor* SMESHGUI::FindActor(CORBA::Object_ptr theObj,
1628                                  Standard_Boolean & theResult, 
1629                                  bool theOnlyInActiveView)
1630 {
1631   theResult = false;
1632   SMESH_Actor* aRes = NULL;
1633   if ( !CORBA::is_nil( theObj ) ) {
1634     SALOMEDS::SObject_var aSObject = SALOMEDS::SObject::_narrow( myStudy->FindObjectIOR( _orb->object_to_string( theObj ) ) );
1635     if( !aSObject->_is_nil()) {
1636       CORBA::String_var anEntry = aSObject->GetID();
1637       aRes = FindActorByEntry( anEntry.in(), theResult, theOnlyInActiveView );
1638     }
1639   }
1640   return aRes;
1641 }
1642
1643 //=============================================================================
1644 /*!
1645  *
1646  */
1647 //=============================================================================
1648 SMESH::SMESH_Mesh_ptr SMESHGUI::ConvertIOinMesh(const Handle(SALOME_InteractiveObject) & IO,
1649                                                 Standard_Boolean & testResult)
1650 {
1651   SMESH::SMESH_Mesh_var aMesh;
1652   testResult = false;
1653
1654   /* case SObject */
1655   if (IO->hasEntry())
1656   {
1657     SALOMEDS::SObject_var obj = myStudy->FindObjectID(IO->getEntry());
1658     SALOMEDS::GenericAttribute_var anAttr;
1659     SALOMEDS::AttributeIOR_var anIOR;
1660     if (!obj->_is_nil())
1661     {
1662       if (obj->FindAttribute(anAttr, "AttributeIOR"))
1663       {
1664         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1665         aMesh =
1666           SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(anIOR->Value()));
1667         if (!aMesh->_is_nil())
1668         {
1669           testResult = true;
1670           return SMESH::SMESH_Mesh::_duplicate(aMesh);
1671         }
1672       }
1673     }
1674   }
1675   return SMESH::SMESH_Mesh::_nil();
1676 }
1677
1678 //=============================================================================
1679 /*!
1680  *
1681  */
1682 //=============================================================================
1683 SMESH::SMESH_subMesh_ptr SMESHGUI::ConvertIOinSubMesh(const Handle(SALOME_InteractiveObject) & IO,
1684                                                       Standard_Boolean & testResult)
1685 {
1686   SMESH::SMESH_subMesh_var aSubMesh;
1687   testResult = false;
1688
1689   /* case SObject */
1690   if (IO->hasEntry())
1691   {
1692     SALOMEDS::SObject_var obj = myStudy->FindObjectID(IO->getEntry());
1693     SALOMEDS::GenericAttribute_var anAttr;
1694     SALOMEDS::AttributeIOR_var anIOR;
1695     if (!obj->_is_nil())
1696     {
1697       if (obj->FindAttribute(anAttr, "AttributeIOR"))
1698       {
1699         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1700         aSubMesh = SMESH::SMESH_subMesh::_narrow(_orb->string_to_object(anIOR->Value()));
1701         if (!aSubMesh->_is_nil())
1702         {
1703           testResult = true;
1704           return SMESH::SMESH_subMesh::_duplicate(aSubMesh);
1705         }
1706       }
1707     }
1708   }
1709   return SMESH::SMESH_subMesh::_nil();
1710 }
1711
1712 //=============================================================================
1713 /*!
1714  *
1715  */
1716 //=============================================================================
1717 SMESH::SMESH_Hypothesis_ptr SMESHGUI::ConvertIOinSMESHHypothesis(const Handle(SALOME_InteractiveObject) & IO,
1718                                                                  Standard_Boolean & testResult)
1719 {
1720   SMESH::SMESH_Hypothesis_var aHyp;
1721   testResult = false;
1722
1723   /* case SObject */
1724   if (IO->hasEntry())
1725   {
1726     SALOMEDS::SObject_var obj = myStudy->FindObjectID(IO->getEntry());
1727     SALOMEDS::GenericAttribute_var anAttr;
1728     SALOMEDS::AttributeIOR_var anIOR;
1729     if (!obj->_is_nil())
1730     {
1731       if (obj->FindAttribute(anAttr, "AttributeIOR"))
1732       {
1733         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1734         aHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(anIOR->Value()));
1735         if (!aHyp->_is_nil())
1736         {
1737           testResult = true;
1738           return SMESH::SMESH_Hypothesis::_duplicate(aHyp);
1739         }
1740       }
1741     }
1742   }
1743   return SMESH::SMESH_Hypothesis::_nil();
1744 }
1745
1746 //=============================================================================
1747 /*!
1748  *
1749  */
1750 //=============================================================================
1751 SMESH::SMESH_Group_ptr SMESHGUI::ConvertIOinSMESHGroup(const Handle(SALOME_InteractiveObject) & IO,
1752                                                        Standard_Boolean & testResult)
1753 {
1754   SMESH::SMESH_Group_var aGroup;
1755   testResult = false;
1756
1757   /* case SObject */
1758   if (IO->hasEntry()) {
1759     SALOMEDS::SObject_var obj = myStudy->FindObjectID(IO->getEntry());
1760     SALOMEDS::GenericAttribute_var anAttr;
1761     SALOMEDS::AttributeIOR_var anIOR;
1762     if (!obj->_is_nil()) {
1763       if (obj->FindAttribute(anAttr, "AttributeIOR")) {
1764         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1765         aGroup = SMESH::SMESH_Group::_narrow(_orb->string_to_object(anIOR->Value()));
1766         if (!aGroup->_is_nil()) {
1767           testResult = true;
1768           return SMESH::SMESH_Group::_duplicate(aGroup);
1769         }
1770       }
1771     }
1772   }
1773   return SMESH::SMESH_Group::_nil();
1774 }
1775
1776 //=============================================================================
1777 /*!
1778  *
1779  */
1780 //=============================================================================
1781 GEOM::GEOM_Shape_ptr SMESHGUI::ConvertIOinGEOMShape(const Handle(SALOME_InteractiveObject) & IO,
1782                                                     Standard_Boolean & testResult)
1783 {
1784   GEOM::GEOM_Shape_var aShape;
1785   testResult = false;
1786
1787   /* case SObject */
1788   if (IO->hasEntry()) {
1789     SALOMEDS::SObject_var obj = myStudy->FindObjectID(IO->getEntry());
1790     SALOMEDS::GenericAttribute_var anAttr;
1791     SALOMEDS::AttributeIOR_var anIOR;
1792     if (!obj->_is_nil()) {
1793       if (obj->FindAttribute(anAttr, "AttributeIOR")) {
1794         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1795         aShape = myComponentGeom->GetIORFromString(anIOR->Value());
1796         if (!aShape->_is_nil()) {
1797           testResult = true;
1798           return GEOM::GEOM_Shape::_duplicate(aShape);
1799         }
1800       }
1801     }
1802   }
1803   return GEOM::GEOM_Shape::_nil();
1804 }
1805
1806 //=============================================================================
1807 /*!
1808  *
1809  */
1810 //=============================================================================
1811 void SMESHGUI::SetViewMode(int commandId){
1812   SALOME_Selection *Sel = SALOME_Selection::Selection(myActiveStudy->getSelection());
1813   if(Sel->IObjectCount() >= 1){
1814     SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
1815     for(; It.More(); It.Next()){
1816       Handle(SALOME_InteractiveObject) IObject = It.Value();
1817       if(IObject->hasEntry()){
1818         if(SMESH_Actor *anActor = ::FindActorByEntry(IObject->getEntry())){
1819           switch(commandId){
1820           case 211:
1821             ChangeRepresentation(anActor,0);
1822             break;
1823           case 212:
1824             ChangeRepresentation(anActor,1);
1825             break;
1826           case 213:
1827             ChangeRepresentation(anActor,2);
1828             break;
1829           case 215:
1830             ChangeRepresentation(anActor,5);
1831             break;
1832           case 1132:
1833             ChangeRepresentation(anActor,3);
1834             break;
1835           }
1836         }
1837       }
1838     }
1839     if(commandId == 1133)
1840       ChangeRepresentation(NULL,4);
1841   }
1842 }
1843
1844 //=============================================================================
1845 /*!
1846  *
1847  */
1848 //=============================================================================
1849 void SMESHGUI::ChangeRepresentation(SMESH_Actor * theActor, int type){
1850   switch (type){
1851   case 0:{
1852     //theActor->UnShrink();
1853     theActor->SetRepresentation(SMESH_Actor::eEdge);
1854     break;
1855   }
1856   case 1:{
1857     //theActor->UnShrink();
1858     theActor->SetRepresentation(SMESH_Actor::eSurface);
1859     break;
1860   }
1861   case 2:{
1862     theActor->IsShrunk() ? theActor->UnShrink() : theActor->SetShrink();
1863     //theActor->SetRepresentation(SMESH_Actor::eSurface);
1864     break;
1865   }
1866   case 5:{
1867     //theActor->UnShrink();
1868     theActor->SetRepresentation(SMESH_Actor::ePoint);
1869     break;
1870   }
1871   case 3:{
1872     float color[3];
1873     theActor->GetSufaceColor(color[0], color[1], color[2]);
1874     int c0 = int (color[0] * 255);
1875     int c1 = int (color[1] * 255);
1876     int c2 = int (color[2] * 255);
1877     QColor c(c0, c1, c2);
1878
1879     float edgecolor[3];
1880     theActor->GetEdgeColor(edgecolor[0], edgecolor[1], edgecolor[2]);
1881     c0 = int (edgecolor[0] * 255);
1882     c1 = int (edgecolor[1] * 255);
1883     c2 = int (edgecolor[2] * 255);
1884     QColor e(c0, c1, c2);
1885
1886     float backfacecolor[3];
1887     theActor->GetBackSufaceColor(backfacecolor[0], backfacecolor[1], backfacecolor[2]);
1888     c0 = int (backfacecolor[0] * 255);
1889     c1 = int (backfacecolor[1] * 255);
1890     c2 = int (backfacecolor[2] * 255);
1891     QColor b(c0, c1, c2);
1892
1893     float nodecolor[3];
1894     theActor->GetNodeColor(nodecolor[0], nodecolor[1], nodecolor[2]);
1895     c0 = int (nodecolor[0] * 255);
1896     c1 = int (nodecolor[1] * 255);
1897     c2 = int (nodecolor[2] * 255);
1898     QColor n(c0, c1, c2);
1899
1900     int Edgewidth = (int)theActor->GetLineWidth();
1901     if(Edgewidth == 0)
1902       Edgewidth = 1;
1903     int intValue = int(theActor->GetNodeSize());
1904     float Shrink = theActor->GetShrinkFactor();
1905
1906     SMESHGUI_Preferences_ColorDlg *aDlg = new SMESHGUI_Preferences_ColorDlg(QAD_Application::getDesktop(),"");
1907     aDlg->SetColor(1, c);
1908     aDlg->SetColor(2, e);
1909     aDlg->SetColor(3, n);
1910     aDlg->SetColor(4, b);
1911     aDlg->SetIntValue(1, Edgewidth);
1912     aDlg->SetIntValue(2, intValue);
1913     aDlg->SetIntValue(3, int(Shrink*100.));
1914     if(aDlg->exec()){
1915       QColor color = aDlg->GetColor(1);
1916       QColor edgecolor = aDlg->GetColor(2);
1917       QColor nodecolor = aDlg->GetColor(3);
1918       QColor backfacecolor = aDlg->GetColor(4);
1919       /* actor color and backface color */
1920       theActor->SetSufaceColor(float (color.red()) / 255.,
1921                                float (color.green()) / 255., 
1922                                float (color.blue()) / 255.);
1923       theActor->SetBackSufaceColor(float (backfacecolor.red()) / 255., 
1924                                    float (backfacecolor.green()) / 255.,
1925                                    float (backfacecolor.blue()) / 255.);
1926
1927       /* edge color */
1928       theActor->SetEdgeColor(float (edgecolor.red()) / 255.,
1929                              float (edgecolor.green()) / 255.,
1930                              float (edgecolor.blue()) / 255.);
1931       
1932       /* Shrink factor and size edges */
1933       theActor->SetShrinkFactor(aDlg->GetIntValue(3) / 100.);
1934       theActor->SetLineWidth(aDlg->GetIntValue(1));
1935       
1936       /* Nodes color and size */
1937       theActor->SetNodeColor(float (nodecolor.red()) / 255.,
1938                              float (nodecolor.green()) / 255.,
1939                              float (nodecolor.blue()) / 255.);
1940       theActor->SetNodeSize(aDlg->GetIntValue(2));
1941
1942       delete aDlg;
1943     }
1944     break;
1945   }
1946   case 4:{
1947     EmitSignalDeactivateDialog();
1948     SMESHGUI_TransparencyDlg *aDlg = new SMESHGUI_TransparencyDlg(QAD_Application::getDesktop(),"",false);
1949     break;
1950   }
1951   }
1952   UpdateView();
1953 }
1954
1955 //=============================================================================
1956 /*!
1957  *
1958  */
1959 //=============================================================================
1960 void SMESHGUI::UpdateView(){
1961   ::RepaintCurrentView();
1962 }
1963
1964 //=============================================================================
1965 /*!
1966  *
1967  */
1968 //=============================================================================
1969 void SMESHGUI::DisplayActor(SMESH_Actor * theActor, bool theVisibility){
1970   theActor->SetVisibility(theVisibility);
1971   ::DisplayActor(myActiveStudy->getActiveStudyFrame(),theActor);
1972 }
1973
1974 //=============================================================================
1975 /*!
1976  *
1977  */
1978 //=============================================================================
1979 void SMESHGUI::EraseActor(SMESH_Actor * theActor){
1980   theActor->SetVisibility(false);
1981 }
1982
1983 //=============================================================================
1984 /*!
1985  *
1986  */
1987 //=============================================================================
1988 bool SMESHGUI::AddActorInSelection(SMESH_Actor * ac)
1989 {
1990         SALOME_Selection *Sel =
1991                 SALOME_Selection::Selection(myActiveStudy->getSelection());
1992         Sel->ClearIObjects();
1993         if (ac->hasIO())
1994                 return Sel->AddIObject(ac->getIO());
1995         else
1996                 return -1;
1997 }
1998
1999 //=============================================================================
2000 /*!
2001  *
2002  */
2003 //=============================================================================
2004 QString SMESHGUI::CheckHomogeneousSelection()
2005 {
2006         SALOME_Selection *Sel =
2007                 SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2008         /* copy the list */
2009         SALOME_ListIO List;
2010         SALOME_ListIteratorOfListIO Itinit(Sel->StoredIObjects());
2011         for (; Itinit.More(); Itinit.Next())
2012         {
2013                 List.Append(Itinit.Value());
2014         }
2015
2016         QString RefType = CheckTypeObject(Sel->firstIObject());
2017         SALOME_ListIteratorOfListIO It(List);
2018         for (; It.More(); It.Next())
2019         {
2020                 Handle(SALOME_InteractiveObject) IObject = It.Value();
2021                 QString Type = CheckTypeObject(IObject);
2022                 if (Type.compare(RefType) != 0)
2023                         return "Heterogeneous Selection";
2024         }
2025
2026         Sel->ClearIObjects();
2027         SALOME_ListIteratorOfListIO It1(List);
2028         for (; It1.More(); It1.Next())
2029         {
2030                 int res = Sel->AddIObject(It1.Value());
2031                 if (res == -1)
2032                         myActiveStudy->highlight(It1.Value(), false);
2033                 if (res == 0)
2034                         myActiveStudy->highlight(It1.Value(), true);
2035         }
2036         return RefType;
2037 }
2038
2039 //=============================================================================
2040 /*!
2041  *
2042  */
2043 //=============================================================================
2044 QString SMESHGUI::CheckTypeObject(const Handle(SALOME_InteractiveObject) & IO)
2045 {
2046   SALOMEDS::SObject_var sobj = smeshGUI->myActiveStudy->getStudyDocument()->FindObjectID(IO->getEntry());
2047   if (!sobj->_is_nil()) {
2048     SALOMEDS::SComponent_var scomp = sobj->GetFatherComponent();
2049     if (strcmp(scomp->GetID(), IO->getEntry()) == 0)
2050       {                                         // component is selected
2051         return "Component";
2052       }
2053   }
2054
2055   SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2056   Sel->ClearIObjects();
2057
2058   Handle(SMESH_TypeFilter) aHypFilter = new SMESH_TypeFilter(HYPOTHESIS);
2059   Handle(SMESH_TypeFilter) anAlgoFilter = new SMESH_TypeFilter(ALGORITHM);
2060   Handle(SMESH_TypeFilter) aMeshFilter = new SMESH_TypeFilter(MESH);
2061   Handle(SMESH_TypeFilter) aSubMeshFilter = new SMESH_TypeFilter(SUBMESH);
2062   Handle(SMESH_TypeFilter) aGroupFilter = new SMESH_TypeFilter(GROUP);
2063   Handle(SMESH_TypeFilter) aMeshOrSubMeshFilter = new SMESH_TypeFilter(MESHorSUBMESH);
2064   Handle(SMESH_TypeFilter) aSubMeshVextexFilter = new SMESH_TypeFilter(SUBMESH_VERTEX);
2065   Handle(SMESH_TypeFilter) aSubMeshEdgeFilter = new SMESH_TypeFilter(SUBMESH_EDGE);
2066   Handle(SMESH_TypeFilter) aSubMeshFaceFilter = new SMESH_TypeFilter(SUBMESH_FACE);
2067   Handle(SMESH_TypeFilter) aSubMeshSolidFilter = new SMESH_TypeFilter(SUBMESH_SOLID);
2068   Handle(SMESH_TypeFilter) aSubMeshCompoundFilter = new SMESH_TypeFilter(SUBMESH_COMPOUND);
2069
2070   Sel->AddFilter(aHypFilter);
2071   if (Sel->AddIObject(IO) != -1) {
2072     Sel->ClearFilters();
2073     return "Hypothesis";
2074   }
2075
2076   Sel->ClearFilters();
2077   Sel->AddFilter(anAlgoFilter);
2078   if (Sel->AddIObject(IO) != -1) {
2079     Sel->ClearFilters();
2080     return "Algorithm";
2081   }
2082
2083   Sel->ClearFilters();
2084   Sel->AddFilter(aMeshFilter);
2085   if (Sel->AddIObject(IO) != -1) {
2086     Sel->ClearFilters();
2087     return "Mesh";
2088   }
2089
2090   Sel->ClearFilters();
2091   Sel->AddFilter(aSubMeshFilter);
2092   if (Sel->AddIObject(IO) != -1) {
2093     Sel->ClearFilters();
2094     return "SubMesh";
2095   }
2096
2097   Sel->ClearFilters();
2098   Sel->AddFilter(aGroupFilter);
2099   if (Sel->AddIObject(IO) != -1) {
2100     Sel->ClearFilters();
2101     return "Group";
2102   }
2103
2104   Sel->ClearFilters();
2105   Sel->AddFilter(aSubMeshVextexFilter);
2106   if (Sel->AddIObject(IO) != -1) {
2107     Sel->ClearFilters();
2108     return "SubMeshVertex";
2109   }
2110
2111   Sel->ClearFilters();
2112   Sel->AddFilter(aSubMeshEdgeFilter);
2113   if (Sel->AddIObject(IO) != -1) {
2114     Sel->ClearFilters();
2115     return "SubMeshEdge";
2116   }
2117
2118   Sel->ClearFilters();
2119   Sel->AddFilter(aSubMeshFaceFilter);
2120   if (Sel->AddIObject(IO) != -1) {
2121     Sel->ClearFilters();
2122     return "SubMeshFace";
2123   }
2124
2125   Sel->ClearFilters();
2126   Sel->AddFilter(aSubMeshSolidFilter);
2127   if (Sel->AddIObject(IO) != -1) {
2128     Sel->ClearFilters();
2129     return "SubMeshSolid";
2130   }
2131
2132   Sel->ClearFilters();
2133   Sel->AddFilter(aSubMeshCompoundFilter);
2134   if (Sel->AddIObject(IO) != -1) {
2135     Sel->ClearFilters();
2136     return "SubMeshCompound";
2137   }
2138
2139   Sel->ClearFilters();
2140   Sel->AddIObject(IO);
2141   return "NoType";
2142 }
2143
2144
2145 static int isStudyLocked(const SALOMEDS::Study_var& theStudy){
2146   return theStudy->GetProperties()->IsLocked();
2147 }
2148
2149 static int checkLock(const SALOMEDS::Study_var& theStudy) {
2150   if (isStudyLocked(theStudy)) {
2151     QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
2152                            QObject::tr("WRN_WARNING"), 
2153                            QObject::tr("WRN_STUDY_LOCKED"),
2154                            QObject::tr("BUT_OK") );
2155     return true;
2156   }
2157   return false;
2158 }
2159
2160 //=============================================================================
2161 /*!
2162  *
2163  */
2164 //=============================================================================
2165 bool SMESHGUI::OnGUIEvent(int theCommandID, QAD_Desktop * parent)
2166 {
2167   SALOMEDS::Study_var aStudy = smeshGUI->myActiveStudy->getStudyDocument(); //Document OCAF de l'etude active
2168   //  QAD_Viewer3d* v3d;
2169   OCCViewer_Viewer3d *v3d;
2170
2171   Handle(AIS_InteractiveContext) ic;
2172   vtkRenderer *Renderer;
2173   vtkRenderWindow *RenWin;
2174
2175   if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
2176     v3d = ((OCCViewer_ViewFrame *) smeshGUI->myActiveStudy->
2177            getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
2178     ic = v3d->getAISContext();
2179   }
2180   else if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2181     Renderer = ((VTKViewer_ViewFrame *) smeshGUI->myActiveStudy->
2182                 getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRenderer();
2183     RenWin = Renderer->GetRenderWindow();
2184   }
2185
2186   switch (theCommandID) {
2187   case 33:                                      // DELETE
2188     if(checkLock(aStudy)) break;
2189     smeshGUI->OnEditDelete();
2190     break;
2191     
2192   case 113:                                     // IMPORT
2193   case 112:
2194   case 111:
2195     {
2196       if(checkLock(aStudy)) break;
2197       //Import_Document(parent, theCommandID);  //NBU
2198       Import_Mesh(parent,theCommandID);
2199       break;
2200     }
2201
2202   case 122:                                     // EXPORT MED
2203   case 121:
2204   case 123:
2205     {
2206       Export_Mesh(parent, theCommandID);
2207       break;
2208     }
2209
2210   case 200:                                     // SCALAR BAR
2211     {
2212       SALOME_Selection *Sel = SALOME_Selection::Selection( smeshGUI->myActiveStudy->getSelection() );
2213       if( Sel && Sel->IObjectCount() ) {
2214         Handle(SALOME_InteractiveObject) anIO = Sel->firstIObject();
2215         if( anIO->hasEntry() ) {
2216           if( SMESH_Actor* anActor = ::FindActorByEntry( anIO->getEntry() ) ) {
2217             anActor->SetControlMode( SMESH_Actor::eNone );
2218           }
2219         }
2220       }
2221       break;
2222     }
2223   case 201:
2224     {
2225       SALOME_Selection* Sel = SALOME_Selection::Selection( smeshGUI->myActiveStudy->getSelection() );
2226       SMESHGUI_Preferences_ScalarBarDlg::ScalarBarProperties( parent, Sel );
2227       break;
2228     }
2229
2230   case 1133:                                    // DISPLAY MODE : WireFrame, Surface, Shrink
2231   case 1132:
2232   case 215:
2233   case 213:
2234   case 212:
2235   case 211:
2236     {
2237       smeshGUI->SetViewMode(theCommandID);
2238       break;
2239     }
2240
2241   case 214:                                     // UPDATE
2242     {
2243       if(checkLock(aStudy)) break;
2244       smeshGUI->Update();
2245       break;
2246     }
2247
2248   case 300:                                     // ERASE
2249   case 301:                                     // DISPLAY
2250   case 302:                                     // DISPLAY ONLY
2251     {
2252       EDisplaing anAction;
2253       switch(theCommandID){
2254       case 300: anAction = eErase; break;
2255       case 301: anAction = eDisplay; break;
2256       case 302: anAction = eDisplayOnly; break;
2257       }
2258       
2259       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2260       if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2261         SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
2262         for (; It.More(); It.Next()) {
2263           Handle(SALOME_InteractiveObject) IOS = It.Value();
2264           if (IOS->hasEntry()) {
2265             ::UpdateView(anAction,IOS->getEntry());
2266           }
2267         }
2268       }
2269       Sel->ClearIObjects();
2270       break;
2271     }
2272
2273   case 400:                                     // NODES
2274     {
2275       if(checkLock(aStudy)) break;
2276       if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2277         smeshGUI->EmitSignalDeactivateDialog();
2278         SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2279         Sel->ClearIObjects();
2280         smeshGUI->myDesktop->SetSelectionMode(NodeSelection, true);
2281         smeshGUI->ViewNodes();
2282         SMESHGUI_NodesDlg *aDlg = new SMESHGUI_NodesDlg(parent, "", Sel);
2283       }
2284       else {
2285         QAD_MessageBox::warn1(QAD_Application::getDesktop(),
2286                               tr("SMESH_WRN_WARNING"), 
2287                               tr("SMESH_WRN_VIEWER_VTK"),
2288                               tr("SMESH_BUT_OK"));
2289       }
2290       break;
2291     }
2292
2293   case 2151:  // FILTER
2294   {
2295     if ( smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK )
2296     {
2297       smeshGUI->EmitSignalDeactivateDialog();
2298       SMESHGUI_FilterDlg *aDlg = new SMESHGUI_FilterDlg( parent, SMESH::EDGE );
2299     }
2300     break;
2301   }
2302     
2303   case 405:                                     // MOVE NODE
2304     {
2305       if(checkLock(aStudy)) break;
2306       smeshGUI->myDesktop->SetSelectionMode(NodeSelection, true);
2307       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2308       SMESHGUI_MoveNodesDlg *aDlg = new SMESHGUI_MoveNodesDlg(parent, "", Sel);
2309       break;
2310     }
2311     
2312   case 701:                                     // COMPUTE MESH 
2313     {
2314       if(checkLock(aStudy)) break;
2315       if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2316         SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2317         int nbSel = Sel->IObjectCount();
2318         if (nbSel != 1){
2319           break;
2320         }
2321
2322         SMESH::SMESH_Mesh_var aMesh;
2323         SMESH::SMESH_subMesh_var aSubMesh;
2324         Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
2325         if (IObject->hasEntry()){
2326           SALOMEDS::SObject_var aMeshSObj = smeshGUI->myStudy->FindObjectID(IObject->getEntry());
2327           GEOM::GEOM_Shape_var aShape = smeshGUI->myStudyAPI.GetShapeOnMeshOrSubMesh( aMeshSObj );
2328           if ( aShape->_is_nil() ) {
2329             // imported mesh
2330             break;
2331           }
2332           SALOMEDS::GenericAttribute_var anAttr;
2333           if(!aMeshSObj->_is_nil() && aMeshSObj->FindAttribute(anAttr, "AttributeIOR")){
2334             SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
2335             MESSAGE("SMESHGUI::OnGUIEvent - Compute mesh : IOR = "<< anIOR->Value());
2336             CORBA::Object_var anObj;
2337             try{
2338               anObj = _orb->string_to_object(anIOR->Value());
2339               if (CORBA::is_nil(anObj)){
2340                 MESSAGE("SMESHGUI::OnGUIEvent - Compute mesh : nil object");
2341               }
2342             }catch(CORBA::COMM_FAILURE & ex){
2343               INFOS("SMESHGUI::OnGUIEvent - Compute mesh : exception (1)");
2344             }
2345             aMesh = SMESH::SMESH_Mesh::_narrow(anObj);
2346             aSubMesh = SMESH::SMESH_subMesh::_narrow(anObj);
2347             if (!aMesh->_is_nil()){
2348               GEOM::GEOM_Shape_var refShape = smeshGUI->myStudyAPI.GetShapeOnMeshOrSubMesh(aMeshSObj);
2349               if (!refShape->_is_nil()) {
2350                 if(!smeshGUI->myComponentMesh->IsReadyToCompute(aMesh,refShape)){
2351                   QAD_MessageBox::warn1(QAD_Application::getDesktop(), 
2352                                         tr("SMESH_WRN_WARNING"),
2353                                         tr("SMESH_WRN_MISSING_PARAMETERS"),
2354                                         tr("SMESH_BUT_OK"));
2355                   break;
2356                 }
2357                 try{
2358                   if (smeshGUI->myComponentMesh->Compute(aMesh,refShape))
2359                     smeshGUI->myStudyAPI.ModifiedMesh(aMeshSObj,true);
2360                   // TO Do : change icon of all submeshes
2361                 }
2362                 catch(const SALOME::SALOME_Exception & S_ex){
2363                   QtCatchCorbaException(S_ex);
2364                 }
2365               }
2366             }else if(!aSubMesh->_is_nil()){
2367               aMesh = aSubMesh->GetFather();
2368               GEOM::GEOM_Shape_var refShape = smeshGUI->myStudyAPI.GetShapeOnMeshOrSubMesh(aMeshSObj);
2369               if(!refShape->_is_nil()){
2370                 bool compute = smeshGUI->myComponentMesh->IsReadyToCompute(aMesh,refShape);
2371                 if(!compute){
2372                   QAD_MessageBox::warn1(QAD_Application::getDesktop(), 
2373                                         tr("SMESH_WRN_WARNING"),
2374                                         tr("SMESH_WRN_MISSING_PARAMETERS"),
2375                                         tr("SMESH_BUT_OK"));
2376                   break;
2377                 }
2378                 try{
2379                   if ( smeshGUI->myComponentMesh->Compute(aMesh,refShape) )
2380                     smeshGUI->myStudyAPI.ModifiedMesh(aMeshSObj,true);
2381                   // TO Do : change icon of all submeshes
2382                 }catch(const SALOME::SALOME_Exception & S_ex){
2383                   QtCatchCorbaException(S_ex);
2384                 }
2385               }
2386             }
2387           }
2388         }
2389         CORBA::Long anId = smeshGUI->myStudy->StudyId();
2390         TVisualObjPtr aVisualObj = GetVisualObj(anId,IObject->getEntry());
2391         if(smeshGUI->myAutomaticUpdate && aVisualObj){
2392           aVisualObj->Update();
2393           SMESH_Actor* anActor = ::FindActorByEntry(IObject->getEntry());
2394           if(!anActor){
2395             anActor = ::CreateActor(smeshGUI->myStudy,IObject->getEntry());
2396             if(anActor){
2397               ::DisplayActor(smeshGUI->myActiveStudy->getActiveStudyFrame(),anActor); //apo
2398               ::FitAll();
2399             }
2400           }
2401         }
2402       }else{
2403         QAD_MessageBox::warn1(QAD_Application::getDesktop(),
2404                               tr("SMESH_WRN_WARNING"), 
2405                               tr("SMESH_WRN_VIEWER_VTK"),
2406                               tr("SMESH_BUT_OK"));
2407       }
2408       break;
2409     }
2410
2411   case 702:                                     // ADD SUB MESH 
2412     {
2413       if(checkLock(aStudy)) break;
2414       if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2415         smeshGUI->EmitSignalDeactivateDialog();
2416         SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2417         SMESHGUI_AddSubMeshDlg *aDlg = new SMESHGUI_AddSubMeshDlg(parent, "", Sel);
2418       }
2419       else {
2420         QAD_MessageBox::warn1(QAD_Application::getDesktop(),
2421                               tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"),
2422                               tr("SMESH_BUT_OK"));
2423       }
2424       break;
2425     }
2426     
2427   case 703:                                     // INIT MESH 
2428     {
2429       if(checkLock(aStudy)) break;
2430       smeshGUI->EmitSignalDeactivateDialog();
2431       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2432       SMESHGUI_InitMeshDlg *aDlg = new SMESHGUI_InitMeshDlg(parent, "", Sel);
2433       break;
2434     }
2435
2436   case 704:                                     // EDIT Hypothesis 
2437     {
2438       if(checkLock(aStudy)) break;
2439       smeshGUI->EmitSignalDeactivateDialog();
2440       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2441       SMESHGUI_EditHypothesesDlg *aDlg = new SMESHGUI_EditHypothesesDlg(parent, "", Sel);
2442       break;
2443     }
2444
2445   case 705:                                     //  EDIT Global Hypothesis
2446     {
2447       if(checkLock(aStudy)) break;
2448       smeshGUI->EmitSignalDeactivateDialog();
2449       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2450       SMESHGUI_EditHypothesesDlg *aDlg = new SMESHGUI_EditHypothesesDlg(parent, "", Sel);
2451       break;
2452     }
2453
2454   case 706:                                     //  EDIT Local Hypothesis
2455     {
2456       if(checkLock(aStudy)) break;
2457       smeshGUI->EmitSignalDeactivateDialog();
2458       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2459       SMESHGUI_EditHypothesesDlg *aDlg = new SMESHGUI_EditHypothesesDlg(parent, "", Sel);
2460       break;
2461     }
2462
2463   case 406:                                     // ORIENTATION ELEMENTS
2464     {
2465       if(checkLock(aStudy)) break;
2466       smeshGUI->EmitSignalDeactivateDialog();
2467       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2468       smeshGUI->myDesktop->SetSelectionMode(FaceSelection, true);
2469       SMESHGUI_OrientationElementsDlg *aDlg = new SMESHGUI_OrientationElementsDlg(parent, "", Sel);
2470       break;
2471     }
2472
2473   case 407:                                     // DIAGONAL INVERSION
2474     {
2475       if(checkLock(aStudy)) break;
2476       smeshGUI->EmitSignalDeactivateDialog();
2477       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2478       Sel->ClearIObjects();
2479       smeshGUI->myDesktop->SetSelectionMode(EdgeOfCellSelection, true);
2480       SMESHGUI_DiagonalInversionDlg *aDlg = new SMESHGUI_DiagonalInversionDlg(parent, "", Sel);
2481       break;
2482     }
2483
2484   case 801:                                     // CREATE GROUP
2485     {
2486       if(checkLock(aStudy)) break;
2487       smeshGUI->EmitSignalDeactivateDialog();
2488       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2489       int nbSel = Sel->IObjectCount();
2490       if (nbSel == 1) {
2491         // check if mesh is selected
2492         Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
2493         if (IObject->hasEntry()) {
2494           SALOMEDS::SObject_var aMeshSObj = smeshGUI->myStudy->FindObjectID(IObject->getEntry());
2495           if (!aMeshSObj->_is_nil()) {
2496             CORBA::Object_var anObj = aMeshSObj->GetObject();
2497             SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObj);
2498             SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(anObj);
2499             if (aMesh->_is_nil() && !aSubMesh->_is_nil()) {
2500               aMesh = aSubMesh->GetFather();
2501             }
2502             if (!aMesh->_is_nil()) {
2503               SMESHGUI_GroupDlg *aDlg = new SMESHGUI_GroupDlg(parent, "", Sel, aMesh);
2504               aDlg->show();
2505             }
2506           }
2507         }
2508       }
2509       break;
2510     }
2511
2512   case 802:                                     // CONSTRUCT GROUP
2513     {
2514       if(checkLock(aStudy)) break;
2515       smeshGUI->EmitSignalDeactivateDialog();
2516       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2517       int nbSel = Sel->IObjectCount();
2518       if (nbSel == 1) {
2519         // check if submesh is selected
2520         Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
2521         if (IObject->hasEntry()) {
2522           SALOMEDS::SObject_var aSObj = smeshGUI->myStudy->FindObjectID(IObject->getEntry());
2523           if(!aSObj->_is_nil()) {
2524             SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(aSObj->GetObject());
2525             if (!aSubMesh->_is_nil()) {
2526               try {
2527                 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
2528                 // get submesh elements list by types
2529                 SMESH::long_array_var aNodes = aSubMesh->GetElementsByType(SMESH::NODE);
2530                 SMESH::long_array_var aEdges = aSubMesh->GetElementsByType(SMESH::EDGE);
2531                 SMESH::long_array_var aFaces = aSubMesh->GetElementsByType(SMESH::FACE);
2532                 SMESH::long_array_var aVolumes = aSubMesh->GetElementsByType(SMESH::VOLUME);
2533                 // create group for each type o elements
2534                 QString aName = IObject->getName();
2535                 MESSAGE("SMESHGUI::OnGUIEvent - Construct group on submesh : "<<aName);
2536                 MESSAGE(" Number of elements : nodes "<<aNodes->length() << ", edges " << aEdges->length()
2537                         << ", faces " << aFaces->length() << ", volumes " << aVolumes->length());
2538                 if (aNodes->length() > 0) {
2539                   SMESH::SMESH_Group_var aGroup = smeshGUI->AddGroup(aMesh, SMESH::NODE, aName);
2540                   aGroup->Add(aNodes);
2541                 }
2542                 if (aEdges->length() > 0) {
2543                   SMESH::SMESH_Group_var aGroup = smeshGUI->AddGroup(aMesh, SMESH::EDGE, aName);
2544                   aGroup->Add(aEdges);
2545                 }
2546                 if (aFaces->length() > 0) {
2547                   SMESH::SMESH_Group_var aGroup = smeshGUI->AddGroup(aMesh, SMESH::FACE, aName);
2548                   aGroup->Add(aFaces);
2549                 }
2550                 if (aVolumes->length() > 0) {
2551                   SMESH::SMESH_Group_var aGroup = smeshGUI->AddGroup(aMesh, SMESH::VOLUME, aName);
2552                   aGroup->Add(aVolumes);
2553                 }
2554                 smeshGUI->myActiveStudy->updateObjBrowser(true);
2555               }catch(const SALOME::SALOME_Exception & S_ex){
2556                 QtCatchCorbaException(S_ex);
2557               }
2558             }
2559           }
2560         }
2561       }
2562       break;
2563     }
2564
2565   case 803:                                     // EDIT GROUP
2566     {
2567       if(checkLock(aStudy)) break;
2568       smeshGUI->EmitSignalDeactivateDialog();
2569       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2570       int nbSel = Sel->IObjectCount();
2571       if (nbSel == 1) {
2572         // check if group is selected
2573         Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
2574         if (IObject->hasEntry()) {
2575           SALOMEDS::SObject_var aSObj = smeshGUI->myStudy->FindObjectID(IObject->getEntry());
2576           if(!aSObj->_is_nil()) {
2577             SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow(aSObj->GetObject());
2578             if (!aGroup->_is_nil()) {
2579               SMESHGUI_GroupDlg *aDlg = new SMESHGUI_GroupDlg(parent, "", Sel, aGroup);
2580               aDlg->show();
2581             }
2582           }
2583         }
2584       }
2585       break;
2586     }
2587
2588   case 804:                                     // Add elements to group
2589     {
2590       if(checkLock(aStudy)) break;
2591       if (smeshGUI->myState == 800) {
2592         SMESHGUI_GroupDlg *aDlg = (SMESHGUI_GroupDlg*) smeshGUI->myActiveDialogBox;
2593         if (aDlg) aDlg->onAdd();
2594       }
2595       break;
2596     }
2597
2598   case 805:                                     // Remove elements from group
2599     {
2600       if(checkLock(aStudy)) break;
2601       if (smeshGUI->myState == 800) {
2602         SMESHGUI_GroupDlg *aDlg = (SMESHGUI_GroupDlg*) smeshGUI->myActiveDialogBox;
2603         if (aDlg) aDlg->onRemove();
2604       }
2605       break;
2606     }
2607
2608   case 900:                                     // MESH INFOS
2609     {
2610       smeshGUI->EmitSignalDeactivateDialog();
2611       SMESHGUI_MeshInfosDlg *aDlg = new SMESHGUI_MeshInfosDlg(parent, "", false);
2612       break;
2613     }
2614
2615   case 1001:                                    // AUTOMATIC UPDATE PREFERENCES
2616     {
2617       parent->menuBar()->setItemChecked(1001, !parent->menuBar()->isItemChecked(1001));
2618       if (parent->menuBar()->isItemChecked(1001)) {
2619         QAD_CONFIG->addSetting("SMESH:AutomaticUpdate", "true");
2620         smeshGUI->myAutomaticUpdate = true;
2621       }
2622       else {
2623         QAD_CONFIG->addSetting("SMESH:AutomaticUpdate", "false");
2624         smeshGUI->myAutomaticUpdate = false;
2625       }
2626       break;
2627     }
2628
2629   case 1003:                                    // MESH PREFERENCES
2630     {
2631       smeshGUI->SetDisplaySettings();
2632       break;
2633     }
2634
2635   case 1005:
2636     {
2637       SMESHGUI_Preferences_ScalarBarDlg::ScalarBarPreferences( parent );
2638       break;
2639     }
2640
2641   case 1006: 
2642     {
2643       SMESHGUI_Preferences_SelectionDlg* aDlg = 
2644         new SMESHGUI_Preferences_SelectionDlg(parent);
2645
2646       QColor aColor;
2647       QString SCr, SCg, SCb;
2648       SCr = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorRed");
2649       SCg = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorGreen");
2650       SCb = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorBlue");
2651       if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
2652         aColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
2653       else aColor = Qt::cyan;
2654       aDlg->SetColor(1, aColor);
2655
2656       SCr = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorRed");
2657       SCg = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorGreen");
2658       SCb = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorBlue");
2659       if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
2660         aColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
2661       else aColor = Qt::yellow;
2662       aDlg->SetColor(2, aColor);
2663
2664       SCr = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorRed");
2665       SCg = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorGreen");
2666       SCb = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorBlue");
2667       if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
2668         aColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
2669       else aColor = Qt::white;
2670       aDlg->SetColor(3, aColor);
2671
2672       QString SW1 = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectWidth");
2673       if (SW1.isEmpty()) SW1 = "5";
2674       aDlg->SetWidth(1, SW1.toInt());
2675
2676       QString SW2 = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectWidth");
2677       if (SW2.isEmpty()) SW2 = "5";
2678       aDlg->SetWidth(2, SW2.toInt());
2679
2680       QString SP1 = QAD_CONFIG->getSetting("SMESH:SettingsNodeSelectTol");
2681       if (SP1.isEmpty()) SP1 = "0.025";
2682       aDlg->SetPrecision(1, SP1.toDouble());
2683
2684       QString SP2 = QAD_CONFIG->getSetting("SMESH:SettingsElementsSelectTol");
2685       if (SP2.isEmpty()) SP2 = "0.001";
2686       aDlg->SetPrecision(2, SP2.toDouble());
2687
2688       if (aDlg->exec()) {
2689         QColor aPreColor = aDlg->GetColor(1);
2690         QAD_CONFIG->addSetting("SMESH:SettingsPreSelectColorRed", aPreColor.red());
2691         QAD_CONFIG->addSetting("SMESH:SettingsPreSelectColorGreen", aPreColor.green());
2692         QAD_CONFIG->addSetting("SMESH:SettingsPreSelectColorBlue", aPreColor.blue());
2693
2694         QColor aSelColor = aDlg->GetColor(2);
2695         QAD_CONFIG->addSetting("SMESH:SettingsItemSelectColorRed", aSelColor.red());
2696         QAD_CONFIG->addSetting("SMESH:SettingsItemSelectColorGreen", aSelColor.green());
2697         QAD_CONFIG->addSetting("SMESH:SettingsItemSelectColorBlue", aSelColor.blue());
2698
2699         QColor aHiColor = aDlg->GetColor(3);
2700         QAD_CONFIG->addSetting("SMESH:SettingsSelectColorRed", aHiColor.red());
2701         QAD_CONFIG->addSetting("SMESH:SettingsSelectColorGreen", aHiColor.green());
2702         QAD_CONFIG->addSetting("SMESH:SettingsSelectColorBlue", aHiColor.blue());
2703
2704         int aPreWidth = aDlg->GetWidth(1);
2705         QAD_CONFIG->addSetting("SMESH:SettingsPreSelectWidth", aPreWidth);
2706         int aSelWidth = aDlg->GetWidth(2);
2707         QAD_CONFIG->addSetting("SMESH:SettingsItemSelectWidth", aSelWidth);
2708
2709         double aTolNodes = aDlg->GetPrecision(1);
2710         QAD_CONFIG->addSetting("SMESH:SettingsNodeSelectTol", aTolNodes);
2711         double aTolItems = aDlg->GetPrecision(2);
2712         QAD_CONFIG->addSetting("SMESH:SettingsElementsSelectTol", aTolItems);
2713
2714         // update current study settings
2715         ::UpdateSelectionProp();
2716
2717         QAD_StudyFrame* studyFrame = smeshGUI->myActiveStudy->getActiveStudyFrame();
2718         if (studyFrame->getTypeView() == VIEW_VTK) {
2719           VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(studyFrame);
2720           // update VTK viewer properties
2721           VTKViewer_RenderWindowInteractor* anInteractor = aViewFrame->getRWInteractor();
2722           if (anInteractor) {
2723             anInteractor->SetSelectionProp(aSelColor.red()/255., aSelColor.green()/255., 
2724                                            aSelColor.blue()/255., aSelWidth);
2725             anInteractor->SetSelectionTolerance(aTolNodes, aTolItems);
2726             VTKViewer_InteractorStyleSALOME* aStyle = anInteractor->GetInteractorStyleSALOME();
2727             if (aStyle)
2728               aStyle->setPreselectionProp(aPreColor.red()/255., aPreColor.green()/255., 
2729                                           aPreColor.blue()/255., aPreWidth);
2730           }
2731           // update actors
2732           vtkRenderer* aRenderer = aViewFrame->getRenderer();
2733           vtkActorCollection *aCollection = aRenderer->GetActors();
2734           aCollection->InitTraversal();
2735           while(vtkActor *anAct = aCollection->GetNextActor()){
2736             if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
2737               anActor->SetHighlightColor(aHiColor.red()/255., aHiColor.green()/255., 
2738                                          aHiColor.blue()/255.);
2739               anActor->SetPreHighlightColor(aPreColor.red()/255., aPreColor.green()/255., 
2740                                             aPreColor.blue()/255.);
2741             }
2742           }
2743         }
2744       }
2745
2746       break;
2747     }
2748
2749   case 1100:                                    // EDIT HYPOTHESIS
2750     {
2751       if(checkLock(aStudy)) break;
2752       SALOME_Selection *Sel =
2753         SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2754       int nbSel = Sel->IObjectCount();
2755
2756       if (nbSel == 1) {
2757         Standard_Boolean res;
2758         SMESH::SMESH_Hypothesis_var Hyp =
2759           smeshGUI->ConvertIOinSMESHHypothesis(Sel->firstIObject(), res);
2760
2761         /* Look for all mesh objects that have this hypothesis affected in order to flag as ModifiedMesh */
2762         /* At end below '...->updateObjBrowser(true)' will change icon of mesh objects                   */
2763         /* Warning : however by internal mechanism all subMeshes icons are changed !                     */
2764         if ( res )
2765         {
2766           char* sName = Hyp->GetName();
2767           SMESHGUI_GenericHypothesisCreator* aCreator = smeshGUI->GetHypothesisCreator(sName);
2768           if (aCreator)
2769           {
2770             aCreator->EditHypothesis(Hyp);
2771           }
2772           else
2773           {
2774             // report error
2775           }
2776         }
2777       }
2778       break;
2779     }
2780
2781   case 1101:                                    // RENAME
2782     {
2783       if(checkLock(aStudy)) break;
2784       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2785       SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
2786       for (; It.More(); It.Next()) {
2787         Handle(SALOME_InteractiveObject) IObject = It.Value();
2788
2789         SALOMEDS::SObject_var obj = smeshGUI->myStudy->FindObjectID(IObject->getEntry());
2790         SALOMEDS::GenericAttribute_var anAttr;
2791         SALOMEDS::AttributeName_var aName;
2792         if (!obj->_is_nil()) {
2793           if (obj->FindAttribute(anAttr, "AttributeName")) {
2794             aName = SALOMEDS::AttributeName::_narrow(anAttr);
2795             QString newName = QString(aName->Value());
2796             newName = SALOMEGUI_NameDlg::getName(QAD_Application::getDesktop(), newName);
2797             if (!newName.isEmpty()) {
2798               smeshGUI->myActiveStudy->renameIObject(IObject, newName);
2799             }
2800           }
2801         }
2802       }
2803       break;
2804     }
2805     
2806   case 1102:                                    // REMOVE HYPOTHESIS / ALGORITHMS
2807     {
2808       if(checkLock(aStudy)) break;
2809       QAD_WaitCursor wc;
2810       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2811       SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
2812       for (int i = 0; It.More(); It.Next(), i++) {
2813         Handle(SALOME_InteractiveObject) IObject = It.Value();
2814         smeshGUI->RemoveHypothesisOrAlgorithmOnMesh(IObject);
2815       }
2816       Sel->ClearIObjects();
2817       smeshGUI->myActiveStudy->updateObjBrowser(true);
2818       break;
2819     }
2820
2821   case 401:                                     // GEOM::EDGE
2822     {
2823       if(checkLock(aStudy)) break;
2824       if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2825         smeshGUI->EmitSignalDeactivateDialog();
2826         SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2827         Sel->ClearIObjects();
2828         smeshGUI->myDesktop->SetSelectionMode(NodeSelection, true);
2829         smeshGUI->ViewNodes();
2830         SMESHGUI_AddEdgeDlg *aDlg = new SMESHGUI_AddEdgeDlg(parent, "", Sel);
2831       }
2832       else {
2833         QAD_MessageBox::warn1(QAD_Application::getDesktop(),
2834                               tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"),
2835                               tr("SMESH_BUT_OK"));
2836       }
2837       break;
2838     }
2839   case 4021:                                    // TRIANGLE
2840     {
2841       if(checkLock(aStudy)) break;
2842       if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2843         smeshGUI->EmitSignalDeactivateDialog();
2844         SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2845         Sel->ClearIObjects();
2846         smeshGUI->myDesktop->SetSelectionMode(NodeSelection, true);
2847         smeshGUI->ViewNodes();
2848         SMESHGUI_AddFaceDlg *aDlg = new SMESHGUI_AddFaceDlg(parent, "", Sel, 3);
2849       }
2850       else {
2851         QAD_MessageBox::warn1(QAD_Application::getDesktop(),
2852                               tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"),
2853                               tr("SMESH_BUT_OK"));
2854       }
2855       break;
2856     }
2857   case 4022:                                    // QUAD
2858     {
2859       if(checkLock(aStudy)) break;
2860       if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2861         smeshGUI->EmitSignalDeactivateDialog();
2862         SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2863         Sel->ClearIObjects();
2864         smeshGUI->myDesktop->SetSelectionMode(NodeSelection, true);
2865         smeshGUI->ViewNodes();
2866         SMESHGUI_AddFaceDlg *aDlg = new SMESHGUI_AddFaceDlg(parent, "", Sel, 4);
2867       }
2868       else
2869         {
2870           QAD_MessageBox::warn1(QAD_Application::getDesktop(),
2871                                 tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"),
2872                                 tr("SMESH_BUT_OK"));
2873         }
2874       break;
2875     }
2876   case 4031:                                    // TETRA
2877     {
2878       if(checkLock(aStudy)) break;
2879       if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2880         smeshGUI->EmitSignalDeactivateDialog();
2881         SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2882         Sel->ClearIObjects();
2883         smeshGUI->myDesktop->SetSelectionMode(NodeSelection, true);
2884         smeshGUI->ViewNodes();
2885         SMESHGUI_AddVolumeDlg *aDlg = new SMESHGUI_AddVolumeDlg(parent, "", Sel, 4);
2886       }
2887       else {
2888         QAD_MessageBox::warn1(QAD_Application::getDesktop(),
2889                               tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"),
2890                               tr("SMESH_BUT_OK"));
2891       }
2892       break;
2893     }
2894   case 4032:                                    // HEXA
2895     {
2896       if(checkLock(aStudy)) break;
2897       if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2898         smeshGUI->EmitSignalDeactivateDialog();
2899         SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2900         Sel->ClearIObjects();
2901         smeshGUI->myDesktop->SetSelectionMode(NodeSelection, true);
2902         smeshGUI->ViewNodes();
2903         SMESHGUI_AddVolumeDlg *aDlg = new SMESHGUI_AddVolumeDlg(parent, "", Sel, 8);
2904       }
2905       else {
2906         QAD_MessageBox::warn1(QAD_Application::getDesktop(),
2907                               tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"),
2908                               tr("SMESH_BUT_OK"));
2909       }
2910       break;
2911     }
2912
2913   case 4041:                                    // REMOVES NODES
2914     {
2915       if(checkLock(aStudy)) break;
2916       if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2917         smeshGUI->EmitSignalDeactivateDialog();
2918         SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2919         Sel->ClearIObjects();
2920         smeshGUI->myDesktop->SetSelectionMode(NodeSelection, true);
2921         smeshGUI->ViewNodes();
2922         SMESHGUI_RemoveNodesDlg *aDlg = new SMESHGUI_RemoveNodesDlg(parent, "", Sel);
2923       }
2924       else {
2925         QAD_MessageBox::warn1(QAD_Application::getDesktop(),
2926                               tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"),
2927                               tr("SMESH_BUT_OK"));
2928       }
2929       break;
2930     }
2931   case 4042:                                    // REMOVES ELEMENTS
2932     {
2933       if(checkLock(aStudy)) break;
2934       if (smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
2935         smeshGUI->EmitSignalDeactivateDialog();
2936         SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2937         Sel->ClearIObjects();
2938         smeshGUI->myDesktop->SetSelectionMode(CellSelection, true);
2939         SMESHGUI_RemoveElementsDlg *aDlg = new SMESHGUI_RemoveElementsDlg(parent, "", Sel);
2940       }
2941       else
2942         {
2943           QAD_MessageBox::warn1(QAD_Application::getDesktop(),
2944                                 tr("SMESH_WRN_WARNING"), tr("SMESH_WRN_VIEWER_VTK"),
2945                                 tr("SMESH_BUT_OK"));
2946         }
2947       break;
2948     }
2949
2950   case 5000: // HYPOTHESIS
2951     {
2952       if(checkLock(aStudy)) break;
2953       smeshGUI->EmitSignalDeactivateDialog();
2954       SMESHGUI_CreateHypothesesDlg *aDlg =
2955         new SMESHGUI_CreateHypothesesDlg (parent, "", FALSE, false);
2956       break;
2957     }
2958   case 5010: // ALGO
2959     {
2960       if(checkLock(aStudy)) break;
2961       smeshGUI->EmitSignalDeactivateDialog();
2962       SMESHGUI_CreateHypothesesDlg *aDlg =
2963         new SMESHGUI_CreateHypothesesDlg (parent, "", FALSE, true);
2964       break;
2965     }
2966
2967   case 6016:                                    // CONTROLS 
2968   case 6015:
2969   case 6014:
2970   case 6013:
2971   case 6012:
2972   case 6011:
2973   case 6001:
2974   case 6003:
2975   case 6004:    
2976     if ( smeshGUI->myActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK ) {
2977       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
2978       if ( Sel->IObjectCount() == 1 && Sel->firstIObject()->hasEntry() ) {
2979         SALOMEDS::SObject_var SO = smeshGUI->myStudy->FindObjectID( Sel->firstIObject()->getEntry() );
2980         if ( !SO->_is_nil() ) {
2981           CORBA::Object_var aObject = SO->GetObject();
2982           SMESH::SMESH_Mesh_var    aMesh    = SMESH::SMESH_Mesh::_narrow( aObject );
2983           SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( aObject );
2984           SMESH::SMESH_Group_var   aGroup   = SMESH::SMESH_Group::_narrow( aObject );
2985           if ( !aMesh->_is_nil() || !aSubMesh->_is_nil() || !aGroup->_is_nil() ) {
2986             smeshGUI->Control( theCommandID );
2987             break;
2988           }
2989         }
2990       }
2991       QAD_MessageBox::warn1(smeshGUI->GetDesktop(), 
2992                             tr( "SMESH_WRN_WARNING" ),
2993                             tr( "SMESH_BAD_SELECTION" ), 
2994                             tr( "SMESH_BUT_OK" ) );
2995       break;
2996     }
2997     else {
2998       QAD_MessageBox::warn1(smeshGUI->GetDesktop(), 
2999                             tr( "SMESH_WRN_WARNING" ),
3000                             tr( "NOT_A_VTK_VIEWER" ), 
3001                             tr( "SMESH_BUT_OK" ) );
3002     }
3003     break;
3004   case 9010:
3005     {
3006       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
3007       if (Sel->IObjectCount() == 1)     {
3008         Handle(SALOME_InteractiveObject) anIObject = Sel->firstIObject();
3009         if(anIObject->hasEntry())
3010           if(SMESH_Actor *anActor = ::FindActorByEntry(anIObject->getEntry())){
3011             anActor->SetPointsLabeled( !anActor->GetPointsLabeled() );
3012           }
3013       }
3014       break;
3015     }
3016   case 9011:
3017     {
3018       SALOME_Selection *Sel = SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
3019       if (Sel->IObjectCount() == 1)     {
3020         Handle(SALOME_InteractiveObject) anIObject = Sel->firstIObject();
3021         if(anIObject->hasEntry())
3022           if(SMESH_Actor *anActor = ::FindActorByEntry(anIObject->getEntry())){
3023             anActor->SetCellsLabeled( !anActor->GetCellsLabeled() );
3024           }
3025       }
3026       break;
3027     }
3028   case 10001:                           // DISPLAY MODE PREFERENCE
3029     {
3030       // Wireframe
3031       parent->menuBar()->setItemChecked(10001, true);
3032       parent->menuBar()->setItemChecked(10002, false);
3033       parent->menuBar()->setItemChecked(10004, false);
3034       parent->menuBar()->setItemEnabled(10003, true);
3035       QAD_CONFIG->addSetting("SMESH:DisplayMode", "Wireframe");
3036       break;
3037     }
3038   case 10002:
3039     {
3040       parent->menuBar()->setItemChecked(10002, true);
3041       parent->menuBar()->setItemChecked(10001, false);
3042       parent->menuBar()->setItemChecked(10004, false);
3043       parent->menuBar()->setItemEnabled(10003, true);
3044       QAD_CONFIG->addSetting("SMESH:DisplayMode", "Shading");
3045       break;
3046     }
3047   case 10003:
3048     {
3049       parent->menuBar()->setItemChecked(10003,!parent->menuBar()->isItemChecked(10003));
3050       QAD_CONFIG->addSetting("SMESH:Shrink", parent->menuBar()->isItemChecked(10003) ? "yes" : "no");
3051       break;
3052     }
3053   case 10004:
3054     {
3055       parent->menuBar()->setItemChecked(10001, false);
3056       parent->menuBar()->setItemChecked(10004, true);
3057       parent->menuBar()->setItemChecked(10002, false);
3058       parent->menuBar()->setItemEnabled(10003, false);
3059       QAD_CONFIG->addSetting("SMESH:DisplayMode", "Nodes");
3060       break;
3061     }
3062     
3063   }
3064
3065   smeshGUI->myActiveStudy->updateObjBrowser(true);
3066   return true;
3067 }
3068
3069 //=============================================================================
3070 /*! function : GetMeshesUsingAlgoOrHypothesis()
3071  *  purpose  : return a list of Study objects (mesh kind) that have 'AlgoOrHyp' affected.
3072  *           : However is supposed here that father of father of an hypothesis is a Mesh Object.
3073  */
3074 //=============================================================================      
3075 SALOMEDS::Study::ListOfSObject *
3076         SMESHGUI::GetMeshesUsingAlgoOrHypothesis(SMESH::
3077         SMESH_Hypothesis_ptr AlgoOrHyp)
3078 {
3079   SALOMEDS::Study::ListOfSObject_var listSOmesh =
3080     new SALOMEDS::Study::ListOfSObject;
3081   listSOmesh->length(0);
3082   unsigned int index = 0;
3083   if (!AlgoOrHyp->_is_nil()) {
3084     SALOMEDS::SObject_var SO_Hypothesis =
3085       smeshGUI->GetStudyAPI().FindObject(AlgoOrHyp);
3086     if (!SO_Hypothesis->_is_nil()) {
3087       SALOMEDS::Study::ListOfSObject_var listSO =
3088         smeshGUI->myStudy->FindDependances(SO_Hypothesis);
3089       MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency number ="<<listSO->length());
3090       for (unsigned int i = 0; i < listSO->length(); i++) {
3091         SALOMEDS::SObject_ptr SO = listSO[i];
3092         if (!SO->_is_nil()) { 
3093           SALOMEDS::SObject_var aFather = SO->GetFather();
3094           if (!aFather->_is_nil()) {
3095             SALOMEDS::SObject_var SOfatherFather = aFather->GetFather();
3096             if (!SOfatherFather->_is_nil()) {
3097               MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): dependency added to list");
3098               index++;
3099               listSOmesh->length(index);
3100               listSOmesh[index - 1] = SOfatherFather;
3101             }
3102           }
3103         }
3104       }
3105     }
3106   }
3107   MESSAGE("SMESHGUI::GetMeshesUsingAlgoOrHypothesis(): completed");
3108   return listSOmesh._retn();
3109 }
3110
3111 //=============================================================================
3112 /*!
3113  *
3114  */
3115 //=============================================================================
3116 void SMESHGUI::Import_Mesh(QAD_Desktop * parent, int theCommandID){
3117   QString filter;
3118   string myExtension;
3119   
3120   if(theCommandID == 113){
3121     filter = tr("MED files (*.med)");
3122   }else if (theCommandID == 112){
3123     filter = tr("IDEAS files (*.unv)");
3124   }else if (theCommandID == 111){
3125     filter = tr("DAT files (*.dat)");
3126   }
3127   QString filename = QAD_FileDlg::getFileName(parent,
3128                                               "",
3129                                               filter,
3130                                               tr("Import mesh"),
3131                                               true);
3132   if(!filename.isEmpty()){
3133     QAD_WaitCursor wc;
3134     SMESH::mesh_array_var aMeshes;
3135     try {
3136       SMESH::DriverMED_ReadStatus res;
3137       aMeshes = smeshGUI->myComponentMesh->CreateMeshesFromMED(filename.latin1(),
3138                                                                res);
3139       if ( res > SMESH::DRS_OK ) {
3140         wc.stop();
3141         QAD_MessageBox::warn1(QAD_Application::getDesktop(),
3142                               tr("SMESH_WRN_WARNING"),
3143                               tr(QString("SMESH_DRS_%1").arg(res)),
3144                               tr("SMESH_BUT_OK"));
3145         wc.start();
3146
3147       }
3148     }
3149     catch (const SALOME::SALOME_Exception& S_ex)
3150     {
3151       wc.stop();
3152       QtCatchCorbaException(S_ex);
3153       wc.start();
3154     }
3155     for ( int i = 0, n = aMeshes->length(); i < n; i++ ) {
3156       SALOMEDS::SObject_var aMeshSO = smeshGUI->myStudyAPI.FindObject( aMeshes[i] );
3157       if ( !aMeshSO->_is_nil() ) {
3158         SALOMEDS::StudyBuilder_var aBuilder = smeshGUI->myStudy->NewBuilder();
3159         SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow( aBuilder->FindOrCreateAttribute( aMeshSO, "AttributePixMap" ) );
3160         aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH_IMPORTED" );
3161       }
3162     }
3163   }
3164 }
3165
3166 //=============================================================================
3167 /*!
3168  *
3169  */
3170 //=============================================================================
3171 void SMESHGUI::Export_Mesh(QAD_Desktop * parent, int theCommandID)
3172 {
3173   SALOME_Selection *Sel =
3174     SALOME_Selection::Selection(smeshGUI->myActiveStudy->getSelection());
3175   int nbSel = Sel->IObjectCount();
3176   if ( !nbSel )
3177     return;
3178   Standard_Boolean res;
3179   Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
3180   SMESH::SMESH_Mesh_var aMesh = smeshGUI->ConvertIOinMesh(IObject, res);
3181   if ( res && ( nbSel == 1 || theCommandID == 122 ) ) {// MED export supports multiple meshes
3182     QString aFilter, aTitle = tr("Export mesh");
3183     switch ( theCommandID ) {
3184     case 122:
3185       aFilter = tr("MED files (*.med)");
3186       break;
3187     case 121:
3188       aFilter = tr("DAT files (*.dat)");
3189       break;
3190     case 123:
3191       aFilter = tr("IDEAS files (*.unv)");
3192       break;
3193     default:
3194       break;
3195     }
3196     
3197     QString filename = QAD_FileDlg::getFileName(parent, "", aFilter, aTitle, false);
3198     
3199     if ( !filename.isEmpty() ) {
3200       // Check whether the file already exists and delete it if yes
3201       QFile aFile( filename );
3202       if ( aFile.exists() )
3203         aFile.remove();
3204       
3205       QAD_WaitCursor wc;
3206       for ( SALOME_ListIteratorOfListIO it( Sel->StoredIObjects() ); it.More(); it.Next() ) {
3207         aMesh = smeshGUI->ConvertIOinMesh( it.Value(), res );
3208         if ( res ) {
3209           switch ( theCommandID ) {
3210           case 122:
3211             aMesh->ExportMED( filename.latin1(), true ); // currently, automatic groups are always created
3212             break;
3213           case 121:
3214             aMesh->ExportDAT( filename.latin1() );
3215             break;
3216           case 123:
3217             aMesh->ExportUNV( filename.latin1() );
3218             break;
3219           default:
3220             break;
3221           }
3222         }
3223       }
3224     }
3225   }
3226 }
3227
3228 //=============================================================================
3229 /*!
3230  *
3231  */
3232 //=============================================================================
3233 bool SMESHGUI::OnMousePress(QMouseEvent * pe, QAD_Desktop * parent,
3234                             QAD_StudyFrame * studyFrame)
3235 {
3236   return false;
3237 }
3238
3239 //=============================================================================
3240 /*!
3241  *
3242  */
3243 //=============================================================================
3244 bool SMESHGUI::OnMouseMove(QMouseEvent * pe, QAD_Desktop * parent,
3245                            QAD_StudyFrame * studyFrame)
3246 {
3247   return true;
3248 }
3249
3250 //=============================================================================
3251 /*!
3252  *
3253  */
3254 //=============================================================================
3255 bool SMESHGUI::OnKeyPress(QKeyEvent * pe, QAD_Desktop * parent,
3256                           QAD_StudyFrame * studyFrame)
3257 {
3258   return true;
3259 }
3260
3261 //=============================================================================
3262 /*!
3263  *
3264  */
3265 //=============================================================================
3266 bool SMESHGUI::SetSettings(QAD_Desktop * parent)
3267 {
3268   MESSAGE("SMESHGUI::SetSettings.");
3269   SMESHGUI::GetOrCreateSMESHGUI(parent);
3270
3271   /* Display mode */
3272   QString DisplayMode = "Shading";
3273   if ( QAD_CONFIG->hasSetting("SMESH:DisplayMode") )
3274     DisplayMode = QAD_CONFIG->getSetting("SMESH:DisplayMode");
3275   else
3276     QAD_CONFIG->addSetting("SMESH:DisplayMode", "Shading");
3277
3278   bool Shrink = false;
3279   if ( QAD_CONFIG->hasSetting("SMESH:Shrink") )
3280     Shrink = QAD_CONFIG->getSetting("SMESH:Shrink") == "yes";
3281
3282   if (DisplayMode == "Wireframe") {
3283     // wireframe
3284     parent->menuBar()->setItemChecked(10004, false);
3285     parent->menuBar()->setItemChecked(10002, false);
3286     parent->menuBar()->setItemChecked(10001, true);
3287     parent->menuBar()->setItemEnabled(10003, true);
3288   }
3289   else if (DisplayMode == "Nodes") {
3290     // poins
3291     parent->menuBar()->setItemChecked(10004, true);
3292     parent->menuBar()->setItemChecked(10002, false);
3293     parent->menuBar()->setItemChecked(10001, false);
3294     parent->menuBar()->setItemEnabled(10003, false);
3295   }
3296   else {
3297     // default is shading
3298     parent->menuBar()->setItemChecked(10004, false);
3299     parent->menuBar()->setItemChecked(10002, true);
3300     parent->menuBar()->setItemChecked(10001, false);
3301     parent->menuBar()->setItemEnabled(10003, true);
3302   }
3303   parent->menuBar()->setItemChecked(10003, Shrink);
3304
3305   /* Automatic Update */
3306   QString AutoUpdate = QAD_CONFIG->getSetting("SMESH:AutomaticUpdate");
3307   if (AutoUpdate.compare("true") == 0) {
3308     parent->menuBar()->setItemChecked(1001, true);
3309     smeshGUI->myAutomaticUpdate = true;
3310   }
3311   else {
3312     parent->menuBar()->setItemChecked(1001, false);
3313     smeshGUI->myAutomaticUpdate = false;
3314   }
3315
3316   /* Selection */
3317   ::UpdateSelectionProp();
3318
3319   /* menus disable */
3320   parent->menuBar()->setItemEnabled(111, false);        // IMPORT DAT
3321   parent->menuBar()->setItemEnabled(112, false);        // IMPORT UNV
3322
3323   return true;
3324 }
3325
3326 //=============================================================================
3327 /*!
3328  *
3329  */
3330 //=============================================================================
3331 void SMESHGUI::DefinePopup(QString & theContext, QString & theParent, QString & theObject)
3332 {
3333   // NRI : Temporary added
3334   //  if ( smeshGUI->myStudy->GetProperties()->IsLocked() ) {
3335   //    theObject = "NothingSelected";
3336   //    theContext = "NothingSelected";
3337   //  }
3338   // NRI
3339
3340   SALOME_Selection *Sel = SALOME_Selection::Selection( smeshGUI->myActiveStudy->getSelection() );
3341   int nbSel = Sel->IObjectCount();
3342   switch ( nbSel ) {
3343   case 0:
3344     theObject = "NothingSelected";
3345     theContext = "NothingSelected";
3346     break;
3347   case 1:
3348     if ( smeshGUI->myState == 800 && Sel->HasIndex( Sel->firstIObject() ) )
3349       theObject = "Elements";
3350     else
3351       theObject = smeshGUI->CheckTypeObject( Sel->firstIObject() );
3352     theContext = "";
3353     break;
3354   default:
3355     theObject = smeshGUI->CheckHomogeneousSelection();
3356     theContext = "";
3357   }
3358 }
3359
3360 //=============================================================================
3361 /*!
3362  *
3363  */
3364 //=============================================================================
3365 bool SMESHGUI::CustomPopup(QAD_Desktop* parent, QPopupMenu* popup, const QString& theContext,
3366                            const QString& theParent, const QString& theObject)
3367 {
3368   // get active study frame
3369   QAD_StudyFrame* studyFrame = smeshGUI->myActiveStudy->getActiveStudyFrame();
3370   
3371   // get parent component which selected object(s) belongs to
3372   QString parentComp = ( (SALOMEGUI_Desktop*)parent )->getComponentFromSelection();
3373
3374   // get selection
3375   SALOME_Selection* Sel = SALOME_Selection::Selection( smeshGUI->myActiveStudy->getSelection() );
3376   int nbSel = Sel->IObjectCount();
3377
3378   if ( nbSel == 0 ) {
3379     popup->clear();
3380   }
3381   else if ( nbSel == 1 ) {
3382     if ( parentComp != parent->getActiveComponent() ) {
3383       // object not belongs to SMESH module - remove all commands except common Display/Erase...
3384       while ( 1 ) {
3385         int id = popup->idAt( 0 );
3386         if ( id <= QAD_TopLabel_Popup_ID )
3387           popup->removeItemAt( 0 );
3388         else
3389           break;
3390       }
3391     }
3392     else {
3393       // get selected interactive object 
3394       Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
3395       SALOMEDS::SObject_var SO = smeshGUI->myStudy->FindObjectID( IObject->getEntry() );
3396       // find popup menu's TopLabel item
3397       int topItem = popup->indexOf( QAD_TopLabel_Popup_ID );
3398       if ( topItem >= 0 ) {
3399         if ( theParent == "Viewer" ) {
3400           // set bold font for popup menu's TopLabel item (Viewer popup)
3401           QFont fnt = QApplication::font(); fnt.setBold( TRUE );
3402           popup->removeItem( QAD_TopLabel_Popup_ID );
3403           popup->insertItem( new CustomItem( QString( IObject->getName() ), fnt ), QAD_TopLabel_Popup_ID, topItem );
3404         }
3405         else if ( theParent == "ObjectBrowser" ) {
3406           // remove popup menu's TopLabel item (Object Browser popup)
3407           popup->removeItem( QAD_TopLabel_Popup_ID );
3408         }
3409       }
3410
3411       // remove "Display only" command for component object
3412       if ( theObject.compare( "Component" ) == 0 ) {
3413         popup->removeItem( QAD_DisplayOnly_Popup_ID );
3414       }
3415       else if ( theObject == "Hypothesis" || theObject == "Algorithm" ) {
3416         // remove Display/Erase commands
3417         popup->removeItem( QAD_Display_Popup_ID );
3418         popup->removeItem( QAD_DisplayOnly_Popup_ID );
3419         popup->removeItem( QAD_Erase_Popup_ID );
3420         // remove "Unassign ..." command if hyp/algo is not assigned
3421         if ( SO->_is_nil() || !IObject->hasReference()/* !IsReferencedObject( SO ) */)
3422           popup->removeItem( 1102 );
3423       }
3424       else if ( theObject == "Mesh" || theObject == "SubMesh" || theObject == "Group" ) {
3425         // get actor
3426         GEOM::GEOM_Shape_var aShape = GetSMESHGUI()->GetStudyAPI().GetShapeOnMeshOrSubMesh( SO );
3427         if ( aShape->_is_nil() ) {
3428           // imported mesh
3429           popup->removeItem( 701 ); // Compute
3430           popup->removeItem( 705 ); // Edit hypothesis
3431           popup->removeItem( 706 ); // ...
3432         }
3433         SMESH_Actor* ac = ::FindActorByEntry(IObject->getEntry());
3434         // if object has actor
3435         if ( ac && studyFrame->getTypeView() == VIEW_VTK ) {
3436           VTKViewer_RenderWindowInteractor* myRenderInter =
3437             ( ( VTKViewer_ViewFrame* ) studyFrame->getRightFrame()->getViewFrame() )->getRWInteractor();
3438           if ( myRenderInter->isVisible( IObject ) ) {
3439             popup->removeItem( QAD_Display_Popup_ID );
3440             popup->setItemChecked( 9010, ac->GetPointsLabeled() ); // Numbering / Display Nodes #
3441             popup->setItemChecked( 9011, ac->GetCellsLabeled() );  // Numbering / Display Elements #
3442             TVisualObjPtr aVisualObj = ac->GetObject();
3443             int aNbEdges = aVisualObj->GetNbEntities(SMESH::EDGE);
3444             int aNbFaces = aVisualObj->GetNbEntities(SMESH::FACE);
3445             int aNbVolumes = aVisualObj->GetNbEntities(SMESH::VOLUME);
3446             QMenuItem* mi = popup->findItem( 1131 );
3447             if ( mi && mi->popup() ) {
3448               int  prType = ac->GetRepresentation();
3449               // Display Mode / Wireframe
3450               if(!aNbFaces && !aNbVolumes && !aNbEdges){
3451                 mi->popup()->removeItem( 211 );
3452               }else{
3453                 mi->popup()->setItemChecked( 211, prType == SMESH_Actor::eEdge );
3454               }
3455               // Display Mode / Shading
3456               if(!aNbFaces && !aNbVolumes){
3457                 mi->popup()->removeItem( 212 );
3458               }else{
3459                 mi->popup()->setItemChecked( 212, prType == SMESH_Actor::eSurface );
3460               }
3461               // Display Mode / Points
3462               mi->popup()->setItemChecked( 215, prType == SMESH_Actor::ePoint );  
3463               // Display Mode / Shrink
3464               bool isShrunk = ac->IsShrunk();
3465               bool isShrunkable = ac->IsShrunkable();
3466               mi->popup()->setItemChecked( 213, isShrunk );   
3467               mi->popup()->setItemEnabled( 213, prType != SMESH_Actor::ePoint && isShrunkable);
3468             }
3469             // Scalar Bar
3470             mi = popup->findItem( 2000 );
3471             if ( mi && mi->popup() ) {
3472               SMESH_Actor::eControl cMode = ac->GetControlMode();
3473               switch ( cMode ) {
3474               case SMESH_Actor::eLengthEdges:
3475                 mi->popup()->setItemChecked( 6001, true ); break;
3476               case SMESH_Actor::eFreeBorders:
3477                 mi->popup()->setItemChecked( 6003, true );
3478                 mi->popup()->removeItem( 201 );
3479                 break;
3480               case SMESH_Actor::eMultiConnection:
3481                 mi->popup()->setItemChecked( 6004, true ); break;
3482               case SMESH_Actor::eArea:
3483                 mi->popup()->setItemChecked( 6011, true ); break;
3484               case SMESH_Actor::eTaper:
3485                 mi->popup()->setItemChecked( 6012, true ); break;
3486               case SMESH_Actor::eAspectRatio:
3487                 mi->popup()->setItemChecked( 6013, true ); break;
3488               case SMESH_Actor::eMinimumAngle:
3489                 mi->popup()->setItemChecked( 6014, true ); break;
3490               case SMESH_Actor::eWarping:
3491                 mi->popup()->setItemChecked( 6015, true ); break;
3492               case SMESH_Actor::eSkew:
3493                 mi->popup()->setItemChecked( 6016, true ); break;
3494               case SMESH_Actor::eNone:
3495               default:
3496                 mi->popup()->removeItem( 200 );
3497                 mi->popup()->removeItem( 201 );
3498                 break;
3499               }
3500               TVisualObjPtr aVisualObj = ac->GetObject();
3501               SMESH::ElementType aType;
3502               if(!aNbEdges){
3503                 mi->popup()->removeItem( 6001 );
3504                 mi->popup()->removeItem( 6003 );
3505                 mi->popup()->removeItem( 6004 );
3506               }
3507               if(!aNbFaces){
3508                 mi->popup()->removeItem( 6011 );
3509                 mi->popup()->removeItem( 6012 );
3510                 mi->popup()->removeItem( 6013 );
3511                 mi->popup()->removeItem( 6014 );
3512                 mi->popup()->removeItem( 6015 );
3513                 mi->popup()->removeItem( 6016 );
3514               }
3515               if(!aNbFaces && !aNbEdges)
3516                 popup->removeItem( 2000 );                         // Scalar Bar
3517             }
3518           }
3519           else {
3520             popup->removeItem( QAD_Erase_Popup_ID );
3521             popup->removeItem( 114 );                              // Numbering popup menu
3522             popup->removeItem( 1131 );                             // Display mode
3523             popup->removeItem( 1132 );                             // Color / size
3524             popup->removeItem( 1133 );                             // Transparency
3525             popup->removeItem( 2000 );                             // Scalar Bar
3526           }
3527         }
3528         else {
3529           // object doesn't have actor
3530           CORBA::Object_var anObject = SO->GetObject();
3531           bool bDisplay = false;
3532           if ( !CORBA::is_nil( anObject ) ) {
3533             SMESH::SMESH_Mesh_var aMeshObj = SMESH::SMESH_Mesh::_narrow( anObject );
3534             if ( !aMeshObj->_is_nil() && ( aMeshObj->NbNodes() > 0 || aMeshObj->NbFaces() > 0  || aMeshObj->NbVolumes() > 0 ) )
3535               bDisplay = true;
3536             SMESH::SMESH_subMesh_var aSubMeshObj = SMESH::SMESH_subMesh::_narrow( anObject );
3537             if ( !aSubMeshObj->_is_nil() && ( aSubMeshObj->GetNumberOfNodes() > 0 || aSubMeshObj->GetNumberOfElements() > 0 ) )
3538               bDisplay = true;
3539             SMESH::SMESH_Group_var aGroupObj = SMESH::SMESH_Group::_narrow( anObject );
3540             if ( !aGroupObj->_is_nil() && aGroupObj->Size() > 0 )
3541               bDisplay = true;
3542           }
3543           if ( !bDisplay ) {
3544             popup->removeItem( QAD_Display_Popup_ID );
3545             popup->removeItem( QAD_DisplayOnly_Popup_ID );
3546           }
3547           popup->removeItem( QAD_Erase_Popup_ID );
3548           popup->removeItem( 114 );                              // Numbering popup menu
3549           popup->removeItem( 1131 );                             // Display mode
3550           popup->removeItem( 1132 );                             // Color / size
3551           popup->removeItem( 1133 );                             // Transparency
3552           popup->removeItem( 2000 );                             // Scalar Bar
3553         }
3554       }
3555       else {
3556         // another SMESH object
3557         popup->removeItem( QAD_Display_Popup_ID );
3558         popup->removeItem( QAD_DisplayOnly_Popup_ID );
3559         popup->removeItem( QAD_Erase_Popup_ID );
3560       }
3561     }
3562   }
3563   else {
3564     // multiple selection
3565     if ( parentComp != parent->getActiveComponent() ) {
3566       // object not belongs to SMESH module - remove all commands except common Display/Erase...
3567       while ( 1 )       {
3568         int id = popup->idAt( 0 );
3569         if ( id <= QAD_TopLabel_Popup_ID )
3570           popup->removeItemAt( 0 );
3571         else
3572           break;
3573       }
3574       if ( parentComp.isNull() ) {
3575         // objects from different components are selected
3576         popup->removeItem( QAD_DisplayOnly_Popup_ID );
3577         popup->removeItem( QAD_Display_Popup_ID );
3578         popup->removeItem( QAD_Erase_Popup_ID );
3579       }
3580     }
3581     else {
3582       QString type = smeshGUI->CheckHomogeneousSelection();
3583       if ( type != "Heterogeneous Selection" ) {
3584         int topItem = popup->indexOf( QAD_TopLabel_Popup_ID );
3585         if ( topItem >= 0 ) {
3586           // set bold font for popup menu's TopLabel item
3587           QFont fnt = QApplication::font(); fnt.setBold( TRUE );
3588           popup->removeItem( QAD_TopLabel_Popup_ID );
3589           popup->insertItem( new CustomItem( QString("%1 ").arg( nbSel ) + type + "(s) ", fnt ), QAD_TopLabel_Popup_ID, topItem );
3590         }
3591       }
3592     }
3593   }
3594   return false;
3595 }
3596
3597 //=============================================================================
3598 /*! Method:  BuildPresentation(const Handle(SALOME_InteractiveObject)& theIO)
3599  *  Purpose: ensures that the actor for the given <theIO> exists in the active VTK view
3600  */
3601 //=============================================================================
3602 void SMESHGUI::BuildPresentation(const Handle(SALOME_InteractiveObject) & theIO)
3603 {
3604   if(theIO->hasEntry()){
3605     QAD_Study* aStudy = SMESHGUI::GetSMESHGUI()->GetActiveStudy();
3606     QAD_StudyFrame *aStudyFrame = aStudy->getActiveStudyFrame();
3607     ::UpdateView(aStudyFrame,eDisplay,theIO->getEntry());
3608   }
3609 }
3610
3611 //=============================================================================
3612 /*!
3613  *
3614  */
3615 //=============================================================================
3616 void SMESHGUI::setOrb()
3617 {
3618         try
3619         {
3620                 ORB_INIT & init = *SINGLETON_ < ORB_INIT >::Instance();
3621                 ASSERT(SINGLETON_ < ORB_INIT >::IsAlreadyExisting());
3622                 _orb = init(0, 0);
3623         } catch(...)
3624         {
3625                 INFOS("internal error : orb not found");
3626                 _orb = 0;
3627         }
3628         ASSERT(!CORBA::is_nil(_orb));
3629 }
3630
3631 //=============================================================================
3632 /*!
3633  *
3634  */
3635 //=============================================================================
3636 SMESH_Actor *SMESHGUI::ReadScript(SMESH::SMESH_Mesh_ptr theMesh){}
3637
3638 //=============================================================================
3639 /*!
3640  *
3641  */
3642 //=============================================================================
3643 void SMESHGUI::Dump(SMESH_Actor * Mactor)
3644 {
3645         vtkUnstructuredGrid *ugrid = Mactor->GetUnstructuredGrid();
3646         vtkPoints *Pts = ugrid->GetPoints();
3647         int nbPts = Pts->GetNumberOfPoints();
3648         int nbCells = ugrid->GetNumberOfCells();
3649
3650         FILE *In;
3651         int i, j;
3652         In = fopen("/tmp/dumpMesh", "w+");
3653         fprintf(In, "%d %d\n", nbPts, nbCells);
3654         for (int i = 0; i < nbPts; i++)
3655         {
3656                 float *p = ugrid->GetPoint(i);
3657                 fprintf(In, "%d %e %e %e\n", i, p[0], p[1], p[2]);
3658         }
3659
3660         for (int i = 0; i < nbCells; i++)
3661         {
3662                 fprintf(In, "%d %d", i, ugrid->GetCell(i)->GetCellType());
3663                 vtkIdList *Id = ugrid->GetCell(i)->GetPointIds();
3664                 for (j = 0; j < Id->GetNumberOfIds(); j++)
3665                 {
3666                         fprintf(In, " %d", Id->GetId(j));
3667                 }
3668                 fprintf(In, "\n");
3669         }
3670         fclose(In);
3671 }
3672
3673 //=============================================================================
3674 /*!
3675  *
3676  */
3677 //=============================================================================
3678 void SMESHGUI::RemoveNodes(SMESH::SMESH_Mesh_ptr theMesh,
3679                            const TColStd_MapOfInteger & MapIndex)
3680 {
3681   QAD_WaitCursor wc;
3682   try{
3683     SALOMEDS::SObject_var aSobj = myStudyAPI.FindObject(theMesh);
3684     CORBA::String_var anEntry = aSobj->GetID();
3685     if(SMESH_Actor* anActor = ::FindActorByEntry(anEntry.in())){
3686       SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
3687       anArrayOfIdeces->length(MapIndex.Extent());
3688       TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
3689       for(int i = 0; ite.More(); ite.Next(), i++){
3690         anArrayOfIdeces[i] = anActor->GetNodeObjId(ite.Key());
3691       }
3692       SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
3693       aMeshEditor->RemoveNodes(anArrayOfIdeces);
3694       if(myAutomaticUpdate){
3695         CORBA::Long anId = smeshGUI->myStudy->StudyId();
3696         if(TVisualObjPtr aVisualObj = GetVisualObj(anId,anEntry.in())){
3697           aVisualObj->Update(true);
3698         }
3699       }
3700     }
3701   }catch(SALOME::SALOME_Exception& exc) {
3702     INFOS("Follow exception was cought:\n\t"<<exc.details.text);
3703   }catch(const std::exception& exc){
3704     INFOS("Follow exception was cought:\n\t"<<exc.what());
3705   }catch(...){
3706     INFOS("Unknown exception was cought !!!");
3707   }
3708 }
3709
3710 //=============================================================================
3711 /*!
3712  *
3713  */
3714 //=============================================================================
3715 void SMESHGUI::RemoveElements(SMESH::SMESH_Mesh_ptr theMesh,
3716                               const TColStd_MapOfInteger & MapIndex)
3717 {
3718   QAD_WaitCursor wc;
3719   try{
3720     SALOMEDS::SObject_var aSobj = myStudyAPI.FindObject(theMesh);
3721     CORBA::String_var anEntry = aSobj->GetID();
3722     if(SMESH_Actor* anActor = ::FindActorByEntry(anEntry.in())){
3723       SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
3724       anArrayOfIdeces->length(MapIndex.Extent());
3725       TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
3726       for(int i = 0; ite.More(); ite.Next(), i++){
3727         anArrayOfIdeces[i] = anActor->GetElemObjId(ite.Key());
3728       }
3729       SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
3730       aMeshEditor->RemoveElements(anArrayOfIdeces);
3731       if(myAutomaticUpdate){
3732         CORBA::Long anId = smeshGUI->myStudy->StudyId();
3733         if(TVisualObjPtr aVisualObj = GetVisualObj(anId,anEntry.in())){
3734           aVisualObj->Update(true);
3735         }
3736       }
3737     }
3738   }catch(SALOME::SALOME_Exception& exc) {
3739     INFOS("Follow exception was cought:\n\t"<<exc.details.text);
3740   }catch(const std::exception& exc){
3741     INFOS("Follow exception was cought:\n\t"<<exc.what());
3742   }catch(...){
3743     INFOS("Unknown exception was cought !!!");
3744   }
3745 }
3746
3747 //=============================================================================
3748 /*!
3749  *
3750  */
3751 //=============================================================================
3752 void SMESHGUI::OrientationElements(SMESH::SMESH_Mesh_ptr aMesh,
3753         const TColStd_MapOfInteger & MapIndex)
3754 {
3755   MESSAGE("OrientationElements - not implemeted yet!");
3756 }
3757
3758 //=============================================================================
3759 /*!
3760  *
3761  */
3762 //=============================================================================
3763 void SMESHGUI::DiagonalInversion(SMESH::SMESH_Mesh_ptr aMesh,
3764         const TColStd_MapOfInteger & MapIndex)
3765 {
3766   MESSAGE("OrientationElements - not implemeted yet!");
3767 }
3768
3769 //=====================================================================================
3770 // EXPORTED METHODS
3771 //=====================================================================================
3772 void SMESHGUI::SupportedViewType(int *buffer, int bufferSize)
3773 {
3774   if (!buffer || !bufferSize)
3775     return;
3776   buffer[0] = (int)VIEW_VTK;
3777 }
3778
3779 void SMESHGUI::Deactivate()
3780 {
3781   if ( SMESHGUI::GetSMESHGUI() ) {
3782     SMESHGUI::GetSMESHGUI()->EmitSignalCloseAllDialogs();
3783   }
3784 }
3785
3786
3787 //=============================================================================
3788 /*!
3789  *
3790  */
3791 //=============================================================================
3792 void SMESHGUI::SetPickable(SMESH_Actor* theActor){
3793   if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
3794     int anIsAllPickable = (theActor == NULL);
3795     vtkRenderer *aRenderer = aViewFrame->getRenderer();
3796     vtkActorCollection *aCollection = aRenderer->GetActors();
3797     aCollection->InitTraversal();
3798     while(vtkActor *anAct = aCollection->GetNextActor()){
3799       if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
3800         if(anActor->GetVisibility()){
3801           anActor->SetPickable(anIsAllPickable);
3802         }
3803       }
3804     }
3805     if(theActor)
3806       theActor->SetPickable(!anIsAllPickable);
3807     RepaintCurrentView();
3808   }
3809 }
3810
3811
3812 void SMESHGUI::ViewNodes(){
3813   EraseSimulationActors();
3814   ::SetPointRepresentation(true);
3815 }
3816
3817 //=============================================================================
3818 /*!
3819  *
3820  */
3821 //=============================================================================
3822 void SMESHGUI::Control( int theCommandID ){
3823   SALOME_Selection *Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
3824   if(Sel){
3825     Handle(SALOME_InteractiveObject) anIO = Sel->firstIObject();
3826     if(!anIO.IsNull()){
3827       QString aTitle;
3828       SMESH_Actor::eControl aControl = SMESH_Actor::eNone;
3829       if(SMESH_Actor *anActor = ::FindActorByEntry(anIO->getEntry())){
3830         switch ( theCommandID ){
3831         case 6001:
3832           aTitle = tr( "LENGTH_EDGES" );
3833           aControl = SMESH_Actor::eLengthEdges;
3834           break;
3835         case 6003:
3836           aTitle = tr( "FREE_BORDERS" );
3837           aControl = SMESH_Actor::eFreeBorders;
3838           break;
3839         case 6004:
3840           aTitle = tr( "MULTI_BORDERS" );
3841           aControl = SMESH_Actor::eMultiConnection;
3842           break;
3843         case 6011:
3844           aTitle = tr( "AREA_ELEMENTS" );
3845           aControl = SMESH_Actor::eArea;
3846           break;
3847         case 6012:
3848           aTitle = tr( "TAPER_ELEMENTS" );
3849           aControl = SMESH_Actor::eTaper;
3850           break;
3851         case 6013:
3852           aTitle = tr( "ASPECTRATIO_ELEMENTS" );
3853           aControl = SMESH_Actor::eAspectRatio;
3854           break;
3855         case 6014:
3856           aTitle = tr( "MINIMUMANGLE_ELEMENTS" );
3857           aControl = SMESH_Actor::eMinimumAngle;
3858           break;
3859         case 6015:
3860           aTitle = tr( "WARP_ELEMENTS" );
3861           aControl = SMESH_Actor::eWarping;
3862           break;
3863         case 6016:
3864           aTitle = tr( "SKEW_ELEMENTS" );
3865           aControl = SMESH_Actor::eSkew;
3866           break;
3867         }
3868         anActor->SetControlMode(aControl);
3869         anActor->GetScalarBarActor()->SetTitle(aTitle.latin1());
3870       }
3871     }
3872   }
3873 }
3874
3875 //=============================================================================
3876 /*!
3877  *
3878  */
3879 //=============================================================================
3880 void SMESHGUI::SetSettingsScalarBar(vtkScalarBarActor * theScalarBar,
3881                                     QString Bold, 
3882                                     QString Italic,
3883                                     QString Shadow, 
3884                                     QString FontFamily,
3885                                     QString Orientation, 
3886                                     float Width, 
3887                                     float Height, 
3888                                     int NbColors, 
3889                                     int NbLabels)
3890 {
3891   if (Bold.isNull() || Bold.isEmpty() || (Bold.compare("true") == 0))
3892     theScalarBar->BoldOn();
3893   else
3894     theScalarBar->BoldOff();
3895   
3896   if (Italic.isNull() || Italic.isEmpty() || (Italic.compare("true") == 0))
3897     theScalarBar->ItalicOn();
3898   else
3899     theScalarBar->ItalicOff();
3900   
3901   if (Shadow.isNull() || Shadow.isEmpty() || (Shadow.compare("true") == 0))
3902     theScalarBar->ShadowOn();
3903   else
3904     theScalarBar->ShadowOff();
3905   
3906   if (FontFamily.compare("Arial") == 0)
3907     theScalarBar->SetFontFamilyToArial();
3908   else if (FontFamily.compare("Courier") == 0)
3909     theScalarBar->SetFontFamilyToCourier();
3910   else if (FontFamily.compare("Times") == 0)
3911     theScalarBar->SetFontFamilyToTimes();
3912   else
3913     theScalarBar->SetFontFamilyToArial();
3914   
3915   if (Orientation.isNull() || Orientation.isEmpty() ||
3916       (Orientation.compare("Vertical") == 0))
3917     theScalarBar->SetOrientationToVertical();
3918   else
3919     theScalarBar->SetOrientationToHorizontal();
3920   
3921   theScalarBar->SetWidth((Width == 0) ? 0.17 : Width);
3922   theScalarBar->SetHeight((Height == 0) ? 0.8 : Height);
3923   
3924   theScalarBar->SetNumberOfLabels((NbLabels == 0) ? 5 : NbLabels);
3925   theScalarBar->SetMaximumNumberOfColors((NbColors == 0) ? 64 : NbColors);
3926 }
3927
3928 //=============================================================================
3929 /*!
3930  *
3931  */
3932 //=============================================================================
3933 void SMESHGUI::SetDisplaySettings()
3934 {
3935   EmitSignalDeactivateDialog();
3936   SMESHGUI_Preferences_ColorDlg *aDlg =
3937     new SMESHGUI_Preferences_ColorDlg(QAD_Application::getDesktop(), "");
3938
3939   QColor color;
3940   QString SCr, SCg, SCb;
3941   SCr = QAD_CONFIG->getSetting("SMESH:SettingsFillColorRed");
3942   SCg = QAD_CONFIG->getSetting("SMESH:SettingsFillColorGreen");
3943   SCb = QAD_CONFIG->getSetting("SMESH:SettingsFillColorBlue");
3944   if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
3945     color = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
3946   else color = QColor(0, 170, 255);
3947   aDlg->SetColor(1, color);
3948
3949   SCr = QAD_CONFIG->getSetting("SMESH:SettingsOutlineColorRed");
3950   SCg = QAD_CONFIG->getSetting("SMESH:SettingsOutlineColorGreen");
3951   SCb = QAD_CONFIG->getSetting("SMESH:SettingsOutlineColorBlue");
3952   if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
3953     color = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
3954   else color = QColor(0, 170, 255);
3955   aDlg->SetColor(2, color);
3956
3957   SCr = QAD_CONFIG->getSetting("SMESH:SettingsNodeColorRed");
3958   SCg = QAD_CONFIG->getSetting("SMESH:SettingsNodeColorGreen");
3959   SCb = QAD_CONFIG->getSetting("SMESH:SettingsNodeColorBlue");
3960   if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
3961     color = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
3962   else color = Qt::red;
3963   aDlg->SetColor(3, color);
3964
3965   QString SBr = QAD_CONFIG->getSetting("SMESH:SettingsBackFaceColorRed");
3966   QString SBg = QAD_CONFIG->getSetting("SMESH:SettingsBackFaceColorGreen");
3967   QString SBb = QAD_CONFIG->getSetting("SMESH:SettingsBackFaceColorBlue");
3968   if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
3969     color = QColor(SBr.toInt(), SBg.toInt(), SBb.toInt());
3970   else color = Qt::blue;
3971   aDlg->SetColor(4, color);
3972
3973   QString intValue = QAD_CONFIG->getSetting("SMESH:SettingsWidth");
3974   if (intValue.isEmpty()) intValue = "1";
3975   aDlg->SetIntValue(1, intValue.toInt());
3976   intValue = QAD_CONFIG->getSetting("SMESH:SettingsNodesSize");
3977   if (intValue.isEmpty()) intValue = "3";
3978   aDlg->SetIntValue(2, intValue.toInt());
3979   intValue = QAD_CONFIG->getSetting("SMESH:SettingsShrinkCoeff");
3980   if (intValue.isEmpty()) intValue = "75";
3981   aDlg->SetIntValue(3, intValue.toInt());
3982
3983   if (aDlg->exec()) {
3984     QColor colorFill = aDlg->GetColor(1);
3985     QAD_CONFIG->addSetting("SMESH:SettingsFillColorRed", colorFill.red());
3986     QAD_CONFIG->addSetting("SMESH:SettingsFillColorGreen", colorFill.green());
3987     QAD_CONFIG->addSetting("SMESH:SettingsFillColorBlue", colorFill.blue());
3988
3989     QColor colorOutline = aDlg->GetColor(2);
3990     QAD_CONFIG->addSetting("SMESH:SettingsOutlineColorRed", colorOutline.red());
3991     QAD_CONFIG->addSetting("SMESH:SettingsOutlineColorGreen", colorOutline.green());
3992     QAD_CONFIG->addSetting("SMESH:SettingsOutlineColorBlue", colorOutline.blue());
3993
3994     QColor colorNode = aDlg->GetColor(3);
3995     QAD_CONFIG->addSetting("SMESH:SettingsNodeColorRed", colorNode.red());
3996     QAD_CONFIG->addSetting("SMESH:SettingsNodeColorGreen", colorNode.green());
3997     QAD_CONFIG->addSetting("SMESH:SettingsNodeColorBlue", colorNode.blue());
3998
3999     QColor colorBackFace = aDlg->GetColor(4);
4000     QAD_CONFIG->addSetting("SMESH:SettingsBackFaceColorRed", colorBackFace.red());
4001     QAD_CONFIG->addSetting("SMESH:SettingsBackFaceColorGreen", colorBackFace.green());
4002     QAD_CONFIG->addSetting("SMESH:SettingsBackFaceColorBlue", colorBackFace.blue());
4003
4004     int width = aDlg->GetIntValue(1);
4005     QAD_CONFIG->addSetting("SMESH:SettingsWidth", width);
4006
4007     int nodes_size = aDlg->GetIntValue(2);
4008     QAD_CONFIG->addSetting("SMESH:SettingsNodesSize", nodes_size);
4009
4010     int shrink_coeff = aDlg->GetIntValue(3);
4011     QAD_CONFIG->addSetting("SMESH:SettingsShrinkCoeff", shrink_coeff);
4012   }
4013
4014   delete aDlg;
4015 }
4016
4017 //=======================================================================
4018 // function : Parameter()
4019 // purpose  : return a parameter (double) from a dialog box
4020 //
4021 //  aValue     : is a double used as a default value displayed
4022 //  aLabel     : is the title for aValue1
4023 //  aTitle     : is the main title
4024 //  bottom     : maximum value to be entered
4025 //  top        : minimum value to be entered
4026 //  decimals   : number of decimals
4027 //=======================================================================
4028 double SMESHGUI::Parameter(Standard_Boolean & res,
4029         const double aValue,
4030         const char *aLabel,
4031         const char *aTitle,
4032         const double bottom, const double top, const int decimals)
4033 {
4034         SMESHGUI_aParameterDlg *Dialog =
4035                 new SMESHGUI_aParameterDlg(QAD_Application::getDesktop(),
4036                 aTitle,
4037                 aLabel,
4038                 bottom, top, decimals,
4039                 TRUE);
4040         Dialog->setValue(aValue);
4041         double X = 0.0;
4042         res = (Dialog->exec() == QDialog::Accepted);
4043         if (res)
4044                 X = Dialog->getDblValue();
4045         return X;
4046 }
4047
4048 //=======================================================================
4049 // function : Parameter()
4050 // purpose  : return a parameter (int) from a dialog box
4051 //
4052 //  aValue     : is a int used as a default value displayed
4053 //  aLabel     : is the title for aValue1
4054 //  aTitle     : is the main title
4055 //  bottom     : maximum value to be entered
4056 //  top        : minimum value to be entered
4057 //=======================================================================
4058 int SMESHGUI::Parameter(Standard_Boolean & res,
4059         const int aValue,
4060         const char *aLabel, const char *aTitle, const int bottom, const int top)
4061 {
4062         SMESHGUI_aParameterDlg *Dialog =
4063                 new SMESHGUI_aParameterDlg(QAD_Application::getDesktop(),
4064                 aTitle,
4065                 aLabel,
4066                 bottom, top,
4067                 TRUE);
4068         Dialog->setValue(aValue);
4069         int X = 0;
4070         res = (Dialog->exec() == QDialog::Accepted);
4071         if (res)
4072                 X = Dialog->getIntValue();
4073         return X;
4074 }
4075
4076 //=============================================================================
4077 /*!
4078  *
4079  */
4080 //=============================================================================
4081 void SMESHGUI::DisplayEdges(SMESH_Actor * ac, bool visibility){}
4082
4083 //=============================================================================
4084 /*!
4085  *
4086  */
4087 //=============================================================================
4088 void SMESHGUI::InitActor(SMESH::SMESH_Mesh_ptr aMesh){}
4089
4090 //=============================================================================
4091 /*!
4092  *
4093  */
4094 //=============================================================================
4095 void SMESHGUI::Update(){
4096   if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
4097     SALOME_Selection *Sel = SALOME_Selection::Selection(myActiveStudy->getSelection());
4098     if(Sel->IObjectCount() == 0){
4099       vtkRenderer* aRenderer = aViewFrame->getRenderer();
4100       vtkActorCollection *aCollection = aRenderer->GetActors();
4101       aCollection->InitTraversal();
4102       while(vtkActor *anAct = aCollection->GetNextActor()){
4103         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
4104           if(anActor->hasIO()){
4105             Update(anActor->getIO());
4106           }
4107         }
4108       }
4109     }else{
4110       SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
4111       for(; It.More(); It.Next()){
4112         Handle(SALOME_InteractiveObject) IO = It.Value();
4113         Update(IO);
4114       }
4115     }
4116     ::RepaintCurrentView();
4117   }
4118 }
4119
4120 //=============================================================================
4121 /*!
4122  *
4123  */
4124 //=============================================================================
4125 void SMESHGUI::Update(const Handle(SALOME_InteractiveObject) & theIO){
4126   CORBA::Long anId = smeshGUI->myStudy->StudyId();
4127   TVisualObjPtr aVisualObj = ::GetVisualObj(anId,theIO->getEntry());
4128   aVisualObj->Update();
4129   ::UpdateView(eDisplay,theIO->getEntry());
4130 }
4131
4132 //=============================================================================
4133 /*!
4134  *
4135  */
4136 //=============================================================================
4137 vtkActor *SMESHGUI::SimulationMoveNode(SMESH_Actor * Mactor, int idnode)
4138 {
4139         if (myActiveStudy->getActiveStudyFrame()->getTypeView() != VIEW_VTK)
4140                 return NULL;
4141
4142         mySimulationActors = vtkActorCollection::New();
4143         vtkRenderer *theRenderer =
4144                 ((VTKViewer_ViewFrame *) myActiveStudy->getActiveStudyFrame()->
4145                 getRightFrame()->getViewFrame())->getRenderer();
4146
4147         vtkUnstructuredGrid *ugrid =
4148                 vtkUnstructuredGrid::SafeDownCast(Mactor->GetUnstructuredGrid());
4149         vtkIdList *IdCells = vtkIdList::New();
4150         ugrid->GetPointCells(idnode, IdCells);
4151
4152         vtkPoints *Pts = vtkPoints::New();
4153         Pts = ugrid->GetPoints();
4154
4155         vtkUnstructuredGrid *ugridSimulation = vtkUnstructuredGrid::New();
4156         ugridSimulation->SetPoints(Pts);
4157         vtkIdList *IdPts = vtkIdList::New();
4158         for (int j = 0; j < IdCells->GetNumberOfIds(); j++)
4159         {
4160                 ugrid->GetCellPoints(IdCells->GetId(j), IdPts);
4161                 ugridSimulation->InsertNextCell(ugrid->GetCellType(IdCells->GetId(j)),
4162                         IdPts);
4163         }
4164
4165         vtkProperty *prop = vtkProperty::New();
4166         prop->SetColor(1., 0., 0.);
4167         prop->SetRepresentationToWireframe();
4168
4169         int Edgewidth = (int)Mactor->GetLineWidth();
4170         if (Edgewidth == 0)
4171                 Edgewidth = 1;
4172         prop->SetLineWidth(Edgewidth + 1);
4173
4174         vtkDataSetMapper *Mapper = vtkDataSetMapper::New();
4175         Mapper->SetInput(ugridSimulation);
4176         vtkActor *ac = vtkActor::New();
4177         ac->SetMapper(Mapper);
4178         ac->SetProperty(prop);
4179
4180         ac->GetMapper()->SetResolveCoincidentTopologyToShiftZBuffer();
4181         ac->GetMapper()->SetResolveCoincidentTopologyZShift(0.02);
4182
4183         mySimulationActors->AddItem(ac);
4184         theRenderer->AddActor(ac);
4185
4186         vtkRenderWindow *renWin = theRenderer->GetRenderWindow();
4187         renWin->Render();
4188
4189         return ac;
4190 }
4191
4192 //=============================================================================
4193 /*!
4194  *
4195  */
4196 //=============================================================================
4197 void SMESHGUI::DisplaySimulationNode(SMESH::SMESH_Mesh_ptr aMesh, float x,
4198         float y, float z)
4199 {
4200         if (myActiveStudy->getActiveStudyFrame()->getTypeView() != VIEW_VTK)
4201                 return;
4202
4203         EraseSimulationActors();
4204         mySimulationActors = vtkActorCollection::New();
4205         vtkRenderer *theRenderer =
4206                 ((VTKViewer_ViewFrame *) myActiveStudy->getActiveStudyFrame()->
4207                 getRightFrame()->getViewFrame())->getRenderer();
4208         Standard_Boolean result;
4209         SMESH_Actor *ac = FindActor(aMesh, result, true);
4210
4211         if (result)
4212         {
4213                 vtkUnstructuredGrid *ugrid =
4214                         vtkUnstructuredGrid::SafeDownCast(ac->GetUnstructuredGrid());
4215                 vtkUnstructuredGrid *newugrid = vtkUnstructuredGrid::New();
4216
4217                 vtkPoints *Pts = ugrid->GetPoints();
4218                 vtkPoints *newPts = vtkPoints::New();
4219                 int nbPts = Pts->GetNumberOfPoints();
4220                 for (int i = 0; i < nbPts; i++)
4221                 {
4222                         newPts->InsertPoint(i, Pts->GetPoint(i));
4223                 }
4224
4225                 newugrid->SetPoints(newPts);
4226                 newugrid->GetPoints()->InsertNextPoint(x, y, z);
4227
4228                 vtkMaskPoints *verts = vtkMaskPoints::New();
4229                 verts->SetInput(newugrid);
4230                 verts->SetGenerateVertices(1);
4231                 verts->SetOnRatio(1);
4232
4233                 vtkPolyDataMapper *vertMapper = vtkPolyDataMapper::New();
4234                 vertMapper->SetInput(verts->GetOutput());
4235                 vertMapper->ScalarVisibilityOff();
4236
4237                 vtkActor *node = vtkActor::New();
4238                 node->SetMapper(vertMapper);
4239
4240                 QString SCr = QAD_CONFIG->getSetting("SMESH:SettingsNodeColorRed");
4241                 QString SCg = QAD_CONFIG->getSetting("SMESH:SettingsNodeColorGreen");
4242                 QString SCb = QAD_CONFIG->getSetting("SMESH:SettingsNodeColorBlue");
4243                 QColor nodecolor(SCr.toInt(), SCg.toInt(), SCb.toInt());
4244                 if (!nodecolor.isValid())
4245 //       nodecolor = QColor(0.,1.,0.);
4246                         nodecolor = QColor(0, 1, 0);
4247
4248                 node->GetProperty()->SetColor(float (nodecolor.red()) / 255.,
4249                         float (nodecolor.green()) / 255., float (nodecolor.blue()) / 255.);
4250
4251                 int intValue =
4252                         QAD_CONFIG->getSetting("SMESH:SettingsNodesSize").toInt();
4253                 if (intValue < 1)
4254                         intValue == 1;
4255
4256                 node->GetProperty()->SetPointSize(intValue);
4257
4258                 node->VisibilityOn();
4259
4260                 mySimulationActors->AddItem(node);
4261                 theRenderer->AddActor(node);
4262         }
4263
4264         vtkRenderWindow *renWin = theRenderer->GetRenderWindow();
4265         renWin->Render();
4266 }
4267
4268 //=============================================================================
4269 /*!
4270  *
4271  */
4272 //=============================================================================
4273 void SMESHGUI::DisplaySimulationMoveNode(vtkActor * ac, int idnode, float x,
4274         float y, float z)
4275 {
4276         if (myActiveStudy->getActiveStudyFrame()->getTypeView() != VIEW_VTK)
4277                 return;
4278
4279         vtkRenderer *theRenderer =
4280                 ((VTKViewer_ViewFrame *) myActiveStudy->getActiveStudyFrame()->
4281                 getRightFrame()->getViewFrame())->getRenderer();
4282         float *pt = ac->GetMapper()->GetInput()->GetPoint(idnode);
4283         pt[0] = x;
4284         pt[1] = y;
4285         pt[2] = z;
4286
4287         ac->GetMapper()->ImmediateModeRenderingOn();
4288         ac->GetProperty()->SetRepresentationToWireframe();
4289
4290         vtkRenderWindow *renWin = theRenderer->GetRenderWindow();
4291
4292         renWin->Render();
4293 }
4294
4295 //=============================================================================
4296 /*!
4297  *
4298  */
4299 //=============================================================================
4300 void SMESHGUI::MoveNode(SMESH::SMESH_Mesh_ptr aMesh, int idnode, float x,
4301         float y, float z)
4302 {
4303         Standard_Boolean result;
4304
4305         SMESH_Actor *MeshActor = FindActor(aMesh, result, true);
4306         if (result)
4307         {
4308         }
4309 }
4310
4311 //=============================================================================
4312 /*!
4313  *
4314  */
4315 //=============================================================================
4316 void SMESHGUI::ScalarVisibilityOff(){
4317   if(vtkRenderer *aRenderer = ::GetCurrentRenderer()){
4318     vtkActorCollection *actorList = aRenderer->GetActors();
4319     actorList->InitTraversal();
4320     while(vtkActor *ac = actorList->GetNextActor()){
4321       if(SMESH_Actor *anActor = SMESH_Actor::SafeDownCast(ac)){
4322         anActor->GetMapper()->ScalarVisibilityOff();
4323       }
4324     }
4325     ::RepaintCurrentView();
4326   }
4327 }
4328
4329 //=============================================================================
4330 /*!
4331  *
4332  */
4333 //=============================================================================
4334 void SMESHGUI::DisplaySimulationEdge(SMESH::SMESH_Mesh_ptr aMesh,
4335         const TColStd_MapOfInteger & MapIndex)
4336 {
4337         if (myActiveStudy->getActiveStudyFrame()->getTypeView() != VIEW_VTK)
4338                 return;
4339
4340         EraseSimulationActors();
4341         mySimulationActors = vtkActorCollection::New();
4342         vtkRenderer *theRenderer =
4343                 ((VTKViewer_ViewFrame *) myActiveStudy->getActiveStudyFrame()->
4344                 getRightFrame()->getViewFrame())->getRenderer();
4345         Standard_Boolean result;
4346         SMESH_Actor *ac = FindActor(aMesh, result, true);
4347         if (result)
4348         {
4349                 TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
4350                 int idNodes[2];
4351                 int pts[2];
4352                 int i = 0;
4353                 vtkIdList *Ids = vtkIdList::New();
4354                 for (; ite.More(); ite.Next())
4355                 {
4356                         idNodes[i] = ite.Key();
4357                         i++;
4358                 }
4359
4360                 Ids->InsertId(0, idNodes[0]);
4361                 pts[0] = idNodes[0];
4362                 Ids->InsertId(1, idNodes[1]);
4363                 pts[1] = idNodes[1];
4364
4365                 vtkUnstructuredGrid *ugrid =
4366                         vtkUnstructuredGrid::SafeDownCast(ac->GetUnstructuredGrid());
4367                 vtkUnstructuredGrid *newugrid = vtkUnstructuredGrid::New();
4368                 newugrid->SetPoints(ugrid->GetPoints());
4369                 newugrid->InsertNextCell(VTK_LINE, 2, pts);
4370
4371                 vtkDataSetMapper *Mapper = vtkDataSetMapper::New();
4372                 Mapper->SetInput(newugrid);
4373                 Mapper->Update();
4374
4375                 vtkActor *edge = vtkActor::New();
4376                 edge->SetMapper(Mapper);
4377                 edge->SetProperty(ac->GetProperty());
4378                 edge->SetBackfaceProperty(ac->GetBackfaceProperty());
4379                 edge->VisibilityOn();
4380                 mySimulationActors->AddItem(edge);
4381                 theRenderer->AddActor(edge);
4382         }
4383
4384         vtkRenderWindow *renWin = theRenderer->GetRenderWindow();
4385         renWin->Render();
4386 }
4387
4388 //=============================================================================
4389 /*!
4390  *
4391  */
4392 //=============================================================================
4393 void SMESHGUI::DisplaySimulationTriangle(SMESH::SMESH_Mesh_ptr aMesh,
4394         const TColStd_MapOfInteger & MapIndex, bool reverse)
4395 {
4396         if (myActiveStudy->getActiveStudyFrame()->getTypeView() != VIEW_VTK)
4397                 return;
4398
4399         EraseSimulationActors();
4400         mySimulationActors = vtkActorCollection::New();
4401         vtkRenderer *theRenderer =
4402                 ((VTKViewer_ViewFrame *) myActiveStudy->getActiveStudyFrame()->
4403                 getRightFrame()->getViewFrame())->getRenderer();
4404         Standard_Boolean result;
4405         SMESH_Actor *ac = FindActor(aMesh, result, true);
4406         if (result)
4407         {
4408                 TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
4409                 int idNodes[3];
4410                 int pts[3];
4411                 int i = 0;
4412                 vtkIdList *Ids = vtkIdList::New();
4413                 for (; ite.More(); ite.Next())
4414                 {
4415                         idNodes[i] = ite.Key();
4416                         i++;
4417                 }
4418
4419                 if (reverse)
4420                 {
4421                         Ids->InsertId(0, idNodes[2]);
4422                         pts[0] = idNodes[2];
4423                         Ids->InsertId(1, idNodes[1]);
4424                         pts[1] = idNodes[1];
4425                         Ids->InsertId(2, idNodes[0]);
4426                         pts[2] = idNodes[0];
4427                 }
4428                 else
4429                 {
4430                         Ids->InsertId(0, idNodes[0]);
4431                         pts[0] = idNodes[0];
4432                         Ids->InsertId(1, idNodes[1]);
4433                         pts[1] = idNodes[1];
4434                         Ids->InsertId(2, idNodes[2]);
4435                         pts[2] = idNodes[2];
4436                 }
4437                 vtkUnstructuredGrid *ugrid =
4438                         vtkUnstructuredGrid::SafeDownCast(ac->GetUnstructuredGrid());
4439                 vtkUnstructuredGrid *newugrid = vtkUnstructuredGrid::New();
4440                 newugrid->SetPoints(ugrid->GetPoints());
4441                 newugrid->InsertNextCell(VTK_TRIANGLE, 3, pts);
4442
4443                 vtkDataSetMapper *Mapper = vtkDataSetMapper::New();
4444                 Mapper->SetInput(newugrid);
4445                 Mapper->Update();
4446
4447                 vtkActor *tri = vtkActor::New();
4448                 tri->SetMapper(Mapper);
4449                 tri->SetProperty(ac->GetProperty());
4450                 tri->SetBackfaceProperty(ac->GetBackfaceProperty());
4451                 tri->VisibilityOn();
4452                 mySimulationActors->AddItem(tri);
4453                 theRenderer->AddActor(tri);
4454         }
4455
4456         vtkRenderWindow *renWin = theRenderer->GetRenderWindow();
4457         renWin->Render();
4458 }
4459
4460 //=============================================================================
4461 /*!
4462  *
4463  */
4464 //=============================================================================
4465 void SMESHGUI::DisplaySimulationQuadrangle(SMESH::SMESH_Mesh_ptr aMesh,
4466         const TColStd_MapOfInteger & MapIndex, bool reverse)
4467 {
4468         if (myActiveStudy->getActiveStudyFrame()->getTypeView() != VIEW_VTK)
4469                 return;
4470
4471         EraseSimulationActors();
4472         mySimulationActors = vtkActorCollection::New();
4473         vtkRenderer *theRenderer =
4474                 ((VTKViewer_ViewFrame *) myActiveStudy->getActiveStudyFrame()->
4475                 getRightFrame()->getViewFrame())->getRenderer();
4476         Standard_Boolean result;
4477         SMESH_Actor *ac = FindActor(aMesh, result, true);
4478
4479         vtkUnstructuredGrid *ugrid =
4480                 vtkUnstructuredGrid::SafeDownCast(ac->GetUnstructuredGrid());
4481
4482         if (result)
4483         {
4484                 TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
4485                 int i = 0;
4486                 int idNodes[4];
4487                 vtkIdList *Ids = vtkIdList::New();
4488                 for (; ite.More(); ite.Next())
4489                 {
4490                         idNodes[i] = ite.Key();
4491                         i++;
4492                 }
4493
4494                 float *p0 = ugrid->GetPoint(idNodes[0]);
4495                 float *p1 = ugrid->GetPoint(idNodes[1]);
4496                 float *p2 = ugrid->GetPoint(idNodes[2]);
4497                 float *p3 = ugrid->GetPoint(idNodes[3]);
4498
4499                 gp_Pnt P0(p0[0], p0[1], p0[2]);
4500
4501                 gp_Vec V1(P0, gp_Pnt(p1[0], p1[1], p1[2]));
4502                 gp_Vec V2(P0, gp_Pnt(p2[0], p2[1], p2[2]));
4503                 gp_Vec V3(P0, gp_Pnt(p3[0], p3[1], p3[2]));
4504
4505                 gp_Vec Cross1 = V1 ^ V2;
4506                 gp_Vec Cross2 = V2 ^ V3;
4507
4508                 int tmp;
4509                 if (Cross1.Dot(Cross2) < 0)
4510                 {
4511                         V1 = gp_Vec(P0, gp_Pnt(p2[0], p2[1], p2[2]));
4512                         V2 = gp_Vec(P0, gp_Pnt(p1[0], p1[1], p1[2]));
4513                         Cross1 = V1 ^ V2;
4514                         Cross2 = V2 ^ V3;
4515
4516                         if (Cross1.Dot(Cross2) < 0)
4517                         {
4518                                 tmp = idNodes[2];
4519                                 idNodes[2] = idNodes[3];
4520                                 idNodes[3] = tmp;
4521                         }
4522                         else
4523                         {
4524                                 tmp = idNodes[1];
4525                                 idNodes[1] = idNodes[2];
4526                                 idNodes[2] = tmp;
4527                         }
4528                 }
4529
4530                 if (reverse)
4531                 {
4532                         Ids->InsertId(0, idNodes[3]);
4533                         Ids->InsertId(1, idNodes[2]);
4534                         Ids->InsertId(2, idNodes[1]);
4535                         Ids->InsertId(3, idNodes[0]);
4536                 }
4537                 else
4538                 {
4539                         Ids->InsertId(0, idNodes[0]);
4540                         Ids->InsertId(1, idNodes[1]);
4541                         Ids->InsertId(2, idNodes[2]);
4542                         Ids->InsertId(3, idNodes[3]);
4543                 }
4544
4545                 //    vtkUnstructuredGrid* ugrid = vtkUnstructuredGrid::SafeDownCast( ac->GetUnstructuredGrid() );
4546                 vtkUnstructuredGrid *newugrid = vtkUnstructuredGrid::New();
4547                 newugrid->SetPoints(ugrid->GetPoints());
4548                 newugrid->InsertNextCell(VTK_QUAD, Ids);
4549
4550                 vtkDataSetMapper *Mapper = vtkDataSetMapper::New();
4551                 Mapper->SetInput(newugrid);
4552                 Mapper->Update();
4553
4554                 vtkActor *quad = vtkActor::New();
4555                 quad->SetMapper(Mapper);
4556                 quad->SetProperty(ac->GetProperty());
4557                 quad->SetBackfaceProperty(ac->GetBackfaceProperty());
4558                 quad->VisibilityOn();
4559                 mySimulationActors->AddItem(quad);
4560                 theRenderer->AddActor(quad);
4561         }
4562         vtkRenderWindow *renWin = theRenderer->GetRenderWindow();
4563         renWin->Render();
4564 }
4565
4566 //=============================================================================
4567 /*!
4568  *
4569  */
4570 //=============================================================================
4571 void SMESHGUI::DisplaySimulationTetra(SMESH::SMESH_Mesh_ptr aMesh,
4572         const TColStd_MapOfInteger & MapIndex)
4573 {
4574         if (myActiveStudy->getActiveStudyFrame()->getTypeView() != VIEW_VTK)
4575                 return;
4576
4577         EraseSimulationActors();
4578         mySimulationActors = vtkActorCollection::New();
4579         vtkRenderer *theRenderer =
4580                 ((VTKViewer_ViewFrame *) myActiveStudy->getActiveStudyFrame()->
4581                 getRightFrame()->getViewFrame())->getRenderer();
4582         Standard_Boolean result;
4583         SMESH_Actor *ac = FindActor(aMesh, result, true);
4584         if (result)
4585         {
4586                 TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
4587                 int i = 0;
4588                 int idNodes[4];
4589                 vtkIdList *Ids = vtkIdList::New();
4590                 for (; ite.More(); ite.Next())
4591                 {
4592                         idNodes[i] = ite.Key();
4593                         i++;
4594                 }
4595
4596                 Ids->InsertId(0, idNodes[0]);
4597                 Ids->InsertId(1, idNodes[1]);
4598                 Ids->InsertId(2, idNodes[2]);
4599                 Ids->InsertId(3, idNodes[3]);
4600
4601                 vtkUnstructuredGrid *ugrid =
4602                         vtkUnstructuredGrid::SafeDownCast(ac->GetUnstructuredGrid());
4603                 vtkUnstructuredGrid *newugrid = vtkUnstructuredGrid::New();
4604                 newugrid->SetPoints(ugrid->GetPoints());
4605                 newugrid->InsertNextCell(VTK_TETRA, Ids);
4606
4607                 vtkDataSetMapper *Mapper = vtkDataSetMapper::New();
4608                 Mapper->SetInput(newugrid);
4609                 Mapper->Update();
4610
4611                 vtkActor *tetra = vtkActor::New();
4612                 tetra->SetMapper(Mapper);
4613                 tetra->SetProperty(ac->GetProperty());
4614                 tetra->SetBackfaceProperty(ac->GetBackfaceProperty());
4615                 tetra->VisibilityOn();
4616                 mySimulationActors->AddItem(tetra);
4617                 theRenderer->AddActor(tetra);
4618         }
4619         vtkRenderWindow *renWin = theRenderer->GetRenderWindow();
4620         renWin->Render();
4621 }
4622
4623 //=============================================================================
4624 /*!
4625  *
4626  */
4627 //=============================================================================
4628 void SMESHGUI::DisplaySimulationHexa(SMESH::SMESH_Mesh_ptr aMesh,
4629         const TColStd_MapOfInteger & MapIndex)
4630 {
4631         if (myActiveStudy->getActiveStudyFrame()->getTypeView() != VIEW_VTK)
4632                 return;
4633
4634         EraseSimulationActors();
4635         mySimulationActors = vtkActorCollection::New();
4636         vtkRenderer *theRenderer =
4637                 ((VTKViewer_ViewFrame *) myActiveStudy->getActiveStudyFrame()->
4638                 getRightFrame()->getViewFrame())->getRenderer();
4639         Standard_Boolean result;
4640         SMESH_Actor *ac = FindActor(aMesh, result, true);
4641         if (result)
4642         {
4643                 TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
4644                 int i = 0;
4645                 int idNodes[8];
4646                 vtkIdList *Ids = vtkIdList::New();
4647                 for (; ite.More(); ite.Next())
4648                 {
4649                         idNodes[i] = ite.Key();
4650                         i++;
4651                 }
4652
4653                 Ids->InsertId(0, idNodes[0]);
4654                 Ids->InsertId(1, idNodes[1]);
4655                 Ids->InsertId(2, idNodes[2]);
4656                 Ids->InsertId(3, idNodes[3]);
4657                 Ids->InsertId(4, idNodes[4]);
4658                 Ids->InsertId(5, idNodes[5]);
4659                 Ids->InsertId(6, idNodes[6]);
4660                 Ids->InsertId(7, idNodes[7]);
4661
4662                 vtkUnstructuredGrid *ugrid =
4663                         vtkUnstructuredGrid::SafeDownCast(ac->GetUnstructuredGrid());
4664                 vtkUnstructuredGrid *newugrid = vtkUnstructuredGrid::New();
4665                 newugrid->SetPoints(ugrid->GetPoints());
4666                 newugrid->InsertNextCell(VTK_HEXAHEDRON, Ids);
4667
4668                 vtkDataSetMapper *Mapper = vtkDataSetMapper::New();
4669                 Mapper->SetInput(newugrid);
4670                 Mapper->Update();
4671
4672                 vtkActor *hexa = vtkActor::New();
4673                 hexa->SetMapper(Mapper);
4674                 hexa->SetProperty(ac->GetProperty());
4675                 hexa->SetBackfaceProperty(ac->GetBackfaceProperty());
4676                 hexa->VisibilityOn();
4677                 mySimulationActors->AddItem(hexa);
4678                 theRenderer->AddActor(hexa);
4679         }
4680         vtkRenderWindow *renWin = theRenderer->GetRenderWindow();
4681         renWin->Render();
4682 }
4683
4684 //=============================================================================
4685 /*!
4686  *
4687  */
4688 //=============================================================================
4689 void SMESHGUI::AddFace(SMESH::SMESH_Mesh_ptr theMesh,
4690                        const TColStd_MapOfInteger & MapIndex, 
4691                        bool reverse)
4692 {
4693   QAD_WaitCursor wc;
4694   try{
4695     SALOMEDS::SObject_var aSobj = myStudyAPI.FindObject(theMesh);
4696     CORBA::String_var anEntry = aSobj->GetID();
4697     if(SMESH_Actor* anActor = ::FindActorByEntry(anEntry.in())){
4698       SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
4699       anArrayOfIdeces->length(MapIndex.Extent());
4700       TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
4701       for(int i = 0; ite.More(); ite.Next(), i++){
4702         anArrayOfIdeces[i] = anActor->GetNodeObjId(ite.Key());
4703       }
4704       int tmp;
4705       if(MapIndex.Extent() == 4){
4706         int idNodes[4];
4707         TColStd_MapIteratorOfMapOfInteger ite1(MapIndex);
4708         for(int i = 0; ite1.More(); ite1.Next(), i++){
4709           idNodes[i] = ite1.Key();
4710         }
4711         vtkUnstructuredGrid *ugrid = anActor->GetUnstructuredGrid();
4712         float *p0 = ugrid->GetPoint(idNodes[0]);
4713         float *p1 = ugrid->GetPoint(idNodes[1]);
4714         float *p2 = ugrid->GetPoint(idNodes[2]);
4715         float *p3 = ugrid->GetPoint(idNodes[3]);
4716         
4717         gp_Pnt P0(p0[0], p0[1], p0[2]);
4718         
4719         gp_Vec V1(P0, gp_Pnt(p1[0], p1[1], p1[2]));
4720         gp_Vec V2(P0, gp_Pnt(p2[0], p2[1], p2[2]));
4721         gp_Vec V3(P0, gp_Pnt(p3[0], p3[1], p3[2]));
4722         
4723         gp_Vec Cross1 = V1 ^ V2;
4724         gp_Vec Cross2 = V2 ^ V3;
4725         
4726         if(Cross1.Dot(Cross2) < 0){
4727           V1 = gp_Vec(P0, gp_Pnt(p2[0], p2[1], p2[2]));
4728           V2 = gp_Vec(P0, gp_Pnt(p1[0], p1[1], p1[2]));
4729           Cross1 = V1 ^ V2;
4730           Cross2 = V2 ^ V3;
4731           
4732           if(Cross1.Dot(Cross2) < 0){
4733             tmp = anArrayOfIdeces[2];
4734             anArrayOfIdeces[2] = anArrayOfIdeces[3];
4735             anArrayOfIdeces[3] = tmp;
4736           }else{
4737             tmp = anArrayOfIdeces[1];
4738             anArrayOfIdeces[1] = anArrayOfIdeces[2];
4739             anArrayOfIdeces[2] = tmp;
4740           }
4741         }
4742       }
4743       if(reverse){
4744         for(int i = 0; i < (MapIndex.Extent() / 2); i++){
4745           tmp = anArrayOfIdeces[i];
4746           anArrayOfIdeces[i] = anArrayOfIdeces[MapIndex.Extent() - i - 1];
4747           anArrayOfIdeces[MapIndex.Extent() - i - 1] = tmp;
4748         }
4749       }
4750       SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
4751       aMeshEditor->AddFace(anArrayOfIdeces);
4752       if(myAutomaticUpdate){
4753         CORBA::Long anId = smeshGUI->myStudy->StudyId();
4754         if(TVisualObjPtr aVisualObj = GetVisualObj(anId,anEntry.in())){
4755           aVisualObj->Update(true);
4756         }
4757         AddActorInSelection(anActor);
4758       }
4759     }
4760   }catch(SALOME::SALOME_Exception& exc) {
4761     INFOS("Follow exception was cought:\n\t"<<exc.details.text);
4762   }catch(const std::exception& exc){
4763     INFOS("Follow exception was cought:\n\t"<<exc.what());
4764   }catch(...){
4765     INFOS("Unknown exception was cought !!!");
4766   }
4767 }
4768
4769 //=============================================================================
4770 /*!
4771  *
4772  */
4773 //=============================================================================
4774 void SMESHGUI::AddVolume(SMESH::SMESH_Mesh_ptr theMesh,
4775                          const TColStd_MapOfInteger & MapIndex)
4776 {
4777   QAD_WaitCursor wc;
4778   try{
4779     SALOMEDS::SObject_var aSobj = myStudyAPI.FindObject(theMesh);
4780     CORBA::String_var anEntry = aSobj->GetID();
4781     if(SMESH_Actor* anActor = ::FindActorByEntry(anEntry.in())){
4782       SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
4783       anArrayOfIdeces->length(MapIndex.Extent());
4784       TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
4785       for(int i = 0; ite.More(); ite.Next(), i++){
4786         anArrayOfIdeces[i] = anActor->GetNodeObjId(ite.Key());
4787       }
4788       SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
4789       aMeshEditor->AddVolume(anArrayOfIdeces);
4790       if(myAutomaticUpdate){
4791         CORBA::Long anId = smeshGUI->myStudy->StudyId();
4792         if(TVisualObjPtr aVisualObj = GetVisualObj(anId,anEntry.in())){
4793           aVisualObj->Update(true);
4794         }
4795       }
4796     }
4797   }catch(SALOME::SALOME_Exception& exc) {
4798     INFOS("Follow exception was cought:\n\t"<<exc.details.text);
4799   }catch(const std::exception& exc){
4800     INFOS("Follow exception was cought:\n\t"<<exc.what());
4801   }catch(...){
4802     INFOS("Unknown exception was cought !!!");
4803   }
4804 }
4805 //=============================================================================
4806 /*!
4807  *
4808  */
4809 //=============================================================================
4810 void SMESHGUI::AddEdge(SMESH::SMESH_Mesh_ptr theMesh,
4811                        const TColStd_MapOfInteger & MapIndex)
4812 {
4813   QAD_WaitCursor wc;
4814   try{
4815     SALOMEDS::SObject_var aSobj = myStudyAPI.FindObject(theMesh);
4816     CORBA::String_var anEntry = aSobj->GetID();
4817     if(SMESH_Actor* anActor = ::FindActorByEntry(anEntry.in())){
4818       SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
4819       anArrayOfIdeces->length(MapIndex.Extent());
4820       TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
4821       for(int i = 0; ite.More(); ite.Next(), i++){
4822         anArrayOfIdeces[i] = anActor->GetNodeObjId(ite.Key());
4823       }
4824       SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
4825       aMeshEditor->AddEdge(anArrayOfIdeces);
4826       if(myAutomaticUpdate){
4827         CORBA::Long anId = smeshGUI->myStudy->StudyId();
4828         if(TVisualObjPtr aVisualObj = GetVisualObj(anId,anEntry.in())){
4829           aVisualObj->Update(true);
4830         }
4831       }
4832     }
4833   }catch(SALOME::SALOME_Exception& exc) {
4834     INFOS("Follow exception was cought:\n\t"<<exc.details.text);
4835   }catch(const std::exception& exc){
4836     INFOS("Follow exception was cought:\n\t"<<exc.what());
4837   }catch(...){
4838     INFOS("Unknown exception was cought !!!");
4839   }
4840 }
4841 //=============================================================================
4842 /*!
4843  *
4844  */
4845 //=============================================================================
4846 void SMESHGUI::AddNode(SMESH::SMESH_Mesh_ptr theMesh, 
4847                        float x, float y, float z)
4848 {
4849   QAD_WaitCursor wc;
4850   try{
4851     SALOMEDS::SObject_var aSobj = myStudyAPI.FindObject(theMesh);
4852     CORBA::String_var anEntry = aSobj->GetID();
4853     if(SMESH_Actor* anActor = ::FindActorByEntry(anEntry.in())){
4854       SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
4855       aMeshEditor->AddNode(x, y, z);
4856       if(myAutomaticUpdate){
4857         CORBA::Long anId = smeshGUI->myStudy->StudyId();
4858         if(TVisualObjPtr aVisualObj = GetVisualObj(anId,anEntry.in())){
4859           aVisualObj->Update(true);
4860         }
4861       }
4862     }
4863   }catch(SALOME::SALOME_Exception& exc) {
4864     INFOS("Follow exception was cought:\n\t"<<exc.details.text);
4865   }catch(const std::exception& exc){
4866     INFOS("Follow exception was cought:\n\t"<<exc.what());
4867   }catch(...){
4868     INFOS("Unknown exception was cought !!!");
4869   }
4870 }
4871 //=============================================================================
4872 /*!
4873  *
4874  */
4875 //=============================================================================
4876 void SMESHGUI::DisplayEdgesConnectivityLegendBox(vtkActor * ac)
4877 {
4878         if (myActiveStudy->getActiveStudyFrame()->getTypeView() != VIEW_VTK)
4879                 return;
4880
4881         EraseSimulationActors();
4882         mySimulationActors2D = vtkActor2DCollection::New();
4883
4884         vtkRenderer *theRenderer =
4885                 ((VTKViewer_ViewFrame *) myActiveStudy->getActiveStudyFrame()->
4886                 getRightFrame()->getViewFrame())->getRenderer();
4887
4888         vtkGlyphSource2D *gs2 = vtkGlyphSource2D::New();
4889         gs2->SetGlyphTypeToTriangle();
4890         gs2->FilledOff();
4891
4892         vtkLegendBoxActor *legend = vtkLegendBoxActor::New();
4893         legend->GetPositionCoordinate()->SetValue(0.025, 0.025, 0.);
4894         legend->GetPosition2Coordinate()->SetValue(0.3, 0.3, 0.);       //relative to Position
4895
4896         legend->SetNumberOfEntries(4);
4897         legend->SetEntryString(0, tr("SMESH_BOUNDARYEDGES"));
4898         legend->SetEntrySymbol(0, gs2->GetOutput());
4899         legend->SetEntryColor(0, ac->GetMapper()->GetLookupTable()->GetColor(0.));
4900         legend->SetEntryString(1, tr("SMESH_MANIFOLDEDGES"));
4901         legend->SetEntrySymbol(1, gs2->GetOutput());
4902         legend->SetEntryColor(1,
4903                 ac->GetMapper()->GetLookupTable()->GetColor(0.666667));
4904         legend->SetEntryString(2, tr("SMESH_NONMANIFOLDEDGES"));
4905         legend->SetEntrySymbol(2, gs2->GetOutput());
4906         legend->SetEntryColor(2,
4907                 ac->GetMapper()->GetLookupTable()->GetColor(0.222222));
4908         legend->SetEntryString(3, tr("SMESH_FEATUREEDGES"));
4909         legend->SetEntrySymbol(3, gs2->GetOutput());
4910         legend->SetEntryColor(3,
4911                 ac->GetMapper()->GetLookupTable()->GetColor(0.444444));
4912         legend->SetPadding(5);
4913         //  legend->GetProperty()->SetColor();
4914
4915         mySimulationActors2D->AddItem(legend);
4916         theRenderer->AddActor2D(legend);
4917
4918         // Update the view
4919         myActiveStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame()->
4920                 Repaint();
4921 }
4922
4923 //===============================================================================
4924 // function : OnEditDelete()
4925 // purpose  :
4926 //===============================================================================
4927 void SMESHGUI::OnEditDelete()
4928 {
4929   if (QAD_MessageBox::warn2
4930       (QAD_Application::getDesktop(),
4931        tr("SMESH_WRN_WARNING"),
4932        tr("SMESH_REALLY_DELETE"),
4933        tr("SMESH_BUT_YES"), tr("SMESH_BUT_NO"), 1, 0, 0) != 1)
4934     return;
4935
4936   int nbSf = myActiveStudy->getStudyFramesCount();
4937
4938   Standard_Boolean found;
4939   SALOMEDS::Study_var aStudy = myActiveStudy->getStudyDocument();
4940   SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
4941   SALOMEDS::GenericAttribute_var anAttr;
4942   SALOMEDS::AttributeIOR_var anIOR;
4943
4944   SALOME_Selection *Sel = SALOME_Selection::Selection(myActiveStudy->getSelection());
4945   SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
4946   for(; It.More(); It.Next()){
4947     Handle(SALOME_InteractiveObject) IObject = It.Value();
4948     if(IObject->hasEntry()){
4949       SALOMEDS::SObject_var SO = myStudy->FindObjectID(IObject->getEntry());
4950
4951       /* Erase child graphical objects */
4952       SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator(SO);
4953       for(; it->More(); it->Next()){
4954         SALOMEDS::SObject_var CSO = it->Value();
4955         if(CSO->FindAttribute(anAttr, "AttributeIOR")){
4956           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
4957           
4958           for(int i = 0; i < nbSf; i++){
4959             QAD_StudyFrame *sf = myActiveStudy->getStudyFrame(i);
4960             CORBA::String_var anEntry = CSO->GetID();
4961             if(SMESH_Actor* anActor = ::FindActorByEntry(sf,anEntry.in())){
4962               RemoveActor(sf,anActor);
4963             }
4964           }
4965         }
4966       }
4967
4968       /* Erase main graphical object */
4969       for(int i = 0; i < nbSf; i++){
4970         QAD_StudyFrame *sf = myActiveStudy->getStudyFrame(i);
4971         if(SMESH_Actor* anActor = ::FindActorByEntry(sf,IObject->getEntry())){
4972           RemoveActor(sf,anActor);
4973         }
4974       }
4975
4976       // Remove object(s) from data structures
4977       SALOMEDS::SObject_var obj = myStudy->FindObjectID(IObject->getEntry());
4978       if(!obj->_is_nil()){
4979         SMESH::SMESH_Group_var     aGroup = SMESH::SMESH_Group  ::_narrow(obj->GetObject());
4980         SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(obj->GetObject());
4981
4982         if ( !aGroup->_is_nil() ) {                          // DELETE GROUP
4983           SMESH::SMESH_Mesh_var aMesh = aGroup->GetMesh();
4984           aMesh->RemoveGroup( aGroup );
4985         }
4986         else if ( !aSubMesh->_is_nil() ) {                   // DELETE SUBMESH
4987           SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
4988           aMesh->RemoveSubMesh( aSubMesh );
4989         }
4990         else {// default action: remove SObject from the study
4991           // san - it's no use opening a transaction here until UNDO/REDO is provided in SMESH
4992           //QAD_Operation *op = new SALOMEGUI_ImportOperation(myActiveStudy);
4993           //op->start();
4994           aStudyBuilder->RemoveObjectWithChildren( obj );
4995           //op->finish();
4996         }
4997       }
4998
4999     } /* IObject->hasEntry() */
5000   } /* more/next */
5001
5002   /* Clear any previous selection */
5003   Sel->ClearIObjects();
5004   myActiveStudy->updateObjBrowser();
5005 }
5006
5007 //=======================================================================
5008 // name    : SMESHGUI::GetFilterMgr
5009 // Purpose : Get filter manager
5010 //=======================================================================
5011 SMESH::FilterManager_ptr SMESHGUI::GetFilterMgr()
5012 {
5013    return myFilterMgr;
5014 }