Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/visu.git] / src / VISUGUI / VisuGUI_Tools.cxx
index c53d8af9b1ab5cb1ba6d096a54b3c50f7b14221b..757b1749ba1008371b112fd7f20a74af62b3c568 100644 (file)
@@ -28,6 +28,9 @@
 
 #include "VisuGUI_Tools.h"
 
+#include "VisuGUI.h"
+#include "VisuGUI_ViewTools.h"
+
 #include "VISU_Gen_i.hh"
 #include "VISU_Prs3d_i.hh"
 #include "VISU_Result_i.hh"
 #include "VISU_ViewManager_i.hh"
 
 #include "VISU_Actor.h"
+#include "VISU_ScalarMapAct.h"
 
 #include "SalomeApp_Module.h"
 #include "SalomeApp_Study.h"
 #include "SalomeApp_Application.h"
-#include "SalomeApp_SelectionMgr.h"
+
+#include "LightApp_DataObject.h"
+#include "LightApp_SelectionMgr.h"
+
+#include "OB_Browser.h"
 
 #include "SALOME_ListIO.hxx"
 #include "SALOME_ListIteratorOfListIO.hxx"
 
 #include "SVTK_ViewWindow.h"
-#include "SVTK_ViewModel.h"
 #include "SVTK_Functor.h"
 
 #include "VTKViewer_Algorithm.h"
@@ -70,7 +77,7 @@ namespace VISU
     return theModule->application()->desktop();
   }
 
-  SalomeApp_SelectionMgr*
+  LightApp_SelectionMgr*
   GetSelectionMgr(const SalomeApp_Module* theModule)
   {
     return theModule->getApp()->selectionMgr();
@@ -89,18 +96,6 @@ namespace VISU
     return theStudy->studyDS();
   }
 
-  SALOMEDS::StudyManager_var
-  GetStudyManager()
-  {
-    static SALOMEDS::StudyManager_var aStudyManager;
-    if(CORBA::is_nil(aStudyManager)){
-      SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
-      CORBA::Object_var anObject = aNamingService->Resolve("/myStudyManager");
-      aStudyManager = SALOMEDS::StudyManager::_narrow(anObject);
-    }
-    return aStudyManager;
-  }
-
   bool
   IsStudyLocked( _PTR(Study) theStudy )
   {
@@ -110,10 +105,83 @@ namespace VISU
   }
 
   bool
-  CheckLock( _PTR(Study) theStudy )
+  CheckLock( _PTR(Study) theStudy,
+            QWidget* theWidget )
+  {
+    if(IsStudyLocked(theStudy)){
+      SUIT_MessageBox::warn1(theWidget,
+                            QObject::tr("WRN_VISU_WARNING"),
+                            QObject::tr("WRN_STUDY_LOCKED"),
+                            QObject::tr("BUT_OK"));
+       return true;
+    }
+    return false;
+  }
+
+  LightApp_DataObject*
+  FindDataObject(SUIT_DataObject* theDataObject,
+                const QString& theEntry,
+                int theLevel)
+  {
+    int aLevel = theDataObject->level() + 2;
+    QString aSourceEntry = theEntry.section(':',0,aLevel);
+    DataObjectList aList;
+    theDataObject->children(aList);
+    DataObjectListIterator aDataObjectIter(aList);
+    while(SUIT_DataObject* aDataObject = aDataObjectIter.current()){
+      if(LightApp_DataObject* aChildDataObject = dynamic_cast<LightApp_DataObject*>(aDataObject)){
+       QString anEntry = aChildDataObject->entry();
+       QString aCurrentEntry = anEntry.section(':',0,aLevel);
+       if(aSourceEntry == aCurrentEntry){
+         if(theLevel == aLevel){
+           return aChildDataObject;
+         }else{
+           return FindDataObject(aChildDataObject,theEntry,theLevel);
+         }
+       }
+      }
+      ++aDataObjectIter;
+    }
+    return NULL;
+  }
+
+  LightApp_DataObject*
+  FindDataObject(CAM_Module* theModule,
+                _PTR(SObject) theSObject)
+  {
+    CAM_DataModel* aDataModel = theModule->dataModel();
+    CAM_DataObject* aRootDataObject = aDataModel->root();
+    if(_PTR(SComponent) aComponent = theSObject)
+      return dynamic_cast<LightApp_DataObject*>(aRootDataObject);
+
+    int aLevel = theSObject->Depth();
+    std::string anEntry = theSObject->GetID();
+    return FindDataObject(aRootDataObject,anEntry.c_str(),aLevel);
+  }
+
+  void
+  UpdateObjBrowser(SalomeApp_Module* theModule,
+                  bool theIsUpdateDataModel,
+                  _PTR(SObject) theSObject)
+  {
+    LightApp_DataObject* aDataObject = NULL;
+    if(theSObject)
+      aDataObject = FindDataObject(theModule,theSObject);
+
+    theModule->updateObjBrowser(theIsUpdateDataModel,aDataObject);
+    theModule->getApp()->updateActions();
+  }
+
+  bool
+  IsSObjectTable( _PTR(SObject) theSObject )
   {
-    if(IsStudyLocked(theStudy))
-      throw std::runtime_error(QObject::tr("WRN_STUDY_LOCKED").latin1());
+    if ( theSObject ) {
+      _PTR(GenericAttribute) anAttr;
+      if (theSObject->FindAttribute( anAttr, "AttributeTableOfInteger" ))
+       return true;
+      if (theSObject->FindAttribute( anAttr, "AttributeTableOfReal" ))
+       return true;
+    }
     return false;
   }
 
@@ -149,32 +217,6 @@ namespace VISU
     return aGen;
   }
 
-  VISU::Storable::TRestoringMap
-  getMapOfValue(SALOMEDS::SObject_var theSObject)
-  {
-    VISU::Storable::TRestoringMap aMap;
-    if(!theSObject->_is_nil()){
-      SALOMEDS::GenericAttribute_var anAttr;
-      if (theSObject->FindAttribute(anAttr, "AttributeComment")) {
-       SALOMEDS::AttributeComment_var aComment = SALOMEDS::AttributeComment::_narrow(anAttr);
-       CORBA::String_var aValue = aComment->Value();
-       QString aString(aValue.in());
-       VISU::Storable::StrToMap(aString,aMap);
-      }
-    }
-    return aMap;
-  }
-
-  QString
-  getValue(SALOMEDS::SObject_var theSObject,
-          QString theKey)
-  {
-    QString aStr("");
-    VISU::Storable::TRestoringMap aMap = getMapOfValue(theSObject);
-    if(!aMap.empty())
-      aStr = VISU::Storable::FindValue(aMap,theKey.latin1());
-    return aStr;
-  }
 
   VISU::Storable::TRestoringMap getMapOfValue (_PTR(SObject) theSObject)
   {
@@ -200,27 +242,27 @@ namespace VISU
     return aStr;
   }
 
