[vtkusers] Exporting 3D Extruded text to a file

David Doria daviddoria+vtk at gmail.com
Thu Jan 14 08:53:02 EST 2010


On Thu, Jan 14, 2010 at 2:31 AM, David Morris <dvmorris at gmail.com> wrote:
> I have some basic code that creates a 3D text, applies a linear extrusion
> filter, and then displays it in a viewport. This works well, but now I would
> like to output the result of the extrusion to an STL file using
> vtkStlWriter. I am extremely new to VTK (just got it to compile two hours
> ago), so I believe I'm just missing something really basic. Here is the
> relevant code:
>
>     ...
>
>     // Create vector text
>     vtkVectorText* vecText = vtkVectorText::New();
>     vecText->SetText("Text!");
>
>     // Apply linear extrusion
>     vtkLinearExtrusionFilter* extrude = vtkLinearExtrusionFilter::New();
>     extrude->SetInputConnection( vecText->GetOutputPort());
>     extrude->SetExtrusionTypeToNormalExtrusion();
>     extrude->SetVector(0, 0, 1 );
>     extrude->SetScaleFactor (0.5);
>
>     // output the mesh to a PolyDataMapper and then to an Actor
>     vtkPolyDataMapper* txtMapper = vtkPolyDataMapper::New();
>     txtMapper->SetInputConnection( extrude->GetOutputPort());
>     vtkActor* txtActor = vtkActor::New();
>     txtActor->SetMapper(txtMapper);
>
>     // add it to the renderer
>     ren->AddActor(txtActor);
>
>     // attempt to write an STL file
>     std::string outputFilename = "test.stl";
>     vtkSmartPointer<vtkPolyData> data = vtkSmartPointer<vtkPolyData>::New();
>
>
>     // data = extrude->getOutput(); // this line causes an error: 'class
> vtkLinearExtrusionFilter' has no member named 'getOutput'
>     vtkSmartPointer<vtkSTLWriter> stlWriter =
> vtkSmartPointer<vtkSTLWriter>::New();
>     stlWriter->SetFileName(outputFilename.c_str());
>
>     stlWriter->SetInput(data); // passing extrude->getOutput() here seems to
> work, but doesn't send the walls of the extrusion
>     stlWriter->Write();
>
>     ...
>
> When I try to run:
>
>     stlWriter->setInput(extrude->getOutput());
>
> it just outputs the text twice, separated by the thickness of the extrusion
> but without the walls. Does that make sense. Let me know if I'm missing
> something or if it is not possible. Thanks for the help, and I can certainly
> send the whole code if someone wants to run it, but I think my problem is
> simple enough that someone can just glance at the code and see what's wrong.
> Thanks for the help in advance,
>
> Dave

Dave,

Welcome to VTK!

I have multiple comments/suggestions for you:

1) You will find it really helpful to use smart pointers rather than
normal pointers (I see that you have done that in some places):

vtkSmartPointer<vtkLinearExtrusionFilter> extrude =
vtkSmartPointer<vtkLinearExtrusionFilter>::New();
rather than:
vtkLinearExtrusionFilter* extrude = vtkLinearExtrusionFilter::New();

with the old style pointers, you are *supposed* to call
extrude->Delete() when you are done using it, but with a smart pointer
that is not necessary. Smart pointers will also help you do things
like return pointers from functions and other tasks with much less
headache.

2) There is no need to create a mapper and an actor if your goal is to
write the result to a file. Mappers/actor are for use with
renderers/renderwindows.

3) You should ALWAYS update filters before you use their output:
extrude->Update();

(until you know exactly what is going on and when the pipeline is updating :) )

4) The preferred method is SetInputConnection(xxx->GetOutputPort())
rather than SetInput(xxx->GetOutput())

5) To verify that your file is "correct", you should use Paraview
(www.paraview.org) to view it.

6) I fixed your code here: http://www.vtk.org/Wiki/VTK/Examples/LinearExtrusion

7) We have been working hard to compile a giant list of examples of
how to do many common things in VTK:

http://www.vtk.org/Wiki/VTK/Examples

Please check that out and let us know if it helps!

Enjoy!

David



More information about the vtkusers mailing list