Plugin Tutorial: Subclassing PythonProgrammableFilter

From KitwarePublic
Revision as of 04:32, 7 January 2015 by DWilches (talk | contribs) (Created first tutorial)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

In this tutorial we show step-by-step how to create a simple subclass of PythonProgrammableFilter. This plugin that shows how to subclass it and pass custom parameters from the GUI to a Python script.

Step 1: Create a CMakeLists.txt

Create a file CMakeLists.txt with the following contents:

<source lang="xml"> cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)

if (NOT ParaView_BINARY_DIR)

 find_package(ParaView REQUIRED)
 include(${PARAVIEW_USE_FILE})

endif()

include(ParaViewPlugins)

  1. create a paraview plugin containing server manager xml and the server
  2. manager classes to build
  3. this plugin can be loaded on the server side

ADD_PARAVIEW_PLUGIN(CPPFilter "1.0"

 SERVER_MANAGER_XML vtkCPPFilter.xml
 SERVER_MANAGER_SOURCES vtkCPPFilter.cxx)

</source>

Step 2: Create the XML file describing the plugin and its GUI

In the Step 1 we are referencing the file "vtkCPPFilter.xml", so you need to create it with the following contents:

<source lang="xml"> <ServerManagerConfiguration> <ProxyGroup name="filters"> <SourceProxy name="CPPFilter" class="vtkCPPFilter" label="CPPFilter"> <InputProperty name="Input" command="SetInputConnection"> <ProxyGroupDomain name="groups"> <Group name="sources"/> <Group name="filters"/> </ProxyGroupDomain> <DataTypeDomain name="input_type"> <DataType value="vtkDataSet"/> </DataTypeDomain> </InputProperty>

<IntVectorProperty name="OutputDataSetType" command="SetOutputDataSetType" number_of_elements="1" default_values="0">

       </IntVectorProperty>

<StringVectorProperty name="Script" command="SetScript" number_of_elements="1" default_values="print 'Sizz value is: %s' % (sizz)"> <Hints> <Widget type="multi_line"/> </Hints> </StringVectorProperty>

<DoubleVectorProperty name="Sizz" command="SetSizz" number_of_elements="1" default_values="245"> </DoubleVectorProperty>

  </SourceProxy>
</ProxyGroup>

</ServerManagerConfiguration> </source>

Step 3: Create the C++ file with the code for the plugin

Here we will create the C++ file referenced in the Step 1: "vtkCPPFilter.cxx"

<source lang="cpp">

  1. include "vtkCPPFilter.h"
  2. include "vtkObjectFactory.h"
  3. include <QMessageBox>

vtkStandardNewMacro(vtkCPPFilter);

vtkCPPFilter::vtkCPPFilter() {}

vtkCPPFilter::~vtkCPPFilter() {}

void vtkCPPFilter::PrintSelf(ostream& os, vtkIndent indent) {

 this->Superclass::PrintSelf(os,indent);

}

// Method that creates a variable for Python using SetParameter void vtkCPPFilter::SetSizz(double value) { /* char s[255]; sprintf(s, "SetSizz was invoked %f\n", d); QMessageBox::information(NULL, "MyAction", s); */

// Create a new variable for Python called 'sizz' SetParameter("sizz", value); } </source>

Step 4: Create the H file with the header of the plugin

Here we will create the H file referenced in the Step 3: "vtkCPPFilter.h"

<source lang="cpp">

  1. ifndef __vtkMyElevationFilter_h
  2. define __vtkMyElevationFilter_h
  1. include "vtkPythonProgrammableFilter.h"

class VTK_EXPORT vtkCPPFilter : public vtkPythonProgrammableFilter { public: static vtkCPPFilter* New(); vtkTypeMacro(vtkCPPFilter, vtkPythonProgrammableFilter); void PrintSelf(ostream& os, vtkIndent indent);

// Method that creates a variable for Python using SetParameter void SetSizz(double);

protected: vtkCPPFilter(); ~vtkCPPFilter();

private: vtkCPPFilter(const vtkCPPFilter&); // Not implemented. void operator=(const vtkCPPFilter&); // Not implemented. };

  1. endif

</source>

Step 5: Generate the rest of the plugin code

Now, you should have 4 files in your working directory:

CMakeLists.txt
vtkCPPFilter.xml
vtkCPPFilter.cxx
vtkCPPFilter.h

The next step is to open CMake and point it to our working folder and specify the build directory. For this example the build directory will be C:/vtkCPPFilter/build.

After you have clicked in "Configure" and then on "Generate" you will have a new file called: C:/vtkCPPFilter/build/Project.sln. You can open this file with Visual Studio 2010 (if you are using other compiler this file may vary).

Step 6: compile the plugin

In VisualStudio build the solution. After this process finishes the following file will be created:

C:/vtkCPPFilter/build/Debug/CPPFilter.dll

or if you set the default configuration to "Release"then the file will be located in:

C:/vtkCPPFilter/build/Release/CPPFilter.dll

Note this configuration should match the one used when you compiled your version of Paraview.