fw4spl
SModelSeriesWriter.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/SModelSeriesWriter.hpp"
8 
9 #include "ioVTK/SMeshWriter.hpp"
10 
11 #include <fwCom/HasSignals.hpp>
12 #include <fwCom/Signal.hpp>
13 #include <fwCom/Signal.hxx>
14 
15 #include <fwCore/base.hpp>
16 
17 #include <fwData/location/Folder.hpp>
18 #include <fwData/Mesh.hpp>
19 #include <fwData/Reconstruction.hpp>
20 
21 #include <fwGui/Cursor.hpp>
22 #include <fwGui/dialog/ILocationDialog.hpp>
23 #include <fwGui/dialog/LocationDialog.hpp>
24 #include <fwGui/dialog/MessageDialog.hpp>
25 
26 #include <fwJobs/IJob.hpp>
27 
28 #include <fwMedData/ModelSeries.hpp>
29 
30 #include <fwServices/macros.hpp>
31 
32 #include <fwTools/UUID.hpp>
33 
34 #include <fwVtkIO/MeshWriter.hpp>
35 
36 #include <boost/filesystem/operations.hpp>
37 
38 namespace ioVTK
39 {
40 
41 fwServicesRegisterMacro( ::fwIO::IWriter, ::ioVTK::SModelSeriesWriter, ::fwMedData::ModelSeries );
42 
43 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL = "jobCreated";
44 
45 //------------------------------------------------------------------------------
46 
47 SModelSeriesWriter::SModelSeriesWriter() noexcept
48 {
49  m_sigJobCreated = newSignal< JobCreatedSignalType >( JOB_CREATED_SIGNAL );
50 }
51 
52 //------------------------------------------------------------------------------
53 
55 {
56  return ::fwIO::FOLDER;
57 }
58 
59 //------------------------------------------------------------------------------
60 
62 {
64  static ::boost::filesystem::path _sDefaultPath("");
65 
67  dialog.setTitle(m_windowTitle.empty() ? "Choose a directory to save meshes" : m_windowTitle);
68  dialog.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
69  dialog.setOption(::fwGui::dialog::ILocationDialog::WRITE);
70  dialog.setType(::fwGui::dialog::ILocationDialog::FOLDER);
71 
72  ::fwData::location::Folder::sptr result;
73 
74  while (result = ::fwData::location::Folder::dynamicCast( dialog.show() ))
75  {
76  if( ::boost::filesystem::is_empty(result->getFolder()) )
77  {
78  break;
79  }
80  // message box
82  messageBox.setTitle("Overwrite confirmation");
83  messageBox.setMessage("The selected directory is not empty. Write anyway ?");
84  messageBox.setIcon(::fwGui::dialog::IMessageDialog::QUESTION);
85  messageBox.addButton(::fwGui::dialog::IMessageDialog::YES);
86  messageBox.addButton(::fwGui::dialog::IMessageDialog::CANCEL);
87  if( messageBox.show() == ::fwGui::dialog::IMessageDialog::YES)
88  {
89  break;
90  }
91  }
92 
93  if (result)
94  {
95  _sDefaultPath = result->getFolder().parent_path();
96  dialog.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
97  this->setFolder(result->getFolder());
98  }
99  else
100  {
101  this->clearLocations();
102  }
103 }
104 
105 //------------------------------------------------------------------------------
106 
108 {
109  SLM_TRACE_FUNC();
110 }
111 
112 //------------------------------------------------------------------------------
113 
115 {
116  SLM_TRACE_FUNC();
117 }
118 
119 //------------------------------------------------------------------------------
120 
122 {
124 }
125 
126 //------------------------------------------------------------------------------
127 
128 void SModelSeriesWriter::info(std::ostream& _sstream )
129 {
130  _sstream << "SModelSeriesWriter::info";
131 }
132 
133 //------------------------------------------------------------------------------
134 
136 {
137  SLM_TRACE_FUNC();
138 
139  if( this->hasLocationDefined() )
140  {
141  // Retrieve dataStruct associated with this service
142  ::fwMedData::ModelSeries::csptr modelSeries = this->getInput< ::fwMedData::ModelSeries >(::fwIO::s_DATA_KEY);
143  if (!modelSeries)
144  {
145  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
146  modelSeries = this->getObject< ::fwMedData::ModelSeries >();
147  }
148  SLM_ASSERT("ModelSeries is not instanced", modelSeries);
149 
150  ::fwGui::Cursor cursor;
151  cursor.setCursor(::fwGui::ICursor::BUSY);
152 
153  const ::fwMedData::ModelSeries::ReconstructionVectorType& recs = modelSeries->getReconstructionDB();
154  for(const ::fwData::Reconstruction::csptr& rec : recs)
155  {
156  SLM_ASSERT("Reconstruction from model series is not instanced", rec);
157  ::fwData::Mesh::sptr mesh = rec->getMesh();
158  SLM_ASSERT("Mesh from reconstruction is not instanced", mesh);
159 
160  ::fwVtkIO::MeshWriter::sptr writer = ::fwVtkIO::MeshWriter::New();
161  m_sigJobCreated->emit(writer->getJob());
162 
163  writer->setObject(mesh);
164  writer->setFile(this->getFolder() / (rec->getOrganName() + "_" + ::fwTools::UUID::get(mesh) + ".vtk"));
165 
166  try
167  {
168  writer->write();
169  }
170  catch (const std::exception& e)
171  {
172  std::stringstream ss;
173  ss << "Warning during saving : " << e.what();
174 
176  "Warning",
177  ss.str(),
178  ::fwGui::dialog::IMessageDialog::WARNING);
179  }
180  catch( ... )
181  {
183  "Warning",
184  "Warning during saving",
185  ::fwGui::dialog::IMessageDialog::WARNING);
186  }
187  }
188 
189  cursor.setDefaultCursor();
190  }
191 }
192 
193 //------------------------------------------------------------------------------
194 
195 } // 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
IOVTK_API void info(std::ostream &_sstream) override
Info method.
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
virtual FWGUI_API void setMessage(const std::string &msg) override
Set the message.
Defines the generic message box for IHM. Use the Delegate design pattern.
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 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.
virtual IOVTK_API void configuring() override
Configuring method.
VTK Model series writer.
virtual FWGUI_API void addButton(IMessageDialog::Buttons button) override
Add a button (OK, YES_NO, YES, NO, CANCEL)
virtual FWGUI_API IMessageDialog::Buttons show() override
Show the message box and return the clicked button.
static FWTOOLS_API const UUIDType & get(::fwTools::Object::sptr object)
Return an uuid to the given object : if no one previously set then generate a new one...
Definition: UUID.cpp:50
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IWriter.cpp:122
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
FWIO_APIconst::boost::filesystem::path & getFolder() const
Returns folder path set by the user or set during service configuration.
Definition: IWriter.cpp:75
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...
virtual FWGUI_API void setIcon(IMessageDialog::Icons icon) override
Set the icon (CRITICAL, WARNING, INFO or QUESTION)
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.
FWIO_API void setFolder(const ::boost::filesystem::path &folder)
Sets folder path.
Definition: IWriter.cpp:84
virtual IOVTK_API void configureWithIHM() override
Configure the mesh path.
virtual IOVTK_API void starting() override
Starting method.
FWGUI_API void setType(::fwGui::dialog::ILocationDialog::Types type) override
Set the type of location for the dialog (SINGLE_FILE, FORLDER, MULTI_FILES)
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
virtual IOVTK_API void stopping() override
Stopping method.
The namespace ioVTK contains reader, writer and helper using the fwVtkIO lib for output and input act...
virtual FWGUI_API void setTitle(const std::string &title) override
Set the title of the message box.
FWIO_API bool hasLocationDefined() const
Returns if a location has been defined ( by the configuration process or directly by user ) ...
Definition: IWriter.cpp:184
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