# vtk class for wrapping/handling external command line program

**URL:** https://discourse.vtk.org/t/vtk-class-for-wrapping-handling-external-command-line-program/7403
**Category:** Support
**Created:** [December 15, 2021, 4:52am UTC](https://discourse.vtk.org/t/vtk-class-for-wrapping-handling-external-command-line-program/7403 "2021-12-15T04:52:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [December 15, 2021, 4:52am UTC](https://discourse.vtk.org/t/vtk-class-for-wrapping-handling-external-command-line-program/7403/1 "2021-12-15T04:52:37Z")

</div>

I am looking to wrap some external commands in a C++ vtk class for developer’s convenience.

What class should I look into ?

Some of the external program are generator of information (source), some are filters and some are sink (e.g. send email/update status on web server via http call).

Cheers

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [December 16, 2021, 2:05am UTC](https://discourse.vtk.org/t/vtk-class-for-wrapping-handling-external-command-line-program/7403/2 "2021-12-16T02:05:40Z")

</div>

Answering my own question, I think the [vtkExecutableRunner](https://vtk.org/doc/nightly/html//classvtkExecutableRunner.html) is what I will start looking into.

It looks promising.

Cheers

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [December 16, 2021, 9:00am UTC](https://discourse.vtk.org/t/vtk-class-for-wrapping-handling-external-command-line-program/7403/3 "2021-12-16T09:00:24Z")

</div>

FYI @Timothee_Chabat 🙂

---

<div class="post-metadata">

### Author: ![Timothee\_Chabat](https://discourse.vtk.org/user_avatar/discourse.vtk.org/timothee_chabat/32/3234_2.png) [@Timothee\_Chabat](https://discourse.vtk.org/u/Timothee_Chabat)
#### Post date: [December 16, 2021, 9:12am UTC](https://discourse.vtk.org/t/vtk-class-for-wrapping-handling-external-command-line-program/7403/4 "2021-12-16T09:12:31Z")

</div>

It was indeed created for that 🙂 just for your information @Nicholas_Yue vtkExecutableRunner is not an actual filter (ie it does not inherit `vtkAlgorithm`) so you will not be able to use it in a pipeline as is, but you can always create your own filters that wraps the class

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [December 16, 2021, 5:18pm UTC](https://discourse.vtk.org/t/vtk-class-for-wrapping-handling-external-command-line-program/7403/5 "2021-12-16T17:18:46Z")

</div>

@Timothee_Chabat just clarifying that I should aim towards something like MyRunnerSI rather than MyRunnerMI

```auto
class MyRunnerSI : public vtkAlgorithm {

public:
  void Execute(const char* command_string) {

    vtkNew<vtkExecutableRunner> runner;

   runner->SetCommand(command_string);
  }
}

```

```auto
class MyRunnerMI : public vtkAlgorithm, public vtkExecutableRunner {
}

```

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [December 16, 2021, 9:16pm UTC](https://discourse.vtk.org/t/vtk-class-for-wrapping-handling-external-command-line-program/7403/6 "2021-12-16T21:16:51Z")

</div>

@Timothee_Chabat

Where would be the most appropriate place the call runner-\>Execute() within the vtkAlgorithm ? I think it should be ProcessRequest()

Here is my initial attempt base on my reading of vtkPolyDataAlgorithm class

```auto
#include "vtkRunnerFilter.h"

#include <vtkCommand.h>
#include <vtkInformation.h>
#include <vtkInformationVector.h>
#include <vtkObjectFactory.h>
#include <vtkPolyData.h>
#include <vtkStreamingDemandDrivenPipeline.h>

vtkStandardNewMacro(vtkRunnerFilter);

//------------------------------------------------------------------------------
vtkRunnerFilter::vtkRunnerFilter()
{
	// by default assume filters have one input and one output
	// subclasses that deviate should modify this setting
	this->SetNumberOfInputPorts(1);
	this->SetNumberOfOutputPorts(1);

	_runnerPtr = vtkSmartPointer<vtkExecutableRunner>::New();

}

//------------------------------------------------------------------------------
vtkRunnerFilter::~vtkRunnerFilter() = default;

//------------------------------------------------------------------------------
void vtkRunnerFilter::PrintSelf(ostream& os, vtkIndent indent)
{
	this->Superclass::PrintSelf(os, indent);
}

//------------------------------------------------------------------------------
vtkDataObject* vtkRunnerFilter::GetOutput()
{
	return this->GetOutput(0);
}

//------------------------------------------------------------------------------
vtkDataObject* vtkRunnerFilter::GetOutput(int port)
{
	return this->GetOutputDataObject(port);
}

//------------------------------------------------------------------------------
void vtkRunnerFilter::SetOutput(vtkDataObject* d)
{
	this->GetExecutive()->SetOutputData(0, d);
}

//------------------------------------------------------------------------------
vtkDataObject* vtkRunnerFilter::GetInput()
{
	return this->GetInput(0);
}

//------------------------------------------------------------------------------
vtkDataObject* vtkRunnerFilter::GetInput(int port)
{
	return this->GetExecutive()->GetInputData(port, 0);
}

//------------------------------------------------------------------------------
vtkTypeBool vtkRunnerFilter::ProcessRequest(
		vtkInformation* request, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
	// generate the data
	if (request->Has(vtkDemandDrivenPipeline::REQUEST_DATA()))
	{
		return this->RequestData(request, inputVector, outputVector);
	}

	if (request->Has(vtkStreamingDemandDrivenPipeline::REQUEST_UPDATE_EXTENT()))
	{
		return this->RequestUpdateExtent(request, inputVector, outputVector);
	}

	// execute information
	if (request->Has(vtkDemandDrivenPipeline::REQUEST_INFORMATION()))
	{
		return this->RequestInformation(request, inputVector, outputVector);
	}

	_runnerPtr->Execute();

	return this->Superclass::ProcessRequest(request, inputVector, outputVector);
}

//------------------------------------------------------------------------------
int vtkRunnerFilter::FillOutputPortInformation(int vtkNotUsed(port), vtkInformation* info)
{
	// now add our info
	info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkExecutableRunner");
	return 1;
}

//------------------------------------------------------------------------------
int vtkRunnerFilter::FillInputPortInformation(int vtkNotUsed(port), vtkInformation* info)
{
	info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkExecutableRunner");
	return 1;
}

//------------------------------------------------------------------------------
int vtkRunnerFilter::RequestInformation(vtkInformation* vtkNotUsed(request),
		vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* vtkNotUsed(outputVector))
{
	// do nothing let subclasses handle it
	return 1;
}

//------------------------------------------------------------------------------
int vtkRunnerFilter::RequestUpdateExtent(vtkInformation* vtkNotUsed(request),
		vtkInformationVector** inputVector, vtkInformationVector* vtkNotUsed(outputVector))
{
	int numInputPorts = this->GetNumberOfInputPorts();
	for (int i = 0; i < numInputPorts; i++)
	{
		int numInputConnections = this->GetNumberOfInputConnections(i);
		for (int j = 0; j < numInputConnections; j++)
		{
			vtkInformation* inputInfo = inputVector[i]->GetInformationObject(j);
			inputInfo->Set(vtkStreamingDemandDrivenPipeline::EXACT_EXTENT(), 1);
		}
	}
	return 1;
}

//------------------------------------------------------------------------------
// This is the superclasses style of Execute method. Convert it into
// an imaging style Execute method.
int vtkRunnerFilter::RequestData(vtkInformation* vtkNotUsed(request),
		vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* vtkNotUsed(outputVector))
{
	return 1;
}

//------------------------------------------------------------------------------
void vtkRunnerFilter::SetInputData(vtkDataObject* input)
{
	this->SetInputData(0, input);
}

//------------------------------------------------------------------------------
void vtkRunnerFilter::SetInputData(int index, vtkDataObject* input)
{
	this->SetInputDataInternal(index, input);
}

//------------------------------------------------------------------------------
void vtkRunnerFilter::AddInputData(vtkDataObject* input)
{
	this->AddInputData(0, input);
}

//------------------------------------------------------------------------------
void vtkRunnerFilter::AddInputData(int index, vtkDataObject* input)
{
	this->AddInputDataInternal(index, input);
}

```

---

<div class="post-metadata">

### Author: ![Timothee\_Chabat](https://discourse.vtk.org/user_avatar/discourse.vtk.org/timothee_chabat/32/3234_2.png) [@Timothee\_Chabat](https://discourse.vtk.org/u/Timothee_Chabat)
#### Post date: [December 17, 2021, 8:16am UTC](https://discourse.vtk.org/t/vtk-class-for-wrapping-handling-external-command-line-program/7403/7 "2021-12-17T08:16:27Z")

</div>

Usually filters are doing the actual work when a REQUEST\_DATA request is given so I would put the Execute code in RequestData. Also I don’t think you need to handle REQUEST\_DATA\_EXTENT or REQUEST\_INFORMATION based on what you said, but you know better than me.

If you’re not sure what they are for and their order of executions I recommand you to read the 3 parts of the blog about the VTK pipeline, especially part 2 and 3 (see first part here [https://www.kitware.com/a-vtk-pipeline-primer-part-1](https://www.kitware.com/a-vtk-pipeline-primer-part-1))
