[vtkusers] cut a stl file and save them

shiyu wang shiyuwang.usc at gmail.com
Fri Jun 26 18:46:15 EDT 2015


Dear all,

I am quiet new to vtk. I meet some problems using vtk. This is what I am trying to do:

1. read an stl
2. cut the stl in half by a plane
3. add the surfaces to each parts to make it solid, ready for 3d printing (intersection of the plane and the stl model)
4. save the two separate stl file.

I don’t know how to add the surface now..

I know add backface on actor will now help…

I referred this:
http://www.paraview.org/Wiki/VTK/Examples/Cxx/Meshes/SolidClip <http://www.paraview.org/Wiki/VTK/Examples/Cxx/Meshes/SolidClip>



This is my current code:

#include "vtkActor.h"
#include "vtkClipPolyData.h"
#include "vtkPlane.h"
#include "vtkInteractorObserver.h"
#include "vtkInteractorStyleSwitch.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkSphereSource.h"
#include <vtkPolyData.h>
#include <vtkSTLReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkClipPolyData.h>
#include <vtkPlane.h>
#include <vtkProperty.h>
#include <vtkSTLWriter.h>

using namespace std;
int main(int argc, char ** argv) {

  if ( argc != 2 )
    {
    cout << "Required parameters: Filename" << endl;
    return EXIT_FAILURE;
    }
 
  std::string inputFilename = argv[1];
 
  vtkSmartPointer<vtkSTLReader> reader =
    vtkSmartPointer<vtkSTLReader>::New();
  reader->SetFileName(inputFilename.c_str());
  reader->Update();


  vtkSmartPointer<vtkClipPolyData> clip =
    vtkSmartPointer<vtkClipPolyData>::New();
  clip->SetValue(0);
  clip->GenerateClippedOutputOn();
  clip->SetInputConnection(reader->GetOutputPort());

  vtkSmartPointer<vtkPlane> plane =
    vtkSmartPointer<vtkPlane>::New();
  //plane->SetNormal(-1.0, 0.0, 0.0);
  plane->SetNormal(0.0, 0.0, -1);
  plane->SetOrigin(0, 0, 12);
  clip->SetClipFunction (plane);




  vtkSmartPointer<vtkPolyDataMapper> polyDataMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  polyDataMapper->SetInputConnection(clip->GetOutputPort());
  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(polyDataMapper);

  // Create a property to be used for the back faces. Turn off all
  // shading by specifying 0 weights for specular and diffuse. Max the
  // ambient.
  vtkSmartPointer<vtkProperty> backFaces =
    vtkSmartPointer<vtkProperty>::New();
  backFaces->SetSpecular(0.0);
  backFaces->SetDiffuse(0.0);
  backFaces->SetAmbient(1.0);
  backFaces->SetAmbientColor(1.0000, 0.3882, 0.2784);
 
  actor->SetBackfaceProperty(backFaces);


  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->SetSize(800,600);
  renderWindow->SetWindowName("VTK");
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(actor);
  renderWindow->AddRenderer(renderer);
  vtkInteractorStyleSwitch * styleSwitch
    = vtkInteractorStyleSwitch::SafeDownCast(
        renderWindowInteractor->GetInteractorStyle());
  if (styleSwitch)
    styleSwitch->SetCurrentStyleToTrackballCamera();
  renderWindow->Render();
  renderWindowInteractor->Start();



  // save stl

  vtkSmartPointer<vtkSTLWriter> stlWriter =
    vtkSmartPointer<vtkSTLWriter>::New();
  stlWriter->SetFileName("cut.stl");
  stlWriter->SetInputConnection(clip->GetOutputPort());
  stlWriter->Write();
}


Any suggestion would be helpful, thanks in advance!

Best wishes,
Shiyu Wang



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150626/358b342e/attachment.html>


More information about the vtkusers mailing list