Back to index

salome-gui  6.5.0
Classes | Defines | Enumerations | Functions | Variables
SalomePy.cxx File Reference
#include <Python.h>
#include <vtkPythonUtil.h>
#include <vtkVersion.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <SALOME_Event.h>
#include <SUIT_Session.h>
#include <LightApp_Application.h>
#include <LightApp_Study.h>
#include <SVTK_ViewManager.h>
#include <SVTK_ViewWindow.h>

Go to the source code of this file.

Classes

class  TGetRendererEvent
class  TGetRenderWindowEvent
class  TGetRenderWindowInteractorEvent

Defines

#define VTK_XVERSION   (VTK_MAJOR_VERSION*10000+VTK_MINOR_VERSION*100+VTK_BUILD_VERSION)
#define PUBLISH_ENUM(i)
 Python wrappings for VTK viewer of the SALOME desktop.

Enumerations

enum  {
  ViewFront, ViewBack, ViewTop, ViewBottom,
  ViewRight, ViewLeft
}
 View operation type. More...
enum  { __Find, __FindOrCreate, __Create }
 VTK window find/create mode. More...

Functions

static PyObject * GetPyClass (const char *theClassName)
 Get Python class object by name.
static SVTK_ViewWindow * GetVTKViewWindow (int toCreate=__FindOrCreate)
 Find or create VTK window.
PyObject * libSalomePy_getRenderer (PyObject *self, PyObject *args)
PyObject * libSalomePy_getRenderWindow (PyObject *self, PyObject *args)
PyObject * libSalomePy_getRenderWindowInteractor (PyObject *self, PyObject *args)
PyObject * libSalomePy_showTrihedron (PyObject *self, PyObject *args)
PyObject * libSalomePy_fitAll (PyObject *self, PyObject *args)
PyObject * libSalomePy_setView (PyObject *self, PyObject *args)
PyObject * libSalomePy_resetView (PyObject *self, PyObject *args)
void initlibSalomePy ()
 Python module initialization.

Variables

static PyMethodDef Module_Methods []

Define Documentation