-  //************************************************************
+  //------------------------------------------------------------
   // Selection
   CORBA::Object_var
   GetSelectedObj(const SalomeApp_Study* theStudy,
                 const QString& theEntry,
                 VISU::Storable::TRestoringMap* theMap)
   {
-    if ( !theStudy || theEntry.isEmpty() )
+    if (!theStudy || theEntry.isEmpty())
       return CORBA::Object::_nil();
 
-    SALOMEDS::Study_var aStudy = GetDSStudy(GetCStudy( theStudy ));
-    SALOMEDS::SObject_var aSObject = aStudy->FindObjectID( theEntry.latin1() );
-    if(!aSObject->_is_nil()){
-      SALOMEDS::GenericAttribute_var anAttr;
-      if(theMap && aSObject->FindAttribute(anAttr,"AttributeComment")){
-       SALOMEDS::AttributeComment_var aComment = SALOMEDS::AttributeComment::_narrow(anAttr);
+    _PTR(Study) aStudy = GetCStudy(theStudy);
+    _PTR(SObject) aSObject = aStudy->FindObjectID(theEntry.latin1());
+    if (aSObject) {
+      _PTR(GenericAttribute) anAttr;
+      if (theMap && aSObject->FindAttribute(anAttr,"AttributeComment")) {
+       _PTR(AttributeComment) aComment (anAttr);
        std::string aValue = aComment->Value();
-       QString aString(aValue.c_str());
-       VISU::Storable::StrToMap(aString,*theMap);
+       QString aString (aValue.c_str());
+       VISU::Storable::StrToMap(aString, *theMap);
       }
-      return VISU::SObjectToObject(aSObject);
+      return VISU::ClientSObjectToObject(aSObject);
     }
     return CORBA::Object::_nil();
   }
@@ -230,7 +272,7 @@ namespace VISU
                 Handle(SALOME_InteractiveObject)* theIO,
                 VISU::Storable::TRestoringMap* theMap)
   {
-    SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
+    LightApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
     SALOME_ListIO aListIO;
     aSelectionMgr->selectedObjects(aListIO);
     SALOME_ListIteratorOfListIO anIter(aListIO);
@@ -244,8 +286,27 @@ namespace VISU
     return CORBA::Object::_nil();
   }
 
+  VISU::Prs3d_i*
+  GetPrsToModify(const SalomeApp_Module* theModule,
+                Handle(SALOME_InteractiveObject)* theIO,
+                VISU::Storable::TRestoringMap* theMap)
+  {
+    if (CheckLock(GetCStudy(GetAppStudy(theModule)),GetDesktop(theModule)))
+      return NULL;
+
+    CORBA::Object_var anObject = GetSelectedObj(theModule, theIO);
+    if (CORBA::is_nil(anObject))
+      return NULL;
+
+    PortableServer::ServantBase_var aServant = VISU::GetServant(anObject);
+    if (!aServant.in())
+      return NULL;
+
+    return dynamic_cast<VISU::Prs3d_i*>(aServant.in());
+  }
+
   void
-  Add(SalomeApp_SelectionMgr* theSelectionMgr,
+  Add(LightApp_SelectionMgr* theSelectionMgr,
       const Handle(SALOME_InteractiveObject)& theIO)
   {
     SALOME_ListIO aListIO;
@@ -255,7 +316,7 @@ namespace VISU
   }
 
   void
-  Remove(SalomeApp_SelectionMgr* theSelectionMgr,
+  Remove(LightApp_SelectionMgr* theSelectionMgr,
         const Handle(SALOME_InteractiveObject)& theIO)
   {
     if (theIO.IsNull()) return;
@@ -271,32 +332,57 @@ namespace VISU
     theSelectionMgr->setSelectedObjects(aNewListIO);
   }
 
+  /*!
+   * \brief Check, if "Delete" popup-menu can be put on current selection
+   *
+   * \param theModule - is used to access SelectionManager, Study and VISU_Gen
+   * \retval bool - returns TRUE if all currently selected objects are removable
+   */
   bool
   IsRemovableSelected (const SalomeApp_Module* theModule)
   {
-    SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
+    LightApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
     SALOME_ListIO aListIO;
     aSelectionMgr->selectedObjects(aListIO);
 
     if (aListIO.Extent() < 1)
       return false;
 
+    _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
+    if (!aStudy) return false;
+
+    // In some cases single selection can have its own popup-menu item for deletion
+    /*if (aListIO.Extent() == 1) {
+      Handle(SALOME_InteractiveObject) anIO = aListIO.First();
+      _PTR(SObject) aSObject = aStudy->FindObjectID(anIO->getEntry());
+      if (aSObject) {
+        VISU::VISUType aType = (VISU::VISUType)getValue(aSObject, "myType").toInt();
+        if (aType == VISU::TVIEW3D) {
+          return false; // special case
+        }
+      }
+    }*/
+
     SALOME_ListIteratorOfListIO anIter (aListIO);
     for (; anIter.More(); anIter.Next()) {
       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
       if (anIO->hasEntry()) {
-        _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
+
+       // asv : if selected object is a Save Point object selected in object browser - return false
+       if ( QString( anIO->getEntry() ).startsWith( QObject::tr( "SAVE_POINT_DEF_NAME" ) ) )
+         return false;
+
         _PTR(SObject) aSObject = aStudy->FindObjectID(anIO->getEntry());
         VISU::Storable::TRestoringMap pMap;
         if (aSObject) {
-         CORBA::Object_var aCORBAObject = VISU::ClientSObjectToObject(aSObject);
-         if (!CORBA::is_nil(aCORBAObject)) {
-           VISU::RemovableObject_var aRemovableObj = VISU::RemovableObject::_narrow(aCORBAObject);
-           if (CORBA::is_nil(aRemovableObj)) {
+          CORBA::Object_var aCORBAObject = VISU::ClientSObjectToObject(aSObject);
+          if (!CORBA::is_nil(aCORBAObject)) {
+            VISU::RemovableObject_var aRemovableObj = VISU::RemovableObject::_narrow(aCORBAObject);
+            if (CORBA::is_nil(aRemovableObj)) {
               // Not removable CORBA object
               return false;
             }
-         } else {
+          } else {
             // Can be removed, if lays directly under VISU
             // (first sub-level) or is a child of such an object
             string aNAME, aVisuNAME = GetVisuGen(theModule)->ComponentDataType();
@@ -334,77 +420,36 @@ namespace VISU
                 return false;
               }
             }
-         }
+          }
         }
       }
     }
     return true;
   }
 
-  //************************************************************
-  // Display/Erase
-
   void
-  ErasePrs (const SalomeApp_Module* theModule,
-            CORBA::Object_ptr theObject, bool theUpdate)
+  DeleteSObject(VisuGUI* theModule,
+               _PTR(Study) theStudy,
+               _PTR(SObject) theSObject)
   {
-    if (!CORBA::is_nil(theObject)) {
-      VISU::Base_var aBase = VISU::Base::_narrow(theObject);
-      if (CORBA::is_nil(aBase)) return;
-      VISU::VISUType aType = aBase->GetType();
-      switch (aType) {
-      case VISU::TCURVE:
-        {
-          if (VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aBase).in()))
-           PlotCurve(theModule, aCurve, VISU::eErase );
-          break;
-        }
-      case VISU::TCONTAINER:
-        {
-          if (VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aBase).in()))
-           PlotContainer(theModule, aContainer, VISU::eErase );
-          break;
-        }
-      case VISU::TTABLE:
-        {
-          if (VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aBase).in()))
-            PlotTable(theModule, aTable, VISU::eErase );
-          break;
-        }
-      default:
-        {
-          if (VISU::Prs3d_i* aPrsObject = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aBase).in())) {
-           ErasePrs3d(theModule, aPrsObject);
-           if (theUpdate) {
-             if (SVTK_ViewWindow* vw = GetViewWindow(theModule))
-               vw->Repaint();
-           }
-          }
-        }
-      } // switch (aType)
-    }
-  }
-
-  void
-  DeleteSObject (SalomeApp_Module* theModule,
-                 SALOMEDS::Study_ptr theStudy,
-                 SALOMEDS::SObject_ptr theSObject)
-  {
-    SALOMEDS::ChildIterator_var aChildIter = theStudy->NewChildIterator(theSObject);
+    _PTR(ChildIterator) aChildIter = theStudy->NewChildIterator(theSObject);
     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
-      SALOMEDS::SObject_var aChildSObject = aChildIter->Value();
-      CORBA::Object_var aChildObj = VISU::SObjectToObject(aChildSObject);
-      ErasePrs(theModule, aChildObj);
+      _PTR(SObject) aChildSObject = aChildIter->Value();
+      CORBA::Object_var aChildObj = VISU::ClientSObjectToObject(aChildSObject);
+      ErasePrs(theModule, aChildObj, /*repaint_view_window = */false);
     }
 
