fw4spl
STrianMeshReader.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 "ioData/STrianMeshReader.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 
13 #include <fwData/location/Folder.hpp>
14 #include <fwData/location/SingleFile.hpp>
15 #include <fwData/Mesh.hpp>
16 
17 #include <fwDataIO/reader/MeshReader.hpp>
18 
19 #include <fwGui/dialog/LocationDialog.hpp>
20 #include <fwGui/dialog/MessageDialog.hpp>
21 
22 #include <fwIO/IReader.hpp>
23 
24 #include <fwServices/macros.hpp>
25 
26 #include <boost/filesystem/operations.hpp>
27 
28 #include <fstream>
29 #include <iostream>
30 
31 fwServicesRegisterMacro( ::fwIO::IReader, ::ioData::STrianMeshReader, ::fwData::Mesh );
32 
33 namespace ioData
34 {
35 
36 //-----------------------------------------------------------------------------
37 
38 void STrianMeshReader::info(std::ostream& _sstream )
39 {
40  this->SuperClass::info( _sstream );
41  _sstream << std::endl << "Trian file reader";
42 }
43 
44 //-----------------------------------------------------------------------------
45 
46 std::vector< std::string > STrianMeshReader::getSupportedExtensions()
47 {
48  std::vector< std::string > extensions;
49  extensions.push_back(".trian");
50  return extensions;
51 }
52 
53 //------------------------------------------------------------------------------
54 
56 {
57  return ::fwIO::FILE;
58 }
59 
60 //------------------------------------------------------------------------------
61 
63 {
65 }
66 
67 //------------------------------------------------------------------------------
68 
70 {
72  static ::boost::filesystem::path _sDefaultPath;
73 
75  dialogFile.setTitle(m_windowTitle.empty() ? "Choose a trian file" : m_windowTitle);
76  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
77  dialogFile.addFilter("Trian file", "*.trian");
78  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
79 
80  ::fwData::location::SingleFile::sptr result;
81  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
82  if (result)
83  {
84  _sDefaultPath = result->getPath().parent_path();
85  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
86  this->setFile(result->getPath());
87  }
88  else
89  {
90  this->clearLocations();
91  }
92 }
93 
94 //------------------------------------------------------------------------------
95 
97 {
99  if( this->hasLocationDefined() )
100  {
101  // Retrieve object
102  ::fwData::Mesh::sptr mesh = this->getInOut< ::fwData::Mesh >(::fwIO::s_DATA_KEY);
103  SLM_ASSERT("mesh not instanced", mesh);
104 
105  ::fwDataIO::reader::MeshReader::sptr reader = ::fwDataIO::reader::MeshReader::New();
106  reader->setObject( mesh );
107  reader->setFile(this->getFile());
108 
109  try
110  {
111  // Launch reading process
112  reader->read();
113  // Notify reading
114  ::fwData::Object::ModifiedSignalType::sptr sig;
116  {
117  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
118  sig->asyncEmit();
119  }
120  }
121  catch (const std::exception& e)
122  {
123  std::stringstream ss;
124  ss << "Warning during loading : " << e.what();
125 
127  "Warning",
128  ss.str(),
129  ::fwGui::dialog::IMessageDialog::WARNING);
130  }
131  catch( ... )
132  {
134  "Warning",
135  "Warning during loading.",
136  ::fwGui::dialog::IMessageDialog::WARNING);
137  }
138  }
139 }
140 
141 }
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IReader.hpp:207
Class allowing to block a Connection.
Definition: Connection.hpp:20
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
IODATA_API void configureWithIHM() override
Configure the mesh path.
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)
Mesh reader service.
virtual IODATA_API std::vector< std::string > getSupportedExtensions() override
returns (filename) extension
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:
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
IODATA_API void updating() override
Updating method. This method is called by update() from base service ( fwServices::IService ) ...
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
#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
virtual IODATA_API void configuring() override
Configuring method : calls implementation from io::IReader
virtual IODATA_API::fwIO::IOPathType getIOPathType() const override
Return path type managed by the service, here FILE.
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.
virtual IODATA_API void info(std::ostream &_sstream) override
Info method.
The namespace ioData contains reader and writer services for basic fwData::Object which doesn&#39;t need ...
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 FWSERVICES_API void info(std::ostream &_sstream)
Write information in a stream.
Definition: IService.cpp:74