Salome HOME
Issue 20443: SIGSEGV in SMESHGUI_VTKUtils.cxx::RemoveVisuData
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_VTKUtils.cxx
index d8144f320dd16aacb844c71be3fa2fd5afd21519..569dc023bf68bbd9997b1a26e4158b66f8803046 100644 (file)
@@ -1,4 +1,6 @@
-//  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
 //  This library is free software; you can redistribute it and/or
 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-
-
+//
+// SMESH SMESHGUI : GUI for SMESH component
+// File   : SMESHGUI_VTKUtils.cxx
+// Author : Open CASCADE S.A.S.
+// SMESH includes
+//
 #include "SMESHGUI_VTKUtils.h"
+
+#include "SMESHGUI.h"
 #include "SMESHGUI_Utils.h"
 #include "SMESHGUI_Filter.h"
 
-#include "SMESHGUI.h"
-#include "SMESH_Actor.h"
-#include "SMESH_ActorUtils.h"
-#include "SMESH_ObjectDef.h"
+#include <SMESH_Actor.h>
+#include <SMESH_ActorUtils.h>
+#include <SMESH_ObjectDef.h>
 #include <SMDS_Mesh.hxx>
 
+// SALOME GUI includes
 #include <SUIT_Desktop.h>
 #include <SUIT_Session.h>
-#include <SUIT_Study.h>
 #include <SUIT_MessageBox.h>
+#include <SUIT_ViewManager.h>
+#include <SUIT_ResourceMgr.h>
 
 #include <SALOME_ListIO.hxx>
 #include <SALOME_ListIteratorOfListIO.hxx>
 #include <SVTK_ViewModel.h>
 #include <SVTK_ViewWindow.h>
 
+#include <VTKViewer_Algorithm.h>
+
 #include <LightApp_SelectionMgr.h>
 #include <SalomeApp_Application.h>
 #include <SalomeApp_Study.h>
 
+// SALOME KERNEL includes
 #include <utilities.h>
 
+// IDL includes
 #include <SALOMEconfig.h>
-#include CORBA_CLIENT_HEADER(SMESH_Gen)
 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
 #include CORBA_CLIENT_HEADER(SMESH_Group)
-#include CORBA_CLIENT_HEADER(SMESH_Hypothesis)
 
-#include <SALOMEDSClient_Study.hxx>
-#include <SALOMEDSClient_SObject.hxx>
-
-// VTK
+// VTK includes
 #include <vtkRenderer.h>
 #include <vtkActorCollection.h>
 #include <vtkUnstructuredGrid.h>
 
-// OCCT
+// OCCT includes
 #include <TColStd_IndexedMapOfInteger.hxx>
 #include <Standard_ErrorHandler.hxx>
 