-    CORBA::Object_var anObj = VISU::SObjectToObject(theSObject);
+    CORBA::Object_var anObj = VISU::ClientSObjectToObject(theSObject);
     if (!CORBA::is_nil(anObj)) {
+      ErasePrs(theModule, anObj, /*repaint_view_window = */true);
+
       VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(anObj);
       if (!CORBA::is_nil(aRemovableObject)) {
         aRemovableObject->RemoveFromStudy();
       }
     } else {
       // Remove aSObject together with all its sub-objects
+
       VISU::RemoveFromStudy(theSObject,
                             false,  // remove not only IOR attribute, but Object With Children
                             false); // not Destroy() sub-objects
@@ -412,56 +457,38 @@ namespace VISU
   }
 
   void
-  DeletePrs3d(SalomeApp_Module* theModule,
-             VISU::Prs3d_i* thePrs,
-             const Handle(SALOME_InteractiveObject)& theIO)
+  DeletePrs3d(VisuGUI* theModule,
+              VISU::Prs3d_i* thePrs,
+              const Handle(SALOME_InteractiveObject)& theIO)
   {
-    if(!thePrs)
+    if (!thePrs)
       return;
-    if(CheckLock(GetCStudy(GetAppStudy(theModule))))
+    if (CheckLock(GetCStudy(GetAppStudy(theModule)),GetDesktop(theModule)))
       return;
-    SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
-    CORBA::String_var anEntry = aSObject->GetID();
-    SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
-    Remove(aSelectionMgr,theIO);
-    TViewWindows aViewWindows = GetViews(theModule);
-    for(int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++){
-      SVTK_ViewWindow* aView = aViewWindows[i];
-      if(VISU_Actor* anActor = FindActor(aView,anEntry.in())){
-       aView->RemoveActor(anActor);
-       anActor->Delete();
-      }
-    }
-    thePrs->RemoveFromStudy();
-    theModule->updateObjBrowser(); //update Object browser
-  }
 
-  void
-  ErasePrs3d(const SalomeApp_Module* theModule,
-            VISU::Prs3d_i* thePrs)
-  {
-    if ( SVTK_ViewWindow* vw = GetViewWindow( theModule ) ){
-      VISU_Actor* anVISUActor = FindActor( vw, thePrs );
-      if (anVISUActor) {
-       anVISUActor->VisibilityOff();
-      }
-    }
+    SVTK_ViewWindow* vw = GetActiveViewWindow<SVTK_ViewWindow>(theModule);
+    RemoveScalarBarPosition(theModule, vw, thePrs);
+
+    thePrs->RemoveFromStudy();
   }
 
-  //************************************************************
   // Presentation management
 
   void
   ChangeRepresentation (const SalomeApp_Module* theModule,
                         VISU::PresentationType  theType)
   {
-    SUIT_ViewWindow* aView = GetActiveView(theModule, VTKViewer_Viewer::Type());
-    if (!aView) return;
-    SVTK_ViewWindow* vw  = (SVTK_ViewWindow*) aView;
+    SVTK_ViewWindow* vw = GetActiveViewWindow<SVTK_ViewWindow>(theModule);
+    if (!vw)
+      return;
 
     Handle(SALOME_InteractiveObject) anIO;
     CORBA::Object_var anObject = GetSelectedObj(theModule, &anIO);
     if (CORBA::is_nil(anObject)) return;
+
+    VISU::Base_var aVisuObj = VISU::Base::_narrow(anObject);
+    if (CORBA::is_nil(aVisuObj)) return;
+
     PortableServer::ServantBase_var aServant = VISU::GetServant(anObject);
     if (!aServant.in()) return;
 
@@ -474,13 +501,13 @@ namespace VISU
             anActor->UnShrink();
           else
             anActor->SetShrink();
-         break;
+          break;
         default:
-         if (VISU::Mesh_i* aMesh = dynamic_cast<VISU::Mesh_i*>(aPrs3d)) {
-           aMesh->SetPresentationType(theType);
-           RecreateActor(theModule, aMesh);
-         } else {
-           anActor->SetRepresentation(theType);
+          if (VISU::Mesh_i* aMesh = dynamic_cast<VISU::Mesh_i*>(aPrs3d)) {
+            aMesh->SetPresentationType(theType);
+            RecreateActor(theModule, aMesh);
+          } else {
+            anActor->SetRepresentation(theType);
           }
         }
         vw->Repaint();
@@ -488,202 +515,118 @@ namespace VISU
     }
   }
 
-  //************************************************************
+  void
+  SetShading ( const SalomeApp_Module* theModule,
+             bool theOn )
+  {
+    SVTK_ViewWindow* vw = GetActiveViewWindow<SVTK_ViewWindow>(theModule);
+    if (!vw)
+      return;
+
+    Handle(SALOME_InteractiveObject) anIO;
+    CORBA::Object_var anObject = GetSelectedObj(theModule, &anIO);
+    if (CORBA::is_nil(anObject)) return;
+
+    VISU::Base_var aVisuObj = VISU::Base::_narrow(anObject);
+    if (CORBA::is_nil(aVisuObj)) return;
+
+    PortableServer::ServantBase_var aServant = VISU::GetServant(anObject);
+    if (!aServant.in()) return;
+
+    VISU::Prs3d_i* aPrs3d = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
+    if (aPrs3d) {
+      if (VISU_Actor* anActor = GetActor(aPrs3d, vw)) {
+       if ( VISU_ScalarMapAct* aScalarMapActor = dynamic_cast<VISU_ScalarMapAct*>(anActor) )
+         aScalarMapActor->SetShading( theOn );
+      }
+      vw->Repaint();
+    }
+  }
+
   // SObject type
 
   bool
   CheckTimeStamp(const SalomeApp_Module* theModule,
-                SALOMEDS::SObject_var& theSObject,
-                Handle(SALOME_InteractiveObject)* theIO)
+                 _PTR(SObject)&          theSObject,
+                 Handle(SALOME_InteractiveObject)* theIO)
   {
     Handle(SALOME_InteractiveObject) anIO;
-    CORBA::Object_var anObject = GetSelectedObj(theModule,&anIO);
-    if(theIO)
+    CORBA::Object_var anObject = GetSelectedObj(theModule, &anIO);
+    if (theIO)
       *theIO = anIO;
-    if(!anIO.IsNull() && anIO->hasEntry()){
-      SALOMEDS::Study_var aStudy = GetDSStudy(GetCStudy(GetAppStudy(theModule)));
+    if (!anIO.IsNull() && anIO->hasEntry()){
+      _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
       theSObject = aStudy->FindObjectID(anIO->getEntry());
       QString aValue = getValue(theSObject,"myType");
-      if(aValue.toInt() == int(VISU::TTIMESTAMP))
-       return true;
+      if (aValue.toInt() == int(VISU::TTIMESTAMP))
+        return true;
     }
     SUIT_MessageBox::warn1(GetDesktop(theModule),
-                          QObject::tr("WRN_VISU"),
-                          QObject::tr("WRN_NO_AVAILABLE_DATA"),
-                          QObject::tr("BUT_OK") );
+                           QObject::tr("WRN_VISU"),
+                           QObject::tr("WRN_NO_AVAILABLE_DATA"),
+                           QObject::tr("BUT_OK") );
     return false;
   }
 
