Salome HOME
Update Help for VISU module.
[modules/visu.git] / src / VISUGUI / VisuGUI_Tools.cxx
index 246590070e6f1abe50e8c1f0f4533251c3e1964d..83eab7448da805b5fd5b2f614c54af15ac54f798 100644 (file)
 #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_SelectionMgr.h"
 
 #include "SALOME_ListIO.hxx"
 #include "SALOME_ListIteratorOfListIO.hxx"
@@ -70,7 +71,7 @@ namespace VISU
     return theModule->application()->desktop();
   }
 
-  SalomeApp_SelectionMgr*
+  LightApp_SelectionMgr*
   GetSelectionMgr(const SalomeApp_Module* theModule)
   {
     return theModule->getApp()->selectionMgr();
@@ -101,7 +102,18 @@ namespace VISU
   CheckLock( _PTR(Study) theStudy )
   {
     if(IsStudyLocked(theStudy))
-      throw std::runtime_error(QObject::tr("WRN_STUDY_LOCKED").latin1());
+      {
+       SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
+         (SUIT_Session::session()->activeApplication());
+       if (anApp)
+         {
+           SUIT_MessageBox::warn1(anApp->desktop(),
+                                   QObject::tr("WRN_VISU_WARNING"),
+                                   QObject::tr("WRN_STUDY_LOCKED"),
+                                   QObject::tr("BUT_OK"));
+           return true;
+         }
+      }
     return false;
   }
 
@@ -192,7 +204,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);
@@ -213,20 +225,20 @@ namespace VISU
   {
     if (CheckLock(GetCStudy(GetAppStudy(theModule))))
       return NULL;
-    
+
     CORBA::Object_var anObject = GetSelectedObj(theModule, theIO);
-    if (CORBA::is_nil(anObject)) 
+    if (CORBA::is_nil(anObject))
       return NULL;
 
     PortableServer::ServantBase_var aServant = VISU::GetServant(anObject);
-    if (!aServant.in()) 
+    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;
@@ -236,7 +248,7 @@ namespace VISU
   }
 
   void
-  Remove(SalomeApp_SelectionMgr* theSelectionMgr,
+  Remove(LightApp_SelectionMgr* theSelectionMgr,
         const Handle(SALOME_InteractiveObject)& theIO)
   {
     if (theIO.IsNull()) return;
@@ -261,7 +273,7 @@ namespace VISU
   bool
   IsRemovableSelected (const SalomeApp_Module* theModule)
   {
-    SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
+    LightApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
     SALOME_ListIO aListIO;
     aSelectionMgr->selectedObjects(aListIO);
 
@@ -392,12 +404,11 @@ namespace VISU
                  _PTR(SObject)     theSObject)
   {
     SalomeApp_Study* study = GetAppStudy( theModule );
-    study->deleteReferencesTo( theSObject );
 
     _PTR(ChildIterator) aChildIter = theStudy->NewChildIterator(theSObject);
     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
       _PTR(SObject) aChildSObject = aChildIter->Value();
-      study->deleteReferencesTo( aChildSObject );
+      
       CORBA::Object_var aChildObj = VISU::ClientSObjectToObject(aChildSObject);
       ErasePrs(theModule, aChildObj);
     }
@@ -412,7 +423,7 @@ namespace VISU
       }
     } 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
@@ -430,7 +441,7 @@ namespace VISU
       return;
     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
     CORBA::String_var anEntry = aSObject->GetID();
-    SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
+    LightApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
     Remove(aSelectionMgr,theIO);
     TViewWindows aViewWindows = GetViews(theModule);
     for(int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++){
@@ -462,13 +473,19 @@ namespace VISU
   ChangeRepresentation (const SalomeApp_Module* theModule,
                         VISU::PresentationType  theType)
   {
-    SUIT_ViewWindow* aView = GetActiveView(theModule, VTKViewer_Viewer::Type());
+    SUIT_ViewWindow* aView = GetActiveView(theModule, SVTK_Viewer::Type());
     if (!aView) return;
-    SVTK_ViewWindow* vw  = (SVTK_ViewWindow*) aView;
+    SVTK_ViewWindow* vw  = dynamic_cast<SVTK_ViewWindow*>( aView );
+    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;
 
@@ -494,6 +511,36 @@ namespace VISU
       }
     }
   }
+  
+  void
+  SetShading ( const SalomeApp_Module* theModule,
+             bool theOn )
+  {
+    SUIT_ViewWindow* aView = GetActiveView(theModule, SVTK_Viewer::Type());
+    if (!aView) return;
+    SVTK_ViewWindow* vw  = dynamic_cast<SVTK_ViewWindow*>( aView );
+    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
@@ -601,6 +648,23 @@ namespace VISU
   }
 
   SVTK_ViewWindow*
