GetMiscCellData
Repository source: GetMiscCellData
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
GetMiscCellData.cxx
#include <vtkCellData.h>
#include <vtkFloatArray.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkXMLPolyDataReader.h>
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
// Parse command line arguments.
if (argc != 3)
{
std::cout << "Required arguments: Filename ArrayName e.g. SuperQuadric.vtp "
"Normals"
<< std::endl;
return EXIT_FAILURE;
}
// Get filename from command line.
std::string filename = argv[1]; // First command line argument.
// Get array name
std::string arrayName = argv[2]; // Second command line argument.
// Read the file.
vtkNew<vtkXMLPolyDataReader> reader;
reader->SetFileName(filename.c_str());
reader->Update();
auto polydata = reader->GetOutput();
// Get the number of cells in the polydata.
vtkIdType idNumCellsInFile = polydata->GetNumberOfCells();
auto array = dynamic_cast<vtkFloatArray*>(
polydata->GetCellData()->GetArray(arrayName.c_str()));
if (array)
{
std::cout << "Got array " << arrayName << " with " << idNumCellsInFile
<< " values" << std::endl;
for (int i = 0; i < idNumCellsInFile; i++)
{
// Since there could be a lot of cells just print the first 10
if (i < 10 || i == idNumCellsInFile - 1)
{
double value;
value = array->GetValue(i);
std::cout << i << ": " << value << std::endl;
}
else
{
if (i == 10)
std::cout << "..." << std::endl;
}
}
}
else
{
std::cout << "The file " << filename
<< " does not have a CellData array named " << arrayName
<< std::endl;
}
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(GetMiscCellData)
find_package(VTK COMPONENTS
CommonCore
CommonDataModel
IOXML
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "GetMiscCellData: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(GetMiscCellData MACOSX_BUNDLE GetMiscCellData.cxx )
target_link_libraries(GetMiscCellData PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS GetMiscCellData
MODULES ${VTK_LIBRARIES}
)
Download and Build GetMiscCellData¶
Click here to download GetMiscCellData and its CMakeLists.txt file. Once the tarball GetMiscCellData.tar has been downloaded and extracted,
cd GetMiscCellData/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./GetMiscCellData
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.