Skip to content

ConstructTable

vtk-examples/Cxx/Utilities/ConstructTable

Description

This example creates a 3x3 table. The columns MUST be added first - if you attempt to call AddRow type functions, they will not do anything (or produce errors) because the table still has 0 columns until you tell it otherwise. This example adds 3 empty columns to the table, then goes back and fills them in.

Warning: the table does not copy the data, so if you add the SAME column to the table each time, i.e.

[vtkSmartPointer](https://www.vtk.org/doc/nightly/html/classvtkSmartPointer.html#details)<[vtkVariant](https://www.vtk.org/doc/nightly/html/classvtkVariant.html#details)Array> col = [vtkSmartPointer](https://www.vtk.org/doc/nightly/html/classvtkSmartPointer.html#details)<[vtkVariant](https://www.vtk.org/doc/nightly/html/classvtkVariant.html#details)Array>::New();

col->InsertNextValue([vtkVariant](https://www.vtk.org/doc/nightly/html/classvtkVariant.html#details)(0.0));
col->InsertNextValue([vtkVariant](https://www.vtk.org/doc/nightly/html/classvtkVariant.html#details)(0.0));
col->InsertNextValue([vtkVariant](https://www.vtk.org/doc/nightly/html/classvtkVariant.html#details)(0.0));
table->AddColumn(Col);
table->AddColumn(Col);
table->AddColumn(Col);

If you modify one of the entries in the table with Table->SetValue (r, c, [vtkVariant](https://www.vtk.org/doc/nightly/html/classvtkVariant.html#details)(counter)); after they have all been set to the same column, the rth entry in EVERY column will be changed.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ConstructTable.cxx

#include <vtkNew.h>
#include <vtkTable.h>
#include <vtkVariant.h>
#include <vtkVariantArray.h>

int main(int, char*[])
{
  // construct an empty table
  vtkNew<vtkTable> table;

  for (unsigned int i = 0; i < 3; i++)
  {
    vtkNew<vtkVariantArray> col;

    col->InsertNextValue(vtkVariant(0.0));
    col->InsertNextValue(vtkVariant(0.0));
    col->InsertNextValue(vtkVariant(0.0));
    table->AddColumn(col);
  }

  // fill the table with values
  unsigned int counter = 0;
  for (vtkIdType r = 0; r < table->GetNumberOfRows(); r++)
  {
    for (vtkIdType c = 0; c < table->GetNumberOfColumns(); c++)
    {
      table->SetValue(r, c, vtkVariant(counter));
      counter++;
    }
  }

  // print information about the table
  std::cout << "NumRows: " << table->GetNumberOfRows() << std::endl;
  std::cout << "NumCols: " << table->GetNumberOfColumns() << std::endl;

  // display the table
  table->Dump(3);

  // access elements of the table
  for (vtkIdType r = 0; r < table->GetNumberOfRows(); r++)
  {
    for (vtkIdType c = 0; c < table->GetNumberOfColumns(); c++)
    {
      vtkVariant v = table->GetValue(r, c);
      std::cout << "(r,c) = (" << r << "," << c << ") = " << v << std::endl;
    }
  }

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ConstructTable)

find_package(VTK COMPONENTS 
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ConstructTable: 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(ConstructTable MACOSX_BUNDLE ConstructTable.cxx )
  target_link_libraries(ConstructTable PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ConstructTable
  MODULES ${VTK_LIBRARIES}
)

Download and Build ConstructTable

Click here to download ConstructTable and its CMakeLists.txt file. Once the tarball ConstructTable.tar has been downloaded and extracted,

cd ConstructTable/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:

./ConstructTable

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.