-
   VISU::Result_i*
   CheckResult(const SalomeApp_Module* theModule,
-             SALOMEDS::SObject_var theSource,
-             VISU::Result_var& theResult)
+              _PTR(SObject)           theSource,
+              VISU::Result_var&       theResult)
   {
-    SALOMEDS::SObject_var aSObj;
-
-    aSObj = theSource->GetFather();
-    if (CORBA::is_nil(aSObj))
+    if(theSource->Depth() < 3) // Bug of SALOMEDS : can\t get father from root object
+      return NULL;
+      
+    _PTR(SObject) aSObj = theSource->GetFather();
+    if (!aSObj)
       return NULL;
 
     aSObj = aSObj->GetFather();
-    if (CORBA::is_nil(aSObj))
+    if (!aSObj)
       return NULL;
 
     aSObj = aSObj->GetFather();
-    if (CORBA::is_nil(aSObj))
+    if (!aSObj)
       return NULL;
 
-    CORBA::Object_var anObject = VISU::SObjectToObject(aSObj);
+    CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObj);
     if (CORBA::is_nil(anObject)) {
       aSObj = aSObj->GetFather();
-      if (CORBA::is_nil(aSObj))
+      if (!aSObj)
         return NULL;
-      anObject = VISU::SObjectToObject(aSObj);
+      anObject = VISU::ClientSObjectToObject(aSObj);
     }
 
     if (CORBA::is_nil(anObject))
       return NULL;
 
     theResult = VISU::Result::_narrow(anObject);
-    VISU::Result_i* pResult = dynamic_cast<VISU::Result_i*>(VISU::GetServant(anObject).in());
-    if(pResult == NULL)
-      SUIT_MessageBox::warn1(GetDesktop(theModule),
-                            QObject::tr("WRN_VISU"),
-                            QObject::tr("WRN_NO_AVAILABLE_DATA"),
-                            QObject::tr("BUT_OK"));
-    return pResult;
-  }
-
-  //************************************************************
-  // Views
-
-  SUIT_ViewWindow* GetActiveView(const SalomeApp_Module* theModule, QString theType)
-  {
-    if(SalomeApp_Application* anApp = theModule->getApp()){
-      if(SUIT_ViewManager* aViewManager = anApp->activeViewManager()){
-       if (!theType.isNull()) {
-         if (aViewManager->getType() != theType)
-           return 0;
-       }
-       return aViewManager->getActiveView();
-      }
-    }
-    return 0;
+    return dynamic_cast<VISU::Result_i*>(VISU::GetServant(anObject).in());
   }
 
-  //************************************************************
   // VTK View
 
-  TViewWindows
-  GetViews(const SalomeApp_Module* theModule)
-  {
-    TViewWindows aViewWindows;
-    if(SalomeApp_Application* anApp = theModule->getApp()){
-      ViewManagerList aViewManagerList;
-      anApp->viewManagers(SVTK_Viewer::Type(),aViewManagerList);
-      QPtrListIterator<SUIT_ViewManager> anIter(aViewManagerList);
-      while(SUIT_ViewManager* aViewManager = anIter.current()){
-       QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
-       for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
-         if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
-           if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
-             aViewWindows.push_back(aView);
-       }
-       ++anIter;
-      }
-    }
-    return aViewWindows;
-  }
-
-  SVTK_ViewWindow*
-  GetViewWindow(const SalomeApp_Module* theModule, const bool theCreate )
-  {
-    if(SalomeApp_Application* anApp = theModule->getApp()){
-      if(SUIT_ViewManager* aViewManager = anApp->getViewManager( SVTK_Viewer::Type(), theCreate )){
-       if(SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()){
-         return dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
-       }
-      }
-    }
-    return NULL;
-  }
-
-
-  SVTK_ViewWindow*
-  GetViewWindow()
-  {
-    SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
-      (SUIT_Session::session()->activeApplication());
-    if (anApp) {
-      if (SUIT_ViewManager* aViewManager = anApp->activeViewManager()) {
-       if (aViewManager->getType() == SVTK_Viewer::Type()) {
-         if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
-           return dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
-         }
-       }
-      }
-    }
-    return NULL;
-  }
-
   VISU_Actor*
-  PublishInView(const SalomeApp_Module* theModule,
-               VISU::Prs3d_i* thePrs)
+  PublishMeshInView(const SalomeApp_Module* theModule,
+                    VISU::Prs3d_i* thePrs,
+                    SVTK_ViewWindow* theViewWindow)
   {
     VISU_Actor* aActor = NULL;
-    if(!thePrs)
+    if (!thePrs || !theViewWindow)
       return aActor;
-    if(SVTK_ViewWindow* aView = GetViewWindow(theModule)){
-      QApplication::setOverrideCursor( Qt::waitCursor );
-      if(aActor = thePrs->CreateActor()){
-       aView->AddActor(aActor);
-      }
-      QApplication::restoreOverrideCursor();
-    }
-    return aActor;
-  }
 
