MeshReaderService.cpp 4.12 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 ****** */

Emilie Harquel's avatar
Emilie Harquel committed
7
#include "ioData/MeshReaderService.hpp"
8

Emilie Harquel's avatar
Emilie Harquel committed
9
10
11
12
13
14
#include <fwCom/Signal.hpp>
#include <fwCom/Signal.hxx>
#include <fwCom/Signals.hpp>

#include <fwCore/base.hpp>

15
16
#include <fwData/location/Folder.hpp>
#include <fwData/location/SingleFile.hpp>
Emilie Harquel's avatar
Emilie Harquel committed
17
18
19
#include <fwData/Mesh.hpp>

#include <fwDataIO/reader/MeshReader.hpp>
20
21

#include <fwGui/dialog/LocationDialog.hpp>
22
#include <fwGui/dialog/MessageDialog.hpp>
23
24
25

#include <fwServices/macros.hpp>

26
27
#include <io/IReader.hpp>

Emilie Harquel's avatar
Emilie Harquel committed
28
#include <boost/filesystem/operations.hpp>
29

Emilie Harquel's avatar
Emilie Harquel committed
30
#include <fstream>
31
#include <iostream>
32

julien.waechter's avatar
julien.waechter committed
33
fwServicesRegisterMacro( ::io::IReader, ::ioData::MeshReaderService, ::fwData::Mesh );
34
35
36
37
38
39

namespace ioData
{

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

40
void MeshReaderService::info(std::ostream& _sstream )
41
{
julien.waechter's avatar
julien.waechter committed
42
43
    this->SuperClass::info( _sstream );
    _sstream << std::endl << "Trian file reader";
44
45
46
47
48
49
}

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

std::vector< std::string > MeshReaderService::getSupportedExtensions()
{
julien.waechter's avatar
julien.waechter committed
50
    std::vector< std::string > extensions;
51
    extensions.push_back(".trian");
julien.waechter's avatar
julien.waechter committed
52
    return extensions;
53
54
55
56
}

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

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

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

64
65
66
67
68
69
70
void MeshReaderService::configuring() throw(::fwTools::Failed)
{
    ::io::IReader::configuring();
}

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

71
72
73
74
75
76
void MeshReaderService::configureWithIHM()
{
    SLM_TRACE_FUNC();
    static ::boost::filesystem::path _sDefaultPath;

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

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

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

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

107
        ::fwDataIO::reader::MeshReader::sptr reader = ::fwDataIO::reader::MeshReader::New();
108
109
        reader->setObject( mesh );
        reader->setFile(this->getFile());
110

111
112
113
        try
        {
            // Launch reading process
114
            reader->read();
115
            // Notify reading
Emilie Harquel's avatar
Emilie Harquel committed
116
117
            ::fwData::Object::ModifiedSignalType::sptr sig;
            sig = mesh->signal< ::fwData::Object::ModifiedSignalType >(::fwData::Object::s_MODIFIED_SIG);
118
            {
Emilie Harquel's avatar
Emilie Harquel committed
119
120
                ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
                sig->asyncEmit();
121
            }
122
        }
123
        catch (const std::exception& e)
124
125
126
127
128
        {
            std::stringstream ss;
            ss << "Warning during loading : " << e.what();

            ::fwGui::dialog::MessageDialog::showMessageDialog(
julien.waechter's avatar
julien.waechter committed
129
130
131
                "Warning",
                ss.str(),
                ::fwGui::dialog::IMessageDialog::WARNING);
132
133
134
135
        }
        catch( ... )
        {
            ::fwGui::dialog::MessageDialog::showMessageDialog(
julien.waechter's avatar
julien.waechter committed
136
137
138
                "Warning",
                "Warning during loading.",
                ::fwGui::dialog::IMessageDialog::WARNING);
139
        }
140
141
142
143
    }
}

}