-// STL
-#include <set>
-using namespace std;
-
-
-namespace SMESH {
-
-  typedef map<TKeyOfVisualObj,TVisualObjPtr> TVisualObjCont;
+namespace SMESH
+{
+  typedef std::map<TKeyOfVisualObj,TVisualObjPtr> TVisualObjCont;
   static TVisualObjCont VISUAL_OBJ_CONT;
 
   //=============================================================================
@@ -104,7 +107,7 @@ namespace SMESH {
     SUIT_ViewManager* aViewManager =
       app ? app->getViewManager(SVTK_Viewer::Type(), true) : 0;
     if ( aViewManager ) {
-      QPtrVector<SUIT_ViewWindow> views = aViewManager->getViews();
+      QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
       for ( int iV = 0; iV < views.count(); ++iV ) {
         if ( SMESH_Actor* actor = FindActorByEntry( views[iV], theEntry)) {
           if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV]))
@@ -138,11 +141,12 @@ namespace SMESH {
     for ( int iM = 0; iM < viewMgrs.count(); ++iM ) {
       SUIT_ViewManager* aViewManager = viewMgrs.at( iM );
       if ( aViewManager && aViewManager->getType() == SVTK_Viewer::Type()) {
-        QPtrVector<SUIT_ViewWindow> views = aViewManager->getViews();
+        QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
         for ( int iV = 0; iV < views.count(); ++iV ) {
           if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV])) {
             vtkRenderer *aRenderer = vtkWnd->getRenderer();
-            vtkActorCollection *actors = aRenderer->GetActors();
+           VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
+            vtkActorCollection *actors = aCopy.GetActors();
             for (int i = 0; i < actors->GetNumberOfItems(); ++i ) {
               // size of actors changes inside the loop
               while (SMESH_Actor *actor = dynamic_cast<SMESH_Actor*>(actors->GetItemAsObject(i)))
@@ -180,14 +184,15 @@ namespace SMESH {
       SUIT_ViewManager* aViewManager = viewMgrs.at( iM );
       if ( aViewManager && aViewManager->getType() == SVTK_Viewer::Type() &&
            aViewManager->study()->id() == studyID ) {
-        QPtrVector<SUIT_ViewWindow> views = aViewManager->getViews();
+        QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
         for ( int iV = 0; iV < views.count(); ++iV ) {
           if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV])) {
             vtkRenderer *aRenderer = vtkWnd->getRenderer();
-            vtkActorCollection *actors = aRenderer->GetActors();
+           VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
+            vtkActorCollection *actors = aCopy.GetActors();
             for (int i = 0; i < actors->GetNumberOfItems(); ++i ) {
               // size of actors changes inside the loop
-              while(SMESH_Actor *actor = dynamic_cast<SMESH_Actor*>(actors->GetItemAsObject(i)))
+              if(SMESH_Actor *actor = dynamic_cast<SMESH_Actor*>(actors->GetItemAsObject(i)))
               {
                 vtkWnd->RemoveActor(actor);
                 actor->Delete();
@@ -225,22 +230,20 @@ namespace SMESH {
       // after or at showing this message, so we do an additional check of available memory
 //       char* buf = new char[100*1024];
 //       delete [] buf;
-      SUIT_MessageBox::warn1 (SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
-                              QObject::tr("SMESH_VISU_PROBLEM"),
-                              QObject::tr("SMESH_BUT_OK"));
+      SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
+                              QObject::tr("SMESH_VISU_PROBLEM"));
     } catch (...) {
       // no more memory at all: last resort
-      cout<< "SMESHGUI_VTKUtils::OnVisuException(), exception even at showing a message!!!" <<endl;
-      cout<< "Try to remove all visual data..." <<endl;
+      MESSAGE_BEGIN ( "SMESHGUI_VTKUtils::OnVisuException(), exception even at showing a message!!!" <<
+                     std::endl << "Try to remove all visual data..." );
       if (theVISU_MemoryReserve) {
         delete theVISU_MemoryReserve;
         theVISU_MemoryReserve = 0;
       }
       RemoveAllObjectsWithActors();
-      SUIT_MessageBox::warn1 (SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
-                              QObject::tr("SMESH_VISU_PROBLEM_CLEAR"),
-                              QObject::tr("SMESH_BUT_OK"));
-      cout<< "...done" << endl;
+      SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
+                              QObject::tr("SMESH_VISU_PROBLEM_CLEAR"));
+      MESSAGE_END ( "...done" );
     }
   }
   //================================================================================
@@ -327,7 +330,7 @@ namespace SMESH {
       }
       catch (...) {
 #ifdef _DEBUG_
-        cout << "Exception in SMESHGUI_VTKUtils::GetVisualObj()" << endl;
+        MESSAGE ( "Exception in SMESHGUI_VTKUtils::GetVisualObj()" );
 #endif
         RemoveVisualObjectWithActors( theEntry ); // remove this object
         OnVisuException();
@@ -344,23 +347,22 @@ namespace SMESH {
       int usedMB = aVisualObj->GetUnstructuredGrid()->GetActualMemorySize() / 1024;
       if ( freeMB > 0 && usedMB * 30 > freeMB ) {
 #ifdef _DEBUG_
-        cout << "SMESHGUI_VTKUtils::GetVisualObj(), freeMB=" << freeMB
-             << ", usedMB=" << usedMB<< endl;
+        MESSAGE ( "SMESHGUI_VTKUtils::GetVisualObj(), freeMB=" << freeMB
+               << ", usedMB=" << usedMB );
 #endif
-        int continu = 0;
+        bool continu = false;
         if ( usedMB * 10 > freeMB )
           // even dont try to show
-          SUIT_MessageBox::warn1 (SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
-                                  QObject::tr("SMESH_NO_MESH_VISUALIZATION"),
-                                  QObject::tr("SMESH_BUT_OK"));
+          SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
+                                  QObject::tr("SMESH_NO_MESH_VISUALIZATION"));
         else
           // there is a chance to succeed
-          continu = SUIT_MessageBox::warn2
+          continu = SUIT_MessageBox::warning
             (SMESHGUI::desktop(),
              QObject::tr("SMESH_WRN_WARNING"),
              QObject::tr("SMESH_CONTINUE_MESH_VISUALIZATION"),
-             QObject::tr("SMESH_BUT_YES"),  QObject::tr("SMESH_BUT_NO"),
-             1, 0, 1);
+             SUIT_MessageBox::Yes | SUIT_MessageBox::No, 
+            SUIT_MessageBox::Yes ) == SUIT_MessageBox::Yes;
         if ( !continu ) {
           // remove the corresponding actors from all views
           RemoveVisualObjectWithActors( theEntry );
@@ -412,8 +414,8 @@ namespace SMESH {
     if( !theMgr )
       return NULL;
 
-    QPtrVector<SUIT_ViewWindow> views = theMgr->getViews();
-    if( views.containsRef( theWindow ) )
+    QVector<SUIT_ViewWindow*> views = theMgr->getViews();
+    if( views.contains( theWindow ) )
       return GetVtkViewWindow( theWindow );
     else
       return NULL;
@@ -453,7 +455,7 @@ namespace SMESH {
       }
       catch (...) {
 #ifdef _DEBUG_
-        cout << "Exception in SMESHGUI_VTKUtils::RepaintCurrentView()" << endl;
+        MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintCurrentView()" );
 #endif
         OnVisuException();
       }
@@ -471,7 +473,7 @@ namespace SMESH {
     }
     catch (...) {
 #ifdef _DEBUG_
-        cout << "Exception in SMESHGUI_VTKUtils::RepaintViewWindow(SVTK_ViewWindow)" << endl;
+      MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintViewWindow(SVTK_ViewWindow*)" );
 #endif
       OnVisuException();
     }
@@ -488,7 +490,7 @@ namespace SMESH {
     }
     catch (...) {
 #ifdef _DEBUG_
-        cout << "Exception in SMESHGUI_VTKUtils::RenderViewWindow(SVTK_ViewWindow)" << endl;
+      MESSAGE ( "Exception in SMESHGUI_VTKUtils::RenderViewWindow(SVTK_ViewWindow*)" );
 #endif
       OnVisuException();
     }
@@ -505,7 +507,7 @@ namespace SMESH {
       }
       catch (...) {
 #ifdef _DEBUG_
-        cout << "Exception in SMESHGUI_VTKUtils::FitAll()" << endl;
+        MESSAGE ( "Exception in SMESHGUI_VTKUtils::FitAll()" );
 #endif
         OnVisuException();
       }
@@ -518,7 +520,8 @@ namespace SMESH {
   {
     if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
       vtkRenderer *aRenderer = aViewWindow->getRenderer();
-      vtkActorCollection *aCollection = aRenderer->GetActors();
+      VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
+      vtkActorCollection *aCollection = aCopy.GetActors();
       aCollection->InitTraversal();
       while(vtkActor *anAct = aCollection->GetNextActor()){
        if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
@@ -611,7 +614,7 @@ namespace SMESH {
       }
       catch (...) {
 #ifdef _DEBUG_
-        cout << "Exception in SMESHGUI_VTKUtils::DisplayActor()" << endl;
+        MESSAGE ( "Exception in SMESHGUI_VTKUtils::DisplayActor()" );
 #endif
         OnVisuException();
       }
@@ -647,10 +650,11 @@ namespace SMESH {
   {
     if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWnd)) {
       vtkRenderer *aRenderer = aViewWindow->getRenderer();
-      vtkActorCollection *aCollection = aRenderer->GetActors();
+      VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
+      vtkActorCollection *aCollection = aCopy.GetActors();
       aCollection->InitTraversal();
       while(vtkActor *anAct = aCollection->GetNextActor())
-       if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct))
+       if(dynamic_cast<SMESH_Actor*>(anAct))
           return false;
     }
     return true;
@@ -666,7 +670,8 @@ namespace SMESH {
     {
       OK = true;
       vtkRenderer *aRenderer = aViewWnd->getRenderer();
-      vtkActorCollection *aCollection = aRenderer->GetActors();
+      VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
+      vtkActorCollection *aCollection = aCopy.GetActors();
       aCollection->InitTraversal();
 
       switch (theAction) {
@@ -743,7 +748,8 @@ namespace SMESH {
 
       if( selected.Extent() == 0){
        vtkRenderer* aRenderer = aWnd->getRenderer();
-       vtkActorCollection *aCollection = aRenderer->GetActors();
+       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
+       vtkActorCollection *aCollection = aCopy.GetActors();
        aCollection->InitTraversal();
        while(vtkActor *anAct = aCollection->GetNextActor()){
          if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
@@ -754,7 +760,7 @@ namespace SMESH {
        }
       }else{
        SALOME_ListIteratorOfListIO anIter( selected );
-       for(; anIter.More(); anIter.Next()){
+       for( ; anIter.More(); anIter.Next()){
          Handle(SALOME_InteractiveObject) anIO = anIter.Value();
          if ( !Update(anIO,true) )
             break; // avoid multiple warinings if visu failed
@@ -796,7 +802,7 @@ namespace SMESH {
       return;
     }
 
-    QPtrVector<SUIT_ViewWindow> views = vm->getViews();
+    QVector<SUIT_ViewWindow*> views = vm->getViews();
 
     SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
     if( !mgr )
@@ -812,6 +818,15 @@ namespace SMESH {
     int SW = mgr->integerValue( "SMESH", "selection_width", 5 ),
         PW = mgr->integerValue( "SMESH", "highlight_width", 5 );
 
+    // adjust highlight_width to the width of mesh entities
+    int aPointSize  = mgr->integerValue("SMESH", "node_size", 3);
+    int aElem0DSize = mgr->integerValue("SMESH", "elem0d_size", 5);
+    int aLineWidth  = mgr->integerValue("SMESH", "element_width", 1);
+    int maxSize = aPointSize;
+    if (aElem0DSize > maxSize) maxSize = aElem0DSize;
+    if (aLineWidth > maxSize) maxSize = aLineWidth;
+    if (PW < maxSize + 2) PW = maxSize + 2;
+
     double SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
            SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 ),
           SP3 = mgr->doubleValue( "SMESH", "selection_precision_object", 0.025 );
@@ -834,7 +849,8 @@ namespace SMESH {
                                      PW);
        // update actors
        vtkRenderer* aRenderer = aVtkView->getRenderer();
-       vtkActorCollection *aCollection = aRenderer->GetActors();
+       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
+       vtkActorCollection *aCollection = aCopy.GetActors();
        aCollection->InitTraversal();
        while(vtkActor *anAct = aCollection->GetNextActor()){
          if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
@@ -885,7 +901,7 @@ namespace SMESH {
 
   void RemoveFilters(SVTK_Selector* theSelector)
   {
-    for ( int id = SMESHGUI_NodeFilter; theSelector && id < SMESHGUI_LastFilter; id++ )
+    for ( int id = SMESH::NodeFilter; theSelector && id < SMESH::LastFilter; id++ )
       theSelector->RemoveFilter( id );
   }
 
@@ -900,7 +916,8 @@ namespace SMESH {
   void SetPointRepresentation(bool theIsVisible){
     if(SVTK_ViewWindow* aViewWindow = GetCurrentVtkView()){
       vtkRenderer *aRenderer = aViewWindow->getRenderer();
-      vtkActorCollection *aCollection = aRenderer->GetActors();
+      VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
+      vtkActorCollection *aCollection = aCopy.GetActors();
       aCollection->InitTraversal();
       while(vtkActor *anAct = aCollection->GetNextActor()){
        if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
@@ -918,7 +935,8 @@ namespace SMESH {
     if(SVTK_ViewWindow* aWnd = GetCurrentVtkView()){
       int anIsAllPickable = (theActor == NULL);
       vtkRenderer *aRenderer = aWnd->getRenderer();
-      vtkActorCollection *aCollection = aRenderer->GetActors();
+      VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
+      vtkActorCollection *aCollection = aCopy.GetActors();
       aCollection->InitTraversal();
       while(vtkActor *anAct = aCollection->GetNextActor()){
        if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
@@ -963,7 +981,7 @@ namespace SMESH {
       anIdContainer.insert(aMapIndex(i));
 
     TIdContainer::const_iterator anIter = anIdContainer.begin();
-    for(; anIter != anIdContainer.end(); anIter++)
+    for( ; anIter != anIdContainer.end(); anIter++)
       theName += QString(" %1").arg(*anIter);
 
     return aMapIndex.Extent();
@@ -1043,12 +1061,12 @@ namespace SMESH {
       if(FindActorByEntry(theIO->getEntry())){
        TColStd_IndexedMapOfInteger aMapIndex;
        theMgr->GetIndexes(theIO,aMapIndex);
-       typedef set<int> TIdContainer;
+       typedef std::set<int> TIdContainer;
        TIdContainer anIdContainer;
        for( int i = 1; i <= aMapIndex.Extent(); i++)
          anIdContainer.insert(aMapIndex(i));
        TIdContainer::const_iterator anIter = anIdContainer.begin();
-       for(; anIter != anIdContainer.end(); anIter++){
+       for( ; anIter != anIdContainer.end(); anIter++){
          theName += QString(" %1").arg(*anIter);
        }
        return aMapIndex.Extent();
@@ -1128,7 +1146,8 @@ namespace SMESH {
     if( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView() )
     {
       vtkRenderer *aRenderer = aWnd->getRenderer();
-      vtkActorCollection *aCollection = aRenderer->GetActors();
+      VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
+      vtkActorCollection *aCollection = aCopy.GetActors();
       aCollection->InitTraversal();
 
       while ( vtkActor *anAct = aCollection->GetNextActor())
@@ -1142,4 +1161,4 @@ namespace SMESH {
 
     }
   }
-}
+} // end of namespace SMESH