-  VISU_Actor*
-  UpdateViewer(const SalomeApp_Module* theModule,
-              VISU::Prs3d_i* thePrs,
-              bool theDispOnly)
-  {
-    SVTK_ViewWindow* vw = GetViewWindow( theModule );
-    if (!vw) return NULL;
-
-    vtkRenderer *aRen = vw->getRenderer();
-    vtkActorCollection *anActColl = aRen->GetActors();
-
-    vtkActor *anActor;
-    VISU_Actor* anVISUActor = NULL;
-    VISU_Actor* aResActor = NULL;
-    for(anActColl->InitTraversal(); (anActor = anActColl->GetNextActor()) != NULL; ){
-      if(!SALOME_Actor::SafeDownCast(anActor))
-       continue;
-      if(anActor->IsA("VISU_Actor")){
-       anVISUActor = VISU_Actor::SafeDownCast(anActor);
-       VISU::Prs3d_i* aPrs = anVISUActor->GetPrs3d();
-       if(aPrs == NULL) continue;
-       if (thePrs == aPrs) {
-         aResActor = anVISUActor->GetParent();
-         thePrs->UpdateActor(aResActor);
-         aResActor->VisibilityOn();
-
-       } else if (theDispOnly) {
-         anVISUActor->GetParent()->VisibilityOff();
-       } else {
-       }
-      } else if (theDispOnly && anActor->GetVisibility()) {
-       anActor->VisibilityOff();
-      } else {
-      }
+    QApplication::setOverrideCursor( Qt::waitCursor );
+    try {
+      if (aActor = thePrs->CreateActor())
+        theViewWindow->AddActor(aActor);
+    } catch(std::exception& exc) {
+      SUIT_MessageBox::warn1
+        (GetDesktop(theModule), QObject::tr("WRN_VISU"),
+         QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(exc.what()),
+         QObject::tr("BUT_OK"));
     }
-    if (aResActor)
-      return aResActor;
+    QApplication::restoreOverrideCursor();
 
-    anVISUActor = PublishInView( theModule, thePrs );
-    return anVISUActor;
+    return aActor;
   }
 
   void
@@ -696,33 +639,33 @@ namespace VISU
       anApp->viewManagers(SVTK_Viewer::Type(),aViewManagerList);
       QPtrListIterator<SUIT_ViewManager> anIter (aViewManagerList);
       while (SUIT_ViewManager* aViewManager = anIter.current()) {
-       QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
-       for (int i = 0, iEnd = aViews.size(); i < iEnd; i++) {
-         if (SUIT_ViewWindow* aViewWindow = aViews.at(i)) {
-           if (SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
+        QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
+        for (int i = 0, iEnd = aViews.size(); i < iEnd; i++) {
+          if (SUIT_ViewWindow* aViewWindow = aViews.at(i)) {
+            if (SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
               if (vw->isVisible(theIObject)) {
                 vw->getRenderer()->ResetCameraClippingRange();
                 vw->Repaint();
                 vw->highlight(theIObject, true, true);
               }
-           }
-         }
-       }
-       ++anIter;
+            }
+          }
+        }
+        ++anIter;
       }
     }
   }
 
   VISU_Actor*
   FindActor(SVTK_ViewWindow* theViewWindow,
-           const char* theEntry)
+            const char* theEntry)
   {
-    using namespace VTK;
+    using namespace SVTK;
     if(vtkRenderer* aRenderer = theViewWindow->getRenderer()){
       if(vtkActorCollection* aCollection = aRenderer->GetActors()){
-       if(VISU_Actor* anActor = Find<VISU_Actor>(aCollection,TIsSameEntry<VISU_Actor>(theEntry))){
-         return anActor->GetParent();
-       }
+        if(VISU_Actor* anActor = Find<VISU_Actor>(aCollection,TIsSameEntry<VISU_Actor>(theEntry))){
+          return anActor;
+        }
       }
     }
     return NULL;
@@ -730,7 +673,7 @@ namespace VISU
 
   VISU_Actor*
   FindActor(SVTK_ViewWindow* theViewWindow,
-           VISU::Prs3d_i* thePrs)
+            VISU::Prs3d_i* thePrs)
   {
     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
     CORBA::String_var anEntry = aSObject->GetID();
@@ -747,33 +690,102 @@ namespace VISU
     CORBA::String_var anEntry = aSObject->GetID();
 
     try {
-      thePrs->Update();
+      thePrs->UpdateActors();
+    } catch (std::runtime_error& exc) {
+      INFOS(exc.what());
+      QApplication::restoreOverrideCursor();
+      SUIT_MessageBox::warn1
+        (GetDesktop(theModule), QObject::tr("WRN_VISU"),
+         QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()),
+         QObject::tr("BUT_OK"));
 
-      TViewWindows aViewWindows = GetViews(theModule);
-      for (int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++) {
-        SVTK_ViewWindow* aView = aViewWindows[i];
-        if (VISU_Actor* anActor = FindActor(aView, anEntry.in())) {
-         thePrs->UpdateActor(anActor);
-        }
+      thePrs->RemoveActors();
+      return;
+    }
+    QApplication::restoreOverrideCursor();
+  }
+
+  static bool ComputeVisiblePropBounds(SVTK_ViewWindow* theViewWindow,
+                                      float             allBounds[6], 
+                                      const char*       theActorClassName = "VISU_Actor")
+  {
+    vtkRenderer *aRen = theViewWindow->getRenderer();
+    vtkActorCollection *anActColl = aRen->GetActors(); 
+    vtkProp *prop;
+    float   *bounds;
+    int     somethingVisible = false;
+
+    allBounds[0] = allBounds[2] = allBounds[4] = VTK_LARGE_FLOAT;
+    allBounds[1] = allBounds[3] = allBounds[5] = -VTK_LARGE_FLOAT;
+    // loop through all props
+    for (anActColl->InitTraversal(); (prop = anActColl->GetNextProp()); ) {
+      // if it's invisible, or has no geometry, we can skip the rest 
+      if (prop->GetVisibility() && prop->IsA(theActorClassName)) {
+        bounds = prop->GetBounds();
+        // make sure we haven't got bogus bounds
+        if (bounds != NULL &&
+            bounds[0] > -VTK_LARGE_FLOAT && bounds[1] < VTK_LARGE_FLOAT &&
+            bounds[2] > -VTK_LARGE_FLOAT && bounds[3] < VTK_LARGE_FLOAT &&
+            bounds[4] > -VTK_LARGE_FLOAT && bounds[5] < VTK_LARGE_FLOAT)
+        {
+         somethingVisible = true;
+         if (bounds[0] < allBounds[0]) allBounds[0] = bounds[0]; 
+         if (bounds[1] > allBounds[1]) allBounds[1] = bounds[1]; 
+         if (bounds[2] < allBounds[2]) allBounds[2] = bounds[2]; 
+         if (bounds[3] > allBounds[3]) allBounds[3] = bounds[3]; 
+         if (bounds[4] < allBounds[4]) allBounds[4] = bounds[4]; 
+         if (bounds[5] > allBounds[5]) allBounds[5] = bounds[5]; 
+        }//not bogus
       }
-    } catch (std::runtime_error& ex) {
-      INFOS(ex.what());
-      QApplication::restoreOverrideCursor();
-      SUIT_MessageBox::warn1 (GetDesktop(theModule), QObject::tr("WRN_VISU"),
-                              QObject::tr("ERR_CANT_BUILD_PRESENTATION") + " " + QObject::tr(ex.what()),
-                              QObject::tr("BUT_OK"));
+    }
+    return somethingVisible;
+  }
 
