fw4spl
Samples/Tutorials/TutoTrianConverterCtrl/src/TutoTrianConverterCtrl/Plugin.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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 "TutoTrianConverterCtrl/Plugin.hpp"
8 
9 #include <fwRuntime/EConfigurationElement.hpp>
10 #include <fwRuntime/profile/Profile.hpp>
11 #include <fwRuntime/utils/GenericExecutableFactoryRegistrar.hpp>
12 
13 #include <fwServices/op/Add.hpp>
14 #include <fwServices/registry/AppConfig.hpp>
15 #include <fwServices/registry/ObjectService.hpp>
16 #include <fwServices/registry/ServiceFactory.hpp>
17 
18 #include <boost/program_options/options_description.hpp>
19 #include <boost/program_options/parsers.hpp>
20 #include <boost/program_options/positional_options.hpp>
21 #include <boost/program_options/variables_map.hpp>
22 
23 namespace TutoTrianConverterCtrl
24 {
25 
26 static ::fwRuntime::utils::GenericExecutableFactoryRegistrar<Plugin> registrar("::TutoTrianConverterCtrl::Plugin");
27 
28 //------------------------------------------------------------------------------
29 
30 Plugin::Plugin() noexcept
31 {
32 }
33 
34 //------------------------------------------------------------------------------
35 
36 Plugin::~Plugin() noexcept
37 {
38 }
39 
40 //------------------------------------------------------------------------------
41 
43 {
44 }
45 
46 //------------------------------------------------------------------------------
47 
49 {
50  namespace po = boost::program_options;
51 
52  ::fwRuntime::profile::Profile::sptr profile = ::fwRuntime::profile::getCurrentProfile();
53  SLM_ASSERT("Profile is not initialized", profile);
54  ::fwRuntime::profile::Profile::ParamsContainer params = profile->getParams();
55 
56  std::string trianMeshPath;
57  std::string vtkMeshPath;
58 
59  po::options_description cmdline_options("TutoTrianConverterCtrl options");
60  cmdline_options.add_options()
61  ("trian", po::value(&trianMeshPath)->required(), "Path of trian mesh file to convert")
62  ("vtk", po::value(&vtkMeshPath)->required(), "Path of created vtk mesh file")
63  ;
64 
65  po::positional_options_description p;
66  p.add("trian", 1)
67  .add("vtk", 2);
68 
69  po::variables_map vm;
70 
71  try
72  {
73  po::store(po::command_line_parser(params)
74  .options(cmdline_options)
75  .positional(p)
76  .run(),
77  vm);
78  po::notify(vm);
79  }
80  catch(po::error& e)
81  {
82  std::cerr << cmdline_options << std::endl << e.what() << std::endl;
83  return;
84  }
85 
86  ::fwServices::registry::ServiceFactory::sptr srvFactory = ::fwServices::registry::ServiceFactory::getDefault();
87 
88  m_mesh = ::fwData::Mesh::New();
89 
90  // create the service
91  m_readerSrv = srvFactory->create("::ioData::STrianMeshReader");
92  // register the mesh to the service
93  ::fwServices::OSR::registerService(m_mesh, "data", ::fwServices::IService::AccessType::INOUT, m_readerSrv);
94 
95  ::fwRuntime::EConfigurationElement::sptr readerCfg = ::fwRuntime::EConfigurationElement::New( "service" );
96  ::fwRuntime::EConfigurationElement::sptr readerFilenameCfg = ::fwRuntime::EConfigurationElement::New( "file" );
97  readerFilenameCfg->setValue(trianMeshPath);
98  readerCfg->addConfigurationElement(readerFilenameCfg);
99  m_readerSrv->setConfiguration( readerCfg );
100  m_readerSrv->configure();
101 
102  // create the service
103  m_writerSrv = srvFactory->create("::ioVTK::SMeshWriter");
104  // register the mesh to the service
105  ::fwServices::OSR::registerService(m_mesh, "data", ::fwServices::IService::AccessType::INPUT, m_writerSrv);
106 
107  ::fwRuntime::EConfigurationElement::sptr writerCfg = ::fwRuntime::EConfigurationElement::New( "service" );
108  ::fwRuntime::EConfigurationElement::sptr writerFilenameCfg = ::fwRuntime::EConfigurationElement::New( "file" );
109  writerFilenameCfg->setValue(vtkMeshPath);
110  writerCfg->addConfigurationElement(writerFilenameCfg);
111  m_writerSrv->setConfiguration( writerCfg );
112  m_writerSrv->configure();
113 
114  m_readerSrv->start();
115  m_writerSrv->start();
116  m_readerSrv->update();
117  m_writerSrv->update();
118 }
119 
120 //------------------------------------------------------------------------------
121 
122 void Plugin::stop() noexcept
123 {
124 }
125 
126 //------------------------------------------------------------------------------
127 
128 void Plugin::uninitialize() noexcept
129 {
130  if (m_writerSrv)
131  {
132  m_writerSrv->stop();
133  ::fwServices::OSR::unregisterService( m_writerSrv );
134  }
135  if(m_readerSrv)
136  {
137  m_readerSrv->stop();
138  ::fwServices::OSR::unregisterService( m_readerSrv );
139  }
140  m_mesh.reset();
141 }
142 
143 //------------------------------------------------------------------------------
144 
145 } // namespace TutoTrianConverterCtrl
TUTOTRIANCONVERTERCTRL_API void initialize()
Notifies the plugin about its initialisation.
TUTOTRIANCONVERTERCTRL_API void stop() noexcept
Overrides stop method. Do nothing.
#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
static FWSERVICES_API ServiceFactory::sptr getDefault()
Return the unique Instance, create it if required at first access.
TUTOTRIANCONVERTERCTRL_API void uninitialize() noexcept
Notifies the plugin about its uninitialisation.
TUTOTRIANCONVERTERCTRL_API ~Plugin() noexcept
Destructor. Do nothing.