00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: $RCSfile: vtkPythonUtil.h,v $ 00005 Language: C++ 00006 00007 00008 Copyright (c) 1993-2001 Ken Martin, Will Schroeder, Bill Lorensen 00009 All rights reserved. 00010 00011 Redistribution and use in source and binary forms, with or without 00012 modification, are permitted provided that the following conditions are met: 00013 00014 * Redistributions of source code must retain the above copyright notice, 00015 this list of conditions and the following disclaimer. 00016 00017 * Redistributions in binary form must reproduce the above copyright notice, 00018 this list of conditions and the following disclaimer in the documentation 00019 and/or other materials provided with the distribution. 00020 00021 * Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names 00022 of any contributors may be used to endorse or promote products derived 00023 from this software without specific prior written permission. 00024 00025 * Modified source versions must be plainly marked as such, and must not be 00026 misrepresented as being the original software. 00027 00028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 00029 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00030 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00031 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR 00032 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00033 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00034 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00035 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00036 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00037 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00038 00039 =========================================================================*/ 00040 #include "vtkObject.h" 00041 #include "vtkTimeStamp.h" 00042 #include "Python.h" 00043 #include "vtkCommand.h" 00044 00045 // This is the VTK/Python 'class,' it contains the method list and a pointer 00046 // to the superclass 00047 typedef vtkObject *(*vtknewfunc)(); 00048 00049 typedef struct { 00050 PyObject_HEAD 00051 PyMethodDef *vtk_methods; 00052 vtknewfunc vtk_new; 00053 char *vtk_name; 00054 char *vtk_module; 00055 char *vtk_doc; 00056 PyObject *vtk_bases; 00057 } PyVTKClass; 00058 00059 // This is the VTK/Python 'object,' it contains the python object header 00060 // plus a pointer to the associated vtkObject and PyVTKClass. 00061 typedef struct { 00062 PyObject_HEAD 00063 vtkObject *vtk_ptr; 00064 PyVTKClass *vtk_class; 00065 } PyVTKObject; 00066 00067 // Standard methods for all vtk/python objects 00068 extern "C" 00069 { 00070 int PyVTKObject_Check(PyObject *obj); 00071 int PyVTKClass_Check(PyObject *obj); 00072 PyObject *PyVTKObject_New(PyObject *vtkclass, vtkObject *ptr); 00073 PyObject *PyVTKClass_New(vtknewfunc constructor, PyMethodDef *methods, 00074 char *classname, char *modulename, char *docstring, 00075 PyObject *base); 00076 00077 // this is a special version of ParseTuple that handles both bound 00078 // and unbound method calls for VTK objects 00079 vtkObject *PyArg_VTKParseTuple(PyObject *self, PyObject *args, 00080 char *format, ...); 00081 } 00082 00083 // Add a PyVTKClass to the type lookup table, this allows us to later 00084 // create object given only the class name. 00085 extern void vtkPythonAddClassToHash(PyObject *obj, char *type); 00086 00087 // Extract the vtkObject from a PyVTKObject. If the PyObject is not a 00088 // PyVTKObject, or is not a PyVTKObject of the specified type, the python 00089 // error indicator will be set. 00090 // Special behaviour: Py_None is converted to NULL without no error. 00091 extern vtkObject *vtkPythonGetPointerFromObject(PyObject *obj, char *type); 00092 00093 // Convert a vtkObject to a PyVTKObject. This will first check to see if 00094 // the PyVTKObject already exists, and create a new PyVTKObject if necessary. 00095 // This function also passes ownership of the reference to the PyObject. 00096 // Special behaviour: NULL is converted to Py_None. 00097 extern PyObject *vtkPythonGetObjectFromPointer(vtkObject *ptr); 00098 00099 // Try to convert some PyObject into a PyVTKObject, currently conversion 00100 // is supported for SWIG-style mangled pointer strings. 00101 extern PyObject *vtkPythonGetObjectFromObject(PyObject *arg, const char *type); 00102 00103 // Add and delete PyVTKObject/vtkObject pairs from the wrapper hash table, 00104 // these methods do not change the reference counts of either the vtkObject 00105 // or the PyVTKObject. 00106 extern void vtkPythonAddObjectToHash(PyObject *obj, vtkObject *anInstance); 00107 extern void vtkPythonDeleteObjectFromHash(PyObject *obj); 00108 00109 // Utility functions for creating/usinge SWIG-style mangled pointer strings. 00110 extern char *vtkPythonManglePointer(void *ptr, const char *type); 00111 extern void *vtkPythonUnmanglePointer(char *ptrText, int *len, 00112 const char *type); 00113 00114 // For use by SetXXMethod() , SetXXMethodArgDelete() 00115 extern void vtkPythonVoidFunc(void *); 00116 extern void vtkPythonVoidFuncArgDelete(void *); 00117 00118 // To allow Python to use the vtkCommand features 00119 class vtkPythonCommand : public vtkCommand 00120 { 00121 public: 00122 vtkPythonCommand(); 00123 ~vtkPythonCommand(); 00124 static vtkPythonCommand *New() { return new vtkPythonCommand; }; 00125 00126 void SetObject(PyObject *o); 00127 void Execute(vtkObject *ptr, unsigned long eventtype, void *); 00128 00129 PyObject *obj; 00130 }; 00131 00132