-      TViewWindows aViewWindows = GetViews(theModule);
-      for (int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++) {
-        SVTK_ViewWindow* aView = aViewWindows[i];
-        if (VISU_Actor* anActor = FindActor(aView, anEntry.in())) {
-         aView->RemoveActor(anActor);
-         anActor->Delete();
+  void SetFitAll(SVTK_ViewWindow* theViewWindow)
+  {
+    static float PRECISION = 0.000001;
+    static float DEVIATION = 600;
+    float XYZ_Bnd[6];
+    if (!ComputeVisiblePropBounds(theViewWindow, XYZ_Bnd)) return;
+
+    float absX = XYZ_Bnd[1] - XYZ_Bnd[0];
+    float absY = XYZ_Bnd[3] - XYZ_Bnd[2];
+    float absZ = XYZ_Bnd[5] - XYZ_Bnd[4];
+
+    enum CameraOrient {e3D, eFront, eLeft, eTop};
+    CameraOrient aCameraOrient = e3D;
+    if (absX <= PRECISION) aCameraOrient = eFront;
+    else {
+      if (absY <= PRECISION) aCameraOrient = eLeft;
+      else {
+        if (absZ <= PRECISION) aCameraOrient = eTop;
+        else {
+          // all the three dimensions exceeds precision
+          float dev_abs_XY = absX / absY;
+          float dev_abs_YZ = absY / absZ;
+          float dev_abs_XZ = absX / absZ;
+          if (dev_abs_XY >= DEVIATION || 1./dev_abs_YZ >= DEVIATION)
+            aCameraOrient = eLeft;
+          else {
+            if (1./dev_abs_XY >= DEVIATION || 1./dev_abs_XZ >= DEVIATION)
+              aCameraOrient = eFront;
+            else {
+              if (dev_abs_XZ >= DEVIATION || dev_abs_YZ >= DEVIATION)
+                aCameraOrient = eTop;
+            }
+          }
         }
       }
-      return;
     }
-    QApplication::restoreOverrideCursor();
+
+    switch (aCameraOrient) {
+    case eFront: theViewWindow->onFrontView(); break;
+    case eLeft:  theViewWindow->onLeftView();  break;
+    case eTop:   theViewWindow->onTopView();   break;
+    case e3D:    theViewWindow->onResetView(); break;
+    }
+    theViewWindow->getRenderer()->ResetCameraClippingRange();
+    theViewWindow->onFitAll();
   }
 
   //************************************************************
@@ -784,7 +796,7 @@ namespace VISU
   {
     if(SalomeApp_Application* anApp = theModule->getApp()){
       if(SUIT_ViewManager* aViewManager = anApp->getViewManager( Plot2d_Viewer::Type(), theCreate )){
-       return dynamic_cast<SPlot2d_Viewer*>(aViewManager->getViewModel());
+        return dynamic_cast<SPlot2d_Viewer*>(aViewManager->getViewModel());
       }
     }
     return NULL;
@@ -793,57 +805,59 @@ namespace VISU
   // Internal function used by several public functions below
   void
   UpdateCurve(VISU::Curve_i* theCurve,
-             Plot2d_ViewFrame* aPlot,
-             SPlot2d_Curve* plotCurve,
-             int theDisplaying)
+              Plot2d_ViewFrame* aPlot,
+              SPlot2d_Curve* plotCurve,
+              int theDisplaying)
   {
     if ( theDisplaying == VISU::eErase ) {
-      if ( plotCurve )
-       aPlot->eraseCurve( plotCurve, false );
+      if ( plotCurve && aPlot )
+        aPlot->eraseCurve( plotCurve, false );
     }
     else if ( theDisplaying == VISU::eDisplay || theDisplaying == VISU::eDisplayOnly ) {
       if ( plotCurve ) {
-       plotCurve->setHorTitle( theCurve->GetHorTitle().c_str() );
-       //plotCurve->setVerTitle( ( theCurve->GetVerTitle().c_str() ) );
-       plotCurve->setVerTitle( theCurve->GetName() );
-       plotCurve->setHorUnits( theCurve->GetHorUnits().c_str() );
-       plotCurve->setVerUnits( theCurve->GetVerUnits().c_str() );
-       double* xList = 0;
-       double* yList = 0;
-       int     nbPoints = theCurve->GetData( xList, yList );
-       if ( nbPoints > 0 && xList && yList ) {
-         plotCurve->setData( xList, yList, nbPoints );
-       }
-       if ( !theCurve->IsAuto() ) {
-         plotCurve->setLine( (Plot2d_Curve::LineType)theCurve->GetLine(), theCurve->GetLineWidth() );
-         plotCurve->setMarker( (Plot2d_Curve::MarkerType)theCurve->GetMarker() );
-         SALOMEDS::Color color = theCurve->GetColor();
-         plotCurve->setColor( QColor( (int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.) ) );
-       }
-       plotCurve->setAutoAssign( theCurve->IsAuto() );
-       aPlot->displayCurve( plotCurve, false );
+        plotCurve->setHorTitle( theCurve->GetHorTitle().c_str() );
+        //plotCurve->setVerTitle( ( theCurve->GetVerTitle().c_str() ) );
+        plotCurve->setVerTitle( theCurve->GetName() );
+        plotCurve->setHorUnits( theCurve->GetHorUnits().c_str() );
+        plotCurve->setVerUnits( theCurve->GetVerUnits().c_str() );
+        double* xList = 0;
+        double* yList = 0;
+        int     nbPoints = theCurve->GetData( xList, yList );
+        if ( nbPoints > 0 && xList && yList ) {
+          plotCurve->setData( xList, yList, nbPoints );
+        }
+        if ( !theCurve->IsAuto() ) {
+          plotCurve->setLine( (Plot2d_Curve::LineType)theCurve->GetLine(), theCurve->GetLineWidth() );
+          plotCurve->setMarker( (Plot2d_Curve::MarkerType)theCurve->GetMarker() );
+          SALOMEDS::Color color = theCurve->GetColor();
+          plotCurve->setColor( QColor( (int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.) ) );
+        }
+        plotCurve->setAutoAssign( theCurve->IsAuto() );
+       if( aPlot )
+         aPlot->displayCurve( plotCurve, false );
       }
       else {
-       Plot2d_Curve* crv = theCurve->CreatePresentation();
-       if ( crv ) {
-         aPlot->displayCurve( crv, false );
-         theCurve->SetLine( (VISU::Curve::LineType)crv->getLine(), crv->getLineWidth() );
-         theCurve->SetMarker( (VISU::Curve::MarkerType)crv->getMarker());
-         SALOMEDS::Color newColor;
-         newColor.R = crv->getColor().red()/255.;
-         newColor.G = crv->getColor().green()/255.;
-         newColor.B = crv->getColor().blue()/255.;
-         theCurve->SetColor( newColor );
-         crv->setAutoAssign( theCurve->IsAuto() );
-       }
+        Plot2d_Curve* crv = theCurve->CreatePresentation();
+        if ( crv ) {
+         if( aPlot )
+           aPlot->displayCurve( crv, false );
+          theCurve->SetLine( (VISU::Curve::LineType)crv->getLine(), crv->getLineWidth() );
+          theCurve->SetMarker( (VISU::Curve::MarkerType)crv->getMarker());
+          SALOMEDS::Color newColor;
+          newColor.R = crv->getColor().red()/255.;
+          newColor.G = crv->getColor().green()/255.;
+          newColor.B = crv->getColor().blue()/255.;
+          theCurve->SetColor( newColor );
+          crv->setAutoAssign( theCurve->IsAuto() );
+        }
       }
     }
   }
 
   void
   PlotTable(const SalomeApp_Module* theModule,
-           VISU::Table_i* table,
-           int theDisplaying)
+            VISU::Table_i* table,
+            int theDisplaying)
   {
     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true ); // create if necessary
     if ( !aView )
@@ -861,29 +875,29 @@ namespace VISU
     if ( TableSO ) {
       _PTR(ChildIterator) Iter = aStudy->NewChildIterator( TableSO );
       for ( ; Iter->More(); Iter->Next() ) {
-       CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
-       if( !CORBA::is_nil( childObject ) ) {
-         CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
-         if( !CORBA::is_nil( aCurve ) ) {
-           VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
-           SPlot2d_Curve* plotCurve = 0;
-           SPlot2d_Curve* tmpCurve;
-           for ( int i = 0; i < clist.count(); i++ ) {
-             tmpCurve = dynamic_cast<SPlot2d_Curve*>( clist.at( i ) );
-             if (tmpCurve && tmpCurve->hasIO() &&
+        CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
+        if( !CORBA::is_nil( childObject ) ) {
+          CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
+          if( !CORBA::is_nil( aCurve ) ) {
+            VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
+            SPlot2d_Curve* plotCurve = 0;
+            SPlot2d_Curve* tmpCurve;
+            for ( int i = 0; i < clist.count(); i++ ) {
+              tmpCurve = dynamic_cast<SPlot2d_Curve*>( clist.at( i ) );
+              if (tmpCurve && tmpCurve->hasIO() &&
                   !strcmp(tmpCurve->getIO()->getEntry(), theCurve->GetEntry())) {
-               plotCurve = tmpCurve;
-               break;
-             }
-           }
+                plotCurve = tmpCurve;
+                break;
+              }
+            }
 
-           UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
+            UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
 
-           if ( theDisplaying == VISU::eErase && plotCurve ) {
-             clist.remove( plotCurve );
-           }
-         }
-       }
+            if ( theDisplaying == VISU::eErase && plotCurve ) {
+              clist.remove( plotCurve );
+            }
+          }
+        }
       }
       aPlot->Repaint();
     }
@@ -891,8 +905,8 @@ namespace VISU
 
   void
   PlotCurve(const SalomeApp_Module* theModule,
-           VISU::Curve_i* theCurve,
-           int theDisplaying)
+            VISU::Curve_i* theCurve,
+            int theDisplaying)
   {
     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true );
     if ( !aView )
@@ -911,9 +925,9 @@ namespace VISU
       tmpCurve = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
       if (tmpCurve && tmpCurve->hasIO() &&
           !strcmp(tmpCurve->getIO()->getEntry(), theCurve->GetEntry())) {
-       plotCurve = tmpCurve;
+        plotCurve = tmpCurve;
       } else if (theDisplaying == VISU::eDisplayOnly) {
-       aPlot->eraseCurve(clist.at(i));
+        aPlot->eraseCurve(clist.at(i));
       }
     }
 
@@ -924,8 +938,8 @@ namespace VISU
 
   void
   PlotContainer(const SalomeApp_Module* theModule,
-               VISU::Container_i* container,
-               int theDisplaying)
+                VISU::Container_i* container,
+                int theDisplaying)
   {
     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true );
     if ( !aView )
@@ -941,17 +955,17 @@ namespace VISU
     if ( container->GetNbCurves() > 0 ) {
       int nbCurves = container->GetNbCurves();
       for ( int k = 1; k <= nbCurves; k++ ) {
-       VISU::Curve_i* theCurve = container->GetCurve( k );
-       if ( theCurve && theCurve->IsValid() ) {
-         SPlot2d_Curve* plotCurve = dynamic_cast<SPlot2d_Curve*>
+        VISU::Curve_i* theCurve = container->GetCurve( k );
+        if ( theCurve && theCurve->IsValid() ) {
+          SPlot2d_Curve* plotCurve = dynamic_cast<SPlot2d_Curve*>
             (aView->getCurveByIO(new SALOME_InteractiveObject (theCurve->GetEntry(), "", "")));
 
-         UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
+          UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
 
-         if ( plotCurve && theDisplaying == VISU::eErase ) {
-           clist.remove( plotCurve );
-         }
-       }
+          if ( plotCurve && theDisplaying == VISU::eErase ) {
+            clist.remove( plotCurve );
+          }
+        }
       }
     }
     aPlot->Repaint();
