read STL begginer

Hey,
I’m new with VTK and I compiled all the basics tutorial without any problems.
But I wanted to try to read a .stl file with the example in the VTK website https://kitware.github.io/vtk-examples/site/Cxx/IO/ReadSTL/
And as you can see on the screenshot, I don’t really know where to put the .stl file to be read…
I’m stuck trying to run this example, so if anyone can explain me how it’s working, it will be very usefull.

Thanks for considering my problem,

Clement Gandillon

Hi @Minigandi ,

You are supposed to put the name as the argument of the command line, do you run from a terminal or from visual studio ?

A simple work around is to simply hard code your path line 22.

best,

Hi @mwestphal,
I am also new with c++ and I don’t know how to do it either, I have tried to enter the path by modifying line 22 in multiple ways and it doesn’t work. I’ve put a “cube.stl” file in the build folder, would you mind showing us how it’s done?
I’m working with visual studio.
Thank you,

Just put a hard path line 22.

eg: C:\path\to\your\cube.stl

Best,

@mwestphal
I’m really sorry, but I really want to learn. I have tried in a thousand ways but I can’t find how to import .stl files.
std::string inputFilename = “C:\Users\HUAWEI\Desktop\cubo.stl”;
Best,

#include <vtkActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSTLReader.h>

int main(int argc, char* argv[])
{
    vtkNew<vtkNamedColors> colors;

    if (argc != 2)
    {
        cout << "Required parameters: Filename(.stl) e.g 42400-IDGH.stl" << endl;
        return EXIT_FAILURE;
    }
    
    std::string inputFilename = "C:/Users/HUAWEI/Desktop/cubo.stl";

    vtkNew<vtkSTLReader> reader;
    reader->SetFileName(inputFilename.c_str());
    reader->Update();

    // Visualize
    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(reader->GetOutputPort());

    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->GetProperty()->SetDiffuse(0.8);
    actor->GetProperty()->SetDiffuseColor(
        colors->GetColor3d("LightSteelBlue").GetData());
    actor->GetProperty()->SetSpecular(0.3);
    actor->GetProperty()->SetSpecularPower(60.0);

    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    renderWindow->SetWindowName("ReadSTL");

    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(colors->GetColor3d("DarkOliveGreen").GetData());

    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

@ Minigandi
Hi, Could you show me how you solved the problem?
Thank you.

#include <vtkActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSTLReader.h>

int main(int argc, char* argv[])
{
    vtkNew<vtkNamedColors> colors;

    std::string inputFilename = "C:\Users\HUAWEI\Desktop\cubo.stl";

    vtkNew<vtkSTLReader> reader;
    reader->SetFileName(inputFilename.c_str());
    reader->Update();

    // Visualize
    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(reader->GetOutputPort());

    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->GetProperty()->SetDiffuse(0.8);
    actor->GetProperty()->SetDiffuseColor(
        colors->GetColor3d("LightSteelBlue").GetData());
    actor->GetProperty()->SetSpecular(0.3);
    actor->GetProperty()->SetSpecularPower(60.0);

    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    renderWindow->SetWindowName("ReadSTL");

    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(colors->GetColor3d("DarkOliveGreen").GetData());

    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

Should work.

1 Like