fw4spl
SMeshReader.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/SMeshReader.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 
13 #include <fwCore/base.hpp>
14 
15 #include <fwData/location/Folder.hpp>
16 #include <fwData/location/SingleFile.hpp>
17 #include <fwData/mt/ObjectWriteLock.hpp>
18 
19 #include <fwGui/Cursor.hpp>
20 #include <fwGui/dialog/LocationDialog.hpp>
21 #include <fwGui/dialog/MessageDialog.hpp>
22 
23 #include <fwJobs/IJob.hpp>
24 
25 #include <fwServices/macros.hpp>
26 
27 #include <fwVtkIO/MeshReader.hpp>
28 
29 #include <boost/filesystem/operations.hpp>
30 
31 namespace ioVTK
32 {
33 
34 fwServicesRegisterMacro( ::fwIO::IReader, ::ioVTK::SMeshReader, ::fwData::Mesh );
35 
36 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL = "jobCreated";
37 
38 //------------------------------------------------------------------------------
39 
41 {
42  return ::fwIO::FILE;
43 }
44 
45 //------------------------------------------------------------------------------
46 
48 {
49  m_sigJobCreated = newSignal< JobCreatedSignalType >(JOB_CREATED_SIGNAL);
50 }
51 
52 //------------------------------------------------------------------------------
53 
55 {
57 
58  static ::boost::filesystem::path _sDefaultPath("");
59 
61  dialogFile.setTitle(m_windowTitle.empty() ? "Choose a vtk file to load Mesh" : m_windowTitle);
62  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
63  dialogFile.addFilter("Vtk", "*.vtk");
64  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
65  dialogFile.setOption(::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST);
66 
67  ::fwData::location::SingleFile::sptr result;
68  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
69  if (result)
70  {
71  _sDefaultPath = result->getPath().parent_path();
72  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
73  this->setFile(result->getPath());
74  }
75  else
76  {
77  this->clearLocations();
78  }
79 
80 }
81 
82 //------------------------------------------------------------------------------
83 
85 {
87 }
88 
89 //------------------------------------------------------------------------------
90 
92 {
94 }
95 
96 //------------------------------------------------------------------------------
97 
99 {
101 }
102 
103 //------------------------------------------------------------------------------
104 
105 void SMeshReader::info(std::ostream& _sstream )
106 {
107  _sstream << "SMeshReader::info";
108 }
109 
110 //------------------------------------------------------------------------------
111 
112 void SMeshReader::loadMesh( const ::boost::filesystem::path vtkFile, ::fwData::Mesh::sptr _pMesh )
113 {
114  SLM_TRACE_FUNC();
115 
116  ::fwVtkIO::MeshReader::sptr reader = ::fwVtkIO::MeshReader::New();
117 
118  m_sigJobCreated->emit(reader->getJob());
119 
120  reader->setObject(_pMesh);
121  reader->setFile(vtkFile);
122 
123  ::fwData::mt::ObjectWriteLock lock(_pMesh);
124 
125  try
126  {
127  reader->read();
128  }
129  catch (const std::exception& e)
130  {
131  std::stringstream ss;
132  ss << "Warning during loading : " << e.what();
133 
135  "Warning",
136  ss.str(),
137  ::fwGui::dialog::IMessageDialog::WARNING);
138  }
139  catch( ... )
140  {
141  std::stringstream ss;
142  ss << "Warning during loading. ";
144  "Warning",
145  "Warning during loading.",
146  ::fwGui::dialog::IMessageDialog::WARNING);
147  }
148 }
149 
150 //------------------------------------------------------------------------------
151 
153 {
154  if( this->hasLocationDefined() )
155  {
156  // Retrieve dataStruct associated with this service
157  ::fwData::Mesh::sptr pMesh = this->getInOut< ::fwData::Mesh >(::fwIO::s_DATA_KEY);
158  if (!pMesh)
159  {
160  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
161  pMesh = this->getObject< ::fwData::Mesh >();
162  }
163  SLM_ASSERT("pMesh not instanced", pMesh);
164 
165  ::fwGui::Cursor cursor;
166  cursor.setCursor(::fwGui::ICursor::BUSY);
167 
168  this->loadMesh(this->getFile(), pMesh);
169  this->notificationOfUpdate();
170 
171  cursor.setDefaultCursor();
172  }
173 }
174 
175 //------------------------------------------------------------------------------
176 
177 void SMeshReader::notificationOfUpdate()
178 {
179  ::fwData::Mesh::sptr pMesh = this->getInOut< ::fwData::Mesh >(::fwIO::s_DATA_KEY);
180  if (!pMesh)
181  {
182  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
183  pMesh = this->getObject< ::fwData::Mesh >();
184  }
185  SLM_ASSERT("pMesh not instanced", pMesh);
186 
187  ::fwData::Object::ModifiedSignalType::sptr sig;
189  {
190  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
191  sig->asyncEmit();
192  }
193 }
194 
195 //------------------------------------------------------------------------------
196 
197 } // 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.
virtual IOVTK_API void configureWithIHM() override
Configure the image path.
Definition: SMeshReader.cpp:54
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IReader.hpp:207
IOVTK_API SMeshReader() noexcept
Constructor.
Definition: SMeshReader.cpp:47
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)
IOVTK_API void info(std::ostream &_sstream) override
Info 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 starting() override
Starting method.
Definition: SMeshReader.cpp:84
A helper to lock object on exclusive mode.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
VTK Mesh Reader.
Definition: SMeshReader.hpp:46
FWIO_APIconst::boost::filesystem::path & getFile() const
Returns the file path set by the user or set during service configuration.
Definition: IReader.cpp:62
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
FWIO_API void setFile(const ::boost::filesystem::path &file)
Sets file path.
Definition: IReader.cpp:71
Reader service API. It manages extension points definition and extension configuration.
Definition: IReader.hpp:34
virtual IOVTK_API void stopping() override
Stopping method.
Definition: SMeshReader.cpp:91
#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 updating() override
Updating method.
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
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.
virtual IOVTK_API void configuring() override
Configuring method.
Definition: SMeshReader.cpp:98
Data holding a geometric structure composed of points, lines, triangles, quads or polygons...
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)
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...
Definition: SMeshReader.cpp:40
The namespace ioVTK contains reader, writer and helper using the fwVtkIO lib for output and input act...
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.