@@ -959,28 +973,33 @@ namespace VISU
 
   void
   CreatePlot(SalomeApp_Module* theModule,
-            _PTR(SObject) theTableSO)
+             _PTR(SObject) theTableSO)
   {
     if ( IsSObjectTable(theTableSO) ) {
       CORBA::Object_var aTable = VISU::ClientSObjectToObject(theTableSO);
       CORBA::Object_var aContainer = GetVisuGen( theModule )->CreateContainer();
       if ( !CORBA::is_nil( aTable ) && !CORBA::is_nil( aContainer ) ) {
-       VISU::Table_i*     pTable     = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aTable).in());
-       VISU::Container_i* pContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aContainer).in());
-
-       if ( pContainer && pTable ) {
-         for ( int i = 2; i <= pTable->GetNbRows(); i++ ) {
-           CORBA::Object_var aNewCurve = GetVisuGen( theModule )->CreateCurve( pTable->_this(), 1, i );
-           if( !CORBA::is_nil( aNewCurve ) ) {
-             VISU::Curve_i* pCrv = dynamic_cast<VISU::Curve_i*>( VISU::GetServant(aNewCurve).in() );
-             if ( pCrv ) {
-               pContainer->AddCurve( pCrv->_this() );
-             }
-           }
-         }
-         theModule->updateObjBrowser();
-         PlotContainer( theModule, pContainer, VISU::eDisplay );
-       }
+        VISU::Table_i*     pTable     = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aTable).in());
+        VISU::Container_i* pContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aContainer).in());
+
+        if ( pContainer && pTable ) {
+          for ( int i = 2; i <= pTable->GetNbRows(); i++ ) {
+            CORBA::Object_var aNewCurve = GetVisuGen( theModule )->CreateCurve( pTable->_this(), 1, i );
+            if( !CORBA::is_nil( aNewCurve ) ) {
+              VISU::Curve_i* pCrv = dynamic_cast<VISU::Curve_i*>( VISU::GetServant(aNewCurve).in() );
+              if ( pCrv ) {
+                pContainer->AddCurve( pCrv->_this() );
+              }
+            }
+          }
+          PlotContainer( theModule, pContainer, VISU::eDisplay );
+
+         QString anEntry = pContainer->GetEntry();
+         _PTR(Study) aStudy = theTableSO->GetStudy();
+         _PTR(SObject) aContainerSO = aStudy->FindObjectID(anEntry.latin1());
+         _PTR(SObject) aParentSO = aContainerSO->GetFather();
+         UpdateObjBrowser(theModule,true,aParentSO);
+        }
       }
     }
   }
@@ -989,14 +1008,14 @@ namespace VISU
   // Others
 
   void CreateMesh (const SalomeApp_Module* theModule,
-                   const Handle(SALOME_InteractiveObject)& theIO)
+                   const Handle(SALOME_InteractiveObject)& theIO,
+                   SVTK_ViewWindow* theViewWindow)
   {
     _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
     //if (CheckLock(aStudy))
     //  return;
 
-    SALOMEDS::SObject_var aResultSObj =
-      GetDSStudy(aStudy)->FindObjectID(theIO->getEntry());
+    _PTR(SObject) aResultSObj = aStudy->FindObjectID(theIO->getEntry());
 
     // Get VISU::Result
     VISU::Result_var aResult;
@@ -1037,7 +1056,7 @@ namespace VISU
     timer.Show();
 #endif
 
-    QApplication::restoreOverrideCursor();
+    //QApplication::restoreOverrideCursor();
     VISU::Mesh_i* pPresent = NULL;
     if (!CORBA::is_nil(aMesh))
       pPresent = dynamic_cast<VISU::Mesh_i*>(VISU::GetServant(aMesh).in());
@@ -1049,28 +1068,31 @@ namespace VISU
       return;
     }
 
-    if (SVTK_ViewWindow* aView = GetViewWindow(theModule)) {
+    if (theViewWindow) {
       try {
 #ifdef CHECKTIME
         Utils_Timer timer;
         timer.Start();
 #endif
-        PublishInView(theModule, pPresent);
-        aView->onFitAll();
+        PublishMeshInView(theModule, pPresent, theViewWindow);
+        SetFitAll(theViewWindow);
 #ifdef CHECKTIME
         timer.Stop();
         MESSAGE("VisuGUI::CreateMesh() - DISPLAY MESH");
         timer.Show();
 #endif
-        theModule->application()->putInfo(QObject::tr("INF_DONE"));
       } catch (std::runtime_error& exc) {
         INFOS(exc.what());
-        SUIT_MessageBox::warn1 (GetDesktop(theModule),
-                                QObject::tr("WRN_VISU"),
-                                QObject::tr("ERR_CANT_CREATE_ACTOR") + " " + QObject::tr(exc.what()),
-                                QObject::tr("BUT_OK"));
+        SUIT_MessageBox::warn1
+          (GetDesktop(theModule), QObject::tr("WRN_VISU"),
+           QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(exc.what()),
+           QObject::tr("BUT_OK"));
       }
     }
+
+    theModule->application()->putInfo(QObject::tr("INF_DONE"));
+    // Make "Save" button active
+    theModule->getApp()->updateActions();
   }
 
   // ========================================================================================
