fw4spl
SModelSeriesReader.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/SModelSeriesReader.hpp"
8 
9 #include "ioVTK/SMeshWriter.hpp"
10 
11 #include <fwCom/Signal.hpp>
12 #include <fwCom/Signal.hxx>
13 #include <fwCom/Signals.hpp>
14 
15 #include <fwCore/base.hpp>
16 
17 #include <fwData/location/Folder.hpp>
18 #include <fwData/location/ILocation.hpp>
19 #include <fwData/location/MultiFiles.hpp>
20 #include <fwData/Mesh.hpp>
21 #include <fwData/mt/ObjectWriteLock.hpp>
22 #include <fwData/Reconstruction.hpp>
23 
24 #include <fwGui/Cursor.hpp>
25 #include <fwGui/dialog/ILocationDialog.hpp>
26 #include <fwGui/dialog/LocationDialog.hpp>
27 #include <fwGui/dialog/MessageDialog.hpp>
28 #include <fwGui/dialog/ProgressDialog.hpp>
29 
30 #include <fwJobs/IJob.hpp>
31 #include <fwJobs/Job.hpp>
32 
33 #include <fwMedData/ModelSeries.hpp>
34 
35 #include <fwServices/macros.hpp>
36 
37 #include <fwTools/UUID.hpp>
38 
39 #include <fwVtkIO/MeshReader.hpp>
40 
41 #include <boost/filesystem/operations.hpp>
42 
43 namespace ioVTK
44 {
45 
46 fwServicesRegisterMacro( ::fwIO::IReader, ::ioVTK::SModelSeriesReader, ::fwMedData::ModelSeries );
47 
48 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL = "jobCreated";
49 
50 //------------------------------------------------------------------------------
51 
53 {
54  m_sigJobCreated = newSignal< JobCreatedSignalType >( JOB_CREATED_SIGNAL );
55 }
56 
57 //------------------------------------------------------------------------------
58 
60 {
61  return ::fwIO::FILES;
62 }
63 
64 //------------------------------------------------------------------------------
65 
67 {
68  static ::boost::filesystem::path _sDefaultPath("");
69 
71  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
72  dialogFile.setType(::fwGui::dialog::ILocationDialog::MULTI_FILES);
73  dialogFile.setTitle(m_windowTitle.empty() ? "Choose vtk files to load Series" : m_windowTitle);
74  dialogFile.addFilter("Vtk files", "*.vtk");
75  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
76  dialogFile.setOption(::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST);
77 
78  ::fwData::location::MultiFiles::sptr result;
79  result = ::fwData::location::MultiFiles::dynamicCast( dialogFile.show() );
80  if (result)
81  {
82  const ::fwData::location::ILocation::VectPathType paths = result->getPaths();
83  if(!paths.empty())
84  {
85  _sDefaultPath = paths[0].parent_path();
86  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
87  }
88  this->setFiles(paths);
89  }
90  else
91  {
92  this->clearLocations();
93  }
94 }
95 
96 //------------------------------------------------------------------------------
97 
99 {
100  SLM_TRACE_FUNC();
101 }
102 
103 //------------------------------------------------------------------------------
104 
106 {
107  SLM_TRACE_FUNC();
108 }
109 
110 //------------------------------------------------------------------------------
111 
113 {
115 }
116 
117 //------------------------------------------------------------------------------
118 
119 void SModelSeriesReader::info(std::ostream& _sstream )
120 {
121  _sstream << "SModelSeriesReader::info";
122 }
123 
124 //------------------------------------------------------------------------------
125 
127 {
128  if( this->hasLocationDefined() )
129  {
130  // Retrieve dataStruct associated with this service
131  ::fwMedData::ModelSeries::sptr modelSeries = this->getInOut< ::fwMedData::ModelSeries >(::fwIO::s_DATA_KEY);
132  if (!modelSeries)
133  {
134  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
135  modelSeries = this->getObject< ::fwMedData::ModelSeries >();
136  }
137 
138  ::fwGui::Cursor cursor;
139  cursor.setCursor(::fwGui::ICursor::BUSY);
140 
141  ::fwMedData::ModelSeries::ReconstructionVectorType recDB = modelSeries->getReconstructionDB();
142  ::fwMedData::ModelSeries::ReconstructionVectorType addedRecs;
143  for(const ::fwData::location::ILocation::PathType& file : this->getFiles())
144  {
145  ::fwData::Mesh::sptr mesh = ::fwData::Mesh::New();
146  this->loadMesh(file, mesh);
147 
148  ::fwData::Reconstruction::sptr rec = ::fwData::Reconstruction::New();
149  rec->setMesh(mesh);
150  rec->setIsVisible(true);
151  rec->setOrganName(file.stem().string());
152  recDB.push_back(rec);
153  addedRecs.push_back(rec);
154  }
155  cursor.setDefaultCursor();
156  modelSeries->setReconstructionDB(recDB);
157 
158  auto sig = modelSeries->signal< ::fwMedData::ModelSeries::ReconstructionsAddedSignalType >(
160  {
161  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
162  sig->asyncEmit(addedRecs);
163  }
164  }
165 }
166 
167 //------------------------------------------------------------------------------
168 
169 void SModelSeriesReader::loadMesh( const ::boost::filesystem::path file, ::fwData::Mesh::sptr mesh )
170 {
171  ::fwVtkIO::MeshReader::sptr reader = ::fwVtkIO::MeshReader::New();
172 
173  reader->setObject(mesh);
174  reader->setFile(file);
175 
176  m_sigJobCreated->emit(reader->getJob());
177 
178  try
179  {
181  reader->read();
182  }
183  catch (const std::exception& e)
184  {
185  std::stringstream stream;
186  stream << "Warning during loading : " << e.what();
188  "Warning",
189  stream.str(),
190  ::fwGui::dialog::IMessageDialog::WARNING);
191  }
192  catch( ... )
193  {
195  "Warning",
196  "Warning during loading.",
197  ::fwGui::dialog::IMessageDialog::WARNING);
198  }
199 }
200 
201 //------------------------------------------------------------------------------
202 
203 } // 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.
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IReader.hpp:207
virtual IOVTK_API::fwIO::IOPathType getIOPathType() const override
This method must be implemented by concrete service readers that use path file system to read data...
Class allowing to block a Connection.
Definition: Connection.hpp:20
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
FWIO_API bool hasLocationDefined() const
Returns if a location has been defined ( by the configuration process or directly by user ) ...
Definition: IReader.cpp:233
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IReader.cpp:144
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
FWIO_API void setFiles(const ::fwIO::LocationsType &files)
Sets file paths.
Definition: IReader.cpp:89
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.
A helper to lock object on exclusive mode.
virtual IOVTK_API void stopping() override
Stopping method.
virtual IOVTK_API void configureWithIHM() override
Configure the mesh path.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
Reader service API. It manages extension points definition and extension configuration.
Definition: IReader.hpp:34
static FWMEDDATA_APIconst::fwCom::Signals::SignalKeyType s_RECONSTRUCTIONS_ADDED_SIG
Key in m_signals map of signal m_sigReconstructionsAdded.
IOVTK_API void updating() override
Updating method.
IOVTK_API void info(std::ostream &_sstream) override
Info method.
FWIO_APIconst::fwIO::LocationsType & getFiles() const
Returns file paths set by the user or set during service configuration.
Definition: IReader.cpp:80
virtual IOVTK_API void starting() override
Starting method.
virtual IOVTK_API void configuring() override
Configuring method.
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
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.
FWGUI_API void setType(::fwGui::dialog::ILocationDialog::Types type) override
Set the type of location for the dialog (SINGLE_FILE, FORLDER, MULTI_FILES)
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
Definition: IReader.cpp:137
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
IOVTK_API SModelSeriesReader() noexcept
Constructor. Do nothing.
The namespace ioVTK contains reader, writer and helper using the fwVtkIO lib for output and input act...