Nektar++
Classes | Macros | Functions
VtkWrapper.hpp File Reference
#include <vtkPoints.h>
#include <vtkUnstructuredGrid.h>
#include <vtkVersion.h>
#include <LibUtilities/BasicUtils/ErrorUtil.hpp>

Go to the source code of this file.

Classes

struct  vtkObjectPointer_to_python< T >
 

Macros

#define VTK_PYTHON_CONVERSION(type)
 

Functions

void * extract_vtk_wrapped_pointer (PyObject *obj)
 

Macro Definition Documentation

◆ VTK_PYTHON_CONVERSION

#define VTK_PYTHON_CONVERSION (   type)
Value:
/* register the to-python converter */ \
py::to_python_converter<type *, vtkObjectPointer_to_python<type *>>(); \
/* register the from-python converter */ \
py::converter::registry::insert(&extract_vtk_wrapped_pointer, \
py::type_id<type>());
void * extract_vtk_wrapped_pointer(PyObject *obj)
Definition: VtkWrapper.hpp:93

Definition at line 145 of file VtkWrapper.hpp.

Function Documentation

◆ extract_vtk_wrapped_pointer()

void * extract_vtk_wrapped_pointer ( PyObject *  obj)

Definition at line 93 of file VtkWrapper.hpp.

94{
95 std::string thisStr = "__this__";
96
97 // first we need to get the __this__ attribute from the Python Object
98 if (!PyObject_HasAttrString(obj, thisStr.c_str()))
99 {
100 return nullptr;
101 }
102
103 PyObject *thisAttr = PyObject_GetAttrString(obj, thisStr.c_str());
104 if (thisAttr == nullptr)
105 {
106 return nullptr;
107 }
108
109#if PY_MAJOR_VERSION == 2
110 std::string str = PyString_AsString(thisAttr);
111#else
112 std::string str = PyUnicode_AsUTF8(thisAttr);
113#endif
114
115 if (str.size() < 21)
116 {
117 return nullptr;
118 }
119
120 char hex_address[32], *pEnd;
121 auto iter = str.find("_p_vtk");
122
123 if (iter == std::string::npos)
124 {
125 return nullptr;
126 }
127
128 auto className = str.substr(iter).find("vtk");
129 if (className == std::string::npos)
130 {
131 return nullptr;
132 }
133
134 long address = stol(str.substr(1, 17));
135 vtkObjectBase *vtkObject = (vtkObjectBase *)((void *)address);
136
137 if (vtkObject->IsA(str.substr(className).c_str()))
138 {
139 return vtkObject;
140 }
141
142 return nullptr;
143}