@@ -1082,116 +1104,165 @@ namespace VISU
     std::vector<VISU::Prs3d_i*> aList;
     if (!theIO.IsNull() && theIO->hasEntry()) {
       _PTR(Study) aCStudy = GetCStudy(GetAppStudy(theModule));
-      SALOMEDS::Study_var aStudy = GetDSStudy(aCStudy);
-      SALOMEDS::SObject_var aSObject = aStudy->FindObjectID(theIO->getEntry());
+      _PTR(SObject) aSObject = aCStudy->FindObjectID(theIO->getEntry());
       aList = GetPrs3dList(theModule, aSObject);
     }
     return aList;
   }
 
   std::vector<VISU::Prs3d_i*> GetPrs3dList (const SalomeApp_Module* theModule,
-                                            SALOMEDS::SObject_ptr theObject)
+                                            _PTR(SObject) theObject)
   {
+    std::vector<VISU::Prs3d_i*> aList; int k = 0;
+    if (!theObject)
+      return aList;
+
     _PTR(Study) aCStudy = GetCStudy(GetAppStudy(theModule));
-    SALOMEDS::Study_var aStudy = GetDSStudy(aCStudy);
 
-    std::vector<VISU::Prs3d_i*> aList; int k = 0;
-    if (!theObject->_is_nil()) {
-      CORBA::Object_var aCORBAObject = VISU::SObjectToObject(theObject);
-      if (!CORBA::is_nil(aCORBAObject)) {
-        VISU::Base_var aVisuObj = VISU::Base::_narrow(aCORBAObject);
-        if (!CORBA::is_nil(aVisuObj)) {
-         VISU::VISUType aType = aVisuObj->GetType();
-         switch (aType) {
-         case VISU::TSCALARMAP:
-         case VISU::TISOSURFACE:
-         case VISU::TDEFORMEDSHAPE:
-         case VISU::TCUTPLANES:
-         case VISU::TCUTLINES:
-         case VISU::TVECTORS:
-         case VISU::TSTREAMLINES:
-         case VISU::TPLOT3D:
-           {
-             PortableServer::ServantBase_var aServant = VISU::GetServant(aCORBAObject);
-             if (aServant.in()) {
-               VISU::Prs3d_i* aPrsObject = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
-               aList.resize(k+1); aList[k] = aPrsObject; k++;
-             }
-           }
-           break;
-         case VISU::TFIELD:
-           {
-             SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theObject);
-             SALOMEDS::SObject_var aTimeStamp;
-             anIter->Next(); // First is reference on support
-             for(;anIter->More();anIter->Next()) {
-               aTimeStamp = anIter->Value();
-                if (aTimeStamp->_is_nil()) continue;
+    CORBA::Object_var aCORBAObject = VISU::ClientSObjectToObject(theObject);
+    if (!CORBA::is_nil(aCORBAObject)) {
+      VISU::Base_var aVisuObj = VISU::Base::_narrow(aCORBAObject);
+      if (!CORBA::is_nil(aVisuObj)) {
+        VISU::VISUType aType = aVisuObj->GetType();
+        switch (aType) {
+        case VISU::TSCALARMAP:
+        case VISU::TISOSURFACE:
+        case VISU::TDEFORMEDSHAPE:
+        case VISU::TCUTPLANES:
+        case VISU::TCUTLINES:
+        case VISU::TVECTORS:
+        case VISU::TSTREAMLINES:
+        case VISU::TPLOT3D:
+       case VISU::TSCALARMAPONDEFORMEDSHAPE:
+          {
+            PortableServer::ServantBase_var aServant = VISU::GetServant(aCORBAObject);
+            if (aServant.in()) {
+              VISU::Prs3d_i* aPrsObject = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
+              aList.resize(k+1);
+              aList[k] = aPrsObject;
+              k++;
+            }
+          }
+          break;
+        case VISU::TFIELD:
+          {
+            _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
+            _PTR(SObject) aTimeStamp;
+            anIter->Next(); // First is reference on support
+            for (; anIter->More(); anIter->Next()) {
+              aTimeStamp = anIter->Value();
+              if (!aTimeStamp) continue;
+              std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aTimeStamp);
+              if (!aSubList.empty()) {
+                int n = aSubList.size();
+                aList.resize(k+n);
+                for (int i = 0; i < n; i++) {
+                  aList[k] = aSubList[i];
+                  k++;
+                }
+              }
+            }
+          }
+          break;
+        }
+      }
+    } else {
+      _PTR(GenericAttribute) anAttr;
+      if (theObject->FindAttribute(anAttr, "AttributeComment")) {
+        _PTR(AttributeComment) aComment (anAttr);
+        string aComm = aComment->Value();
+        QString strIn (aComm.c_str());
+        VISU::Storable::TRestoringMap pMap;
+        VISU::Storable::StrToMap(strIn, pMap);
+        bool isExist;
+        VISU::VISUType aType =
+          (VISU::VISUType)VISU::Storable::FindValue(pMap,"myType",&isExist).toInt();
+        if (isExist) {
+          switch (aType) {
+          case VISU::TFIELD:
+            {
+              _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
+              _PTR(SObject) aTimeStamp;
+              anIter->Next(); // First is reference on support
+              for (; anIter->More(); anIter->Next()) {
+                aTimeStamp = anIter->Value();
+                if (!aTimeStamp) continue;
                 std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aTimeStamp);
                 if (!aSubList.empty()) {
-                 int n = aSubList.size();
+                  int n = aSubList.size();
                   aList.resize(k+n);
-                  for (int i = 0; i < n; i++)
-                   { aList[k] = aSubList[i]; k++; }
-               }
-             }
-           }
-           break;
-         }
-        }
-      } else {
-        SALOMEDS::GenericAttribute_var anAttr;
-        if (theObject->FindAttribute(anAttr, "AttributeComment")) {
-         SALOMEDS::AttributeComment_var aComment =
-            SALOMEDS::AttributeComment::_narrow(anAttr);
-         string aComm = aComment->Value();
-         QString strIn( aComm.c_str() );
-         VISU::Storable::TRestoringMap pMap;
-         VISU::Storable::StrToMap( strIn, pMap );
-         bool isExist;
-         VISU::VISUType aType = (VISU::VISUType)VISU::Storable::FindValue(pMap,"myType",&isExist).toInt();
-         if (isExist) {
-           switch (aType) {
-           case VISU::TFIELD:
-             {
-               SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theObject);
-               SALOMEDS::SObject_var aTimeStamp;
-               anIter->Next(); // First is reference on support
-               for (;anIter->More();anIter->Next()) {
-                 aTimeStamp = anIter->Value();
-                  if (aTimeStamp->_is_nil()) continue;
-                  std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aTimeStamp);
-                  if (!aSubList.empty()) {
-                   int n = aSubList.size();
-                    aList.resize(k+n);
-                    for (int i = 0; i < n; i++)
-                     { aList[k] = aSubList[i]; k++; }
-                 }
-               }
-             }
-             break;
-           case VISU::TTIMESTAMP:
-             {
-               SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theObject);
-               SALOMEDS::SObject_var aPrs;
-               for (;anIter->More();anIter->Next()) {
-                 aPrs = anIter->Value();
-                  if (aPrs->_is_nil()) continue;
-                  std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aPrs);
-                  if (!aSubList.empty()) {
-                   int n = aSubList.size();
-                    aList.resize(k+n);
-                    for (int i = 0; i < n; i++)
-                     { aList[k] = aSubList[i]; k++; }
-                 }
-               }
-             }
-             break;
-           }
-         }
+                  for (int i = 0; i < n; i++) {
+                    aList[k] = aSubList[i];
+                    k++;
+                  }
+                }
+              }
+            }
+            break;
+          case VISU::TTIMESTAMP:
+            {
+              _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
+              _PTR(SObject) aPrs;
+              for (; anIter->More(); anIter->Next()) {
+                aPrs = anIter->Value();
+                if (!aPrs) continue;
+                std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aPrs);
+                if (!aSubList.empty()) {
+                  int n = aSubList.size();
+                  aList.resize(k+n);
+                  for (int i = 0; i < n; i++) {
+                    aList[k] = aSubList[i];
+                    k++;
+                  }
+                }
+              }
+            }
+            break;
+          }
         }
       }
     }
     return aList;
   }
+
+  int GetFreePositionOfDefaultScalarBar(VisuGUI* theModule, SVTK_ViewWindow* theViewWindow)
+  {
+    int minIndx = 1;
+    std::set<int> aIndexes;
+    TViewToPrs3d aMap = theModule->getScalarBarsMap();
+    TViewToPrs3d::const_iterator aViewToPrsIter = aMap.find(theViewWindow);
+    if (aViewToPrsIter != aMap.end()) {
+      TSetPrs3d::const_iterator aPrsIter = (aViewToPrsIter->second).begin();
+      for (; aPrsIter != (aViewToPrsIter->second).end(); aPrsIter++) {
+       aIndexes.insert((*aPrsIter).second);
+      }
+    }
+    std::set<int>::const_iterator aIter = aIndexes.begin();
+    for (int i = 1,length = aIndexes.size(); i <= length; i++) {
+      std::set<int>::const_iterator aIter = aIndexes.find(i);
+      if (aIter == aIndexes.end()) { minIndx = i; break; } 
+      else minIndx = i + 1;
+    }
+    return minIndx;
+  }
+
+  void AddScalarBarPosition (VisuGUI* theModule, SVTK_ViewWindow* theViewWindow,
+                             VISU::Prs3d_i* thePrs3d, int pos)
+  {
+    TViewToPrs3d& aMap = theModule->myScalarBarsMap;
+    TPrs3dToInd aPair; aPair.first = thePrs3d; aPair.second = pos;
+    aMap[theViewWindow].insert(aPair);
+  }
+
+  void RemoveScalarBarPosition(VisuGUI* theModule, SVTK_ViewWindow* theViewWindow,
+                               VISU::Prs3d_i* thePrs3d)
+  {
+    TViewToPrs3d& aMap = theModule->myScalarBarsMap;
+    TSetPrs3d::iterator aIter = aMap[theViewWindow].begin();
+    for (; aIter != aMap[theViewWindow].end(); aIter++)
+      if ((*aIter).first == thePrs3d) {
+       aMap[theViewWindow].erase(*aIter);
+       return;
+      }
+  }
 }