I am trying to add a vtkStringArray
to a vtkUnstructuredGrid
using the AddArray
method. The string array shows up in the spreadsheet view for serial vtu files, but does not appear when loading a pvtu file in ParaView.
I add a string array by building on the Polyhedron example from https://lorensen.github.io/VTKExamples/site/Cxx/GeometricObjects/Polyhedron/
The source code for adding a string array to a polyhedron and writing out to a serial vtu file is shown below:
#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkDataArray.h>
#include <vtkDataSetMapper.h>
#include <vtkIdList.h>
#include <vtkNamedColors.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyhedron.h>
#include <vtkSmartPointer.h>
#include <vtkStringArray.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLUnstructuredGridWriter.h>
int main(int, char *[])
{
// Create a Polyhedron (cube)
vtkIdType pointIds[8] = {0, 1, 2, 3, 4, 5, 6, 7};
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(-1.0,-1.0,-1.0);
points->InsertNextPoint( 1.0,-1.0,-1.0);
points->InsertNextPoint( 1.0, 1.0,-1.0);
points->InsertNextPoint(-1.0, 1.0,-1.0);
points->InsertNextPoint(-1.0,-1.0, 1.0);
points->InsertNextPoint( 1.0,-1.0, 1.0);
points->InsertNextPoint( 1.0, 1.0, 1.0);
points->InsertNextPoint(-1.0, 1.0, 1.0);
// Define faces according to point IDs
vtkSmartPointer<vtkCellArray> faces =
vtkSmartPointer<vtkCellArray>::New();
vtkIdType face0[4] = {0, 3, 2, 1};
vtkIdType face1[4] = {0, 4, 7, 3};
vtkIdType face2[4] = {4, 5, 6, 7};
vtkIdType face3[4] = {5, 1, 2, 6};
vtkIdType face4[4] = {0, 1, 5, 4};
vtkIdType face5[4] = {2, 3, 7, 6};
faces->InsertNextCell(4, face0);
faces->InsertNextCell(4, face1);
faces->InsertNextCell(4, face2);
faces->InsertNextCell(4, face3);
faces->InsertNextCell(4, face4);
faces->InsertNextCell(4, face5);
vtkSmartPointer<vtkUnstructuredGrid> ugrid =
vtkSmartPointer<vtkUnstructuredGrid>::New();
ugrid->SetPoints(points);
ugrid->InsertNextCell(VTK_POLYHEDRON, 8, pointIds,
6, faces->GetPointer());
// Create string scalar
vtkSmartPointer<vtkStringArray> string_data =
vtkSmartPointer<vtkStringArray>::New();
string_data->SetName("A_String_Array");
string_data->InsertNextValue("Hello");
// Add string array to unstructured grid
ugrid->GetCellData()->AddArray(string_data);
// Here write out serial vtu
vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer =
vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
writer->SetInputData(ugrid);
writer->SetFileName("polyhedron.vtu");
writer->SetDataModeToAscii();
writer->Update();
writer->Write();
For the pvtu file, I simply replace the writer in the above code with:
vtkSmartPointer<vtkXMLPUnstructuredGridWriter> writer =
vtkSmartPointer<vtkXMLPUnstructuredGridWriter>::New();
writer->SetInputData(ugrid);
writer->SetFileName("parallel_polyhedron.pvtu");
writer->SetNumberOfPieces(1);
writer->SetStartPiece(0);
writer->SetEndPiece(0);
writer->SetDataModeToAscii();
writer->Update();
writer->Write();
and replace the #include <vtkXMLUnstructuredGridWriter.h>
to #include <vtkXMLPUnstructuredGridWriter.h>
.
When loading the vtu files for both cases (“polyhedron.vtu” and “parallel_polyhedron_0.vtu”), the string value "Hello"
show up in the spreadsheet view under "test_name"
. However, when the pvtu file (“parallel_polyhedron.pvtu”) is loaded, the string value is blank under "test_name"
in the spreadsheet view.
How can I add a string array such that its string value shows up properly in the spreadsheet view when loading the pvtu file?
Thank you,
Victor