MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkNavigationToolStorageSerializer.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
15#include "mitkIGTException.h"
16#include "mitkIGTIOException.h"
17#include <mitkStandaloneDataStorage.h>
18#include <mitkSceneIO.h>
19#include <mitkIOUtil.h>
20
21// Poco
22#include <Poco/Zip/Compress.h>
23
24//ITKsys
25#include <itksys/SystemTools.hxx>
26
27#include <sstream>
28
30{
31 //create temp directory
32 m_tempDirectory = mitk::IOUtil::CreateTemporaryDirectory("NavigationToolStorageSerializerTmp_XXXXXX");
33}
34
36{
37 //remove temp directory
38
39 try
40 {
41 if (itksys::SystemTools::FileExists(m_tempDirectory.c_str())) std::remove(m_tempDirectory.c_str());
42 }
43 catch(...)
44 {
45 MITK_ERROR << "Can't remove temp directory " << m_tempDirectory << "!";
46 }
47}
48
49void mitk::NavigationToolStorageSerializer::Serialize(const std::string& filename, mitk::NavigationToolStorage::Pointer storage)
50{
51 //save every tool to temp directory
52 mitk::NavigationToolWriter::Pointer myToolWriter = mitk::NavigationToolWriter::New();
53 for(unsigned int i=0; i<storage->GetToolCount();i++)
54 {
55 std::string tempFileName = m_tempDirectory + mitk::IOUtil::GetDirectorySeparator() + "NavigationTool" + convertIntToString(i) + ".tool";
56 if (!myToolWriter->DoWrite(tempFileName,storage->GetTool(i)))
57 {
58 mitkThrowException(mitk::IGTIOException) << "Could not write tool to tempory directory: " << tempFileName;
59 }
60 }
61 //add all files to zip archive
62 std::ofstream file( filename.c_str(), std::ios::binary | std::ios::out);
63 if (!file.good()) //test if the zip archive is ready for writing
64 {
65 //first: clean up
66 for (unsigned int i=0; i<storage->GetToolCount();i++)
67 {
68 std::string tempFileName = m_tempDirectory + mitk::IOUtil::GetDirectorySeparator() + "NavigationTool" + convertIntToString(i) + ".tool";
69 std::remove(tempFileName.c_str());
70 }
71 //then: throw an exception
72 mitkThrowException(mitk::IGTIOException) << "Could not open a file for writing: " << filename;
73 }
74 Poco::Zip::Compress zipper( file, true );
75 for (unsigned int i=0; i<storage->GetToolCount();i++)
76 {
77 std::string fileName = m_tempDirectory + mitk::IOUtil::GetDirectorySeparator() + "NavigationTool" + convertIntToString(i) + ".tool";
78 zipper.addFile(fileName, myToolWriter->GetFileWithoutPath(fileName));
79 std::remove(fileName.c_str()); //delete file
80 }
81 zipper.close();
82 file.close();
83}
84
86 {
87 std::string s;
88 std::stringstream out;
89 out << i;
90 s = out.str();
91 return s;
92 }
An object of this class represents an exception of the MITK-IGT module which are releated to the inpu...
void Serialize(const std::string &filename, mitk::NavigationToolStorage::Pointer storage)
Saves a mitk navigation tool storage to a file.