[vtk-developers] .NET wrappers for VTK
Andrew J. Dolgert
ajd27 at cornell.edu
Sun Mar 19 14:54:50 EST 2006
Hi All,
I wrote wrappers to use VTK in .NET languages. The University of West
Bohemia also wrote wrappers, but I needed them in the VTK CMake files
along with Tcl, Python, and Java.
- Writes wrappers in managed C++ v2.0, so it only works for Visual
Studio 2005, not 2003.
- Because CMake cannot create a managed C++ project, there is a Visual
Studio macro to convert all of the CMake wrapper projects to managed.
- Includes a Windows Forms Control that looks much like the new
vtkMFCWindow. The control does not show up well in design mode but works
fine.
- 10 examples in C#, one in managed C++.
- Uses a single vtk namespace, like Python, but it could also use
several, vtkCommon, vtkFiltering, etc, if we want that change.
http://www.tc.cornell.edu/~ajd27/VTK/DotNetWrap.html
Please tell me how I can contribute this work. I've tried to make this
conform as best I can.
I noticed that the method HyperOctreeCutter::GetCellTypeDimensions does
not have a function body. It causes wrapping to fail.
There is one design decision I think I should describe. There are two
possible ways to write wrapper class constructors. Like the Bohemian
wrappers, these use a public constructor:
vtk::vtkPolyData::vtkPolyData() : (false) {
m_nativeInstance = IntPtr( ::vtkPolyData::New() );
}
Using this approach, the wrapper class is not necessarily the wrapper
for the most derived class. For instance, a vtkRenderWindow wrapper will
actually wrap a vtkWin32OpenGLRenderWindow. Instead of using
dynamic_cast to determine a derived type, or the "is" operator in C#,
the programmer has to use SafeDownCast(). We could, instead, use a
static method, similar to what is done in the VTK library. The static
method would ask the native object for its most derived type and use
.NET reflection services to create the correct wrapper class.
vtk::vtkPolyData^ vtk::vtkPolyData::New()
{
::vtkPolyData* nativePolyData = ::vtkPolyData::New();
const char* className = nativePolyData->GetClassName();
System::Type^ wrapperType = System::Type::GetType("vtk." + className);
return UseReflectionToCreateWrapper( wrapperType, nativePolyData);
}
The advantage of this second technique is that the user of these
wrappers can use dynamic_cast to determine derived types. The
disadvantage is that reflection is about a tenth the speed of direct
creation of a type. (I measured it.) A new wrapper instance is created
every time a method returns a native class, as in
coneActor->GetProperty()->SetColor(1,0,0).
Thanks,
Drew Dolgert
More information about the vtk-developers
mailing list