fw4spl
io/fwVtkIO/src/fwVtkIO/MeshWriter.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwVtkIO/MeshWriter.hpp"
8 
9 #include "fwVtkIO/helper/Mesh.hpp"
10 #include "fwVtkIO/helper/vtkLambdaCommand.hpp"
11 
12 #include <fwCore/base.hpp>
13 
14 #include <fwDataIO/writer/registry/macros.hpp>
15 
16 #include <fwJobs/IJob.hpp>
17 #include <fwJobs/Observer.hpp>
18 
19 #include <vtkGenericDataObjectWriter.h>
20 #include <vtkPolyData.h>
21 #include <vtkSmartPointer.h>
22 
23 fwDataIOWriterRegisterMacro( ::fwVtkIO::MeshWriter );
24 
25 namespace fwVtkIO
26 {
27 //------------------------------------------------------------------------------
28 
30  ::fwData::location::enableSingleFile< ::fwDataIO::writer::IObjectWriter >(this),
31  m_job(::fwJobs::Observer::New("Mesh writer"))
32 {
34 }
35 
36 //------------------------------------------------------------------------------
37 
39 {
41 }
42 
43 //------------------------------------------------------------------------------
44 
46 {
47  using namespace fwVtkIO::helper;
48 
49  assert( !m_object.expired() );
50  assert( m_object.lock() );
51 
52  ::fwData::Mesh::csptr pMesh = getConcreteObject();
53 
54  vtkSmartPointer< vtkGenericDataObjectWriter > writer = vtkSmartPointer< vtkGenericDataObjectWriter >::New();
55  vtkSmartPointer< vtkPolyData > vtkMesh = vtkSmartPointer< vtkPolyData >::New();
56  ::fwVtkIO::helper::Mesh::toVTKMesh( pMesh, vtkMesh);
57  writer->SetInputData( vtkMesh );
58  writer->SetFileName(this->getFile().string().c_str());
59  writer->SetFileTypeToBinary();
60 
61  vtkSmartPointer<vtkLambdaCommand> progressCallback;
62 
63  progressCallback = vtkSmartPointer<vtkLambdaCommand>::New();
64  progressCallback->SetCallback(
65  [&](vtkObject* caller, long unsigned int, void* )
66  {
67  auto filter = static_cast<vtkGenericDataObjectWriter*>(caller);
68  m_job->doneWork( filter->GetProgress()*100 );
69  }
70  );
71  writer->AddObserver(vtkCommand::ProgressEvent, progressCallback);
72 
73  m_job->addSimpleCancelHook([&] { writer->AbortExecuteOn(); });
74 
75  writer->Update();
76 
77  m_job->finish();
78 }
79 
80 //------------------------------------------------------------------------------
81 
82 std::string MeshWriter::extension()
83 {
84  return ".vtk";
85 }
86 
87 //------------------------------------------------------------------------------
88 
90 {
91  return m_job;
92 }
93 
94 //------------------------------------------------------------------------------
95 
96 } // namespace fwVtkIO
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
FWVTKIO_API ~MeshWriter()
Destructor.
static FWVTKIO_API void toVTKMesh(const ::fwData::Mesh::csptr &_mesh, vtkSmartPointer< vtkPolyData > _polyData)
Convert a ::fwData::Mesh::csptr to a vtkPolyData.
This namespace fwDataIO contains reader and writer for several framework&#39;s data.
::fwTools::Object::cwptr m_object
Object write on filesystem by the process.
virtual std::shared_ptr< const DataType > getConcreteObject() const
m_object getter.
FWVTKIO_API MeshWriter(::fwDataIO::writer::IObjectWriter::Key key)
Constructor.
FWVTKIO_API std::string extension() override
FWVTKIO_API void write() override
Reading operator.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
std::shared_ptr< ::fwJobs::IJob > sptr
Cancel request callback type.
Definition: IJob.hpp:54
FWVTKIO_API std::shared_ptr< ::fwJobs::IJob > getJob() const override
Contains the representation of the data objects used in the framework.
This namespace fwJobs provides jobs management.