fw4spl
MeshWriterService.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/MeshWriterService.hpp"
8 
9 #include <fwData/location/Folder.hpp>
10 #include <fwData/location/SingleFile.hpp>
11 #include <fwData/Mesh.hpp>
12 
13 #include <fwDataIO/writer/MeshWriter.hpp>
14 
15 #include <fwGui/dialog/LocationDialog.hpp>
16 #include <fwGui/dialog/MessageDialog.hpp>
17 
18 #include <fwServices/macros.hpp>
19 
20 #include <boost/filesystem/operations.hpp>
21 
22 fwServicesRegisterMacro( ::fwIO::IWriter, ::ioData::MeshWriterService, ::fwData::Mesh );
23 
24 namespace ioData
25 {
26 
28 {
29 }
30 
31 //-----------------------------------------------------------------------------
32 
33 void MeshWriterService::info(std::ostream& _sstream )
34 {
35  this->SuperClass::info( _sstream );
36  _sstream << std::endl << " Mesh writer";
37 }
38 
39 //-----------------------------------------------------------------------------
40 
42 {
43 }
44 
45 //------------------------------------------------------------------------------
46 
48 {
49  return ::fwIO::FILE;
50 }
51 
52 //------------------------------------------------------------------------------
53 
55 {
57 }
58 
59 //------------------------------------------------------------------------------
60 
62 {
64  static ::boost::filesystem::path _sDefaultPath("");
65 
67  dialogFile.setTitle(m_windowTitle.empty() ? "Choose a TrianMesh file" : m_windowTitle);
68  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
69  dialogFile.addFilter("TrianMesh", "*.trian");
70  dialogFile.setOption(::fwGui::dialog::ILocationDialog::WRITE);
71 
72  ::fwData::location::SingleFile::sptr result;
73  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
74  if (result)
75  {
76  _sDefaultPath = result->getPath().parent_path();
77  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
78  this->setFile(result->getPath());
79  }
80  else
81  {
82  this->clearLocations();
83  }
84 }
85 
86 //------------------------------------------------------------------------------
87 
89 {
91  if(this->hasLocationDefined())
92  {
93  // Retrieve object
94  ::fwData::Mesh::csptr mesh = this->getInput< ::fwData::Mesh >(::fwIO::s_DATA_KEY);
95  if (!mesh)
96  {
97  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
98  mesh = this->getObject< ::fwData::Mesh >();
99  }
100 
101  SLM_ASSERT("Mesh not instanced", mesh);
102 
103  ::fwDataIO::writer::MeshWriter::sptr writer = ::fwDataIO::writer::MeshWriter::New();
104  writer->setObject( mesh );
105  writer->setFile(this->getFile());
106 
107  try
108  {
109  writer->write();
110  }
111  catch (const std::exception& e)
112  {
113  std::stringstream ss;
114  ss << "Warning during writing Mesh : " << e.what();
115 
117  "Warning",
118  ss.str(),
119  ::fwGui::dialog::IMessageDialog::WARNING);
120  }
121 
122  }
123 }
124 
125 //------------------------------------------------------------------------------
126 
127 }
IODATA_API ~MeshWriterService() noexcept
destructor
IODATA_API MeshWriterService()
Constructor : does nothing.
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
virtual IODATA_API void configuring() override
Configuring method : calls implementation from io::IWriter
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
IODATA_API void updating() override
Updating method. This method is called by update() from base service ( fwServices::IService ) ...
virtual IODATA_API::fwIO::IOPathType getIOPathType() const override
Return path type managed by the service, here FILE.
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 IODATA_API void info(std::ostream &_sstream) override
Info method.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
FWIO_API void setFile(const ::boost::filesystem::path &file)
Sets file path.
Definition: IWriter.cpp:49
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IWriter.cpp:122
FWIO_APIconst::boost::filesystem::path & getFile() const
Returns the file path set by the user or set during service configuration.
Definition: IWriter.cpp:40
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
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.
The namespace ioData contains reader and writer services for basic fwData::Object which doesn&#39;t need ...
Mesh writer service.
Data holding a geometric structure composed of points, lines, triangles, quads or polygons...
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
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 FWSERVICES_API void info(std::ostream &_sstream)
Write information in a stream.
Definition: IService.cpp:74
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IWriter.hpp:171
IODATA_API void configureWithIHM() override
Configure the mesh path.