#define PUBLISH_ENUM (   i)
Value:
{                                                    \
  PyObject *w;                                       \
  int rc;                                            \
  if ( ( w = PyInt_FromLong( i ) ) == NULL ) return; \
  rc = PyDict_SetItemString( aModuleDict, #i, w );   \
  Py_DECREF( w );                                    \
  if ( rc < 0 ) return;                              \
}

Python wrappings for VTK viewer of the SALOME desktop.

All methods are implemented using Event mechanism. The module provides the following functions:

  • getRenderer()
  • getRenderWindow()
  • getRenderWindowInteractor()
  • showTrihedron()
  • fitAll()
  • setView()
  • resetView()

Usage in Python:

  import SalomePy
  renderer = SalomePy.getRenderer()     # get VTK renderer
  window   = SalomePy.getRenderWindow() # get render window

The methods getRenderer(), getRenderWindow() and getRenderWindowInteractor() open new VTK viewer if there is no one opened. In case of any error these methods return None object to the Python.

Definition at line 79 of file SalomePy.cxx.

#define VTK_XVERSION   (VTK_MAJOR_VERSION*10000+VTK_MINOR_VERSION*100+VTK_BUILD_VERSION)

Definition at line 52 of file SalomePy.cxx.


Enumeration Type Documentation

anonymous enum

View operation type.

Enumerator:
ViewFront 

front view

ViewBack 

back view

ViewTop 

top view

ViewBottom 

bottom view

ViewRight 

right view

ViewLeft 

left view

Definition at line 90 of file SalomePy.cxx.

anonymous enum

VTK window find/create mode.

Enumerator:
__Find 
__FindOrCreate 
__Create 

Definition at line 132 of file SalomePy.cxx.

     { 
  __Find,          // try to find VTK window; if not found, do nothing
  __FindOrCreate,  // try to find VTK window; if not found, create new one
  __Create };      // create new VTK window

Function Documentation

static PyObject* GetPyClass ( const char *  theClassName) [static]

Get Python class object by name.

Parameters:
theClassNamePython class name
Returns:
Python class object or None object if class is not found

Definition at line 105 of file SalomePy.cxx.

{
  static PyObject* aVTKModule = 0;
  PyObject* aPyClass = 0;
  if( !aVTKModule ) {
#if VTK_XVERSION < 30000
    aVTKModule = PyImport_ImportModule( "libVTKGraphicsPython" ); 
#elif VTK_XVERSION < 50700
    aVTKModule = PyImport_ImportModule( "vtk.libvtkRenderingPython" ); 
#else
    aVTKModule = PyImport_ImportModule( "vtkRenderingPython" ); 
#endif
    if( PyErr_Occurred() ) {
      PyErr_Print();
    }
  }
  if ( aVTKModule ) {
    PyObject* aVTKDict = PyModule_GetDict( aVTKModule );
    aPyClass = PyDict_GetItemString(aVTKDict, const_cast<char*>( theClassName ) );
  }
  return aPyClass;
}

Here is the caller graph for this function:

static SVTK_ViewWindow* GetVTKViewWindow ( int  toCreate = __FindOrCreate) [static]

Find or create VTK window.

Parameters:
toCreatewindow find/create mode
Returns:
VTK window pointer or 0 if it could not be found/created

Definition at line 143 of file SalomePy.cxx.

                                                                          {
  SVTK_ViewWindow* aVW = 0;
  if ( SUIT_Session::session() ) {
    // get application
    LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() );
    if ( anApp ) {
      // get active study
      LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( anApp->activeStudy() );
      if ( aStudy ) {
        // find or create VTK view manager
        if ( toCreate == __Create ) {
          SVTK_ViewManager* aVM = dynamic_cast<SVTK_ViewManager*>( anApp->createViewManager( "VTKViewer" ) );
          if ( aVM ) {
            aVW = dynamic_cast<SVTK_ViewWindow*>( aVM->getActiveView() );
            if ( !aVW )
              aVW = dynamic_cast<SVTK_ViewWindow*>( aVM->createViewWindow() );
            // VSR : When new view window is created it can be not active yet at this moment,
            // so the following is a some workaround
            if ( !aVW && !aVM->getViews().isEmpty() )
              aVW = dynamic_cast<SVTK_ViewWindow*>( aVM->getViews()[0] );
          }
        }
        else {
          SVTK_ViewManager* aVM = dynamic_cast<SVTK_ViewManager*>( anApp->getViewManager( "VTKViewer", toCreate == __FindOrCreate ) );
          if ( aVM ) {
            aVW = dynamic_cast<SVTK_ViewWindow*>( aVM->getActiveView() );
            // VSR : When new view window is created it can be not active yet at this moment,
            // so the following is a some workaround
            if ( !aVW && !aVM->getViews().isEmpty() )
              aVW = dynamic_cast<SVTK_ViewWindow*>( aVM->getViews()[0] );
          }
        }
      }
    }
  }
  return aVW;
}

Here is the caller graph for this function:

Python module initialization.

Definition at line 498 of file SalomePy.cxx.

{
  static char* modulename = (char*)"libSalomePy";

  // init module
  PyObject* aModule = Py_InitModule( modulename, Module_Methods );
  if( PyErr_Occurred() ) {
    PyErr_Print();
    return;
  }

  // get module's dictionary
  PyObject *aModuleDict = PyModule_GetDict( aModule );
  if ( aModuleDict == NULL )
    return;

  // export View type enumeration
  PUBLISH_ENUM( ViewFront );
  PUBLISH_ENUM( ViewBack );
  PUBLISH_ENUM( ViewTop );
  PUBLISH_ENUM( ViewBottom );
  PUBLISH_ENUM( ViewRight );
  PUBLISH_ENUM( ViewLeft );
}
PyObject* libSalomePy_fitAll ( PyObject *  self,
PyObject *  args 
)

Definition at line 385 of file SalomePy.cxx.

{
  class TEvent: public SALOME_Event
  {
  public:
    TEvent() {}
    virtual void Execute()
    {
      if( SVTK_ViewWindow* aVTKViewWindow = GetVTKViewWindow( __Find ) ) {
        aVTKViewWindow->onFitAll();
      }
    }
  };
  
  ProcessVoidEvent( new TEvent() );
  return Py_None;
}

Here is the call graph for this function:

PyObject* libSalomePy_getRenderer ( PyObject *  self,
PyObject *  args 
)

Definition at line 223 of file SalomePy.cxx.

{
  PyObject* aResult = Py_None;
  int toCreate = 0;
  if ( !PyArg_ParseTuple( args, "|i:getRenderer", &toCreate ) )
    PyErr_Print();
  else
    aResult = ProcessEvent( new TGetRendererEvent( toCreate ) );
  return aResult;
}

Here is the call graph for this function:

PyObject* libSalomePy_getRenderWindow ( PyObject *  self,
PyObject *  args 
)

Definition at line 276 of file SalomePy.cxx.

