VTK/Examples/Cxx/PolyData/LinearExtrusion

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Revision as of 20:43, 8 December 2012 by Lorensen (Talk | contribs)

Jump to: navigation, search

This example creates some text, extrudes it to make it 3D, then writes it to an STL file.

LinearExtrusion.cxx

#include <vtkSmartPointer.h>
#include <vtkVectorText.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkLinearExtrusionFilter.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkTriangleFilter.h>
 
int main(int, char *[])
{
  // Create vector text
  vtkSmartPointer<vtkVectorText> vecText = 
    vtkSmartPointer<vtkVectorText>::New();
  vecText->SetText("Text!");
  vecText->Update();
 
  // Apply linear extrusion 
  vtkSmartPointer<vtkLinearExtrusionFilter> extrude = 
    vtkSmartPointer<vtkLinearExtrusionFilter>::New();
  extrude->SetInputConnection( vecText->GetOutputPort());
  extrude->SetExtrusionTypeToNormalExtrusion();
  extrude->SetVector(0, 0, 1 );
  extrude->SetScaleFactor (0.5);
  extrude->Update();
 
  vtkSmartPointer<vtkTriangleFilter> triangleFilter =
    vtkSmartPointer<vtkTriangleFilter>::New();
  triangleFilter->SetInputConnection(extrude->GetOutputPort());
  triangleFilter->Update();
 
  // write an STL file
  std::string outputFilename = "extruded.vtp";
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = 
    vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName(outputFilename.c_str());
  writer->SetInputConnection(triangleFilter->GetOutputPort());
  writer->Write();
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
PROJECT(LinearExtrusion)
 
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
add_executable(LinearExtrusion MACOSX_BUNDLE LinearExtrusion.cxx)
 
if(VTK_LIBRARIES)
  target_link_libraries(LinearExtrusion ${VTK_LIBRARIES})
else()
  target_link_libraries(LinearExtrusion vtkHybrid vtkWidgets)
endif()

Download and Build LinearExtrusion

Click here to download LinearExtrusion. and its CMakeLists.txt file.

Once the tarball LinearExtrusion.tar has been downloaded and extracted,

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

./LinearExtrusion

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