MeshWriterService.cpp 3.6 KB
Newer Older
1
/* ***** BEGIN LICENSE BLOCK *****
2
 * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3
4
5
6
 * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
 * published by the Free Software Foundation.
 * ****** END LICENSE BLOCK ****** */

7
#include "ioData/MeshWriterService.hpp"
8
9
10

#include <fwData/location/Folder.hpp>
#include <fwData/location/SingleFile.hpp>
11
12
13
#include <fwData/Mesh.hpp>

#include <fwDataIO/writer/MeshWriter.hpp>
14
15
16
17

#include <fwGui/dialog/LocationDialog.hpp>
#include <fwGui/dialog/MessageDialog.hpp>

18
#include <fwServices/macros.hpp>
19

20
#include <boost/filesystem/operations.hpp>
21

julien.waechter's avatar
julien.waechter committed
22
fwServicesRegisterMacro( ::io::IWriter, ::ioData::MeshWriterService, ::fwData::Mesh );
23
24
25
26

namespace ioData
{

27
MeshWriterService::MeshWriterService()
28
29
30
31
32
{
}

//-----------------------------------------------------------------------------

33
void MeshWriterService::info(std::ostream& _sstream )
34
{
julien.waechter's avatar
julien.waechter committed
35
36
    this->SuperClass::info( _sstream );
    _sstream << std::endl << " Mesh writer";
37
38
39
40
41
42
}

//-----------------------------------------------------------------------------

std::vector< std::string > MeshWriterService::getSupportedExtensions()
{
julien.waechter's avatar
julien.waechter committed
43
    std::vector< std::string > extensions;
44
    extensions.push_back(".trian");
julien.waechter's avatar
julien.waechter committed
45
    return extensions;
46
47
48
49
50
}

//-----------------------------------------------------------------------------

MeshWriterService::~MeshWriterService() throw()
julien.waechter's avatar
julien.waechter committed
51
52
{
}
53
54
55

//------------------------------------------------------------------------------

56
::io::IOPathType MeshWriterService::getIOPathType() const
57
{
58
    return ::io::FILE;
59
60
61
62
}

//------------------------------------------------------------------------------

63
64
65
66
67
68
69
void MeshWriterService::configuring() throw(::fwTools::Failed)
{
    ::io::IWriter::configuring();
}

//------------------------------------------------------------------------------

70
71
72
73
74
75
void MeshWriterService::configureWithIHM()
{
    SLM_TRACE_FUNC();
    static ::boost::filesystem::path _sDefaultPath("");

    ::fwGui::dialog::LocationDialog dialogFile;
76
    dialogFile.setTitle(m_windowTitle.empty() ? "Choose a TrianMesh file" : m_windowTitle);
77
    dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
78
    dialogFile.addFilter("TrianMesh", "*.trian");
79
80
    dialogFile.setOption(::fwGui::dialog::ILocationDialog::WRITE);

julien.waechter's avatar
julien.waechter committed
81
82
    ::fwData::location::SingleFile::sptr result;
    result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
83
84
    if (result)
    {
85
        _sDefaultPath = result->getPath().parent_path();
86
        dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
87
88
89
90
91
        this->setFile(result->getPath());
    }
    else
    {
        this->clearLocations();
92
93
94
95
96
97
98
99
    }
}

//------------------------------------------------------------------------------

void MeshWriterService::updating() throw(::fwTools::Failed)
{
    SLM_TRACE_FUNC();
100
    if(this->hasLocationDefined())
101
102
103
104
105
    {
        // Retrieve object
        ::fwData::Mesh::sptr mesh = this->getObject< ::fwData::Mesh >( );
        SLM_ASSERT("Mesh not instanced", mesh);

106
        ::fwDataIO::writer::MeshWriter::sptr writer = ::fwDataIO::writer::MeshWriter::New();
107
        writer->setObject( mesh );
108
        writer->setFile(this->getFile());
109
110
111
112
113

        try
        {
            writer->write();
        }
114
        catch (const std::exception& e)
115
116
117
118
119
        {
            std::stringstream ss;
            ss << "Warning during writing Mesh : " << e.what();

            ::fwGui::dialog::MessageDialog::showMessageDialog(
julien.waechter's avatar
julien.waechter committed
120
121
122
                "Warning",
                ss.str(),
                ::fwGui::dialog::IMessageDialog::WARNING);
123
124
125
126
127
128
129
130
        }

    }
}

//------------------------------------------------------------------------------

}