fw4spl
SSurfaceSegmentationWriter.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2017-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 "ioGdcm/SSurfaceSegmentationWriter.hpp"
8 
9 #include <fwData/location/Folder.hpp>
10 #include <fwData/Vector.hpp>
11 
12 #include <fwGdcmIO/helper/Fiducial.hpp>
13 #include <fwGdcmIO/writer/Series.hpp>
14 #include <fwGdcmIO/writer/SurfaceSegmentation.hpp>
15 
16 #include <fwGui/Cursor.hpp>
17 #include <fwGui/dialog/LocationDialog.hpp>
18 #include <fwGui/dialog/MessageDialog.hpp>
19 #include <fwGui/dialog/ProgressDialog.hpp>
20 #include <fwGui/dialog/SelectorDialog.hpp>
21 
22 #include <fwIO/IWriter.hpp>
23 
24 #include <fwMedData/DicomSeries.hpp>
25 #include <fwMedData/Series.hpp>
26 
27 #include <fwServices/macros.hpp>
28 
29 #include <fwTools/ProgressToLogger.hpp>
30 
31 #include <string>
32 
33 namespace ioGdcm
34 {
35 
37 
38 //------------------------------------------------------------------------------
39 
41 {
42 }
43 
44 //------------------------------------------------------------------------------
45 
47 {
48 }
49 
50 //------------------------------------------------------------------------------
51 
53 {
54  static ::boost::filesystem::path _sDefaultPath;
55 
57  dialogFile.setTitle(m_windowTitle.empty() ? "Choose a directory for DICOM images" : m_windowTitle);
58  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
59  dialogFile.setOption(::fwGui::dialog::ILocationDialog::WRITE);
60  dialogFile.setType(::fwGui::dialog::LocationDialog::FOLDER);
61 
62  ::fwData::location::Folder::sptr result;
63  result = ::fwData::location::Folder::dynamicCast( dialogFile.show() );
64  if (result)
65  {
66  _sDefaultPath = result->getFolder();
67  this->setFolder( result->getFolder() );
68  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
69  }
70  else
71  {
72  this->clearLocations();
73  }
74 }
75 
76 //------------------------------------------------------------------------------
77 
79 {
80 }
81 
82 //------------------------------------------------------------------------------
83 
85 {
86 }
87 
88 //------------------------------------------------------------------------------
89 
91 {
93 }
94 
95 //------------------------------------------------------------------------------
96 
98 {
99  if( this->hasLocationDefined() )
100  {
101  const ::boost::filesystem::path& folder = this->getFolder();
102  if(!::boost::filesystem::is_empty(folder))
103  {
105  dialog.setMessage("Folder '"+folder.string()+"' isn't empty, files can be overwritten."
106  "\nDo you want to continue ?");
107  dialog.setTitle("Folder not empty.");
108  dialog.setIcon(::fwGui::dialog::MessageDialog::QUESTION);
109  dialog.addButton( ::fwGui::dialog::MessageDialog::YES_NO );
111 
112  if(button == ::fwGui::dialog::MessageDialog::NO)
113  {
114  return;
115  }
116  }
117 
118  // Retrieve dataStruct associated with this service
119  ::fwMedData::ModelSeries::csptr model = this->getInput< ::fwMedData::ModelSeries >(::fwIO::s_DATA_KEY);
120 
121  if(!model->getDicomReference())
122  {
124  "Warning", "DICOM image reference is missing, DICOM Surface Segmentation cannot be generated",
125  ::fwGui::dialog::IMessageDialog::WARNING);
126  return;
127  }
128 
129  /* Build up the filename */
130  ::boost::filesystem::path outputPath = this->getFolder() / "imSeg";
131 
132  /* Write the data */
133  ::fwGui::Cursor cursor;
134  cursor.setCursor(::fwGui::ICursor::BUSY);
135  saveSurfaceSegmentation( outputPath, model );
136  cursor.setDefaultCursor();
137  }
138 }
139 
140 //------------------------------------------------------------------------------
141 
142 void SSurfaceSegmentationWriter::saveSurfaceSegmentation( const ::boost::filesystem::path filename,
143  const ::fwMedData::ModelSeries::csptr& model)
144 {
145  ::fwGdcmIO::writer::SurfaceSegmentation::sptr writer = ::fwGdcmIO::writer::SurfaceSegmentation::New();
146 
147  writer->setObject(model);
148 
149  ::fwData::location::SingleFile::sptr location = ::fwData::location::SingleFile::New();
150  location->setPath(filename);
151  writer->setLocation(location);
152 
153  try
154  {
155  writer->write();
156  }
157  catch (const std::exception& e)
158  {
159  std::stringstream ss;
160  ss << "Warning during saving: " << e.what();
162  "Warning", ss.str(), ::fwGui::dialog::IMessageDialog::WARNING);
163  }
164  catch( ... )
165  {
167  "Warning", "Warning during saving", ::fwGui::dialog::IMessageDialog::WARNING);
168  }
169 }
170 
171 //-----------------------------------------------------------------------------
172 
174 {
175  return ::fwIO::FOLDER;
176 }
177 
178 } // namespace ioGdcm
virtual IOGDCM_API ~SSurfaceSegmentationWriter() noexcept override
Destructor.
virtual FWGUI_API void setCursor(::fwGui::ICursor::CursorType cursor) override
Set the cursor.
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)
virtual IOGDCM_API void starting() override
Does nothing.
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 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.
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IWriter.cpp:122
Services to write in DICOM 3D format from ModelSeries.
IOGDCM_API SSurfaceSegmentationWriter() noexcept
Constructor.
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
Definition: IWriter.cpp:115
FWIO_APIconst::boost::filesystem::path & getFolder() const
Returns folder path set by the user or set during service configuration.
Definition: IWriter.cpp:75
IOGDCM_API void updating() override
Write the ImageSeries in DICOM format.
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
virtual IOGDCM_API void configureWithIHM() override
Propose a directory selection where to save the DICOM files.
ioGdcm contains services use to deal with DICOM using the GDCM library.
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
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 IOGDCM_API void stopping() override
Does nothing.
virtual IOGDCM_API void configuring() override
Configuring method. This method is used to configure the service.
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
IOGDCM_API::fwIO::IOPathType getIOPathType() const override
Return path type managed by the service, here FOLDER.