Commit d19f1fd3 authored by Pat Marion's avatar Pat Marion

Fix the SetParameter API overloads of the vtkPythonProgrammableFilter

Commit fc8cabcd (April 2007) introduced a SetParameter function on the
programmable filter.  It added a string vector property to filter proxy
that could be used to set arbitrary parameter name/value pairs that would
be defined in the filter python script.

Commit 8311d712 (July 2011) introduced several SetParameter overloads so
that custom string, int, and double vector properties were supported.
This allowed proxy xml to declare custom properties on the python
programmable filter that would be presented in the GUI like regular
properties.  But, this commit also changed the behavior of the original
Parameter string vector property.

Commit 9176ba5c (March 2012) accidentally broke the SetParameter
overloads.

This commit (May 2013) fixes the SetParameter overloads and original
Parameter string vector property (but both cannot be used together).
This commit adds a test for the SetParameter API and adds an example
proxy xml file that demonstrates how to define a python programmable
filter with custom parameters.

Change-Id: I3583fbfdd7d9d442799c714bb2c34693d34ed2ea
parent e4926369
......@@ -193,41 +193,55 @@ void vtkPythonProgrammableFilter::SetParameterInternal(const char *raw_name,
this->Modified();
}
//----------------------------------------------------------------------------
void vtkPythonProgrammableFilter::SetParameter(const char *raw_name,
const int value)
int value)
{
std::ostringstream buf;
buf << value;
this->SetParameterInternal(raw_name, buf.str().c_str() );
}
//----------------------------------------------------------------------------
void vtkPythonProgrammableFilter::SetParameter(const char *raw_name,
const double value)
double value)
{
std::ostringstream buf;
buf << value;
this->SetParameterInternal(raw_name, buf.str().c_str() );
}
//----------------------------------------------------------------------------
void vtkPythonProgrammableFilter::SetParameter(const char *raw_name,
const char *value)
{
std::ostringstream buf;
buf << value;
buf << "'" << value << "'";
this->SetParameterInternal(raw_name, buf.str().c_str() );
}
//----------------------------------------------------------------------------
void vtkPythonProgrammableFilter::SetParameter(
const char *raw_name,
const double value1,
const double value2,
const double value3)
double value1,
double value2,
double value3)
{
std::ostringstream buf;
buf << value1 << value2 << value3;
buf << "[" << value1 << ", " << value2 << ", " << value3 << "]";
this->SetParameterInternal(raw_name, buf.str().c_str() );
}
//----------------------------------------------------------------------------
void vtkPythonProgrammableFilter::SetParameter(
const char *raw_name,
double value1,
double value2)
{
std::ostringstream buf;
buf << "[" << value1 << ", " << value2 << "]";
this->SetParameterInternal(raw_name, buf.str().c_str() );
}
//----------------------------------------------------------------------------
void vtkPythonProgrammableFilter::ClearParameters()
......
......@@ -73,10 +73,10 @@ public:
// when it is run
void SetParameterInternal(const char *name, const char *value);
void SetParameter(const char *name, const char *value);
void SetParameter(const char *name, const int value);
void SetParameter(const char *name, const double value);
void SetParameter(const char *name, const double value1,
const double value2, const double value3);
void SetParameter(const char *name, int value);
void SetParameter(const char *name, double value);
void SetParameter(const char *name, double value1, double value2);
void SetParameter(const char *name, double value1, double value2, double value3);
// Description:
// Clear all name-value parameters
......
......@@ -80,6 +80,7 @@ SET(PY_TESTS_NO_BASELINE
CSVWriterReader
IntegrateAttributes
ProgrammableFilter
ProgrammableFilterProperties
ProxyManager
SMPropertyAdaptor
VRMLSource
......
import os
import random
import textwrap
import SMPythonTesting
import paraview.simple as smp
def testScript(programmableFilter, script):
arrayName = str(random.random())
programmableFilter.Script = script + textwrap.dedent('''
passedArray = vtk.vtkIntArray()
passedArray.SetName('%s')
self.GetOutput().GetFieldData().AddArray(passedArray)''' % arrayName)
programmableFilter.UpdatePipeline()
return programmableFilter.GetClientSideObject().GetOutput().GetFieldData().GetArray(arrayName) is not None
SMPythonTesting.ProcessCommandLineArguments()
tempDir = SMPythonTesting.TempDir
stateDir = SMPythonTesting.SMStatesDir
sphere = smp.Sphere()
f = smp.ProgrammableFilter(sphere)
script = '''
assert 1+1 == 2
'''
assert testScript(f, script)
script = '''
assert foo == 'bar'
'''
f.SetPropertyWithName('Parameters', ['foo', '"bar"'])
assert testScript(f, script)
smp.LoadPlugin(os.path.join(stateDir, 'ProgrammableFilterPropertiesTest.xml'))
f = smp.ProgrammableFilterPropertiesTest(sphere)
assert f.DoubleTest == 1.23
assert f.IntTest == 123
assert f.StringTest == 'string value'
assert testScript(f, f.Script)
<ServerManagerConfiguration>
<ProxyGroup name="filters">
<SourceProxy name="ProgrammableFilterPropertiesTest" class="vtkPythonProgrammableFilter" label="Programmable Filter Properties Test">
<Documentation
long_help="Programmable Filter Properties Test"
short_help="Programmable Filter Properties Test">
</Documentation>
<InputProperty
name="Input"
command="SetInputConnection">
<ProxyGroupDomain name="groups">
<Group name="sources"/>
<Group name="filters"/>
</ProxyGroupDomain>
<DataTypeDomain name="input_type">
<DataType value="vtkPolyData"/>
</DataTypeDomain>
</InputProperty>
<IntVectorProperty
name="IntTest"
label="Int Test"
command="SetParameter"
number_of_elements="1"
animateable="1"
initial_string="int_test"
default_values="123" >
</IntVectorProperty>
<DoubleVectorProperty
name="DoubleTest"
label="Double Test"
command="SetParameter"
number_of_elements="1"
animateable="1"
initial_string="double_test"
default_values="1.23" >
</DoubleVectorProperty>
<DoubleVectorProperty
name="DoubleTwoTest"
label="Double Two Test"
command="SetParameter"
number_of_elements="2"
animateable="1"
initial_string="double_two_test"
default_values="1.1 2.2" >
</DoubleVectorProperty>
<DoubleVectorProperty
name="DoubleThreeTest"
label="Double Three Test"
command="SetParameter"
number_of_elements="3"
animateable="1"
initial_string="double_three_test"
default_values="1.1 2.2 3.3" >
</DoubleVectorProperty>
<StringVectorProperty
name="StringTest"
label="String Test"
command="SetParameter"
number_of_elements="1"
animateable="1"
initial_string="string_test"
default_values="string value" >
</StringVectorProperty>
<StringVectorProperty
name="Script"
command="SetScript"
number_of_elements="1"
default_values="assert int_test == 123&#xA;assert double_test == 1.23&#xA;assert double_two_test == [1.1, 2.2]&#xA;assert double_three_test == [1.1, 2.2, 3.3]&#xA;assert string_test == &quot;string value&quot;&#xA;&#xA;# this tests that whitespace and newlines are kept intact&#xA;# when the script is stored as an xml attribute string&#xA;def addOne(x):&#xA;&#xA; return x + 1&#xA;&#xA;assert addOne(5) == 6&#xA;&#xA;# this tests that we can store &quot;&lt;&quot; and &quot;&gt;&quot;&#xA;# characters in the xml attribute string&#xA;assert 1 &lt; 2 and 2 &gt; 1&#xA;&#xA;&#xA;"
panel_visibility="advanced">
<Hints>
<Widget type="multi_line"/>
</Hints>
</StringVectorProperty>
</SourceProxy>
</ProxyGroup>
</ServerManagerConfiguration>
......@@ -276,7 +276,7 @@
</IntVectorProperty>
<StringVectorProperty animateable="0"
clean_command="ClearParameters"
command="SetParameter"
command="SetParameterInternal"
is_internal="1"
name="Parameters"
number_of_elements_per_command="2"
......@@ -369,7 +369,7 @@
</StringVectorProperty>
<StringVectorProperty animateable="0"
clean_command="ClearParameters"
command="SetParameter"
command="SetParameterInternal"
is_internal="1"
name="Parameters"
number_of_elements_per_command="2"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment