fw4spl
SMeshWriter.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 "ioVTK/SMeshWriter.hpp"
8 
9 #include <fwCom/HasSignals.hpp>
10 #include <fwCom/Signal.hpp>
11 #include <fwCom/Signal.hxx>
12 
13 #include <fwCore/base.hpp>
14 
15 #include <fwData/location/Folder.hpp>
16 #include <fwData/location/SingleFile.hpp>
17 #include <fwData/Mesh.hpp>
18 
19 #include <fwGui/Cursor.hpp>
20 #include <fwGui/dialog/LocationDialog.hpp>
21 #include <fwGui/dialog/MessageDialog.hpp>
22 #include <fwGui/dialog/ProgressDialog.hpp>
23 
24 #include <fwJobs/IJob.hpp>
25 
26 #include <fwServices/macros.hpp>
27 
28 #include <fwVtkIO/MeshWriter.hpp>
29 
30 namespace ioVTK
31 {
32 
33 fwServicesRegisterMacro( ::fwIO::IWriter, ::ioVTK::SMeshWriter, ::fwData::Mesh );
34 
35 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL = "jobCreated";
36 
37 //------------------------------------------------------------------------------
38 
40 {
41  m_sigJobCreated = newSignal< JobCreatedSignalType >( JOB_CREATED_SIGNAL );
42 }
43 
44 //------------------------------------------------------------------------------
45 
47 {
48  return ::fwIO::FILE;
49 }
50 
51 //------------------------------------------------------------------------------
52 
54 {
56  static ::boost::filesystem::path _sDefaultPath("");
57 
59  dialogFile.setTitle(m_windowTitle.empty() ? "Choose a vtk file to save Mesh" : m_windowTitle);
60  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
61  dialogFile.addFilter("Vtk", "*.vtk");
62  dialogFile.setOption(::fwGui::dialog::ILocationDialog::WRITE);
63 
64  ::fwData::location::SingleFile::sptr result;
65  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
66  if (result)
67  {
68  _sDefaultPath = result->getPath().parent_path();
69  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
70  this->setFile(result->getPath());
71 
72  }
73  else
74  {
75  this->clearLocations();
76  }
77 }
78 
79 //------------------------------------------------------------------------------
80 
82 {
84 }
85 
86 //------------------------------------------------------------------------------
87 
89 {
91 }
92 
93 //------------------------------------------------------------------------------
94 
96 {
98 }
99 
100 //------------------------------------------------------------------------------
101 
102 void SMeshWriter::info(std::ostream& _sstream )
103 {
104  _sstream << "SMeshWriter::info";
105 }
106 
107 //------------------------------------------------------------------------------
108 
110 {
111  if( this->hasLocationDefined() )
112  {
113  // Retrieve dataStruct associated with this service
114  ::fwData::Mesh::csptr pMesh = this->getInput< ::fwData::Mesh >(::fwIO::s_DATA_KEY);
115  if (!pMesh)
116  {
117  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
118  pMesh = this->getObject< ::fwData::Mesh >();
119  }
120  SLM_ASSERT("pMesh not instanced", pMesh);
121 
122  ::fwGui::Cursor cursor;
123  cursor.setCursor(::fwGui::ICursor::BUSY);
124 
125  ::fwVtkIO::MeshWriter::sptr writer = ::fwVtkIO::MeshWriter::New();
126 
127  m_sigJobCreated->emit(writer->getJob());
128 
129  writer->setObject(pMesh);
130  writer->setFile(this->getFile());
131 
132  try
133  {
134  writer->write();
135  }
136  catch (const std::exception& e)
137  {
138  std::stringstream ss;
139  ss << "Warning during saving : " << e.what();
140 
142  "Warning",
143  ss.str(),
144  ::fwGui::dialog::IMessageDialog::WARNING);
145  }
146  catch( ... )
147  {
149  "Warning",
150  "Warning during saving",
151  ::fwGui::dialog::IMessageDialog::WARNING);
152  }
153 
154  cursor.setDefaultCursor();
155  }
156 }
157 
158 //------------------------------------------------------------------------------
159 
160 } // namespace ioVtk
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
virtual FWGUI_API void setCursor(::fwGui::ICursor::CursorType cursor) override
Set the cursor.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
IOVTK_API void updating() override
Updating method.
virtual IOVTK_API void stopping() override
Stopping method.
Definition: SMeshWriter.cpp:88
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
FWGUI_API::fwGui::dialog::ILocationDialog & setOption(::fwGui::dialog::ILocationDialog::Options option) override
allow to set option to the file dialog mode=READ/WRITE, check=FILE_MUST_EXIST
FWGUI_API::fwData::location::ILocation::sptr show() override
Display the dialog.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
virtual IOVTK_API void starting() override
Starting method.
Definition: SMeshWriter.cpp:81
virtual IOVTK_API void configureWithIHM() override
Configure the mesh path.
Definition: SMeshWriter.cpp:53
FWIO_API void setFile(const ::boost::filesystem::path &file)
Sets file path.
Definition: IWriter.cpp:49
virtual IOVTK_API::fwIO::IOPathType getIOPathType() const override
This method must be implemented by concrete service writers that use path file system to write data...
Definition: SMeshWriter.cpp:46
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IWriter.cpp:122
FWIO_APIconst::boost::filesystem::path & getFile() const
Returns the file path set by the user or set during service configuration.
Definition: IWriter.cpp:40
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
Definition: IWriter.cpp:115
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
IOVTK_API void info(std::ostream &_sstream) override
Info method.
virtual IOVTK_API void configuring() override
Configuring method.
Definition: SMeshWriter.cpp:95
IOVTK_API SMeshWriter() noexcept
Constructor. Do nothing.
Definition: SMeshWriter.cpp:39
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
Writer service API. It manages extension points definition and extension configuration.
Definition: IWriter.hpp:33
FWGUI_API void setTitle(const std::string &title) override
Set the title for the dialog.
Defines the generic file/folder selector dialog for IHM.
Defines the generic cursor for IHM. Use the Delegate design pattern.
Data holding a geometric structure composed of points, lines, triangles, quads or polygons...
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
The namespace ioVTK contains reader, writer and helper using the fwVtkIO lib for output and input act...
FWIO_API bool hasLocationDefined() const
Returns if a location has been defined ( by the configuration process or directly by user ) ...
Definition: IWriter.cpp:184
VTK Mesh Writer.
Definition: SMeshWriter.hpp:53
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IWriter.hpp:171