+  GetViewWindow( const SalomeApp_Module* theModule, const bool theCreate )
+  {
+    if (SalomeApp_Application* anApp = theModule->getApp())
+    {
+      SVTK_ViewWindow* wnd = dynamic_cast<SVTK_ViewWindow*>(anApp->desktop()->activeWindow());
+      if( wnd )
+       return wnd;
+      else
+      {
+       SUIT_ViewManager* aViewManager = anApp->getViewManager( SVTK_Viewer::Type(), theCreate );
+       return aViewManager ? dynamic_cast<SVTK_ViewWindow*>( aViewManager->getActiveView() ) : 0;
+      }
+    }
+    return NULL;
+  }
+
+  /*SVTK_ViewWindow*
   GetViewWindow(const SalomeApp_Module* theModule, const bool theCreate )
   {
     if(SalomeApp_Application* anApp = theModule->getApp()){
@@ -611,7 +675,7 @@ namespace VISU
       }
     }
     return NULL;
-  }
+  }*/
 
 
   SVTK_ViewWindow*
@@ -646,7 +710,7 @@ namespace VISU
       }catch(std::exception& exc){
        SUIT_MessageBox::warn1(GetDesktop(theModule),
                               QObject::tr("WRN_VISU"),
-                              QObject::tr("ERR_CANT_CREATE_ACTOR"),
+                              QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(exc.what()),
                               QObject::tr("BUT_OK"));
       }
       QApplication::restoreOverrideCursor();
@@ -677,9 +741,16 @@ namespace VISU
         if(aPrs == NULL) continue;
         if (thePrs == aPrs) {
           aResActor = anVISUActor->GetParent();
-          thePrs->UpdateActor(aResActor);
-          aResActor->VisibilityOn();
-
+          try {
+            thePrs->UpdateActor(aResActor);
+            aResActor->VisibilityOn();
+          } catch (std::runtime_error& ex) {
+            aResActor->VisibilityOff();
+            INFOS(ex.what());
+            SUIT_MessageBox::warn1(GetDesktop(theModule), QObject::tr("WRN_VISU"),
+                                   QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(ex.what()),
+                                   QObject::tr("BUT_OK"));
+          }
         } else if (theDispOnly) {
           anVISUActor->GetParent()->VisibilityOff();
         } else {
@@ -692,7 +763,7 @@ namespace VISU
     if (aResActor)
       return aResActor;
 
-    anVISUActor = PublishInView( theModule, thePrs );
+    //anVISUActor = PublishInView( theModule, thePrs );
     return anVISUActor;
   }
 
@@ -752,6 +823,7 @@ namespace VISU
                  VISU::Prs3d_i* thePrs)
   {
     QApplication::setOverrideCursor(Qt::waitCursor);
+    bool isPublished = false;
 
     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
     CORBA::String_var anEntry = aSObject->GetID();
@@ -763,6 +835,7 @@ namespace VISU
       for (int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++) {
         SVTK_ViewWindow* aView = aViewWindows[i];
         if (VISU_Actor* anActor = FindActor(aView, anEntry.in())) {
+          isPublished = true;
           thePrs->UpdateActor(anActor);
         }
       }
@@ -770,7 +843,7 @@ namespace VISU
       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("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(ex.what()),
                               QObject::tr("BUT_OK"));
 
       TViewWindows aViewWindows = GetViews(theModule);
@@ -784,6 +857,92 @@ namespace VISU
       return;
     }
     QApplication::restoreOverrideCursor();
+
+    if (!isPublished)
+      PublishInView(theModule, thePrs);
+  }
+
+  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
+      }
+    }
+    return somethingVisible;
+  }
+
+  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;
+            }
+          }
+        }
+      }
+    }
+
+    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();
   }
 
   //************************************************************
@@ -808,7 +967,7 @@ namespace VISU
               int theDisplaying)
   {
     if ( theDisplaying == VISU::eErase ) {
-      if ( plotCurve )
+      if ( plotCurve && aPlot )
         aPlot->eraseCurve( plotCurve, false );
     }
     else if ( theDisplaying == VISU::eDisplay || theDisplaying == VISU::eDisplayOnly ) {
@@ -831,12 +990,14 @@ namespace VISU
           plotCurve->setColor( QColor( (int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.) ) );
         }
         plotCurve->setAutoAssign( theCurve->IsAuto() );
-        aPlot->displayCurve( plotCurve, false );
+       if( aPlot )
+         aPlot->displayCurve( plotCurve, false );
       }
       else {
         Plot2d_Curve* crv = theCurve->CreatePresentation();
         if ( crv ) {
-          aPlot->displayCurve( crv, false );
+         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;
@@ -1064,7 +1225,8 @@ namespace VISU
         timer.Start();
 #endif
         PublishInView(theModule, pPresent);
-        aView->onFitAll();
+        //aView->onFitAll();
+        SetFitAll(aView);
 #ifdef CHECKTIME
         timer.Stop();
         MESSAGE("VisuGUI::CreateMesh() - DISPLAY MESH");
@@ -1077,7 +1239,7 @@ namespace VISU
         INFOS(exc.what());
         SUIT_MessageBox::warn1 (GetDesktop(theModule),
                                 QObject::tr("WRN_VISU"),
-                                QObject::tr("ERR_CANT_CREATE_ACTOR") + " " + QObject::tr(exc.what()),
+                                QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(exc.what()),
                                 QObject::tr("BUT_OK"));
       }
     }