{
  PyObject* aResult = Py_None;
  int toCreate = 0;
  if ( !PyArg_ParseTuple( args, "|i:getRenderWindow", &toCreate ) )
    PyErr_Print();
  else
    aResult = ProcessEvent( new TGetRenderWindowEvent( toCreate ) );
  return aResult;
}

Here is the call graph for this function:

PyObject* libSalomePy_getRenderWindowInteractor ( PyObject *  self,
PyObject *  args 
)

Definition at line 329 of file SalomePy.cxx.

{
  PyObject* aResult = Py_None;
  int toCreate = 0;
  if ( !PyArg_ParseTuple( args, "|i:getRenderWindowInteractor", &toCreate ) )
    PyErr_Print();
  else
    aResult = ProcessEvent( new TGetRenderWindowInteractorEvent( toCreate ) );
  return aResult;
}

Here is the call graph for this function:

PyObject* libSalomePy_resetView ( PyObject *  self,
PyObject *  args 
)

Definition at line 464 of file SalomePy.cxx.

{
  class TEvent: public SALOME_Event
  {
  public:
    TEvent() {}
    virtual void Execute()
    {
      if( SVTK_ViewWindow* aVTKViewWindow = GetVTKViewWindow( __Find ) ) {
        aVTKViewWindow->onResetView();
      }
    }
  };
  
  ProcessVoidEvent( new TEvent() );
  return Py_None;
}

Here is the call graph for this function:

PyObject* libSalomePy_setView ( PyObject *  self,
PyObject *  args 
)

Definition at line 413 of file SalomePy.cxx.

{
  class TEvent: public SALOME_Event
  {
  public:
    long myType;
    TEvent( long type ) : myType( type) {}
    virtual void Execute()
    {
      if( SVTK_ViewWindow* aVTKViewWindow = GetVTKViewWindow( __Find ) ) {
        switch( myType ) {
        case ViewFront:
          aVTKViewWindow->onFrontView();  break;
        case ViewBack:
          aVTKViewWindow->onBackView();   break;
        case ViewTop:
          aVTKViewWindow->onTopView();    break;
        case ViewBottom:
          aVTKViewWindow->onBottomView(); break;
        case ViewRight:
          aVTKViewWindow->onRightView();  break;
        case ViewLeft:
          aVTKViewWindow->onLeftView();   break;
        default:
          PyErr_Format(PyExc_ValueError,"setView%: wrong parameter value; must be between %d and %d", ViewFront, ViewLeft );
          break;
        }
      }
    }
  };
  
  long type = -1;
  if ( !PyArg_ParseTuple( args, "l:setView", &type ) )
    PyErr_Print();
  else {
    ProcessVoidEvent( new TEvent( type ) );
    if( PyErr_Occurred() )
      PyErr_Print();
  }
  return Py_None;
}

Here is the call graph for this function:

PyObject* libSalomePy_showTrihedron ( PyObject *  self,
PyObject *  args 
)

Definition at line 350 of file SalomePy.cxx.

{
  class TEvent: public SALOME_Event
  {
  public:
    int myShow;
    TEvent( int bShow )
      : myShow( bShow ) {}
    virtual void Execute()
    {
      if( SVTK_ViewWindow* aVTKViewWindow = GetVTKViewWindow( __Find ) ) {
        if ( aVTKViewWindow->isTrihedronDisplayed() != myShow )
          aVTKViewWindow->onViewTrihedron();
      }
    }
  };
  
  PyObject* aResult = Py_None;
  int bShow = 0;
  if ( !PyArg_ParseTuple( args, "i:showTrihedron", &bShow ) )
    PyErr_Print();
  else
    ProcessVoidEvent( new TEvent( bShow ) );
  return aResult;
}

Here is the call graph for this function:


Variable Documentation

PyMethodDef Module_Methods[] [static]
Initial value:
 
{
  { "getRenderer",               libSalomePy_getRenderer,               METH_VARARGS },
  { "getRenderWindow",           libSalomePy_getRenderWindow,           METH_VARARGS },
  { "getRenderWindowInteractor", libSalomePy_getRenderWindowInteractor, METH_VARARGS },
  { "showTrihedron",             libSalomePy_showTrihedron,             METH_VARARGS },
  { "fitAll",                    libSalomePy_fitAll,                    METH_NOARGS  },
  { "setView",                   libSalomePy_setView,                   METH_VARARGS },
  { "resetView",                 libSalomePy_resetView,                 METH_NOARGS  },
  { NULL, NULL }
}

Definition at line 482 of file SalomePy.cxx.