/builds/gitlab-kitware-sciviz-ci/Utilities/Doxygen/pages/PluginHowto.md
Go to the documentation of this file.
1 # Plugin Howto {#PluginHowto}
2 
3 ## Introduction
4 
5 ParaView comes with plethora of functionality bundled in: several readers,
6 multitude of filters, different types of views, etc. However, it is not
7 uncommon for developers to want to add new functionality to ParaView to, for
8 example, add support to their new file format or incorporate a new filter into
9 ParaView. ParaView makes it possible to add new functionality by using an
10 extensive plugin mechanism.
11 
12 Plugins can be used to extend ParaView in several ways:
13 
14  * Add new readers, writers, filters
15  * Add custom GUI components such as toolbar buttons to perform common tasks
16  * Add new views in for display data
17 
18 Examples for different types of plugins are provided with the ParaView source
19 under `Examples/Plugins/`.
20 
21 This document has major sections:
22 
23  * First section covers how to use existing plugins in ParaView.
24  * Second section contains information for developers about writing new
25  plugins for ParaView.
26 
27 ## Using Plugins
28 
29 ### Types of plugins
30 
31 Plugins are distributed as shared libraries (`*.so` on Unix and macOS, and
32 `*.dll` on Windows). For a plugin to be loadable in ParaView, it must be built
33 with the same version of ParaView as it is expected to be deployed on. Plugins
34 can be classified into two broad categories:
35 
36  * *Server-side plugins*: These are plugins that extend the algorithmic
37  capabilities for ParaView. For example, new filters, readers or writers.
38  Since ParaView processes data on the server-side, these plugins need to be
39  loaded on the server.
40  * *Client-side plugins*: These are plugins that extend the ParaView GUI
41  including property panels for new filters, toolbars or views. These plugins
42  need to be loaded on the client.
43 
44 Oftentimes a plugin has both server-side as well as client-side components to
45 it. For example, a plugin that adds a new filter and a property panel that goes
46 with that filter. Such plugins need to be loaded both on the server as well as
47 the client.
48 
49 Generally, users don't have to worry whether a plugin is a server-side or
50 client-side plugin. Simply load the plugin on the server as well as the client.
51 ParaView will include relevant components from plugin on each of the processes.
52 
53 ### Loading plugins
54 
55 There are four ways for loading plugins:
56 
57  * *Using the GUI* (`Plugin Manager`)
58  - Plugins can be loaded into ParaView using the `Plugin Manager` accessible
59  from the `Tools | Manage Plugins/Extensions` menu. The `Plugin Manager`
60  has two sections for loading client plugins and server plugins (shown
61  only when connected to a server). To load a plugin on the client and
62  server side, simply browse to the plugin shared library. If the plugin is
63  loaded successfully, it will appear in the list of loaded plugins. The
64  `Plugin Manager` also lists the paths it searched to load plugins
65  automatically.
66  - The `Plugin Manager` remembers all loaded plugins across ParaView
67  instances, so once a plugin is loaded once, it will appear in the future
68  (unloaded).
69  - You can set up ParaView to automatically load the plugin at startup (for
70  client plugins) or on connecting to the server (for server plugins) by
71  checking the "Auto Load" checkbox on a loaded plugin.
72  - Plugin configuration file can also be added interactively in the UI by clicking on the
73  "Add plugin configuration file". This will add plugins to the list and potentially
74  load them if they are flagged as auto load plugins. Once a plugin have been added
75  this way, it is then saved in the settings. See below for the specification.
76 
77 ![Plugin Manager when not connected to a remote server, showing loaded plugins on the local site.](images/LocalPlugin_Manager.png)
78 ![Plugin Manager when connected to a server showing loaded plugins on the local as well as remote sites.](images/RemotePlugin_Manager.png)
79 
80  * *Using environment variable* (Auto-loading plugins)
81  - In order to have ParaView automatically load a set of plugins on startup,
82  one can use the `PV_PLUGIN_PATH` environment variable. `PV_PLUGIN_PATH`
83  may be used to list a set of directories (separated by colon (`:`) for
84  Unix platforms or semi-colon (`;`) on Windows) which ParaView will search
85  on startup to load plugins. This environment variable needs to be set on
86  both the client and server sides to load their respective plugins. Note
87  that plugins in PV_PLUGIN_PATH are always auto-loaded irrespective of the
88  status of the `Auto Load` checkbox in the `Plugin Manager`. Paths in this
89  list may also be of the structure created by the ParaView plugin macros
90  (e.g., `MyPlugin/MyPlugin.so`).
91  - Finer control can be used using the `PV_PLUGIN_CONFIG_FILE` environment
92  variable. `PV_PLUGIN_CONFIG_FILE` can be used to list a set of XML plugin
93  configuration files (separated by colon (`:`) on Unix platforms or
94  semi-colon (`;`) on Windows). ParaView will read these files on startup
95  to load specified plugins. The XML plugin configuration file format looks
96  like this (see below for the complete specification):
97 
98 ```xml
99 <?xml version="1.0"?>
100 <Plugins>
101  <Plugin name="MyPlugin" filename="/absolute/path/to/libMyPlugin.so"/>
102  <!-- Note that relative paths are calculated from the directory of this XML file. -->
103  <Plugin name="MyPluginRel" filename="relative/path/to/libMyPlugin.so"/>
104 </Plugins>
105 ```
106 
107  Plugins listed this way will always be loaded, irrespective of the status
108  of the `Auto Load` checkbox in the `Plugin Manager`.
109  * *Using the ParaView internal plugin configuration file `.plugins`*
110  (Make plugins available and possibly Auto-load plugins)
111  - Plugins that are listed in the `.plugins` file on the client and server
112  will automatically be listed in the `Plugin Manager`, and may optionally
113  be auto loaded. ParaView creates its own `.plugins` file listing plugins
114  known during its build and uses it as the default. An example `.plugins`
115  file, auto loading H5PartReader, looks like this (see below for the complete specification):
116 
117 ```xml
118 <?xml version="1.0"?>
119 <Plugins>
120  <Plugin name="Moments" auto_load="0"/>
121  <Plugin name="PrismPlugin" auto_load="0"/>
122  <Plugin name="PointSprite_Plugin" auto_load="0"/>
123  <Plugin name="pvblot" auto_load="0"/>
124  <Plugin name="H5PartReader" auto_load="1"/>
125 </Plugins>
126 ```
127 
128  * *Default search paths*
129  - Recognized locations are:
130  * A `plugins` subdirectory under the `paraview-X.Y` directory in the
131  library path (usually `lib` on Unix platforms and `bin` on Windows).
132 
133 ## Delayed load plugins
134 
135 When loading a plugin configuration file (see above), it is possible to specify certain parameters
136 that will affect how the plugin will be loaded, eg: `auto_load="1"` will load the plugin as soon as the
137 plugin configuration file is loaded.
138 
139 It is also possible to delay the loading of plugin until they are actually needed, by first
140 only loading the XML part of a plugin and loading the actual plugin shared library file only
141 when the proxy provided by the XML is created.
142 
143 This means that any part of the plugin that is visible to users but not described as XML, such as Qt additions,
144 will not be visible until a proxy is created.
145 
146 This also means that the XML of the plugin must be available alongside the actual plugin and this is where the specific
147 syntax of the plugin configuration file comes into play.
148 
149 A plugin configuration file for a delayed load plugin would look like this:
150 
151 ```
152 <?xml version="1.0"?>
153 <Plugins>
154  <Plugin name="ElevationFilter" auto_load="0" delayed_load="1">
155  <XML filename="ElevationFilter/MyElevationFilter.xml"/>
156  </Plugin>
157 </Plugins>
158 ```
159 
160 The `XML` `filename` argument is a relative or absolute path to a XML file.
161 
162 Such file can be automatically generated during the compilation of a ParaView plugin.
163 
164 It would look like this in the `CMakeLists.txt` of a plugin:
165 
166 ```
167 paraview_plugin_build(
168  RUNTIME_DESTINATION "${CMAKE_INSTALL_BINDIR}"
169  LIBRARY_DESTINATION "${CMAKE_INSTALL_LIBDIR}"
170  LIBRARY_SUBDIRECTORY "${PARAVIEW_PLUGIN_SUBDIR}"
171  PLUGINS_FILE_NAME "elev.plugins.xml"
172  DELAYED_LOAD ElevationFilter
173  PLUGINS ${plugins})
174 ```
175 
176 The XML files are also required to be listed as `SERVER_MANAGER_XMLS` in the `paraview_plugin_add` call,
177 XML files added using `paraview_add_server_manager_xmls` are not supported.
178 
179 ## Plugin configuration file XML Schema
180 
181 Here is the exhaustive plugin configuration file XML schema
182 
183 Complete example:
184 
185 ```
186 <?xml version="1.0"?>
187 <Plugins>
188  <Plugin name="PluginName" filename="relative/or/absolute/path/to/plugin.ext" auto_load="bool" delayed_load="bool">
189  <XML filename="relative/or/absolute/path/to/file.xml"/>
190  <XML filename="relative/or/absolute/path/to/file2.xml"/>
191  </Plugin>
192 </Plugins>
193 ```
194 
195  * The `<Plugins>` tag is the root element of the document, which contains zero-to-many `<Plugin>` tags.
196  * Each `<Plugin>` tag represents a plugin to add with the following configuration:
197  * The `name` attribute (required) uniquely identifies the plugin, it is also used to find the plugin filename in directories relative to the plugin configuration file if the `filename` attribute is not provided.
198  * The `filename` attribute (optional) is a relative or absolute path to the plugin file to be loaded.
199  * The `auto_load` attribute (optional) is a boolean that control if the plugin should be loaded as soon as the plugin is added.
200  * The `delayed_load` attribute (optional) is a boolean that control if the plugin should be *actually* loaded only when needed (see above).
201  * Each `<Plugin>` tag contains zero-to-many `<XML>` tags.
202  * Each `<XML>` tag represents a XML file associated with the plugin, only used in context of `delayed_load="1"`
203  * The `filename` attribute (required) is a relative or absolute path to a the XML file to be loaded in the context of the delayed load mechanism
204 
205 ## Debugging Plugins
206 
207 If plugin loading fails, the `PV_PLUGIN_DEBUG` environment variable may be set
208 for either the client or server processes. ParaView will then print verbose
209 information about each step and causes for failure, as show below.
210 
211 ```
212 ***************************************************
213 Attempting to load /home/utkarsh/Kitware/ParaView3/ParaView3Bin/bin/libSurfaceLIC.so
214 Loaded shared library successfully. Now trying to validate that it's a ParaView plugin.
215 Updating Shared Library Paths: /home/utkarsh/Kitware/ParaView3/ParaView3Bin/bin
216 Plugin instance located successfully. Now loading components from the plugin instance based on the interfaces it implements.
217 ----------------------------------------------------------------
218 Plugin Information:
219  Name : SurfaceLIC
220  Version : 1.0
221  ReqOnServer : 1
222  ReqOnClient : 1
223  ReqPlugins :
224  ServerManager Plugin : Yes
225  Python Plugin : No
226 ```
227 
228 # Writing plugins
229 
230 This section covers writing and compiling different types of Plugins. To create
231 a plugin, one must have their own build of ParaView. Binaries downloaded from
232 www.paraview.org do not include necessary header files or import libraries
233 (where applicable) for compiling plugins.
234 
235 The `CMakeLists.txt` file used in all following examples start off with the
236 following code:
237 
238 ```cmake
239 # ParaView requires CMake 3.8 in order to be used.
240 cmake_minimum_required(VERSION 3.8)
241 project(myplugin C CXX)
242 
243 find_package(ParaView REQUIRED)
244 ```
245 
246 Where CMake will ask for the `ParaView_DIR` which you point to the ParaView
247 build or install tree you would to build your with.
248 
249 Note that the `C` and `CXX` languages are required in general because ParaView
250 may need to find other packages which are written with only C in mind (MPI is
251 the usual culprit here) and need to know about the C compiler that is
252 available.
253 
254 ## Exposing an Existing Filter
255 
256 Sometimes, the filter that one wants to add to ParaView is already available in
257 VTK, it's just not exposed through the ParaView GUI. This is the easiest type
258 of plugin to create. There are two options:
259 
260  1. setup the plugin using only an XML file; and
261  2. actually compile the plugin into a shared library.
262 
263 The first option is the easiest, but the second option will prepare you for
264 creating a custom filter in the future as the process is nearly identical.
265 
266 ## Adding a New Filter
267 
268 It is also possible to add new filters to ParaView. The filter has to be a
269 VTK-based algorithm, written as following the standard procedures for writing
270 VTK algorithms. Generally for such cases where we are adding a new VTK class to
271 ParaView (be it a filter, reader or a writer), we need to do the following
272 tasks:
273 
274  * Write a *Server Manager Configuration XML* which describes the `Proxy`
275  interface for the filter. Basically, this defines the interface for the
276  client to create and modify instances of the new class on the server side.
277  Please refer to the [ParaView Guide][] for details about writing these
278  server-manager XMLs.
279  * Write a configuration XML for the GUI to make ParaView GUI aware of this
280  new class, if applicable. For filters, this is optional, since ParaView
281  automatically recognizes filters added through plugins and lists them in
282  the *Alphabetical* sub-menu. One may use the GUI configuration XML to add
283  the new filter to a specific category in the *Filters* menu, or add a new
284  category. For readers and writers, this is required since ParaView GUI
285  needs to know what extensions your reader/writer supports etc.
286 
287 ## Plugin Resources
288 
289 Plugins may access resources relative to themselves by using the
290 `paraview_plugin_add_location` interface to get the location of the plugin at
291 runtime. Note that this only works when built as a shared plugin. For static
292 plugins, the path will come in as a `nullptr`. If a plugin with resources
293 intends to support resources, it is recommended to use a `.qrc` file to embed
294 the resources into the plugin.
295 
296 For installation of resources, the `_paraview_build_plugin_directory` variable
297 contains the location of the plugin under the build and install prefixes. Build
298 resources may be placed under
299 `"${CMAKE_BINARY_DIR}/${_paraview_build_plugin_directory}"` and installed with
300 `DESTINATION "${_paraview_build_plugin_directory}"`. The plugin itself belongs
301 to the `${_paraview_build_PLUGINS_COMPONENT}` component, so resources should
302 generally use a component with a related name.
303 
304 ## Examples
305 
306 ### XML Plugins
307 
308 If you have not built ParaView from source, using an XML plugin is your only
309 option.
310 
311 First, a server manager XML for the filter is required. The GUI XML to add the
312 filter to any specific category is optional.
313 
314 For example, let's say we simply want to expose the `vtkCellDerivatives` filter
315 in VTK. Then first, we'll write the server manager configuration XML (call it
316 `CellDerivatives.xml`), similar to what we would have done for adding a new
317 filter.
318 
319 ```xml
320 <ServerManagerConfiguration>
321  <ProxyGroup name="filters">
322  <SourceProxy name="MyCellDerivatives" class="vtkCellDerivatives" label="My Cell Derivatives">
323  <Documentation
324  long_help="Create point attribute array by projecting points onto an elevation vector."
325  short_help="Create a point array representing elevation.">
326  </Documentation>
327  <InputProperty
328  name="Input"
329  command="SetInputConnection">
330  <ProxyGroupDomain name="groups">
331  <Group name="sources"/>
332  <Group name="filters"/>
333  </ProxyGroupDomain>
334  <DataTypeDomain name="input_type">
335  <DataType value="vtkDataSet"/>
336  </DataTypeDomain>
337  </InputProperty>
338  </SourceProxy>
339  </ProxyGroup>
340 </ServerManagerConfiguration>
341 ```
342 
343 At this point, we can stop and use the plugin in ParaView by loading the XML
344 file directly into the plugin manager.
345 
346 Please note that if you are writing the XML for a filter that takes just one
347 input, you *must* set the `name` attribute for the `InputProperty` XML element
348 to `Input`. If you do not, then the filter will not be displayed properly in
349 ParaView's pipeline browser.
350 
351 ### Compiling into a Shared Library
352 
353 If you have built ParaView from source, it is possible to compile the plugin
354 into into a shared library. To do this, we can use the following top-level:
355 `CMakeLists.txt`:
356 
357 ```cmake
358 # Standard CMake boilerplate. ParaView's `find_package` requires at least 3.8.
359 cmake_minimum_required(VERSION 3.8)
360 project(sharedlibrary)
361 
362 # These five lines are required in order to set up installation directories
363 # (which also control build directory locations) and enable shared builds
364 # (CMake's default is for a static build).
365 include(GNUInstallDirs)
366 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
367 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
368 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
369 set(BUILD_SHARED_LIBS ON)
370 
371 # Find ParaView. This will bring in ParaView's CMake API and imported targets.
372 find_package(ParaView REQUIRED)
373 
374 # Scan the plugin file in order to set up internal data structures for building
375 # plugins.
376 paraview_plugin_scan(
377  # The `paraview.plugin` file describing the plugin.
378  PLUGIN_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Plugin/paraview.plugin"
379  # A result variable for the (enabled) plugins found during the scan.
380  PROVIDES_PLUGINS plugins
381  # Enable plugins during this scan by default.
382  ENABLE_BY_DEFAULT ON)
383 
384 # Build the plugins discovered during the scan.
385 paraview_plugin_build(
386  PLUGINS ${plugins})
387 ```
388 
389 The mentioned `paraview.plugin` file describes the plugin to the build system:
390 
391 ```cmake
392 NAME
393  CellDerivatives
394 DESCRIPTION
395  Expose the vtkCellDerivatives class to ParaView.
396 REQUIRES_MODULES
397  # This module provides the `vtkCellDerivatives` filter.
398  VTK::FiltersGeneral
399 ```
400 
401 In the `Plugin` directory (beside the `paraview.plugin` file), the plugin is
402 given the information it needs to build:
403 
404 ```cmake
405 paraview_add_plugin(CellDerivatives
406  VERSION "1.0"
407  SERVER_MANAGER_XML CellDerivatives.xml)
408 ```
409 
410 Then using CMake, one can build a plugin for this new filter. We can now load
411 the plugin through the plugin manager by selecting the created `.so` or `.dll`
412 file.
413 
414 ### Qt resource plugins
415 
416 Similarly compiled Qt resources (`*.bqrc`) can be loaded at runtime. A `.bqrc`
417 file is a binary file containing resources which can include icons, the GUI
418 configuration XML for adding categories, etc. A `.bqrc` can be made from a
419 `.qrc` by running the `rcc` utility provided by Qt:
420 
421 ```sh
422 rcc -binary -o myfile.bqrc myfile.qrc
423 ```
424 
425 ### Adding a New Filter
426 
427 For this example, refer to `Examples/Plugins/ElevationFilter` in the ParaView
428 source. Let's say we have written a new `vtkMyElevationFilter`
429 (`vtkMyElevationFilter.{h,cxx}`), which extends the functionality of the
430 `vtkElevationFilter` and we want to package that as a plugin for ParaView. For
431 starters, we simply want to use this filter in ParaView (e.g., not doing
432 anything fancy with *Filters* menu categories). As described, we need to write
433 the server manager configuration XML (`MyElevationFilter.xml`). Once that's
434 done, we write a `CMakeLists.txt` file to package this into a plugin.
435 
436 This `CMakeLists.txt` needs to include the following lines:
437 
438 ```cmake
439 cmake_minimum_required(VERSION 3.8)
440 project(newfilter)
441 
442 include(GNUInstallDirs)
443 set(BUILD_SHARED_LIBS ON)
444 
445 find_package(ParaView REQUIRED)
446 
447 paraview_plugin_scan(
448  PLUGIN_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Plugin/paraview.plugin"
449  PROVIDES_PLUGINS plugins
450  ENABLE_BY_DEFAULT ON)
451 
452 paraview_plugin_build(
453  PLUGINS ${plugins})
454 ```
455 
456 The referenced `paraview.plugin` file contains:
457 
458 ```cmake
459 NAME
460  ElevationFilter
461 DESCRIPTION
462  An example paraview plugin containing server manager XML and the server
463  manager classes to build. This plugin can be loaded on the server side.
464 REQUIRES_MODULES
465  VTK::CommonCore
466  VTK::FiltersCore
467 ```
468 
469 And the `CMakeLists.txt` file beside it contains:
470 
471 ```cmake
472 paraview_add_plugin(ElevationFilter
473  VERSION "1.0"
474  MODULES ElevationFilters
475  MODULE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/ElevationFilters/vtk.module")
476 ```
477 
478 Because we are building our own custom filter, it needs to be a VTK module in
479 order to support having its information available to the XML code. First, the
480 module is declared in a `vtk.module` file:
481 
482 ```cmake
483 NAME
484  ElevationFilters
485 DEPENDS
486  VTK::FiltersCore
487 PRIVATE_DEPENDS
488  VTK::CommonCore
489 ```
490 
491 And then the module is built with its associated server manager XML file
492 attached to the module. Note that the module name cannot be the same as the
493 plugin name due to the way the library targets are managed internally.
494 
495 ```cmake
496 set(classes
497  vtkMyElevationFilter)
498 
499 # Find external packages here using `find_package`.
500 
501 vtk_module_add_module(ElevationFilters
502  CLASSES ${classes})
503 
504 # Link to external packages here using `vtk_module_link(ElevationFilters)`.
505 
506 paraview_add_server_manager_xmls(
507  XMLS MyElevationFilter.xml)
508 ```
509 
510 Then using CMake, one can build a plugin for this new filter. Once this plugin
511 is loaded the filter will appear under the *Alphabetical* list in the *Filters*
512 menu. Note that there will be two libraries in the resulting directory. Be sure
513 to load the `ElevationFilter` one which is the plugin, not the
514 `ElevationFilters` module library.
515 
516 ### Filters with Multiple Input Ports
517 
518 If a filter requires multiple input ports, there are two options:
519 
520  1. Create helper functions in the VTK filter such as `SetYourInputName` which
521  deal with addressing the VTK pipeline in the C++ code; and
522  2. Address/access the input connection by number in the XML. The `port_index`
523  property specifies which input connection the particular input will be
524  connected to. The `SetInputConnection` function is the command that will
525  actually be called with this `port_index` to setup the pipeline.
526 
527 An example XML file for a filter with multiple inputs is below. The filter
528 takes three `vtkPolyData` objects as input.
529 
530 ```xml
531 <ServerManagerConfiguration>
532  <ProxyGroup name="filters">
533  <SourceProxy name="LandmarkTransformFilter" class="vtkLandmarkTransformFilter" label="LandmarkTransformFilter">
534  <Documentation
535  long_help="Align two point sets using vtkLandmarkTransform to compute the best transformation between the two point sets."
536  short_help="vtkLandmarkTransformFilter.">
537  </Documentation>
538 
539  <InputProperty
540  name="SourceLandmarks"
541  port_index="0"
542  command="SetInputConnection">
543  <ProxyGroupDomain name="groups">
544  <Group name="sources"/>
545  <Group name="filters"/>
546  </ProxyGroupDomain>
547  <DataTypeDomain name="input_type">
548  <DataType value="vtkPolyData"/>
549  </DataTypeDomain>
550  <Documentation>
551  Set the source data set. This data set that will move towards the target data set.
552  </Documentation>
553  </InputProperty>
554 
555  <InputProperty
556  name="TargetLandmarks"
557  port_index="1"
558  command="SetInputConnection">
559  <ProxyGroupDomain name="groups">
560  <Group name="sources"/>
561  <Group name="filters"/>
562  </ProxyGroupDomain>
563  <DataTypeDomain name="input_type">
564  <DataType value="vtkPolyData"/>
565  </DataTypeDomain>
566  <Documentation>
567  Set the target data set. This data set will stay stationary.
568  </Documentation>
569  </InputProperty>
570 
571  <InputProperty
572  name="SourceDataSet"
573  port_index="2"
574  command="SetInputConnection">
575  <ProxyGroupDomain name="groups">
576  <Group name="sources"/>
577  <Group name="filters"/>
578  </ProxyGroupDomain>
579  <DataTypeDomain name="input_type">
580  <DataType value="vtkPolyData"/>
581  </DataTypeDomain>
582  <Documentation>
583  Set the source data set landmark points.
584  </Documentation>
585  </InputProperty>
586 
587  <Hints>
588  <!-- see below for what options to put here -->
589  </Hints>
590 
591  </SourceProxy>
592  </ProxyGroup>
593 </ServerManagerConfiguration>
594 ```
595 
596 To set the inputs in ParaView, simply select one of the inputs in the *Pipeline
597 Browser* and then select the filter from the *Filters* menu. This will open a
598 dialog box which will allow you to specify which object to connect to each
599 input port.
600 
601 ### Adding *Categories* to the *Filters* Menu
602 
603 Now suppose we want to add a new category to the *Filters* menu, called
604 *Extensions* and then show this filter in that menu. In that case we need to
605 add a hint to the XML file that tells ParaView what category to display this
606 filter in. In this case, the `Hints` element of the XML file can contain:
607 
608 ```xml
609 <Hints>
610  <ShowInMenu category="Extensions" />
611 </Hints>
612 ```
613 
614 If the name of the category is same as an already existing category such as
615 *Data Analysis*, then the filter gets added to the existing category.
616 
617 ### Adding Icons
618 
619 You can see that some filters in the *Filters* menu (e.g., *Clip*) have icons
620 associated with them. It's possible for the plugin to add icons for filters it
621 adds as well. For that you need to write a Qt resource file (say
622 `MyElevation.qrc`) as follows:
623 
624 ```xml
625 <RCC>
626  <qresource prefix="/MyIcons" >
627  <file>MyElevationIcon.png</file>
628  </qresource>
629 </RCC>
630 ```
631 
632 To use the icon for a filter in the pipeline add the following hint to the
633 server manager XML.
634 
635 ```xml
636 <Hints>
637  <ShowInMenu icon=":/MyIcons/MyElevationIcon.png" />
638 </Hints>
639 ```
640 
641 Finally, the plugin's `CMakeLists.txt` file much change to include our
642 `MyElevation.qrc` file as follows:
643 
644 ```cmake
645 paraview_add_plugin(ElevationFilter
646  VERSION "1.0"
647  MODULES ElevationFilters
648  MODULE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/ElevationFilters/vtk.module"
649  UI_RESOURCES MyElevation.qrc)
650 ```
651 
652 ### Adding GUI Parameters
653 
654 Simply add these in the server manager XML to expose parameters of the filter
655 to the ParaView user.
656 
657 #### Button property
658 
659 This property appears as a button containing the text described by the value of the "name" attribute, here: "My Button".
660 When a user click on the button, a call to the command, `MyButtonClicked` is performed directly on VTK class associated with the proxy.
661 
662 ```xml
663 <Property
664  name="MyButton"
665  command="MyButtonClicked">
666 </Property>
667 ```
668 
669 #### Integer Property
670 
671 This property appears as a text box.
672 
673 ```xml
674 <IntVectorProperty
675  name="bStartByMatchingCentroids"
676  command="SetbStartByMatchingCentroids"
677  number_of_elements="1"
678  default_values="1">
679 </IntVectorProperty>
680 ```
681 
682 #### Boolean Property
683 
684 This property appears as a check box control. A boolean property uses the
685 `IntVectorProperty` with an extra line (`BooleanDomain`) indicating this should
686 be a check box rather than a text field.
687 
688 ```xml
689 <IntVectorProperty
690  name="bStartByMatchingCentroids"
691  command="SetbStartByMatchingCentroids"
692  number_of_elements="1"
693  default_values="1">
694  <BooleanDomain name="bool"/>
695 </IntVectorProperty>
696 ```
697 
698 #### String Property
699 
700 This property appears as a text box.
701 
702 ```xml
703 <StringVectorProperty
704  name="YourStringVariable"
705  command="SetYourStringVariable"
706  number_of_elements="1"
707  default_values="1">
708 </StringVectorProperty>
709 ```
710 
711 #### Double Property
712 
713 This property appears as a text box.
714 
715 ```xml
716 <DoubleVectorProperty
717  name="YourDoubleVariable"
718  command="SetYourDoubleVariable"
719  number_of_elements="1"
720  default_values="1">
721 </DoubleVectorProperty>
722 ```
723 
724 #### Multi-Value Double Property
725 
726 This property appears as a text box.
727 
728 ```xml
729 <DoubleVectorProperty
730  name="YourDoubleVectorVariable"
731  command="SetYourDoubleVectorVariable"
732  number_of_elements="3"
733  default_values="1.0 0.0 0.0">
734 </DoubleVectorProperty>
735 ```
736 
737 #### Double Property Slider
738 
739 This creates a slider that ranges from `0.0` to `1.0`.
740 
741 ```xml
742 <DoubleVectorProperty
743  name="PercentToRemove"
744  command="SetPercentToRemove"
745  number_of_elements="1"
746  default_values="0.1">
747  <DoubleRangeDomain name="range" min="0.0" max="1.0" />
748 </DoubleVectorProperty>
749 ```
750 
751 #### Drop Down List
752 
753 This creates a drop down list with 3 choices. The values associated with the
754 choices may be specified.
755 
756 ```xml
757 <IntVectorProperty
758  name="TransformMode"
759  command="SetTransformMode"
760  number_of_elements="1"
761  default_values="1">
762  <EnumerationDomain name="enum">
763  <Entry value="6" text="RigidBody"/>
764  <Entry value="7" text="Similarity"/>
765  <Entry value="12" text="Affine"/>
766  </EnumerationDomain>
767  <Documentation>
768  This property indicates which transform mode will be used.
769  </Documentation>
770 </IntVectorProperty>
771 ```
772 
773 #### Drop Down List with Values from Input Arrays
774 
775 This creates a list that lets you choose among the input arrays of the input of
776 a `ProgrammableFilter`:
777 
778 ```xml
779 <StringVectorProperty
780  name="SelectInputScalars"
781  label="Array"
782  command="SetInputArrayToProcess"
783  number_of_elements="5"
784  element_types="0 0 0 0 2"
785  animateable="0">
786  <ArrayListDomain
787  name="array_list"
788  attribute_type="Scalars"
789  input_domain_name="inputs_array">
790  <RequiredProperties>
791  <Property name="Input" function="Input" />
792  </RequiredProperties>
793  </ArrayListDomain>
794 </StringVectorProperty>
795 ```
796 
797 This will look like the following image:
798 
799 ![Drop down list with values from input arrays](images/DropboxWithInputArrays.jpg)
800 
801 #### Drop Down List with Values from Input File
802 
803 If you need to populate a list with values from a file and be able to
804 select/deselect list entries (e.g., to pick which variables are loaded from the
805 file), use a XML similar to this:
806 
807 ```xml
808 <StringVectorProperty information_only="1"
809  name="CellArrayInfo">
810  <ArraySelectionInformationHelper attribute_name="Cell" />
811 </StringVectorProperty>
812 <StringVectorProperty
813  command="SetCellArrayStatus"
814  element_types="2 0"
815  information_property="CellArrayInfo"
816  label="Cell Arrays"
817  name="CellArrayStatus"
818  number_of_elements="0"
819  number_of_elements_per_command="2"
820  repeat_command="1">
821  <ArraySelectionDomain name="array_list">
822  <RequiredProperties>
823  <Property function="ArrayList"
824  name="CellArrayInfo" />
825  </RequiredProperties>
826  </ArraySelectionDomain>
827  <Documentation>
828  This property lists which cell-centered arrays to read.
829  </Documentation>
830 </StringVectorProperty>
831 ```
832 
833 You can see an example in use in ParaView's [core readers][] XML.
834 
835 You may also do it in the following manner:
836 
837 ```xml
838 <StringVectorProperty
839  command="SetCellArrayStatus"
840  element_types="2 0"
841  information_property="CellArrayInfo"
842  label="Cell Arrays"
843  name="CellArrayStatus"
844  number_of_elements="0"
845  number_of_elements_per_command="2"
846  repeat_command="1">
847  <ArrayListDomain name="array_list"
848  attribute_type="Scalars"
849  input_domain_name="inputs_array">
850  <RequiredProperties>
851  <Property name="Input" function="Input" />
852  </RequiredProperties>
853  </ArrayListDomain>
854 </StringVectorProperty>
855 ```
856 
857 In which case the result will look like this:
858 
859 ![Drop down list with values from input file](images/DropdownListFromFile.jpg)
860 
861 <!-- TODO: port this section
862 #### Tutorials for creating filters ====
863 
864 Go to this page for the main article for the tutorials: [[Python Filters Tutorials]]
865 -->
866 
867 #### Adding a Reader
868 
869 Adding a new reader through a plugin is similar to adding a filter. The only
870 difference is that we do not need to specify what category the reader should be
871 added to in the GUI. For the latest version of ParaView we do not need to
872 specify anything special for the GUI as all of the details of the reader are
873 available in the XML proxy definition of the reader. For ParaView version 4.0.1
874 and earlier we need the XML to define what file extensions this reader can
875 handle. This XML (`MyReaderGUI.xml`) looks like this:
876 
877 ```xml
878 <ParaViewReaders>
879  <Reader name="MyPNGReader" extensions="png"
880  file_description="My PNG Files">
881  </Reader>
882 </ParaViewReaders>
883 ```
884 
885 An example `MyPNGReader.xml` is shown below. In almost all cases you must have
886 a `SetFileName` function property. You are free to have other properties as
887 well, as with a standard (non-reader) filter. Also, the `Hints` section is needed
888 in order to associate the file extension with the reader on the client. The
889 `ReaderFactory` hint is what the client uses to identify readers from sources.
890 
891 Optionally, you can provide an information property `RegistrationName` to specify the reader
892 pipeline name to use. `RegistrationName` is a feature available for any proxy, not just readers.
893 
894 ```xml
895 <ServerManagerConfiguration>
896  <ProxyGroup name="sources">
897  <SourceProxy name="MyPNGReader" class="vtkMyPNGReader" label="PNGReader">
898  <Documentation
899  long_help="Read a PNG file."
900  short_help="Read a PNG file.">
901  </Documentation>
902  <StringVectorProperty
903  name="FileName"
904  animateable="0"
905  command="SetFileName"
906  number_of_elements="1">
907  <FileListDomain name="files"/>
908  <Documentation>
909  This property specifies the file name for the PNG reader.
910  </Documentation>
911  </StringVectorProperty>
912 
913  <StringVectorProperty
914  name="RegistrationName"
915  number_of_elements="1"
916  default_values="MyCustomName"
917  command="GetRegistrationName"
918  panel_visibility="never"
919  information_only="1">
920  <Documentation>
921  This property specify the pipeline name for the reader, using the return value of `command`.
922  If `command` attributes is not specified, it uses `default_values`.
923  </Documentation>
924  </StringVectorProperty>
925 
926  <Hints>
927  <ReaderFactory extensions="png"
928  file_description="PNG File Format" />
929  </Hints>
930  </SourceProxy>
931  </ProxyGroup>
932 </ServerManagerConfiguration>
933 ```
934 
935 The CMake code for a reader plugin uses the same structure as the filter
936 example. The only likely difference is that the plugin should also pass
937 `REQUIRED_ON_SERVER` to `paraview_add_plugin` since the server side needs the
938 reader available for its use.
939 
940 If you want your reader to work correctly with a file series, please refer to [[Animating legacy VTK file series#Making custom readers work with file series|file series animation]] for details.
941 
942 Once you generate the project using CMake and compile the project, in ParaView
943 go to *Tools > Manage Plugins/Extensions*. Under *Local Plugins*, click *Load
944 New* and browse for the shared library file you just created. You should now
945 see your new file type in the *Files of type* list in the *Open file* dialog.
946 
947 #### Adding a Writer
948 
949 Similar to a reader plugin, for a writer plugin we need to tell ParaView what
950 extensions this writer supports. For the current version of ParaView this is
951 done in the `Hints` section of the server manager XML definition as follows:
952 
953 ```xml
954 <Hints>
955  <WriterFactory extensions="tif"
956  file_description="My Tiff Files" />
957 </Hints>
958 ```
959 
960 #### Adding Customizations for Properties Panel
961 
962 <!-- TODO link to external page -->
963 
964 [[ParaView/Properties Panel|Properties Panel]] is the primary panel in ParaView
965 used to change the parameters for visualization modules and displays. Plugins
966 can provide new types of [`pqPropertyWidget`][pqPropertyWidget] subclasses that
967 can be used to control properties/property groups on this Properties panel.
968 
969 To register a new `pqPropertyWidget` subclass to be associated with a
970 particular widget type for a property (`vtkSMProperty`), use the following
971 CMake code in your plugin:
972 
973 ```cmake
974 paraview_plugin_add_property_widget
975  KIND kind
976  TYPE my_property_widget_type
977  CLASS_NAME ClassName
978  INTERFACES interfaces
979  SOURCES sources)
980 
981 paraview_add_plugin(propwidget
982  VERSION "1.0"
983  UI_INTERFACES ${interfaces}
984  SOURCES ${sources})
985 ```
986 
987 The `KIND` argument must be one of `WIDGET`, `GROUP_WIDGET`, or
988 `WIDGET_DECORATOR`. For a `vtkSMProperty`, `WIDGET` is required.
989 
990 The `CLASS_NAME` argument must refer to a `pqPropertyWidget` subclass with a
991 constructor with the following signature:
992 
993 ```cpp
994 ClassName(vtkSMProxy *smproxy, vtkSMProperty *smproperty, QWidget *parentObject)
995 ```
996 
997 The `TYPE` argument specifies the string that will be used in the server
998 manager XML as the value for the `panel_widget` attribute to request creation
999 of this widget for a `vtkSMProperty` subclass.
1000 
1001 To register a new `pqPropertyWidget` subclass to be associated with a
1002 particular widget type for a property group (`vtkSMPropertyGroup`), use
1003 `GROUP_WIDGET` for the `KIND` argument. The referenced `CLASS_NAME` must
1004 subclass `pqPropertyWidget` and have a constructor with the signature:
1005 
1006 ```cpp
1007 ClassName(vtkSMProxy *smproxy, vtkSMPropertyGroup *smgroup, QWidget *parentObject);
1008 ```
1009 
1010 As before, the `TYPE` specifies the string that will be used in the server
1011 manager XML as the value for the `panel_widget` attribute on a `PropertyGroup`
1012 element to request creation of this widget for that group.
1013 
1014 Another mechanism for adding customizations for *Properties* panel is to
1015 provide [`pqPropertyWidgetDecorator`][pqPropertyWidgetDecorator] subclasses to
1016 add custom control logic for widgets on the panel.
1017 
1018 Decorators use the `WIDGET_DECORATOR` argument to `KIND`.
1019 
1020 The `CLASS_NAME` must point to a `pqPropertyWidgetDecorator` subclass and the
1021 `TYPE` is the string name used to request the creation of the decorator in the
1022 server manager XML as described [[ParaView/Properties Panel|here]].
1023 
1024 An example for customizing the Properties panel can be found in the ParaView
1025 source under `Examples/Plugins/PropertyWidgets`.
1026 
1027 #### Adding Documentation for Plugins
1028 
1029 Developers can provide documentation for plugins that is shown in ParaView's
1030 *Help* window. There are two mechanisms for adding documentation from plugins.
1031 
1032  * Any server manager XML files added directly to the `paraview_add_plugin`
1033  function or those attached to modules passed to its `MODULES` argument
1034  using `paraview_add_server_manager_xmls` are automatically parsed to
1035  process `Documentation` elements. HTML pages summarizing the proxy and
1036  properties are automatically generated. This ensures that when the user
1037  clicks "?" for a filter or source added via the plugin, the help window
1038  shows appropriate help pages.
1039  * Using the `DOCUMENTATION_DIR` argument to `paraview_add_plugin` to specify
1040  a directory containing HTML pages and images that gets added a the
1041  documentation for the plugin (in addition to the documentation generated
1042  using the `SERVER_MANAGER_XML` files. For example:
1043 
1044 ```cmake
1045 paraview_add_plugin(SurfaceLIC
1046  VERSION "1.0"
1047  DOCUMENTATION_DIR "${CMAKE_CURRENT_SOURCE_DIR}/doc")
1048 ```
1049 
1050 This results in adding documentation to the *ParaView Online Help* when the
1051 plugin is loaded, as shown below.
1052 
1053 ![](images/Paraview_doc_plugin.png)
1054 
1055 It is also possible to customize further the documentation with 3 additional
1056 options:
1057 
1058  * `DOCUMENTATION_ADD_PATTERNS`: If specified, add patterns to search for the
1059  documentation files within `DOCUMENTATION_DIR` other than the default ones
1060  (i.e. `*.html`, `*.css`, `*.png`, `*.js` and `*.jpg`). This can be used to
1061  add new file extension (ex: `*.txt`) or even subdirectories (ex:
1062  `subDir/*.*`). Subdirectory hierarchy is kept so if you store all of your
1063  images in a `img/` sub directory and if your html file is at the root level
1064  of your documentation directory, then you should reference them using
1065  `<img src="img/my_image.png"/>` in the html file.
1066  * `DOCUMENTATION_TOC`: If specified, the function will use the given string to
1067  describe the table of content for the documentation. A TOC is divided into
1068  sections. Every section points to a specific file (`ref` keyword) that is
1069  accessed when selected in the UI. A section that contains other
1070  sections can be folded into the UI. An example of such a string is:
1071 
1072 ```html
1073 <toc>
1074  <section title="Top level section title" ref="page1.html">
1075  <section title="Page Title 1" ref="page1.html"/>
1076  <section title="Sub section Title" ref="page2.html">
1077  <section title="Page Title 2" ref="page2.html"/>
1078  <section title="Page Title 3" ref="page3.html"/>
1079  </section>
1080  </section>
1081 </toc>
1082 ```
1083 
1084  * `DOCUMENTATION_DEPENDENCIES`: Targets that are needed to be built before
1085  actually building the documentation. This can be useful when the plugin
1086  developer relies on a third party documentation generator like Doxygen for
1087  example.
1088 
1089 #### Adding a Toolbar
1090 
1091 Filters, reader, and writers are by far the most common ways for extending
1092 ParaView. However, ParaView plugin functionality goes far beyond that. The
1093 following sections cover some of these advanced plugins that can be written.
1094 
1095 Applications use toolbars to provide easy access to commonly used
1096 functionality. It is possible to have plugins that add new toolbars to
1097 ParaView. The plugin developer implements his own C++ code to handle the
1098 callback for each button on the toolbar. Hence one can do virtually any
1099 operation using the toolbar plugin with some understanding of the ParaView
1100 Server Manager framework and the ParaView GUI components.
1101 
1102 Please refer to `Examples/Plugins/SourceToolbar` for this section. There we are
1103 adding a toolbar with two buttons to create a sphere and a cylinder source. For
1104 adding a toolbar, one needs to implement a subclass for
1105 [`QActionGroup`][QActionGroup] which adds the [`QAction`][QAction]s for each of
1106 the toolbar button and then implements the handler for the callback when the
1107 user clicks any of the buttons. In the example `SourceToobarActions.{h,cxx}` is
1108 the `QActionGroup` subclass that adds the two tool buttons.
1109 
1110 To build the plugin, the `CMakeLists.txt` file is:
1111 
1112 ```cmake
1113 # This is a macro for adding QActionGroup subclasses automatically as toolbars.
1114 paraview_plugin_add_action_group(
1115  CLASS_NAME SourceToolbarActions
1116  GROUP_NAME "ToolBar/SourceToolbar"
1117  INTERFACES interfaces
1118  SOURCES sources)
1119 
1120 # Now create a plugin for the toolbar. Here we pass the `interfaces` and
1121 # `sources` returned by the above call.
1122 paraview_add_plugin(SourceToolbar
1123  VERSION "1.0"
1124  UI_INTERFACES ${interfaces}
1125  SOURCES ${sources}
1126  SourceToolbarActions.cxx)
1127 ```
1128 
1129 For the `GROUP_NAME`, we are using `ToolBar/SourceToolbar`; here `ToolBar` is a
1130 keyword which implies that the action group is a toolbar (and shows up under
1131 *View > Toolbars* menu) with the name `SourceToolbar`. When the plugin is
1132 loaded, this toolbar will show up with two buttons.
1133 
1134 #### Adding a Menu
1135 
1136 Adding a menu to the menu bar of the main window is almost identical to adding
1137 a toolbar. The only difference is that you use the keyword `MenuBar` in lieu
1138 of `ToolBar` in the `GROUP_NAME` of the action group. So if you change the
1139 `paraview_plugin_add_action_group` command above to the following, the plugin
1140 will add a menu titled `MyActions` to the menu bar.
1141 
1142 ```cmake
1143 paraview_plugin_add_action_group(
1144  CLASS_NAME SourceToolbarActions
1145  GROUP_NAME "MenuBar/MyActions"
1146  INTERFACES interfaces
1147  SOURCES sources)
1148 ```
1149 
1150 If you give the name of an existing menu, then the commands will be added to
1151 that menu rather than create a new one. So, for example, if the `GROUP_NAME`
1152 is `MenuBar/File`, the commands will be added to the bottom of the *File* menu.
1153 
1154 #### Adding a Context Menu
1155 
1156 Context menus are popup menus created when a user right-clicks inside a view
1157 (typically a render-view, but possible with any view). You can register a plugin
1158 that will create a context menu with items specific to the object underneath the
1159 cursor at the time the user right-clicks. The first instance of
1160 `pqContextMenuInterface` that returns a non-null menu is used; returning a
1161 null menu indicates the object(s) selected when the user right clicks are not
1162 relevant to your interface. For this reason, your subclass should avoid creating
1163 menus unrelated to a specific application or object type.
1164 
1165 To add a `pqContextMenuInterface` subclass to ParaView, simply pass your class
1166 name to the `UI_INTERFACES` argument of `paraview_add_plugin()` and the source,
1167 as usual, to the `SOURCES` argument:
1168 
1169 ```cmake
1170 paraview_add_plugin(FancyMenu
1171  ...
1172  UI_INTERFACES FancyMenu
1173  SOURCES FancyMenu.h FancyMenu.cxx
1174  ...
1175 )
1176 ```
1177 
1178 See the `Examples/Plugins/ContextMenu` directory for a simple example.
1179 
1180 #### Autostart Plugins
1181 
1182 This refers to a plugin which needs to be notified when ParaView starts up or
1183 the plugin is loaded which ever happens later and then notified when ParaView
1184 quits. Example is in `Examples/Plugins/Autostart` in the ParaView source. For
1185 such a plugin, we need to provide a `QObject` subclass
1186 (`pqMyApplicationStarter`) with methods that need to be called on startup and
1187 shutdown.
1188 
1189 ```cpp
1190 class pqMyApplicationStarter : public QObject
1191 {
1192 Q_OBJECT
1193 public:
1194  // Callback for startup.
1195  // This cannot take any arguments
1196  void onStartup();
1197 
1198  // Callback for shutdown.
1199  // This cannot take any arguments
1200  void onShutdown();
1201 };
1202 ```
1203 
1204 The `CMakeLists.txt` looks as follows:
1205 
1206 ```cmake
1207 # Macro for auto-start plugins. We specify the class name and the methods to
1208 # call on startup and shutdown on an instance of that class. It returns the
1209 # interface and sources created in the variables passed to the `INTERFACES` and
1210 # `SOURCES` arguments, respectively.
1211 paraview_plugin_add_auto_start(
1212  CLASS_NAME pqMyApplicationStarter # the class name for our class
1213  STARTUP onStartup # specify the method to call on startup
1214  SHUTDOWN onShutdown # specify the method to call on shutdown
1215  INTERFACES interfaces
1216  SOURCES sources)
1217 
1218 # Create a plugin for this starter
1219 paraview_add_plugin(Autostart
1220  VERSION "1.0"
1221  UI_INTERFACES ${interfaces}
1222  SOURCES pqMyApplicationStarter.cxx ${interfaces})
1223 ```
1224 
1225 #### Getting the Location of a Dynamically-Loaded Plugin
1226 
1227 Some dynamically-loaded plugins include data or text files in the same
1228 directory as the plugin binary object (DLL or shared object). To locate
1229 these files at runtime, plugins can register a callback that is notified
1230 with the file system location of the plugin when it is loaded. To do this,
1231 we need to provide a `QObject` subclass (`pqMyLocationPlugin`) with a
1232 method to store the plugin location.
1233 
1234 ```cpp
1235 class pqMyPluginLocation : public QObject
1236 {
1237 Q_OBJECT
1238 public:
1239  // Callback when plugin is loaded.
1240  void StoreLocation(const char* location);
1241 };
1242 ```
1243 
1244 The `CMakeLists.txt` looks as follows:
1245 
1246 ```cmake
1247 # Macro for adding the location callback. We specify the class name and the
1248 # method to call with the filesystem location as `CLASS_NAME` and `STORE`
1249 # arguments. It returns the interface and sources created in the variables
1250 # passed to the `INTERFACES` and `SOURCES` arguments.
1251 paraview_plugin_add_location(
1252  CLASS_NAME pqMyPluginLocation # the class name for our class
1253  STORE StoreLocation # the method to call when the plugin is loaded
1254  INTERFACES interfaces
1255  SOURCES sources)
1256 ```
1257 
1258 #### Adding new Representations for 3D View using Plugins
1259 
1260 ParaView's 3D view the most commonly used view for showing polygonal or
1261 volumetric data. By default, ParaView provides representation-types for showing
1262 the dataset as surface, wireframe, points etc. It’s possible to add
1263 representations using plugins that extends this set of available
1264 representation types.
1265 
1266 Before we start looking at how to write such a plugin, we need to gain some
1267 understanding of the 3D view and its representations. The 3D view uses 3 basic
1268 representation proxies for rendering all types of data:
1269 
1270  * (representations, `UnstructuredGridRepresentation`) – for
1271  `vtkUnstructuredGrid` or a composite dataset consisting of
1272  `vtkUnstructuredGrid`.
1273  * (representations, `UniformGridRepresentation`) – for `vtkImageData` or a
1274  composite dataset consisting of `vtkImageData`
1275  * (representations, `GeometryRepresentation`) – for all other data types.
1276 
1277 Each of these representation proxies are basically composite representation
1278 proxies that use other representation proxies to do the actual rendering, e.g.,
1279 `GeometryRepresentation` uses `SurfaceRepresentation` for rendering the data as
1280 wireframe, points, surface, and surface-with-edges and `OutlineRepresentation`
1281 for rendering an outline for the data. Subsequently, the 3 composite
1282 representation proxies provide a property named `Representation` which allows
1283 the user to pick the representation type he wants to see the data as. The
1284 composite representation proxy has logic to enable one of its internal
1285 representations based on the type chosen by the user.
1286 
1287 These 3 composite representation types are fixed and cannot be changed by
1288 plugins. What plugins can do is add more internal representations to any of
1289 these 3 composite representations to support new representations types that the
1290 user can choose using the representation type combo box on the display tab or
1291 in the toolbar.
1292 
1293 ![Representation type combo-box allowing user to choose the sub-representation to use](images/Representationplugin.png)
1294 
1295 ##### Using a New Mapper
1296 
1297 In this example, we see how to integrate a special polydata mapper written in
1298 VTK into ParaView. Let’s say the mapper is called `vtkMySpecialPolyDataMapper`
1299 which is simply a subclass of `vtkPainterPolyDataMapper`. In practice,
1300 `vtkMySpecialPolyDataMapper` can internally use different painters to do
1301 perform special rendering tasks.
1302 
1303 To integrate this mapper into ParaView, first we need to create a
1304 `vtkSMRepresentationProxy` subclass for that uses this mapper. In this example,
1305 since the mapper is a simple replacement for the standard
1306 `vtkPainterPolyDataMapper`, we can define our representation proxy as a
1307 specialization of the `SurfaceRepresentation` as follows:
1308 
1309 ```xml
1310 <ServerManagerConfiguration>
1311  <ProxyGroup name="representations">
1312  <RepresentationProxy
1313  name="MySpecialRepresentation"
1314  class="vtkMySpecialRepresentation"
1315  processes="client|renderserver|dataserver"
1316  base_proxygroup="representations"
1317  base_proxyname="SurfaceRepresentation">
1318  <Documentation>
1319  This is the new representation type we are adding. This is identical to
1320  the SurfaceRepresentation except that we are overriding the mapper with
1321  our mapper.
1322  </Documentation>
1323  </RepresentationProxy>
1324  </ProxyGroup>
1325 </ServerManagerConfiguration>
1326 ```
1327 
1328 `vtkMySpecialRepresentation` is a subclass of
1329 `vtkGeometryRepresentationWithFaces` where in the constructor we simply
1330 override the mappers as follows:
1331 
1332 ```cpp
1333 vtkMySpecialRepresentation::vtkMySpecialRepresentation()
1334 {
1335  // Replace the mappers created by the superclass.
1336  this->Mapper->Delete();
1337  this->LODMapper->Delete();
1338 
1339  this->Mapper = vtkMySpecialPolyDataMapper::New();
1340  this->LODMapper = vtkMySpecialPolyDataMapper::New();
1341 
1342  // Since we replaced the mappers, we need to call SetupDefaults() to ensure
1343  // the pipelines are setup correctly.
1344  this->SetupDefaults();
1345 }
1346 ```
1347 
1348 Next we need to register this new type with the any (or all) of the 3 standard
1349 composite representations so that it will become available to the user to
1350 choose in the representation type combo box. To decide which of the 3 composite
1351 representations we want to add our representation to, think of the input data
1352 types our representation supports. If it can support any type of data set, then
1353 we can add our representation all the 3 representations (as is the case with
1354 this example). However if we are adding a representation for volume rendering
1355 of `vtkUnstructuredGrid` then we will add it only to the
1356 `UnstructuredGridRepresentation`. This is done by using the `Extension` XML
1357 tag. It simply means that we are extending the original XML for the proxy
1358 definition with the specified additions. Now to make this representation
1359 available as a type to the user, we use the `RepresentationType` element , with
1360 `text` used as the text shown for the type in the combo-box, `subproxy`
1361 specifies the name of representation subproxy to activate when the user chooses
1362 the specified type. Optionally one can also specify the `subtype` attribute,
1363 which if present is the value set on a property named `Representation` for the
1364 subproxy when the type is chosen. This allows for the subproxy to provide more
1365 than one representation type.
1366 
1367 ```xml
1368 <ServerManagerConfiguration>
1369  <ProxyGroup name="representations">
1370  <Extension name="GeometryRepresentation">
1371  <Documentation>
1372  Extends standard GeometryRepresentation by adding
1373  MySpecialRepresentation as a new type of representation.
1374  </Documentation>
1375 
1376  <!-- this adds to what is already defined in PVRepresentationBase -->
1377  <RepresentationType
1378  subproxy="MySpecialRepresentation"
1379  text="Special Mapper"
1380  subtype="1" />
1381 
1382  <SubProxy>
1383  <Proxy name="MySpecialRepresentation"
1384  proxygroup="representations"
1385  proxyname="MySpecialRepresentation">
1386  </Proxy>
1387  <ShareProperties subproxy="SurfaceRepresentation">
1388  <Exception name="Input" />
1389  <Exception name="Visibility" />
1390  <Exception name="Representation" />
1391  </ShareProperties>
1392  </SubProxy>
1393  </Extension>
1394  </ProxyGroup>
1395 </ServerManagerConfiguration>
1396 ```
1397 
1398 The `CMakeLists.txt` file is not much different from what it would be like for
1399 adding a simple filter or a reader where the representation class is placed
1400 into the contained module.
1401 
1402 Source code for this example is available under
1403 `Examples/Plugins/Representation` in the ParaView source directory.
1404 
1405 ## Examples
1406 
1407 The ParaView git repository contains many examples in the `Examples/Plugins`
1408 directory.
1409 
1410 ## Adding plugins to ParaView source
1411 
1412 There are several plugins that are included in ParaView source itself and are
1413 built as part of ParaView's build process. To add such a plugin to the ParaView
1414 build there are two options, adding it to the `ParaView/Plugins` directory is
1415 currently the only supported mechanism.
1416 
1417 <!-- TODO: Restore the external plugin building process to support custom plugins in static builds.
1418 
1419  1. Place the source for the plugin in a directory under `ParaView/Plugins`.
1420  2. Add the source directory to the CMake variable
1421  `EXTRA_EXTERNAL_PLUGIN_DIRS` when building ParaView.
1422 
1423 Both approaches result in identical behavior.
1424 -->
1425 
1426 In general users should simply build their plugins separately, outside the
1427 ParaView source. However, when building ParaView statically, adding the plugin
1428 to be built as part of ParaView ensures that the static executables load the
1429 plugin, otherwise there is no mechanism for loading a plugin in statically
1430 built executables.
1431 
1432 In your plugin source directory, ParaView searches for a file name
1433 `paraview.plugin` which provides ParaView with information about the plugin.
1434 This file should contain the following contents:
1435 
1436 ```cmake
1437 # Comments are allowed.
1438 NAME
1439  PluginName
1440 DESCRIPTION
1441  A description of the plugin. This text is attached to the CMake option to
1442  build this plugin.
1443 REQUIRES_MODULES
1444  # List of VTK modules required by the code contained in the plugin. This
1445  # allows ParaView to build the full set of requested modules if the plugin is
1446  # being built.
1447  VTK::CommonCore
1448 ```
1449 
1450 If now the plugin is enabled (by the user or by default) by turning ON the
1451 `PARAVIEW_PLUGIN_ENABLE_PluginName` CMake option, then CMake will look for a
1452 `CMakeLists.txt` file next to the `paraview.plugin`. This file contains the
1453 calls to build the plugin including the `paraview_add_plugin` call, and
1454 building of any other libraries that the plugin needs.
1455 
1456 A good place to start would be look at examples under `ParaView/Plugins`
1457 directory.
1458 
1459 ## Plugins in Static Applications
1460 
1461 It is possible to import plugins into a ParaView-based application at compile
1462 time. When building ParaView-based applications statically, this is the only
1463 option to bring in components from plugins. When built statically (i.e., with
1464 `BUILD_SHARED_LIBS` set to false), ParaView will automatically link and load
1465 plugins that were enabled via CMake by inserting the necessary
1466 `PV_PLUGIN_IMPORT_INIT` and `PV_PLUGIN_IMPORT` macros.
1467 
1468 The code below shows how the `PV_PLUGIN` macros would be used to statically load
1469 plugins in custom applications:
1470 
1471 ```cpp
1472 #define PARAVIEW_BUILDING_PLUGIN
1473 #include "vtkPVPlugin.h"
1474 
1475 // Adds required forward declarations.
1476 PV_PLUGIN_IMPORT_INIT(MyFilterPlugin)
1477 PV_PLUGIN_IMPORT_INIT(MyReaderPlugin)
1478 
1479 class MyMainWindow : public QMainWindow
1480 {
1481  // ....
1482 };
1483 
1484 MyMainWindow::MyMainWindow(...)
1485 {
1486  // ... after initialization ...
1487 
1488  // Calls relevant callbacks to load the plugins and update the
1489  // GUI/Server-Manager
1490  PV_PLUGIN_IMPORT(MyFilterPlugin);
1491  PV_PLUGIN_IMPORT(MyReaderPlugin);
1492 }
1493 ```
1494 
1495 ## Pitfalls
1496 
1497 ### *Tools > Manage Plugins* is not visible!
1498 
1499 Plugins can only be loaded dynamically when ParaView is built with shared
1500 libraries. You must recompile ParaView with `BUILD_SHARED_LIBS=ON`.
1501 
1502 ### Compile error `invalid conversion from 'vtkYourFiltersSuperClass*' to 'vtkYourFilter*'`
1503 
1504 Any VTK object that needs to be treated as a filter or source has to be a
1505 `vtkAlgorithm` subclass. The particular superclass a filter is derived from has
1506 to be given not only in the standard C++ way:
1507 
1508 ```cpp
1509 class VTKMODULE_EXPORT vtkMyElevationFilter : public vtkElevationFilter
1510 ```
1511 
1512 but additionally declared with help of the `vtkTypeMacro`. For the example
1513 given above:
1514 
1515 ```cpp
1516 class VTKMODULE_EXPORT vtkMyElevationFilter : public vtkElevationFilter
1517 {
1518 public:
1519  vtkTypeMacro(vtkMyElevationFilter, vtkElevationFilter);
1520 }
1521 ```
1522 
1523 Otherwise, compiling the filter will fail with a variety of error messages
1524 (depending on superclass) like
1525 
1526 ```
1527 vtkMyElevationFilter.cxx:19: error: no 'void vtkMyElevationFilter::CollectRevisions(std::ostream&)'
1528  member function declared in class 'vtkMyElevationFilter'
1529 ```
1530 or
1531 
1532 ```
1533 vtkMyElevationFilterClientServer.cxx:97: error: invalid conversion from ‘vtkPolyDataAlgorithm*’ to
1534  ‘vtkICPFilter*’
1535 ```
1536 
1537 ### Mysterious Segmentation Faults in Plugins that use Custom VTK Classes
1538 
1539 This primarily concerns plugins that make calls to your own custom `vtkMy` (or
1540 whatever you called it) library of VTK extensions.
1541 
1542 Symptoms:
1543 
1544  * The plugin will load, but causes a segfault when you try to use it.
1545  * If you use a debugger you may notice that in some cases when your code
1546  calls `vtkClassA.MethodB`, what actually gets called is
1547  `vtkClassC.MethodD`, where `MethodB` is a virtual member function. This is
1548  occurs because of different vtable entries in the Paraview-internal
1549  versions of the VTK libraries.
1550 
1551 The solution is to make sure that your `vtkMy` library is compiled against
1552 ParaView's internal VTK libraries. Even if you compiled VTK and ParaView using
1553 the same VTK sources, you *must not* link against the external VTK libraries.
1554 (The linker won't complain, because it will find all the symbols it needs, but
1555 this leads to unexpected behaviour.)
1556 
1557 To be explicit, when compiling your `vtkMy` library, you must set the CMake
1558 variable `VTK_DIR` to point to the `VTK` subdirectory in the directory in which
1559 you built ParaView. (On my system, CMake automatically finds VTK at
1560 `/usr/lib/vtk-5.2`, and I must change `VTK_DIR` to
1561 `~/source/ParaView3/build/VTK`.)
1562 
1563 ### "Is not a valid Qt plugin" in Windows
1564 
1565 Make sure that all the DLLs that your plugin depends on are on the `PATH`. If
1566 in doubt, try placing your plugin and all its dependent DLLs in the `bin`
1567 directory of your build and load it from there.
1568 
1569 ### The system cannot find the path specified. `error MSB6006: "cmd.exe" exited with code 3.`
1570 
1571 You may get an error like this when trying to build your plugin with
1572 Visual Studio:
1573 
1574 ```
1575 1> CS Wrapping - generating vtkMyElevationFilterClientServer.cxx
1576 1> The system cannot find the path specified.
1577 1>C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(151,5): error MSB6006: "cmd.exe" exited with code 3.
1578 1>Done executing task "CustomBuild" -- FAILED.
1579 ```
1580 
1581 This is caused for a mismatch between the configuration you used when building
1582 ParaView (e.g. Debug, Release, etc.) and the configuration currently chosen for
1583 building your plugin. So ensure those match.
1584 
1585 The problem is caused because inside the Linker properties there are references
1586 to the `*.lib` files, including the name of the directory that matches the
1587 configuration type, which may look something like
1588 `C:\Users\MyUser\ParaView-v4.2.0-build\lib\Release\vtkPVAnimation-pv4.2.lib`.
1589 
1590 ### Changing ParaView_DIR after the first configuration do not work as expected
1591 
1592 The plugin infrastructure and package finding logic do not support that as
1593 clearing the cache is not something we can reliably do as a config.cmake file.
1594 
1595 Just remove the build directory content and configure from scratch.
1596 
1597 [ParaView Guide]: http://www.kitware.com/products/books/paraview.html
1598 [core readers]: https://gitlab.kitware.com/paraview/paraview/-/blob/87babdbeab6abe20aac6f8b2692788abc6bb20ac/ParaViewCore/ServerManager/SMApplication/Resources/readers.xml#L158-179
1599 [pqPropertyWidget]: https://www.paraview.org/paraview-docs/nightly/cxx/classpqPropertyWidget.html
1600 [pqPropertyWidgetDecorator]: https://www.paraview.org/paraview-docs/nightly/cxx/classpqPropertyWidgetDecorator.html
1601 [QActionGroup]: https://doc.qt.io/qt-5/qactiongroup.html
1602 [QAction]: https://doc.qt.io/qt-5/qaction.html